Skip to content

Commit c725b4e

Browse files
committed
Tufty 2040: Vector examples.
Add AdvRe.af for PicoW Explorer and Tufty 2040 spectrometer examples.
1 parent a334899 commit c725b4e

File tree

4 files changed

+238
-2
lines changed

4 files changed

+238
-2
lines changed
3.66 KB
Binary file not shown.

micropython/examples/picow_explorer/vector_spectrometer.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from picographics import PicoGraphics, DISPLAY_PICO_W_EXPLORER, PEN_RGB332
66
from picovector import PicoVector, Polygon, RegularPolygon, ANTIALIAS_X4
77

8-
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
98
PINS_PICO_EXPLORER = {"sda": 20, "scl": 21}
109
i2c = PimoroniI2C(**PINS_PICO_EXPLORER)
1110

@@ -25,7 +24,7 @@
2524
vector = PicoVector(display)
2625
vector.set_antialiasing(ANTIALIAS_X4)
2726

28-
# Load an Alright Font
27+
# Load an Alright Font, find this in common/AdvRe.af
2928
result = vector.set_font("/AdvRe.af", 30)
3029

3130
WIDTH, HEIGHT = display.get_bounds()
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import time
2+
import gc
3+
4+
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
5+
from picovector import PicoVector, Polygon, RegularPolygon, Rectangle, ANTIALIAS_X4
6+
7+
8+
display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_RGB332)
9+
10+
vector = PicoVector(display)
11+
vector.set_antialiasing(ANTIALIAS_X4)
12+
13+
RED = display.create_pen(200, 0, 0)
14+
BLACK = display.create_pen(0, 0, 0)
15+
GREY = display.create_pen(200, 200, 200)
16+
WHITE = display.create_pen(255, 255, 255)
17+
18+
"""
19+
# Redefine colours for a Blue clock
20+
RED = display.create_pen(200, 0, 0)
21+
BLACK = display.create_pen(135, 159, 169)
22+
GREY = display.create_pen(10, 40, 50)
23+
WHITE = display.create_pen(14, 60, 76)
24+
"""
25+
26+
WIDTH, HEIGHT = display.get_bounds()
27+
28+
hub = RegularPolygon(int(WIDTH / 2), int(HEIGHT / 2), 24, 5)
29+
30+
face = RegularPolygon(int(WIDTH / 2), int(HEIGHT / 2), 48, int(HEIGHT / 2))
31+
32+
print(time.localtime())
33+
34+
last_second = None
35+
36+
while True:
37+
t_start = time.ticks_ms()
38+
year, month, day, hour, minute, second, _, _ = time.localtime()
39+
40+
if last_second == second:
41+
continue
42+
43+
last_second = second
44+
45+
display.set_pen(0)
46+
display.clear()
47+
48+
display.set_pen(BLACK)
49+
display.circle(int(WIDTH / 2), int(HEIGHT / 2), int(HEIGHT / 2))
50+
display.set_pen(WHITE)
51+
display.circle(int(WIDTH / 2), int(HEIGHT / 2), int(HEIGHT / 2) - 4)
52+
53+
display.set_pen(GREY)
54+
55+
for a in range(60):
56+
tick_mark = Rectangle(int(WIDTH / 2) - 3, 10, 6, int(HEIGHT / 48))
57+
vector.rotate(tick_mark, 360 / 60.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
58+
vector.translate(tick_mark, 0, 2)
59+
vector.draw(tick_mark)
60+
61+
for a in range(12):
62+
hour_mark = Rectangle(int(WIDTH / 2) - 5, 10, 10, int(HEIGHT / 10))
63+
vector.rotate(hour_mark, 360 / 12.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
64+
vector.translate(hour_mark, 0, 2)
65+
vector.draw(hour_mark)
66+
67+
angle_second = second * 6
68+
second_hand_length = int(HEIGHT / 2) - int(HEIGHT / 8)
69+
second_hand = Polygon((-2, -second_hand_length), (-2, int(HEIGHT / 8)), (2, int(HEIGHT / 8)), (2, -second_hand_length))
70+
vector.rotate(second_hand, angle_second, 0, 0)
71+
vector.translate(second_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
72+
73+
angle_minute = minute * 6
74+
angle_minute += second / 10.0
75+
minute_hand_length = int(HEIGHT / 2) - int(HEIGHT / 24)
76+
minute_hand = Polygon((-5, -minute_hand_length), (-10, int(HEIGHT / 16)), (10, int(HEIGHT / 16)), (5, -minute_hand_length))
77+
vector.rotate(minute_hand, angle_minute, 0, 0)
78+
vector.translate(minute_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
79+
80+
angle_hour = (hour % 12) * 30
81+
angle_hour += minute / 2
82+
hour_hand_length = int(HEIGHT / 2) - int(HEIGHT / 8)
83+
hour_hand = Polygon((-5, -hour_hand_length), (-10, int(HEIGHT / 16)), (10, int(HEIGHT / 16)), (5, -hour_hand_length))
84+
vector.rotate(hour_hand, angle_hour, 0, 0)
85+
vector.translate(hour_hand, int(WIDTH / 2), int(HEIGHT / 2) + 5)
86+
87+
display.set_pen(GREY)
88+
89+
vector.draw(minute_hand)
90+
vector.draw(hour_hand)
91+
vector.draw(second_hand)
92+
93+
display.set_pen(BLACK)
94+
95+
for a in range(60):
96+
tick_mark = Rectangle(int(WIDTH / 2) - 3, 10, 6, int(HEIGHT / 48))
97+
vector.rotate(tick_mark, 360 / 60.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
98+
vector.draw(tick_mark)
99+
100+
for a in range(12):
101+
hour_mark = Rectangle(int(WIDTH / 2) - 5, 10, 10, int(HEIGHT / 10))
102+
vector.rotate(hour_mark, 360 / 12.0 * a, int(WIDTH / 2), int(HEIGHT / 2))
103+
vector.draw(hour_mark)
104+
105+
vector.translate(minute_hand, 0, -5)
106+
vector.translate(hour_hand, 0, -5)
107+
vector.draw(minute_hand)
108+
vector.draw(hour_hand)
109+
110+
display.set_pen(RED)
111+
vector.translate(second_hand, 0, -5)
112+
vector.draw(second_hand)
113+
vector.draw(hub)
114+
115+
display.update()
116+
gc.collect()
117+
118+
t_end = time.ticks_ms()
119+
print(f"Took {t_end - t_start}ms")
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import math
2+
import time
3+
from pimoroni_i2c import PimoroniI2C
4+
from breakout_as7262 import BreakoutAS7262
5+
from picographics import PicoGraphics, DISPLAY_TUFTY_2040, PEN_RGB332
6+
from picovector import PicoVector, Polygon, RegularPolygon, ANTIALIAS_X4
7+
8+
PINS_TUFTY_2040 = {"sda": 4, "scl": 5}
9+
i2c = PimoroniI2C(**PINS_TUFTY_2040)
10+
11+
# Set up the AS7262 Spectrometer
12+
as7262 = BreakoutAS7262(i2c)
13+
as7262.set_gain(BreakoutAS7262.X16)
14+
as7262.set_measurement_mode(BreakoutAS7262.CONT_ROYGBR)
15+
as7262.set_illumination_current(BreakoutAS7262.MA12)
16+
as7262.set_indicator_current(BreakoutAS7262.MA4)
17+
as7262.set_leds(True, True)
18+
19+
# Set up the display
20+
display = PicoGraphics(DISPLAY_TUFTY_2040, pen_type=PEN_RGB332)
21+
display.set_backlight(0.8)
22+
23+
# Set up PicoVector
24+
vector = PicoVector(display)
25+
vector.set_antialiasing(ANTIALIAS_X4)
26+
27+
# Load an Alright Font, find this in common/AdvRe.af
28+
result = vector.set_font("/AdvRe.af", 30)
29+
30+
WIDTH, HEIGHT = display.get_bounds()
31+
32+
CENTER_X = int(WIDTH / 2)
33+
CENTER_Y = int(HEIGHT / 2)
34+
35+
RADIUS = 90
36+
DEBUG = False
37+
38+
RED = display.create_pen(255, 0, 0)
39+
ORANGE = display.create_pen(255, 128, 0)
40+
YELLOW = display.create_pen(255, 255, 0)
41+
GREEN = display.create_pen(0, 255, 0)
42+
BLUE = display.create_pen(0, 0, 255)
43+
VIOLET = display.create_pen(255, 0, 255)
44+
45+
BLACK = display.create_pen(0, 0, 0)
46+
GREY = display.create_pen(128, 128, 128)
47+
WHITE = display.create_pen(255, 255, 255)
48+
49+
LABELS = ["R", "O", "Y", "G", "B", "V"]
50+
COLS = [RED, ORANGE, YELLOW, GREEN, BLUE, VIOLET]
51+
52+
53+
# Custom regular_polygon function to give each point its own "radius"
54+
def regular_polygon(o_x, o_y, radius, rotation):
55+
sides = 6
56+
angle = math.radians(360 / sides)
57+
rotation = math.radians(rotation)
58+
59+
points = []
60+
61+
for side in range(sides):
62+
current_angle = side * angle + rotation
63+
x = math.cos(current_angle) * radius[side]
64+
y = math.sin(current_angle) * radius[side]
65+
points.append((int(x) + o_x, int(y) + o_y))
66+
67+
return points
68+
69+
70+
lines = RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS)
71+
label_points = list(RegularPolygon(CENTER_X, CENTER_Y, 6, RADIUS * 0.7, -(360 / 12)))
72+
73+
74+
while True:
75+
# Clear to black
76+
display.set_pen(BLACK)
77+
display.clear()
78+
79+
# Add the title
80+
display.set_pen(WHITE)
81+
vector.text("Spectrograph", 5, -5)
82+
83+
# Get the spectrometer readings
84+
reading = list(as7262.read())
85+
86+
# Print out the readings
87+
if DEBUG:
88+
for i in range(6):
89+
print(f"{LABELS[i]}: {reading[i]:0.2f}", end=" ")
90+
print("")
91+
92+
# Draw the lines separating each section
93+
display.set_pen(GREY)
94+
for (x, y) in lines:
95+
display.line(CENTER_X, CENTER_Y, int(x), int(y))
96+
97+
# Scale readings for display
98+
for i in range(6):
99+
reading[i] = int(reading[i] / 3.0)
100+
reading[i] = min(reading[i], RADIUS)
101+
102+
# Create a 6 point polygon with each points distance from the center
103+
# scaled by the corresponding reading.
104+
points = regular_polygon(CENTER_X, CENTER_Y, reading, 0)
105+
106+
# Split the polygon into six triangles, one for each channel
107+
# draw each one, along with its corresponding label
108+
point_a = points[-1]
109+
for i in range(6):
110+
point_b = points[i]
111+
label_x, label_y = label_points[i]
112+
display.set_pen(COLS[i])
113+
vector.text(LABELS[i], int(label_x) - 5, int(label_y) - 20)
114+
vector.draw(Polygon(point_a, point_b, (CENTER_X, CENTER_Y)))
115+
point_a = point_b
116+
117+
display.update()
118+
time.sleep(1.0 / 60)

0 commit comments

Comments
 (0)