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