Skip to content

Commit 246a6fd

Browse files
authored
Merge pull request #63 from pimoroni/examples/mss-temperature
Examples/mss temperature
2 parents 4d41681 + 72699a4 commit 246a6fd

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

boards/presto/manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ cubes.py
66
image_gallery.py
77
random_maze.py
88
secrets.py
9+
sensor-stick-temperature.py
910
stop_watch.py
1011
tomato.py
1112
vector_clock_full.py
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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 Presto display
11+
presto = Presto(ambient_light=True)
12+
display = presto.display
13+
WIDTH, HEIGHT = display.get_bounds()
14+
15+
CX = WIDTH // 2
16+
CY = HEIGHT // 2
17+
18+
# Colours
19+
BLACK = display.create_pen(0, 0, 0)
20+
hue = 0.05
21+
BACKGROUND = display.create_pen_hsv(hue, 0.8, 1.0) # We'll use this one for the background.
22+
FOREGROUND = display.create_pen_hsv(hue, 0.5, 1.0) # Slightly lighter for foreground elements.
23+
TEXT_COLOUR = display.create_pen_hsv(hue, 0.2, 1.0)
24+
25+
# Pico Vector
26+
vector = PicoVector(display)
27+
vector.set_antialiasing(ANTIALIAS_BEST)
28+
t = Transform()
29+
30+
vector.set_font("Roboto-Medium.af", 96)
31+
vector.set_font_letter_spacing(100)
32+
vector.set_font_word_spacing(100)
33+
vector.set_transform(t)
34+
35+
36+
def show_message(text):
37+
display.set_pen(BACKGROUND)
38+
display.clear()
39+
display.set_pen(FOREGROUND)
40+
display.text(f"{text}", 5, 10, WIDTH, 2)
41+
presto.update()
42+
43+
44+
# Setup for the i2c and bme sensor
45+
try:
46+
bme = BreakoutBME280(machine.I2C())
47+
except RuntimeError:
48+
while True:
49+
show_message("No Multi-Sensor stick detected!\n\nConnect and try again.")
50+
51+
52+
class Widget(object):
53+
def __init__(self, x, y, w, h, radius=10, text_size=42):
54+
self.x = x
55+
self.y = y
56+
self.w = w
57+
self.h = h
58+
self.r = radius
59+
self.text = None
60+
self.size = text_size
61+
self.title = None
62+
63+
self.widget = Polygon()
64+
self.widget.rectangle(self.x, self.y, self.w, self.h, (self.r, self.r, self.r, self.r))
65+
66+
def draw(self):
67+
68+
display.set_pen(FOREGROUND)
69+
vector.draw(self.widget)
70+
71+
if self.text:
72+
display.set_pen(TEXT_COLOUR)
73+
vector.set_font_size(self.size)
74+
x, y, w, h = vector.measure_text(self.text)
75+
tx = int((self.x + self.w // 2) - (w // 2))
76+
ty = int((self.y + self.h // 2) + (h // 2)) + 5
77+
vector.text(self.text, tx, ty)
78+
79+
if self.title:
80+
display.set_pen(TEXT_COLOUR)
81+
vector.set_font_size(14)
82+
x, y, w, h = vector.measure_text(self.title)
83+
tx = int((self.x + self.w // 2) - (w // 2))
84+
ty = self.y + 15
85+
vector.text(self.title, tx, ty)
86+
87+
def set_label(self, text):
88+
self.text = text
89+
90+
def set_title(self, title):
91+
self.title = title
92+
93+
94+
# We'll use a rect with rounded corners for the background.
95+
background_rect = Polygon()
96+
background_rect.rectangle(0, 0, WIDTH, HEIGHT, (10, 10, 10, 10))
97+
98+
widgets = [
99+
Widget(10, 7, WIDTH - 20, HEIGHT // 2 - 5, 10, 82), # Temperature
100+
Widget(10, CY + 10, (WIDTH // 2) - 15, HEIGHT // 2 - 17, 10, 26), # Pressure
101+
Widget(CX + 5, CY + 10, (WIDTH // 2) - 15, HEIGHT // 2 - 17, 10, 52) # Humidity
102+
]
103+
104+
105+
widgets[0].set_title("Temperature")
106+
widgets[1].set_title("Pressure")
107+
widgets[2].set_title("Humidity")
108+
109+
while True:
110+
111+
# Clear screen and draw our background rectangle
112+
display.set_pen(BLACK)
113+
display.clear()
114+
display.set_pen(BACKGROUND)
115+
vector.draw(background_rect)
116+
117+
# Get readings and format strings
118+
try:
119+
reading = bme.read()
120+
except RuntimeError:
121+
while True:
122+
show_message("Failed to get reading from BME280.\n\nCheck connection and reset :)")
123+
124+
temp_string = f"{reading[0]:.1f}C"
125+
pressure_string = f"{reading[1] // 100:.0f} hPa"
126+
humidity_string = f"{reading[2]:.0f}%"
127+
128+
# Update the widget labels
129+
widgets[0].set_label(temp_string)
130+
widgets[1].set_label(pressure_string)
131+
widgets[2].set_label(humidity_string)
132+
133+
# Draw all of our widgets to the display
134+
for w in widgets:
135+
w.draw()
136+
137+
# Update the screen so we can see our changes
138+
presto.update()

0 commit comments

Comments
 (0)