-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
162 lines (125 loc) · 4.42 KB
/
main.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
import asyncio
import gc
import json
import time
import network
import requests
from machine import Pin
from picographics import DISPLAY_PICO_DISPLAY_2, PEN_P4, PicoGraphics
from pimoroni import RGBLED
from anim_display import AnimatedDisplay
DEPARTMENT = None
URL_TO_QUERY = None
URL_ALIVE = None
# stored ssid and password
with open("config.json", "r") as f:
config = json.load(f)
DEPARTMENT = config.get("department")
rest_url = config.get("server_rest_url_prefix")
URL_TO_QUERY = rest_url + f"{DEPARTMENT.lower()}-open-notifications"
URL_ALIVE = rest_url + f"{DEPARTMENT.lower()}-status"
print(
f"config.json loaded, settings are: \n{DEPARTMENT=}\n{URL_TO_QUERY=}\n{URL_ALIVE=}"
)
##################### Display setup ###########################################
# We're only using a few colours so we can use a 4 bit/16 colour palette and save RAM!
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)
display.set_backlight(0.4)
display.set_font("bitmap8")
led = RGBLED(26, 27, 28)
led.set_rgb(0, 0, 0)
swA = Pin(12, Pin.IN, Pin.PULL_UP)
swB = Pin(13, Pin.IN, Pin.PULL_UP)
swX = Pin(14, Pin.IN, Pin.PULL_UP)
swY = Pin(15, Pin.IN, Pin.PULL_UP)
def sw_handlerX(pin):
time.sleep(0.05)
global display
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=180)
def sw_handlerY(pin):
time.sleep(0.05)
global display
display = PicoGraphics(display=DISPLAY_PICO_DISPLAY_2, pen_type=PEN_P4, rotate=0)
swX.irq(trigger=Pin.IRQ_FALLING, handler=sw_handlerX)
swY.irq(trigger=Pin.IRQ_FALLING, handler=sw_handlerY)
WHITE = display.create_pen(255, 255, 255)
BLACK = display.create_pen(0, 0, 0)
RED = display.create_pen(255, 0, 0)
ORANGE = display.create_pen(255, 165, 0)
GREEN = display.create_pen(0, 220, 0)
# a handy function to clear the screen
def clear():
display.set_pen(BLACK)
led.set_rgb(0, 0, 0)
display.clear()
display.update()
def connect_to_wifi():
led.set_rgb(255, 0, 0)
wlan = network.WLAN(network.STA_IF) # Access WLAN interface
wlan.active(True) # Activate the interface
mac = wlan.config("mac") # Get MAC address
mac_address = ":".join(["{:02x}".format(b) for b in mac])
wifi_ssid = "USB-PSK"
wifi_password = config.get(mac_address)
wlan.connect(wifi_ssid, wifi_password)
while not wlan.isconnected():
led.set_rgb(0, 0, 255)
print("Waiting for connection...")
time.sleep(1)
led.set_rgb(0, 255, 0)
ip_address = wlan.ifconfig()[0]
print(f"MAC: {mac_address}")
print(f"IP: {ip_address}")
return mac_address, ip_address
def parse_response(url):
response = requests.get(url).text
parts = response.strip().split(";")
status = int(parts[1])
prio = parts[2]
active = status == 1
return active, prio
mac, ip_address = connect_to_wifi()
display.set_pen(GREEN)
led.set_rgb(0, 100, 0)
INIT_MESSAGE_TIMEOUT = 5
display.set_pen(BLACK)
display.text("WiFi connected!", 10, 20, scale=3)
display.text(f"MAC: {mac}", 10, 60, scale=3)
display.text(f"IP: {ip_address}", 10, 100, scale=3)
display.text("Display will go off in sec", 10, 140, scale=3)
display.text("3 sec", 10, 180, scale=3)
display.update()
time.sleep(3)
clear()
async def keep_alive():
while True:
print("sending alive signal")
res = requests.get(URL_ALIVE)
print(f"got response: {res.text=}")
gc.collect()
await asyncio.sleep(20)
async def run_animation(display):
while True:
if display.active:
display.animate_circles()
await asyncio.sleep(0.002)
async def check_and_display(display):
while True:
print(f"Checking {DEPARTMENT} ...")
active, prio = parse_response(URL_TO_QUERY)
print(f"Got response: {active=},{prio=}")
display.active = active
if active:
display.set_prio(prio)
else:
# Clear the display if not active
display.clear_display()
await asyncio.sleep(10)
async def main(display, department):
display = AnimatedDisplay(display, department)
# Schedule check_and_display and keep_alive tasks
await asyncio.gather(
run_animation(display), check_and_display(display), keep_alive()
)
# Start the asyncio event loop
asyncio.run(main(display, DEPARTMENT))