commit 4a4410927296c7f5cbcc93c3ca9db07c7b52e191 from: Matthias L. Jugel date: Wed Nov 15 19:13:03 2023 UTC add hsv based rainbow colors commit - a95202bac3e85ef9bcd0d844d5412ad53831b16c commit + 4a4410927296c7f5cbcc93c3ca9db07c7b52e191 blob - f211938c51feaa91f77f2202114b4ae2b9e7ee19 blob + 27edab4823db3a3d4f60cdb9bf4552fabe8bdf4a --- src/main.py +++ src/main.py @@ -7,7 +7,9 @@ import ntptime import ledmatrix import framebuf +from hsvrgb import hsv_to_rgb565 + class Display: _buf = None fb = None @@ -37,26 +39,39 @@ def localtime(): return cet +def rgb(r, g, b): + """RGB565 - (31,63,31)""" + return (r << 11) | (g << 5) | b + + def main(): - (ssid, password) = credentials["network"] + try: + (ssid, password) = credentials["network"] - nic = network.WLAN(network.STA_IF) - if not nic.isconnected(): - nic.active(True) - nic.connect(ssid, password) - while not nic.isconnected(): - pass - else: - print("Already connected") + nic = network.WLAN(network.STA_IF) + if not nic.isconnected(): + nic.active(True) + nic.connect(ssid, password) + tries = 10 + while not nic.isconnected(): + print(tries) + tries = tries - 1 + if tries <= 0: + raise OSError + pass + print("NIC: connection established") + else: + print("NIC: already connected") - print("Connection successful") - print(nic.ifconfig()) + print(nic.ifconfig()) - rtc = RTC() - ntptime.settime() - (year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime() - print("UTC Time: ") - print((year, month, day, hours, minutes, seconds)) + rtc = RTC() + ntptime.settime() + (year, month, day, weekday, hours, minutes, seconds, subseconds) = rtc.datetime() + print("UTC Time: ") + print((year, month, day, hours, minutes, seconds)) + except OSError as e: + print(f"NIC: connection could not be established {e}") # initialize the LED display ledmatrix.init(io_colors=(25, 26, 27, 14, 12, 13), @@ -65,13 +80,21 @@ def main(): ledmatrix.set_brightness(62) display = Display() - color = 0xffff + color = rgb(31, 63, 31) + w = 0.0 while True: + w = (w + 2) % 360 t = localtime() (year, month, day, hour, minute, seconds) = t[0:6] display.fb.fill(0) - display.fb.rect(0, 0, 64, 64, 0xff20) + display.fb.rect(0, 0, 64, 64, rgb(31, 63, 0)) + + display.fb.rect(2, 2, 10, 10, rgb(31, 0, 0), True) + display.fb.rect(13, 2, 10, 10, rgb(0, 63, 0), True) + display.fb.rect(24, 2, 10, 10, rgb(0, 0, 31), True) + display.fb.rect(35, 2, 10, 10, hsv_to_rgb565(w/360.0, 1, 1), True) + display.fb.text(f"{day:02d}.{month:02d}.{str(year)[2:]}", 0, 24, color) display.fb.text(f"{hour:02d}:{minute:02d}:{seconds:02d}", 0, 34, color) display.show() blob - /dev/null blob + 5c936f0d0ba83b9788367a0bba670d6f83d99628 (mode 644) --- /dev/null +++ src/hsvrgb.py @@ -0,0 +1,31 @@ +def rgb565(red, green, blue): + return (int(red / 255 * 31) << 11) | (int(green / 255 * 63) << 5) | (int(blue / 255 * 31)) + + +def hsv_to_rgb565(h: float, s: float, v: float) -> int: + if s: + if h == 1.0: + h = 0.0 + i = int(h * 6.0) + f = h * 6.0 - i + + w = int(255 * (v * (1.0 - s))) + q = int(255 * (v * (1.0 - s * f))) + t = int(255 * (v * (1.0 - s * (1.0 - f)))) + v = int(255 * v) + + if i == 0: + return rgb565(v, t, w) + if i == 1: + return rgb565(q, v, w) + if i == 2: + return rgb565(w, v, t) + if i == 3: + return rgb565(w, q, v) + if i == 4: + return rgb565(t, w, v) + if i == 5: + return rgb565(v, w, q) + else: + v = int(255 * v) + return rgb565(v, v, v)