Skip to content

Commit

Permalink
Examples: updated for use with new built-in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdr committed Dec 2, 2024
1 parent e9b9d93 commit 20a18b2
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 166 deletions.
28 changes: 9 additions & 19 deletions examples/agile_pricing_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@
Shows the current, next and last energy price for Octopus Energys Agile Price tarrif
'''

import asyncio
import datetime
import time

import machine
import ntptime
import requests
from ezwifi import EzWiFi
from picographics import DISPLAY_PRESTO, PicoGraphics
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
from presto import Presto

machine.freq(264000000)

# Constants
API_URL = 'https://api.octopus.energy/v1/products/AGILE-FLEX-22-11-25/electricity-tariffs/E-1R-AGILE-FLEX-22-11-25-C/standard-unit-rates/'

# WiFi setup
wifi = EzWiFi(verbose=True)
connected = asyncio.get_event_loop().run_until_complete(wifi.connect(retries=2))
# Setup for the Presto display

presto = Presto(reactive_backlight=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
presto.connect()

# Set the correct time using the NTP service.
ntptime.settime()

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
WIDTH, HEIGHT = display.get_bounds()

# Pico Vector
vector = PicoVector(display)
vector.set_antialiasing(ANTIALIAS_BEST)
Expand All @@ -42,7 +34,6 @@
vector.set_font_word_spacing(100)
vector.set_transform(t)


# Couple of colours for use later
ORANGE = display.create_pen(255, 99, 71)
ORANGE_2 = display.create_pen(255, 99 + 50, 71 + 50)
Expand All @@ -53,13 +44,12 @@

MARGIN = 15

# Clear the screen and use blue as the background colour
# Clear the screen and use orange as the background colour
display.set_pen(ORANGE)
display.clear()
display.set_pen(ORANGE_3)
display.text("Getting prices...", 10, 90 + 2, WIDTH, 4)
presto.update(display)
presto.update(display)
presto.update()

# Keep a record of the last time we updated.
# We only want to be requesting new information every half an hour.
Expand Down Expand Up @@ -140,4 +130,4 @@ def get_prices():
vector.text(f"{next_price}p", MARGIN, 215)

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
18 changes: 6 additions & 12 deletions examples/backlight_ball.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
Watch the backlighting react to a ball moving on screen
'''

from picographics import PicoGraphics, DISPLAY_PRESTO
from presto import Presto
import time
import math
from PrestoLight import Reactive
import time

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
from presto import Presto

presto = Presto(reactive_backlight=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

# Couple of colours for use later
Expand All @@ -26,8 +24,6 @@
# Set our initial pen colour
pen = display.create_pen_hsv(1.0, 1.0, 1.0)

backlight = Reactive()

while True:

display.set_pen(BLACK)
Expand All @@ -45,7 +41,5 @@

display.circle(WIDTH // 2 + int(math.cos(rad) * 100), HEIGHT // 2 + int(math.sin(rad) * 100), 80)

backlight.update(display)

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
35 changes: 13 additions & 22 deletions examples/backlight_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,38 @@
A demo that flips between 2 images and changes the backlighting
'''

from picographics import PicoGraphics, DISPLAY_PRESTO
from presto import Presto
import time

import jpegdec
from PrestoLight import Reactive
from presto import Presto

# File names for your 2 images. The reactive backlighting works best with images that match the resolution of the screen
# In this example we're running at 240 x 240

IMAGE_1 = "image1.jpg"
IMAGE_2 = "image2.jpg"

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
presto = Presto(reactive_backlight=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

# JPEG
j = jpegdec.JPEG(display)

backlight = Reactive()

# Couple of colours for use later
BLUE = display.create_pen(28, 181, 202)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(230, 60, 45)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(9, 185, 120)
PINK = display.create_pen(250, 125, 180)
PURPLE = display.create_pen(118, 95, 210)
BLACK = display.create_pen(0, 0, 0)

flip = True

while True:

if flip:
j.open_file("colour_pencils.jpg")
j.open_file(IMAGE_1)
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True)
flip = not flip
else:
j.open_file("car.jpg")
j.open_file(IMAGE_2)
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True)
flip = not flip

backlight.update(display)

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
time.sleep(1)
6 changes: 3 additions & 3 deletions examples/balls_demo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import random
from picographics import PicoGraphics, DISPLAY_PRESTO

from presto import Presto

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

# We're creating 170 balls with their own individual colour and 1 BG colour
Expand Down Expand Up @@ -63,4 +63,4 @@ def __init__(self, x, y, r, dx, dy, pen):
display.circle(int(ball.x), int(ball.y), int(ball.r))

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
22 changes: 5 additions & 17 deletions examples/cheerlights_bulb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import asyncio
import time

import machine
import plasma
import requests
from ezwifi import EzWiFi
from picographics import DISPLAY_PRESTO, PicoGraphics
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
from presto import Presto
from touch import FT6236

BULB_OUTLINE = [(130.44, 0.0),
(150.36, 1.51),
Expand Down Expand Up @@ -104,19 +99,13 @@
# How long we'll wait between updates
INTERVAL = 60

machine.freq(264000000)

# WiFi setup
wifi = EzWiFi(verbose=True)
connected = asyncio.get_event_loop().run_until_complete(wifi.connect(retries=2))

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
display = presto.display
WIDTH, HEIGHT = display.get_bounds()
wifi = presto.connect()

# We'll need this for the touch element of the screen
touch = FT6236()
touch = presto.touch

# Pico Vector
vector = PicoVector(display)
Expand Down Expand Up @@ -210,10 +199,9 @@ def get_cheerlight():

while True:

# Poll the touch so we can see if anything changed since the last time
touch.poll()

if connected:
if wifi:
# If the user is touching the screen we'll do the following
if touch.state:
bulb_on = not bulb_on
Expand Down Expand Up @@ -250,4 +238,4 @@ def get_cheerlight():
else:
print("Lost connection to network")

presto.update(display)
presto.update()
8 changes: 4 additions & 4 deletions examples/cubes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import time
import math
import time
from random import randint, randrange
from picographics import PicoGraphics, DISPLAY_PRESTO

from presto import Presto

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

BLACK = display.create_pen(0, 0, 0)
Expand Down Expand Up @@ -152,5 +152,5 @@ def draw(self):
cubes[i] = Cube(8, 4, randint(10, WIDTH), randint(10, HEIGHT), randrange(4, 9) / 10)

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
time.sleep(0.01)
7 changes: 3 additions & 4 deletions examples/hello.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from picographics import PicoGraphics, DISPLAY_PRESTO
from presto import Presto

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto))
presto = Presto(reactive_backlight=True)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

# Couple of colours for use later
Expand All @@ -23,4 +22,4 @@
display.text("Hello!", 10, 85, WIDTH, 8)

# Finally we update the screen with our changes :)
presto.update(display)
presto.update()
21 changes: 9 additions & 12 deletions examples/image_gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
import plasma
import sdcard
import uos
from picographics import DISPLAY_PRESTO, PicoGraphics
from presto import Presto
from touch import FT6236

machine.freq(264000000)

# The total number of LEDs to set, the Presto has 7
NUM_LEDS = 7
Expand All @@ -35,15 +31,15 @@

# Setup for the Presto display
presto = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(presto), layers=2)
display = presto.display
WIDTH, HEIGHT = display.get_bounds()

BACKGROUND = display.create_pen(1, 1, 1)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)

# We'll need this for the touch element of the screen
touch = FT6236()
touch = presto.touch

# JPEG Dec
j = jpegdec.JPEG(display)
Expand Down Expand Up @@ -130,7 +126,7 @@ def fizzlefade():
if lfsr == 1:
break

presto.update(display)
presto.update()
if lfsr == 1:
break

Expand Down Expand Up @@ -250,8 +246,7 @@ def file_exists(filename):
# We're not passing the arg for 'show_next' or 'show_previous' so it'll show whichever image is current
clear()
show_image()
presto.update(display)
presto.update(display)
presto.update()

while True:

Expand All @@ -263,7 +258,7 @@ def file_exists(filename):

last_updated = time.time()
show_image(show_next=True)
presto.update(display)
presto.update()

# if the screen is reporting that there is touch we want to handle that here
if touch.state:
Expand All @@ -273,20 +268,22 @@ def file_exists(filename):
for i in LEDS_RIGHT:
bl.set_rgb(i, 255, 255, 255)
show_image(show_next=True)
presto.update(display)
presto.update()
last_updated = time.time()
for i in LEDS_RIGHT:
bl.set_rgb(i, 0, 0, 0)
time.sleep(0.01)

# Left half of the screen moves to the previous image
elif touch.x < WIDTH // 2:
for i in LEDS_LEFT:
bl.set_rgb(i, 255, 255, 255)
show_image(show_previous=True)
presto.update(display)
presto.update()
last_updated = time.time()
for i in LEDS_LEFT:
bl.set_rgb(i, 0, 0, 0)
time.sleep(0.01)

# Wait here until the user stops touching the screen
while touch.state:
Expand Down
Loading

0 comments on commit 20a18b2

Please sign in to comment.