picowidget/main.py
2024-11-19 20:19:14 +00:00

131 lines
4.6 KiB
Python

#Copyright 2024 Frederick Boniface
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from Pico_ePaper_2_13_v4 import EPD_2in13_V4_Landscape
from ezFBfont import ezFBfont
import ezFBfont_spleen_16x32_ascii_30 as spleen30Font
import ezFBfont_spleen_8x16_ascii_14 as spleen14Font
import ntptime, network, utime
import config, open_meteo
epd = EPD_2in13_V4_Landscape()
WHT = 0xff
BLK = 0x00
spleen30wht = ezFBfont(epd, spleen30Font, fg=WHT, bg=BLK)
spleen30blk = ezFBfont(epd, spleen30Font, fg=BLK, bg=WHT)
spleen14wht = ezFBfont(epd, spleen14Font, fg=WHT, bg=BLK)
spleen14blk = ezFBfont(epd, spleen14Font, fg=BLK, bg=WHT)
epd.fill(WHT)
epd.display(epd.buffer)
## Functions
def update_time():
try:
ntptime.host = "time.fjla.net"
ntptime.settime()
except Exception as e:
raise e
# Load icon into buffer
def blit_to_framebuffer(framebuffer, fb_width, x, y, image_bytes, img_width, img_height):
for row in range(img_height):
for col in range(img_width):
byte_index = (row * (img_width // 8)) + (col // 8)
bit_index = 7 - (col % 8)
bit = (image_bytes[byte_index] >> bit_index) & 1
framebuffer.pixel(x + col, y + row, bit)
# Load weather data into buffer
def update_weather():
# Get and load weather data
weather = open_meteo.get_weather()
blit_to_framebuffer(epd, epd.width, 3, 5, weather['icon'], 72, 72)
spleen30wht.write(f"{str(weather['current_temp'])} C", 3, 82)
spleen14wht.write(f"Min: {str(weather['min'])} C", 82, 10)
spleen14wht.write(f"Max: {str(weather['max'])} C", 82, 25)
#epd.image1Gray.text("Apparent min: " + str(weather['apparent_min']) + "C", 75, 25, WHITE)
#epd.image1Gray.text("Apparent max: " + str(weather['apparent_max']) + "C", 75, 35, WHITE)
return
def get_day(day_no):
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
return days[day_no]
## Init
spleen30blk.write("PicoWidget-v0.1", 5, 5)
epd.text("Initialising", 5, 40, BLK)
epd.display(epd.buffer)
epd.displayPartial(epd.buffer)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(config.WIFI_SSID, config.WIFI_PASS)
max_attempts = 10
attempt = 0
while not wlan.isconnected() and attempt < max_attempts:
utime.sleep(1)
attempt += 1
if wlan.isconnected():
epd.text(f"WiFi Connection ({config.WIFI_SSID}): OK", 5, 52, BLK)
else:
epd.text(f"WiFi Connection ({config.WiFi_SSID}): FAIL", 5, 52, BLK)
try:
update_time()
epd.text(f"NTP Update: OK", 5, 64, BLK)
except Exception as e:
epd.text(f"NTP Update: FAIL", 5, 64, BLK)
print(e)
epd.text(f"Location: {config.LATLONG}", 5, 76, BLK)
epd.text(f"Buffering display...", 5, 88, BLK)
epd.displayPartial(epd.buffer)
tick = 0
displayed_time = None
displayed_msg = None
while True:
partial_display_needed = False
full_display_needed = False
if tick % 360 == 0:
epd.fill(BLK)
update_weather()
full_display_needed = True
update_time()
t = utime.localtime()
t_str = "{:02}:{:02}".format(t[3], t[4])
d_str = "{:02}/{:02}/{:4}".format(t[2], t[1], t[0])
if t_str != displayed_time or full_display_needed:
spleen30wht.write(t_str, 160, 60)
spleen14wht.write(get_day(t[6]), 160, 90)
spleen14wht.write(d_str, 160, 110)
displayed_time = t_str
partial_display_needed = True
if not full_display_needed:
if partial_display_needed:
epd.displayPartial(epd.buffer)
if full_display_needed:
epd.display(epd.buffer)
epd.displayPartial(epd.buffer)
tick += 1
if tick >= 7200:
tick = 0
utime.sleep(20)