Skip to content

Commit a2a0564

Browse files
authored
Merge pull request #3096 from FoamyGuy/fruit_jam_examples
Fruit Jam CircuitPython examples
2 parents 11fb234 + d655c58 commit a2a0564

File tree

13 files changed

+645
-0
lines changed

13 files changed

+645
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
CircuitPython Digital Input Example - Blinking an LED using the built-in button.
6+
"""
7+
import board
8+
import digitalio
9+
10+
led = digitalio.DigitalInOut(board.LED)
11+
led.direction = digitalio.Direction.OUTPUT
12+
13+
button = digitalio.DigitalInOut(board.BUTTON1)
14+
button.switch_to_input(pull=digitalio.Pull.UP)
15+
16+
while True:
17+
if not button.value:
18+
led.value = False
19+
else:
20+
led.value = True
Binary file not shown.
Binary file not shown.
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
3+
# SPDX-FileCopyrightText: Adapted from Phil B.'s 16bit_hello Arduino Code
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
import gc
8+
import math
9+
from random import randint
10+
import time
11+
import displayio
12+
import vectorio
13+
import terminalio
14+
import supervisor
15+
import simpleio
16+
from adafruit_bitmap_font import bitmap_font
17+
from adafruit_display_text import label, wrap_text_to_lines
18+
from adafruit_display_shapes.rect import Rect
19+
from adafruit_display_shapes.circle import Circle
20+
from adafruit_display_shapes.roundrect import RoundRect
21+
from adafruit_display_shapes.triangle import Triangle
22+
from adafruit_display_shapes.line import Line
23+
24+
display = supervisor.runtime.display
25+
26+
bitmap = displayio.Bitmap(display.width, display.height, 3)
27+
28+
red = 0xff0000
29+
yellow = 0xcccc00
30+
orange = 0xff5500
31+
blue = 0x0000ff
32+
pink = 0xff00ff
33+
purple = 0x5500ff
34+
white = 0xffffff
35+
green = 0x00ff00
36+
aqua = 0x125690
37+
38+
palette = displayio.Palette(3)
39+
palette[0] = 0x000000 # black
40+
palette[1] = white
41+
palette[2] = yellow
42+
43+
palette.make_transparent(0)
44+
45+
tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette)
46+
47+
group = displayio.Group()
48+
49+
def clean_up(group_name):
50+
for _ in range(len(group_name)):
51+
group_name.pop()
52+
gc.collect()
53+
54+
def show_shapes():
55+
gc.collect()
56+
cx = int(display.width / 2)
57+
cy = int(display.height / 2)
58+
minor = min(cx, cy)
59+
pad = 5
60+
size = minor - pad
61+
half = int(size / 2)
62+
rect = Rect(cx - minor, cy - minor, size, size, stroke = 1, fill=red, outline = red)
63+
tri = Triangle(cx + pad, cy - pad, cx + pad + half, cy - minor,
64+
cx + minor - 1, cy - pad, fill=green, outline = green)
65+
circ = Circle(cx - pad - half, cy + pad + half, half, fill=blue, stroke = 1, outline = blue)
66+
rnd = RoundRect(cx + pad, cy + pad, size, size, int(size / 5), stroke = 1,
67+
fill=yellow, outline = yellow)
68+
69+
group.append(rect)
70+
group.append(tri)
71+
group.append(circ)
72+
group.append(rnd)
73+
rect.fill = None
74+
tri.fill = None
75+
circ.fill = None
76+
rnd.fill = None
77+
78+
time.sleep(2)
79+
80+
rect.fill = red
81+
tri.fill = green
82+
circ.fill = blue
83+
rnd.fill = yellow
84+
time.sleep(2)
85+
clean_up(group)
86+
del rect
87+
del tri
88+
del circ
89+
del rnd
90+
gc.collect()
91+
92+
def sine_chart():
93+
gc.collect()
94+
cx = int(display.width / 2)
95+
cy = int(display.height / 2)
96+
minor = min(cx, cy)
97+
major = max(cx, cy)
98+
99+
group.append(Line(cx, 0, cx, display.height, blue)) # v
100+
group.append(Line(0, cy, display.width, cy, blue)) # h
101+
102+
for i in range(10):
103+
_n = simpleio.map_range(i, 0, 10, 0, major - 1)
104+
n = int(_n)
105+
group.append(Line(cx - n, cy - 5, cx - n, (cy - 5) + 11, blue)) # v
106+
group.append(Line(cx + n, cy - 5, cx + n, (cy - 5) + 11, blue)) # v
107+
group.append(Line(cx - 5, cy - n, (cx - 5) + 11, cy - n, blue)) # h
108+
group.append(Line(cx - 5, cy + n, (cx - 5) + 11, cy + n, blue)) # h
109+
110+
for x in range(display.width):
111+
y = cy - int(math.sin((x - cx) * 0.05) * float(minor * 0.5))
112+
bitmap[x, y] = 1
113+
group.append(tile_grid)
114+
time.sleep(2)
115+
clean_up(group)
116+
117+
def widget0():
118+
gc.collect()
119+
data = [31, 42, 36, 58, 67, 88]
120+
num_points = len(data)
121+
122+
text_area = label.Label(terminalio.FONT, text="Widget Sales", color=white)
123+
text_area.anchor_point = (0.5, 0.0)
124+
text_area.anchored_position = (display.width / 2, 3)
125+
group.append(text_area)
126+
for i in range(11):
127+
_x = simpleio.map_range(i, 0, 10, 0, display.width - 1)
128+
x = int(_x)
129+
group.append(Line(x, 20, x, display.height, blue))
130+
_y = simpleio.map_range(i, 0, 10, 20, display.height - 1)
131+
y = int(_y)
132+
group.append(Line(0, y, display.width, y, blue))
133+
prev_x = 0
134+
_prev_y = simpleio.map_range(data[0], 0, 100, display.height - 1, 20)
135+
prev_y = int(_prev_y)
136+
for i in range(1, num_points):
137+
_new_x = simpleio.map_range(i, 0, num_points - 1, 0, display.width - 1)
138+
new_x = int(_new_x)
139+
_new_y = simpleio.map_range(data[i], 0, 100, display.height - 1, 20)
140+
new_y = int(_new_y)
141+
group.append(Line(prev_x, prev_y, new_x, new_y, aqua))
142+
prev_x = new_x
143+
prev_y = new_y
144+
145+
for i in range(num_points):
146+
_x = simpleio.map_range(i, 0, num_points - 1, 0, display.width - 1)
147+
x = int(_x)
148+
_y = simpleio.map_range(data[i], 0, 100, display.height - 1, 20)
149+
y = int(_y)
150+
group.append(Circle(x, y, 5, fill=None, stroke = 2, outline = white))
151+
152+
time.sleep(2)
153+
clean_up(group)
154+
155+
def widget1():
156+
gc.collect()
157+
data = [31, 42, 36, 58, 67, 88]
158+
num_points = len(data)
159+
bar_width = int(display.width / num_points) - 4
160+
x_mapped_w = display.width + 2
161+
h_mapped_h = display.height + 20
162+
163+
text_area = label.Label(terminalio.FONT, text="Widget Sales", color=white)
164+
text_area.anchor_point = (0.5, 0.0)
165+
text_area.anchored_position = (display.width / 2, 3)
166+
group.append(text_area)
167+
for i in range(11):
168+
_y = simpleio.map_range(i, 0, 10, 20, display.height - 1)
169+
y = int(_y)
170+
group.append(Line(0, y, display.width, y, blue))
171+
for i in range(num_points):
172+
_x = simpleio.map_range(i, 0, num_points, 0, x_mapped_w)
173+
x = int(_x)
174+
_height = simpleio.map_range(data[i], 0, 100, h_mapped_h, 0)
175+
height = int(_height)
176+
group.append(vectorio.Rectangle(pixel_shader=palette, width=bar_width,
177+
height=display.height + 1, x=x, y=height, color_index = 2))
178+
179+
time.sleep(2)
180+
clean_up(group)
181+
182+
def text_align():
183+
gc.collect()
184+
TEXT = "hello world"
185+
186+
text_area_top_left = label.Label(terminalio.FONT, text=TEXT, color=red)
187+
text_area_top_left.anchor_point = (0.0, 0.0)
188+
text_area_top_left.anchored_position = (0, 0)
189+
190+
text_area_top_middle = label.Label(terminalio.FONT, text=TEXT, color=orange)
191+
text_area_top_middle.anchor_point = (0.5, 0.0)
192+
text_area_top_middle.anchored_position = (display.width / 2, 0)
193+
194+
text_area_top_right = label.Label(terminalio.FONT, text=TEXT, color=yellow)
195+
text_area_top_right.anchor_point = (1.0, 0.0)
196+
text_area_top_right.anchored_position = (display.width, 0)
197+
198+
text_area_middle_left = label.Label(terminalio.FONT, text=TEXT, color=green)
199+
text_area_middle_left.anchor_point = (0.0, 0.5)
200+
text_area_middle_left.anchored_position = (0, display.height / 2)
201+
202+
text_area_middle_middle = label.Label(terminalio.FONT, text=TEXT, color=aqua)
203+
text_area_middle_middle.anchor_point = (0.5, 0.5)
204+
text_area_middle_middle.anchored_position = (display.width / 2, display.height / 2)
205+
206+
text_area_middle_right = label.Label(terminalio.FONT, text=TEXT, color=blue)
207+
text_area_middle_right.anchor_point = (1.0, 0.5)
208+
text_area_middle_right.anchored_position = (display.width, display.height / 2)
209+
210+
text_area_bottom_left = label.Label(terminalio.FONT, text=TEXT, color=purple)
211+
text_area_bottom_left.anchor_point = (0.0, 1.0)
212+
text_area_bottom_left.anchored_position = (0, display.height)
213+
214+
text_area_bottom_middle = label.Label(terminalio.FONT, text=TEXT, color=pink)
215+
text_area_bottom_middle.anchor_point = (0.5, 1.0)
216+
text_area_bottom_middle.anchored_position = (display.width / 2, display.height)
217+
218+
text_area_bottom_right = label.Label(terminalio.FONT, text=TEXT, color=white)
219+
text_area_bottom_right.anchor_point = (1.0, 1.0)
220+
text_area_bottom_right.anchored_position = (display.width, display.height)
221+
222+
group.append(text_area_top_middle)
223+
group.append(text_area_top_left)
224+
group.append(text_area_top_right)
225+
group.append(text_area_middle_middle)
226+
group.append(text_area_middle_left)
227+
group.append(text_area_middle_right)
228+
group.append(text_area_bottom_middle)
229+
group.append(text_area_bottom_left)
230+
group.append(text_area_bottom_right)
231+
232+
time.sleep(2)
233+
clean_up(group)
234+
235+
def custom_font():
236+
gc.collect()
237+
my_font = bitmap_font.load_font("/Helvetica-Bold-16.pcf")
238+
text_sample = "The quick brown fox jumps over the lazy dog."
239+
text_sample = "\n".join(wrap_text_to_lines(text_sample, 28))
240+
text_area = label.Label(my_font, text="Custom Font", color=white)
241+
text_area.anchor_point = (0.0, 0.0)
242+
text_area.anchored_position = (0, 0)
243+
244+
sample_text = label.Label(my_font, text=text_sample)
245+
sample_text.anchor_point = (0.5, 0.5)
246+
sample_text.anchored_position = (display.width / 2, display.height / 2)
247+
248+
group.append(text_area)
249+
group.append(sample_text)
250+
251+
time.sleep(2)
252+
clean_up(group)
253+
254+
del my_font
255+
gc.collect()
256+
257+
def bitmap_example():
258+
gc.collect()
259+
blinka_bitmap = displayio.OnDiskBitmap("/blinka_computer.bmp")
260+
blinka_grid = displayio.TileGrid(blinka_bitmap, pixel_shader=blinka_bitmap.pixel_shader)
261+
gc.collect()
262+
group.append(blinka_grid)
263+
264+
time.sleep(2)
265+
clean_up(group)
266+
267+
del blinka_grid
268+
del blinka_bitmap
269+
gc.collect()
270+
271+
def sensor_values():
272+
gc.collect()
273+
text_x = "X: %d" % randint(-25, 25)
274+
text_y = "Y: %d" % randint(-25, 25)
275+
text_z = "Z: %d" % randint(-25, 25)
276+
x_text = label.Label(terminalio.FONT, text=text_x, color=red)
277+
x_text.anchor_point = (0.0, 0.0)
278+
x_text.anchored_position = (2, 0)
279+
y_text = label.Label(terminalio.FONT, text=text_y, color=green)
280+
y_text.anchor_point = (0.0, 0.0)
281+
y_text.anchored_position = (2, 10)
282+
z_text = label.Label(terminalio.FONT, text=text_z, color=blue)
283+
z_text.anchor_point = (0.0, 0.0)
284+
z_text.anchored_position = (2, 20)
285+
group.append(x_text)
286+
group.append(y_text)
287+
group.append(z_text)
288+
289+
for i in range(40):
290+
if i == 10:
291+
group.scale = 2
292+
elif i == 20:
293+
group.scale = 3
294+
elif i == 30:
295+
group.scale = 4
296+
x_text.text = "X: %d" % randint(-50, 50)
297+
y_text.text = "Y: %d" % randint(-50, 50)
298+
z_text.text = "Z: %d" % randint(-50, 50)
299+
time.sleep(0.1)
300+
time.sleep(0.1)
301+
clean_up(group)
302+
group.scale = 1
303+
304+
display.root_group = group
305+
306+
while True:
307+
show_shapes()
308+
sine_chart()
309+
widget0()
310+
widget1()
311+
text_align()
312+
custom_font()
313+
bitmap_example()
314+
sensor_values()

0 commit comments

Comments
 (0)