Skip to content

Commit 597e7dd

Browse files
authored
Merge pull request #2500 from adafruit/dvi_helloworld
Adding DVI hello world example
2 parents 7598f83 + 1dfd0a6 commit 597e7dd

File tree

3 files changed

+333
-0
lines changed

3 files changed

+333
-0
lines changed
Binary file not shown.
Binary file not shown.

CircuitPython_DVI_Hello_World/code.py

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

0 commit comments

Comments
 (0)