-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcv.py
270 lines (206 loc) · 6.57 KB
/
rcv.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# PiWX receiver code
#
# Using an Adafruit 16x8 LED "backpack". For now, simplistic code - no object wrapper for display.
#
# No scrolling, just fading in and out?
# TODO: send/rcv a packet number, just for fun?
# stdlibs
import board
import digitalio
import json
import os
import random
import time
# adafruit
import neopixel
import adafruit_rfm69
from adafruit_ht16k33 import matrix
import adafruit_vcnl4020
# mine
import led8x8Font
DISPLAY_HEIGHT = 8
DISPLAY_WIDTH = 16
# Define some stuff
RADIO_FREQ_MHZ = 915.0
CS = digitalio.DigitalInOut(board.RFM_CS)
RESET = digitalio.DigitalInOut(board.RFM_RST)
########################################################
# Read 16-character encryption key.
# TODO: can this fail?
ENCRYPTION_KEY = os.getenv("ENCRYPTION_KEY")
# TODO: this may be important.
# We send every X seconds, we should probably wait for 2X seconds??
LISTEN_TIMEOUT = 8
DISPLAY_TIMEOUT = 2
def get_ambient_lux(light_sensor):
# print(f"Proximity is: {light_sensor.proximity}")
if light_sensor is None:
return 1000 # full brightness, sorta
lux = light_sensor.lux
# print(f"Ambient is: {lux}")
return lux
def get_message(rfm):
'''Return the dictionary of values received by the radio, or None'''
# Look for a new packet - wait up to given timeout
print("Listening...")
packet = rfm.receive(timeout=LISTEN_TIMEOUT)
print(" Got a packet")
# If no packet was received after the timeout then None is returned.
result = None
if packet is None:
print("No packet?")
else:
pstr = packet.decode('utf8')
# print(f"Rcvd: '{pstr}'")
dict = json.loads(pstr)
# result = dict["T"] + "F " + dict["H"] + "%"
# print(f" display: '{result}'")
result = dict
return result
# def fade_to(m, start, end, step, delay):
# b = start
# while b < end:
# m.brightness = b
# time.sleep(delay)
# b += step
FADE_STEP = 0.1
FADE_SLEEP_TIME = 0.05
def fade_in(m, max=1):
b = 0
while b <= max:
m.brightness = b
time.sleep(FADE_SLEEP_TIME)
b += FADE_STEP
m.brightness = max
time.sleep(FADE_SLEEP_TIME)
def fade_out(m, start=1):
b = start
while b >= 0:
m.brightness = b
time.sleep(FADE_SLEEP_TIME)
b -= FADE_STEP
m.brightness = 0
time.sleep(FADE_SLEEP_TIME)
# For the string, create the big list of bit values (columns), left to right.
#
def make_V_rasters(string):
if len(string) == 0: # is there a better way to handle this null-input case?
string = "(no input given!)"
vrasters = []
for char in string:
# bl is the list of *horizontal* rasters for the char
bl = byte_list_for_char(char)
for bitIndex in range(DISPLAY_HEIGHT-1,-1,-1):
thisVR = 0
for hRasterIndex in range(DISPLAY_HEIGHT-1,-1,-1):
bitVal = ((1 << bitIndex) & bl[hRasterIndex])
if bitVal > 0:
thisVR += (1 << (DISPLAY_HEIGHT-hRasterIndex-1))
vrasters.append(thisVR)
# print(f"vraster (len {len(vrasters)}): {vrasters}")
return vrasters
# Display the given raster lines - full display width.
#
def display_rasters(matrix, rasters):
for y in range(DISPLAY_HEIGHT):
for x in range(len(rasters)):
matrix[x, y] = rasters[x] & (1<<(DISPLAY_HEIGHT-y-1))
def blank(matrix):
for y in range(DISPLAY_HEIGHT):
for x in range(DISPLAY_WIDTH):
matrix[x, y] = 0
def set_mode_indicator(matrix, is_temperature):
"""What?"""
# horizontal line under all digits?
# for x in range(DISPLAY_WIDTH):
# matrix[x, 7] = one_or_zero
# vertical line to left? that's better
if is_temperature:
ys = [0,1,2,3]
else:
ys = [4,5,6,7]
for y in ys:
matrix[0, y] = 1
# Return a list of the bytes for the given character.
# TODO: catch missing chars?
#
def byte_list_for_char(char):
bits = led8x8Font.FontData[char]
return bits
def init_hardware():
# Initialize RFM69 radio
rfm = adafruit_rfm69.RFM69(board.SPI(), CS, RESET, RADIO_FREQ_MHZ, encryption_key=ENCRYPTION_KEY)
print(f" {rfm.bitrate=}")
print(f" {rfm.encryption_key=}")
print(f" {rfm.frequency_deviation=}")
print(f" {rfm.rssi=}")
print(f" {rfm.temperature=}")
print()
leds = matrix.MatrixBackpack16x8(board.I2C())
# Initialize VCNL4020
sensor = None
try:
vcln = adafruit_vcnl4020.Adafruit_VCNL4020(board.I2C())
except:
print("No light sensor? Continuing....")
return rfm, leds, vcln
# ##################################################
def run():
radio, mx, sensor = init_hardware()
while True:
# adjust display brighness acccording to ambient light
lux = get_ambient_lux(sensor)
print(f"Adjust to {lux}")
# 1000 lux, "indoors near the windows on a clear day", gets full LED value.
# This seems OK, but not very scientific
max_brightness = lux / 1000
if max_brightness > 1:
max_brightness = 1
# print(f"Setting max brightness to {max_brightness}")
data = get_message(radio)
print(f"{data=}")
if data == None:
mx.brightness = .5
display_rasters(mx, make_V_rasters("??"))
else:
print("Beginning data display...")
for key in ["T", "W"]:
val = data[key]
if len(val) < 2:
val = " " + val
# print(f" {key} = '{val}'")
mx.brightness = 0
print(f" display '{val}'")
display_rasters(mx, make_V_rasters(val))
if key == "T":
set_mode_indicator(mx, True)
else:
set_mode_indicator(mx, False)
fade_in(mx, max = max_brightness)
time.sleep(DISPLAY_TIMEOUT)
fade_out(mx, start = max_brightness)
print("End data display.\n")
def test():
"""Test stuff - display timing, etc"""
mx = matrix.MatrixBackpack16x8(board.STEMMA_I2C())
while True:
for s in ["64", " 0", "66", "12"]:
mx.brightness = 0
display_rasters(mx, make_V_rasters(s))
fade_in(mx)
time.sleep(1)
fade_out(mx)
blank(mx)
# test()
import traceback
while True:
try:
run()
except KeyboardInterrupt:
break
except Exception as e:
print(f"Got exception {e}; going around again!")
traceback.print_exception(e)
print("DONE!")
# while True:
# pass