Skip to content

Commit a738b89

Browse files
committed
Examples: adding sensor-stick-temperature.py
1 parent 96a732b commit a738b89

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# ICON [[(-20.0, 16.67), (-20.0, 12.22), (-15.56, 7.78), (-15.56, 16.67), (-20.0, 16.67)], [(-11.11, 16.67), (-11.11, 3.33), (-6.67, -1.11), (-6.67, 16.67), (-11.11, 16.67)], [(-2.22, 16.67), (-2.22, -1.11), (2.22, 3.39), (2.22, 16.67), (-2.22, 16.67)], [(6.67, 16.67), (6.67, 3.39), (11.11, -1.06), (11.11, 16.67), (6.67, 16.67)], [(15.56, 16.67), (15.56, -5.56), (20.0, -10.0), (20.0, 16.67), (15.56, 16.67)], [(-20.0, 5.17), (-20.0, -1.11), (-4.44, -16.67), (4.44, -7.78), (20.0, -23.33), (20.0, -17.06), (4.44, -1.5), (-4.44, -10.39), (-20.0, 5.17)]]
2+
# NAME Temperature
3+
# DESC Display data from your Multi Sensor Stick!
4+
5+
from presto import Presto
6+
from breakout_bme280 import BreakoutBME280
7+
from picovector import ANTIALIAS_BEST, PicoVector, Polygon, Transform
8+
import machine
9+
10+
# Setup for the i2c and bme sensor
11+
bme = BreakoutBME280(machine.I2C())
12+
13+
# Setup for the Presto display
14+
presto = Presto(ambient_light=True)
15+
display = presto.display
16+
WIDTH, HEIGHT = display.get_bounds()
17+
18+
CX = WIDTH // 2
19+
CY = HEIGHT // 2
20+
21+
# Colours
22+
BLACK = display.create_pen(0, 0, 0)
23+
hue = 0.05
24+
BACKGROUND = display.create_pen_hsv(hue, 0.8, 1.0) # We'll use this one for the background.
25+
FOREGROUND = display.create_pen_hsv(hue, 0.5, 1.0) # Slightly lighter for foreground elements.
26+
TEXT_COLOUR = display.create_pen_hsv(hue, 0.2, 1.0)
27+
28+
# Pico Vector
29+
vector = PicoVector(display)
30+
vector.set_antialiasing(ANTIALIAS_BEST)
31+
t = Transform()
32+
33+
vector.set_font("Roboto-Medium.af", 96)
34+
vector.set_font_letter_spacing(100)
35+
vector.set_font_word_spacing(100)
36+
vector.set_transform(t)
37+
38+
39+
class Widget(object):
40+
def __init__(self, x, y, w, h, radius=10, text_size=42):
41+
self.x = x
42+
self.y = y
43+
self.w = w
44+
self.h = h
45+
self.r = radius
46+
self.text = None
47+
self.size = text_size
48+
self.title = None
49+
50+
self.widget = Polygon()
51+
self.widget.rectangle(self.x, self.y, self.w, self.h, (self.r, self.r, self.r, self.r))
52+
53+
def draw(self):
54+
55+
display.set_pen(FOREGROUND)
56+
vector.draw(self.widget)
57+
58+
if self.text:
59+
display.set_pen(TEXT_COLOUR)
60+
vector.set_font_size(self.size)
61+
x, y, w, h = vector.measure_text(self.text)
62+
tx = int((self.x + self.w // 2) - (w // 2))
63+
ty = int((self.y + self.h // 2) + (h // 2)) + 5
64+
vector.text(self.text, tx, ty)
65+
66+
if self.title:
67+
display.set_pen(TEXT_COLOUR)
68+
vector.set_font_size(14)
69+
x, y, w, h = vector.measure_text(self.title)
70+
tx = int((self.x + self.w // 2) - (w // 2))
71+
ty = self.y + 15
72+
vector.text(self.title, tx, ty)
73+
74+
def set_label(self, text):
75+
self.text = text
76+
77+
def set_title(self, title):
78+
self.title = title
79+
80+
81+
# We'll use a rect with rounded corners for the background.
82+
background_rect = Polygon()
83+
background_rect.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
84+
85+
widgets = [
86+
Widget(10, 7, WIDTH - 20, HEIGHT // 2 - 5, 10, 82), # Temperature
87+
Widget(10, CY + 10, (WIDTH // 2) - 15, HEIGHT // 2 - 17, 10, 26), # Pressure
88+
Widget(CX + 5, CY + 10, (WIDTH // 2) - 15, HEIGHT // 2 - 17, 10, 52) # Humidity
89+
]
90+
91+
92+
widgets[0].set_title("Temperature")
93+
widgets[1].set_title("Pressure")
94+
widgets[2].set_title("Humidity")
95+
96+
while True:
97+
98+
# Clear screen and draw our background rectangle
99+
display.set_pen(BLACK)
100+
display.clear()
101+
display.set_pen(BACKGROUND)
102+
vector.draw(background_rect)
103+
104+
# Get readings and format strings
105+
reading = bme.read()
106+
temp_string = f"{reading[0]:.1f}C"
107+
pressure_string = f"{reading[1] // 100:.0f} hPa"
108+
humidity_string = f"{reading[2]:.0f}%"
109+
110+
# Update the widget labels
111+
widgets[0].set_label(temp_string)
112+
widgets[1].set_label(pressure_string)
113+
widgets[2].set_label(humidity_string)
114+
115+
# Draw all of our widgets to the display
116+
for w in widgets:
117+
w.draw()
118+
119+
# Update the screen so we can see our changes
120+
presto.update()

0 commit comments

Comments
 (0)