-
Notifications
You must be signed in to change notification settings - Fork 1
/
code.py
152 lines (123 loc) · 4.41 KB
/
code.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import time
import alarm
import board
import busio
import digitalio
import neopixel_write
import adafruit_htu31d
from adafruit_epd.epd import Adafruit_EPD
from adafruit_epd.ssd1680 import Adafruit_SSD1680 # pylint: disable=unused-import
NUMBER_OF_SAMPLES = 300
def disable_led(led_pin):
pin = digitalio.DigitalInOut(led_pin)
pin.direction = digitalio.Direction.OUTPUT
pixel_off = bytearray([0, 0, 0])
neopixel_write.neopixel_write(pin, pixel_off)
def median(numbers):
numbers.sort()
return numbers[len(numbers) // 2]
def zero_values(values):
return [None for i in range(len(values))]
def initialize_display():
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
ecs = digitalio.DigitalInOut(board.D9)
dc = digitalio.DigitalInOut(board.D10)
srcs = digitalio.DigitalInOut(board.D6) # can be None to use internal memory
rst = None # digitalio.DigitalInOut(board.D8) # can be None to not use this pin
busy = None # digitalio.DigitalInOut(board.D7) # can be None to not use this pin
display = Adafruit_SSD1680(
122,
250, # 2.13" HD mono display
spi,
cs_pin=ecs,
dc_pin=dc,
sramcs_pin=srcs,
rst_pin=rst,
busy_pin=busy,
)
display.rotation = 1
return display
def update_display(display, relative_humidity, temperature):
display.fill(Adafruit_EPD.WHITE)
display.pixel(10, 100, Adafruit_EPD.BLACK)
display.text("Humble", 68, 8, Adafruit_EPD.BLACK, size=3)
display.text("Humidity Sensor", 34, 40, Adafruit_EPD.BLACK, size=2)
display.text(
"Humidity {:.1f}%".format(relative_humidity), 8, 70, Adafruit_EPD.BLACK, size=2
)
display.text(
"Temperature {:.1f} C".format(temperature), 8, 100, Adafruit_EPD.BLACK, size=2
)
display.display()
def reset_sleep_memory(
relative_humidity_index,
relative_humidity_values,
temperature_index,
temperature_values,
):
return (
0,
zero_values(relative_humidity_values),
0,
zero_values(temperature_values),
)
disable_led(board.NEOPIXEL)
# Temporary workaround for light sleep.
memory = [None for i in range(602)]
relative_humidity_values = memory[1:301] # alarm.sleep_memory[1:301]
temperature_values = memory[302:602] # alarm.sleep_memory[302:602]
relative_humidity_index = memory[0] # alarm.sleep_memory[0]
temperature_index = memory[301] # alarm.sleep_memory[301]
# Zero the memory if we haven't slept yet.
# if not alarm.wake_alarm:
(
memory[0],
relative_humidity_values,
memory[301],
temperature_values,
) = reset_sleep_memory(
memory[0], relative_humidity_values, memory[301], temperature_values
)
display = initialize_display()
i2c = board.I2C()
htu = adafruit_htu31d.HTU31D(i2c)
# Temporary workaround for light sleep.
first_run = True
while True:
(
temperature_values[memory[301]],
relative_humidity_values[memory[0]],
) = htu.measurements
# Display initial measurements so that we don't have to wait for a full collection of samples before getting a reading.
if first_run:
# if not alarm.wake_alarm:
update_display(
display,
relative_humidity_values[memory[0]],
temperature_values[memory[301]],
)
first_run = False
# Update the indices.
# alarm.sleep_memory[0] = relative_humidity_index + 1
# alarm.sleep_memory[301] = temperature_index + 1
memory[0] += 1
memory[301] += 1
# Update the display if finished taking samples.
if memory[0] >= NUMBER_OF_SAMPLES:
median_relative_humidity = median(relative_humidity_values)
median_temperature = median(temperature_values)
print("Temperature: %0.1f C" % median_temperature)
print("Humidity: %0.1f %%" % median_relative_humidity)
print("")
update_display(display, median_relative_humidity, median_temperature)
(
memory[0],
relative_humidity_values,
memory[301],
temperature_values,
) = reset_sleep_memory(
memory[0], relative_humidity_values, memory[301], temperature_values
)
# todo Deep sleep for 1 second.
al = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 1)
alarm.light_sleep_until_alarms(al)