-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
96 lines (79 loc) · 2.96 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
import time
import board
import wifi
import socketpool
import terminalio
import displayio
import os
import adafruit_requests as requests
from adafruit_matrixportal.matrixportal import MatrixPortal
from adafruit_matrixportal.matrix import Matrix
from adafruit_display_text import label
import adafruit_display_text
# Initialize MatrixPortal
matrixportal = MatrixPortal(status_neopixel=board.NEOPIXEL, debug=True)
# Wi-Fi settings and connection
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to wifi")
print(f"My IP address is {wifi.radio.ipv4_address}")
# Initialize the socket and requests
pool = socketpool.SocketPool(wifi.radio)
requests = requests.Session(pool)
matrix = Matrix()
display = matrix.display
g = displayio.Group()
# Static text area
text_area = label.Label(terminalio.FONT, text="", color=0xC60003)
g.append(text_area)
# Scrolling text area
scroll_text_area = label.Label(terminalio.FONT, text="Waiting for data...", color=0xffffff)
scroll_text_area.x = display.width
scroll_text_area.y = display.height - 10
g.append(scroll_text_area)
# Set the group on the display
display.show(g)
# Timing for API updates
last_update_time = time.monotonic()
update_interval = 10 # Update every 10 seconds
API_URL = os.getenv("API_URL")
def scroll(line):
line.x = line.x - 1
line_width = line.bounding_box[2]
if line.x < -line_width:
line.x = display.width
def format_number_with_commas(number_str):
formatted_number = ""
for i in range(len(number_str)-1, -1, -1):
formatted_number = number_str[i] + formatted_number
if (len(number_str)-i) % 3 == 0 and i != 0:
formatted_number = "," + formatted_number
return formatted_number
# Main loop
while True:
current_time = time.monotonic()
# Update data every 10 seconds
if current_time - last_update_time > update_interval:
# Fetch data from API
response = requests.get(API_URL)
print(response)
data = response.json()
# Update static text area
message = str(data["count"][0]["value"])
# if message is the same as the previous message, don't update
if text_area.text == message:
continue
formatted_message = format_number_with_commas(message)
text_area.text = formatted_message
text_width = text_area.bounding_box[2]
text_area.x = (display.width - text_width) // 2
text_area.y = (display.height - text_area.bounding_box[3]) // 2
# Update scrolling text area
scrolling_data = str(data["reason"][0]["reason"]) # Adjust field name as needed
scroll_text_area.text = scrolling_data
scroll_text_area.x = display.width # Reset position for scrolling
# Update last update time
last_update_time = current_time
# Scroll the text continuously
scroll(scroll_text_area)
# Short delay for smooth scrolling
time.sleep(0.05) # Adjust for desired scrolling speed