Skip to content

Commit

Permalink
Examples: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thirdr committed Nov 12, 2024
1 parent 7af2da2 commit 177faa8
Show file tree
Hide file tree
Showing 13 changed files with 1,028 additions and 0 deletions.
51 changes: 51 additions & 0 deletions examples/backlight_ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''
This demo does something
'''

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

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

# 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)

# Set our initial pen colour
pen = display.create_pen_hsv(1.0, 1.0, 1.0)

backlight = Reactive()

while True:

display.set_pen(BLACK)
display.clear()

# We'll use this for cycling through the rainbow
t = time.ticks_ms() / 5000

degrees = (t * 360) / 5
rad = math.radians(degrees)

display.reset_pen(pen)
pen = display.create_pen_hsv(t, 1.0, 1.0)
display.set_pen(pen)

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 :)
portal.update(display)
16 changes: 16 additions & 0 deletions examples/backlight_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
A basic example to show how to set the LED backlights
'''

import plasma

# The total number of LEDs to set, the Presto has 7
NUM_LEDS = 7

# Plasma setup
bl = plasma.WS2812(7, 0, 0, 33)
bl.start()

# Cycle through each LED and set the colour to purple.
for i in range(NUM_LEDS):
bl.set_rgb(i, 255, 0, 255)
71 changes: 71 additions & 0 deletions examples/backlight_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'''
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

# Setup for the Presto display
portal = Presto()
display = PicoGraphics(DISPLAY_PRESTO, buffer=memoryview(portal))
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.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True)
flip = not flip
else:
j.open_file("car.jpg")
j.decode(0, 0, jpegdec.JPEG_SCALE_FULL, dither=True)
flip = not flip

backlight.update(display)

''''
display.set_pen(WHITE)
MAX = const(160)
MID = const(80)
MIN = const(0)
SIZE = 79
display.rectangle(MAX, MAX, SIZE, SIZE)
display.rectangle(MAX, MID, SIZE, SIZE)
display.rectangle(MAX, MIN, SIZE, SIZE)
display.rectangle(MID, MIN, SIZE, SIZE)
display.rectangle(MIN, MIN, SIZE, SIZE)
display.rectangle(MIN, MID, SIZE, SIZE)
display.rectangle(MIN, MAX, SIZE, SIZE)
display.set_pen(BLACK)
for x in range(0, WIDTH, 5):
for y in range(0, HEIGHT, 14):
display.pixel(x, y)
'''

# Finally we update the screen with our changes :)
portal.update(display)
time.sleep(1)
67 changes: 67 additions & 0 deletions examples/balls_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

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

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

# We're creating 170 balls with their own individual colour and 1 BG colour
# for a total of 171 colours, which will all fit in the custom 256 entry palette!


class Ball:
def __init__(self, x, y, r, dx, dy, pen):
self.x = x
self.y = y
self.r = r
self.dx = dx
self.dy = dy
self.pen = pen


# initialise shapes
balls = []
for i in range(0, 170):
r = random.randint(0, 10) + 3
balls.append(
Ball(
random.randint(r, r + (WIDTH - 2 * r)),
random.randint(r, r + (HEIGHT - 2 * r)),
r,
(14 - r) / 2,
(14 - r) / 2,
display.create_pen(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
)
)

BG = display.create_pen(40, 40, 40)

while True:

display.set_layer(0)
display.set_pen(BG)
display.clear()

for ball in balls:
ball.x += ball.dx
ball.y += ball.dy

xmax = WIDTH - ball.r
xmin = ball.r
ymax = HEIGHT - ball.r
ymin = ball.r

if ball.x < xmin or ball.x > xmax:
ball.dx *= -1

if ball.y < ymin or ball.y > ymax:
ball.dy *= -1

display.set_pen(ball.pen)
display.circle(int(ball.x), int(ball.y), int(ball.r))

# Finally we update the screen with our changes :)
portal.update(display)
156 changes: 156 additions & 0 deletions examples/cubes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import time
import math
from random import randint, randrange
from picographics import PicoGraphics, DISPLAY_PRESTO
from presto import Presto

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

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


class Cube(object):
# The corners of the cube
vertices = [[-1, -1, 1],
[1, -1, 1],
[1, -1, -1],
[-1, -1, -1],
[-1, 1, 1],
[1, 1, 1],
[1, 1, -1],
[-1, 1, -1]]

# The corners that will be connected together to make a cube :)
edges = [(0, 1), (1, 2), (2, 3), (3, 0),
(4, 5), (5, 6), (6, 7), (7, 4),
(0, 4), (1, 5), (2, 6), (3, 7)]

def __init__(self, fov, distance, x, y, speed):
self.tick = time.ticks_ms() / 1000.0
self.cos = math.cos(self.tick)
self.sin = math.sin(self.tick)
self.fov = fov
self.distance = distance
self.pos_x = x
self.pos_y = y
self.speed = speed

self.cube_points = []

# Project our points
def to_2d(self, x, y, z, pos_x, pos_y, fov, distance):
factor = fov / (distance + z)
x = x * factor + pos_x
y = -y * factor + pos_y

return int(x), int(y)

def return_tick(self):
return self.tick

# Clear our points and recalculate the sin and cos values
def _update(self):

self.cube_points = []

self.tick = time.ticks_ms() / (self.speed * 1000)
self.cos = math.cos(self.tick)
self.sin = math.sin(self.tick)

def set_fov(self, fov):
self.fov = fov

def set_distance(self, distance):
self.distance = distance

def set_speed(self, speed):
self.speed = speed

def set_x(self, x):
self.pos_x = x

def set_y(self, y):
self.pos_y = y

def get_fov(self):
return self.fov

# Rotate on XYZ and save the new points in our list
def rotate(self):

for v in self.vertices:

start_x, start_y, start_z = v

# X
y = start_y * self.cos - start_z * self.sin
z = start_y * self.sin + start_z * self.cos

# Y
x = start_x * self.cos - z * self.sin
z = start_x * self.sin + z * self.cos

# Z
n_y = x * self.sin + y * self.cos
n_x = x * self.cos - y * self.sin

y = n_y
x = n_x

point = self.to_2d(x, y, z, self.pos_x, self.pos_y, self.fov, self.distance)
self.cube_points.append(point)

# Draw the edges of the cube so we can see it on screen!
def draw(self):

for edge in self.edges:
display.line(self.cube_points[edge[0]][0], self.cube_points[edge[0]][1], self.cube_points[edge[1]][0], self.cube_points[edge[1]][1])

self._update()


# Setup the first 3 cubes.
cubes = [Cube(16, 4, WIDTH / 2, HEIGHT / 2, 1.0), Cube(32, 4, 100, 100, 0.9), Cube(32, 4, 100, 100, 0.5)]

# Set our initial pen colour
pen = display.create_pen_hsv(1.0, 1.0, 1.0)

while 1:

# We'll use this for cycling through the rainbow
t = time.ticks_ms() / 1000

# Set the layer we're going to be drawing to.
display.set_layer(0)

# Clear the screen and set the pen colour for the cubes
display.set_pen(BLACK)
display.clear()
display.set_pen(WHITE)
display.text("Flying Cubes!", 90, 110, 320, 1)
display.reset_pen(pen)
pen = display.create_pen_hsv(t, 1.0, 1.0)
display.set_pen(pen)

# Now we go through each Cube object we have in 'cubes'
# and increase the FOV angle so it appears closer to the screen.
# We'll also rotate the cube during this loop too.
for i, cube in enumerate(cubes):
fov = cube.get_fov()
fov += 5
cube.set_fov(fov)
cube.rotate()
cube.draw()

# We want the cubes to disappear randomly as they appear close to the screen, so we'll decide when this happens based on the current FOV
# We'll replace that cube with a new one and start the process from the beginning!
if fov > randint(250, 600):
cubes[i] = Cube(8, 4, randint(10, WIDTH), randint(10, HEIGHT), randrange(4, 9) / 10)

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

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

# Couple of colours for use later
BLUE = display.create_pen(28, 181, 202)
WHITE = display.create_pen(255, 255, 255)


while True:

# Clear the screen and use blue as the background colour
display.set_pen(BLUE)
display.clear()
# Set the pen to a different colour otherwise we won't be able to see the text!
display.set_pen(WHITE)

# draw the text
display.text("Hello!", 10, 85, WIDTH, 8)

# Finally we update the screen with our changes :)
portal.update(display)
Binary file added examples/micro_sd.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 177faa8

Please sign in to comment.