From d4186e0e43798cc3aca537a2fe17ff5c3227ec39 Mon Sep 17 00:00:00 2001 From: Nicole O'Connor Date: Mon, 4 Nov 2024 19:25:23 -0800 Subject: [PATCH] state sync: contains * adafruit screen test script * mainloop with threads * a mostly finished screen thread * a partly finished blocklist update thread --- blocklists.py | 57 ++++++++++++++++++++++++++ config.ini | 2 + config.py | 8 ++++ mainloop.py | 47 ++++++++++++++++++++++ pitft-screen-test.py | 58 +++++++++++++++++++++++++++ res/image/dns.png | Bin 0 -> 1162 bytes res/image/download.png | Bin 0 -> 568 bytes res/image/hourglass.png | Bin 0 -> 1402 bytes res/image/okay.png | Bin 0 -> 2267 bytes res/image/problem.png | Bin 0 -> 1688 bytes res/image/wifi.png | Bin 0 -> 1047 bytes screen.py | 86 ++++++++++++++++++++++++++++++++++++++++ 12 files changed, 258 insertions(+) create mode 100644 blocklists.py create mode 100644 config.ini create mode 100644 config.py create mode 100755 mainloop.py create mode 100755 pitft-screen-test.py create mode 100644 res/image/dns.png create mode 100644 res/image/download.png create mode 100644 res/image/hourglass.png create mode 100644 res/image/okay.png create mode 100644 res/image/problem.png create mode 100644 res/image/wifi.png create mode 100644 screen.py diff --git a/blocklists.py b/blocklists.py new file mode 100644 index 0000000..7b449e2 --- /dev/null +++ b/blocklists.py @@ -0,0 +1,57 @@ +import atexit +import logging +import os +import pathlib +import time + +import requests + +import config +import screen + +log = logging.getLogger("blocklists") +running = True + +blocklist_data_src = "https://github.com/hagezi/dns-blocklists/archive/refs/heads/main.zip" +blocklist_data_src_path = "dnsmasq" +blocklist_lib_path = "/var/lib/starcontrol" + +log.debug("preparing cache directories") +pth_cache_root = pathlib.Path(config.cparse["internal"]["cache_root"]) +pth_cache_root.mkdir(exist_ok=True, parents=True) +pth_blocklist_lib = pathlib.Path(blocklist_lib_path) +pth_blocklist_lib.mkdir(exist_ok=True, parents=True) + +def blocklist_check(): + log.debug("checking blocklist mtimes and updating if needed") + try: + ts_payload = os.path.getmtime(pth_blocklist_lib / "dns-blocklists-main.zip") + ts_now = time.time() + if (ts_now - ts_payload) >= 604800: + update_blocklists() + except OSError: + update_blocklists() + +def update_blocklists(): + log.info("blocklist update in progress") + screen.download_state = 1 + + fd_listpayload = open(pth_blocklist_lib / "dns-blocklists-main.zip", "wb") + hnd_download = requests.get(blocklist_data_src, stream=True) + + for chunk in hnd_download.iter_content(chunk_size=256): + fd_listpayload.write(chunk) + + hnd_download.close() + fd_listpayload.close() + screen.download_state = 0 + +def blocklist_thread(): + while running: + blocklist_check() + time.sleep(3600) # 1 hour + +@atexit.register +def stop_blocklist(): + global running + running = False \ No newline at end of file diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..d6df357 --- /dev/null +++ b/config.ini @@ -0,0 +1,2 @@ +[internal] +cache_root=/run/starcontrol \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..257ceac --- /dev/null +++ b/config.py @@ -0,0 +1,8 @@ +import configparser +import logging + +log = logging.getLogger("main") + +log.debug("reading configuration file") +cparse = configparser.ConfigParser() +cparse.read("config.ini") \ No newline at end of file diff --git a/mainloop.py b/mainloop.py new file mode 100755 index 0000000..89c7ad7 --- /dev/null +++ b/mainloop.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import logging +import os +import math +import pathlib +import time +import threading + +from PIL import Image, ImageDraw + +import blocklists +import screen + +log = logging.getLogger("main") + +if __name__ == "__main__": + screen.screen_init() + screen.scr_light.value = True + + img_canvas = Image.new("RGB", (135, 240)) + hnd_canvas = ImageDraw.Draw(img_canvas) + hnd_canvas.rectangle((0,0,135,240), (0,0,0)) + img_canvas.paste(Image.open("res/image/hourglass.png"),(math.floor(135/2) - math.ceil(49/2),math.floor(240/2) - math.ceil(90/2))) + screen.scr.image(img_canvas) + + thr_blocklists = threading.Thread(target=blocklists.blocklist_thread) + thr_screen = threading.Thread(target=screen.screen_thread) + thr_blocklists.start() + thr_screen.start() + + ok = True + while ok: + try: + thr_blocklists.join(5.0) + thr_screen.join(5.0) + ok = thr_blocklists.is_alive() and thr_screen.is_alive() + except KeyboardInterrupt: + ok = False + print("KEYBOARD INTERRUPT") + except: + ok = False + + screen.running = False + blocklists.running = False + thr_screen.join() + thr_blocklists.join() \ No newline at end of file diff --git a/pitft-screen-test.py b/pitft-screen-test.py new file mode 100755 index 0000000..e0c80bf --- /dev/null +++ b/pitft-screen-test.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +import digitalio +import board + +from adafruit_rgb_display.rgb import color565 +from adafruit_rgb_display import st7789 + +# Configuration for CS and DC pins for Raspberry Pi +cs_pin = digitalio.DigitalInOut(board.CE0) +dc_pin = digitalio.DigitalInOut(board.D25) +reset_pin = None +BAUDRATE = 64000000 # The pi can be very fast! +# Create the ST7789 display: +display = st7789.ST7789( + board.SPI(), + cs=cs_pin, + dc=dc_pin, + rst=reset_pin, + baudrate=BAUDRATE, + width=135, + height=240, + x_offset=53, + y_offset=40, +) + +backlight = digitalio.DigitalInOut(board.D22) +backlight.switch_to_output() +backlight.value = True +buttonA = digitalio.DigitalInOut(board.D23) +buttonB = digitalio.DigitalInOut(board.D24) +buttonA.switch_to_input() +buttonB.switch_to_input() + +buttonAToggle = False +buttonBToggle = False + +# Main loop: +while True: + if buttonA.value and buttonB.value: + backlight.value = False # turn off backlight + else: + backlight.value = True # turn on backlight + if buttonB.value and not buttonA.value: # just button A pressed + display.fill(color565(255, 0, 0)) # red + if buttonA.value and not buttonB.value: # just button B pressed + display.fill(color565(0, 0, 255)) # blue + if not buttonA.value and not buttonB.value: # none pressed + display.fill(color565(0, 255, 0)) # green + + if buttonA.value != buttonAToggle: + print("buttonA toggle state") + buttonAToggle = buttonA.value + if buttonB.value != buttonBToggle: + print("buttonB toggle state") + buttonBToggle = buttonB.value \ No newline at end of file diff --git a/res/image/dns.png b/res/image/dns.png new file mode 100644 index 0000000000000000000000000000000000000000..f212ace8d8ff1a2b5373d1787f71dede03fb036a GIT binary patch literal 1162 zcmV;51aGe|Z5+_RQ79W-NNuvPR18ZUHBJ3`o_Efb``zC8oDsAG z&v)K??z!jQ@BW6xv>a~CrU7`=4DN<2*L)4O zyOFNcNv0p?*^Oym)e(ItvcziiaQFy*&oamJJj-{jvq^1j9w#~c#>1!ZHmrdaZH&SU zcLr|2L@=w}OEG)?3J=1g5Ab7p9R***z3$vUW*^_cXy|kR^Ryygg&u)EADlxUg6E=6 z(r&eU11?lG{U=}{+WL;Lson`& zci61c=lv-EgZAit2`+cF^C89PvEa+ZBbJVyTKN&Ie<8{qp}etXJ21O-hff;7z9@Tq zO>X78pdFiio;9FH!a=0bl>^Z3On5}|0ob|H1Lh7T4PYtHcA%J9-+&Z9=|8aj?J@L1 z(3Y)F`Izdi6cz3b_scBs1m&`m!YOzQ7QiQP3AScVRa`fr7l((zV0E$*qer#O1vjx- zcO6nb$=gq$urz$MKbuvy^E@6dI30)kXX^I!r6q6^UWz)Wdv;8t)2wtyd3}^sX$iW* zW{k8HuY|vSrWl8u0k2WYoY1O2!I`-RJun}fa^)6S-xuZe=%Zn?PI0QgfYb03yb0T_ z2$>tY*U>DC(R!1yd_UM7eJ=SwQn71qf-)69;^uLxcfhOQOKB~X@)`!Y8~K1nyR(MA zgvP*T=u6Y>tI>SmtFn9@>(doeHdM8a2KkacLD?I#ml_jP(}l!Kf?~!iFC<|c=a<(uV}5G~h^e|7SUx(_<l&ii68i01!$&KlzQvBR9f%y5=0Zwgd@3Xd-!zX>3 zx!WCx`>VFET4Ukt5p@lR@^R{{`7u%*wN_NOltGHgqZ6KnnJ^jh&-0%t`%dVFeURQ2 cJPs`X0z5Ps5#((;umAu607*qoM6N<$f*x}|MF0Q* literal 0 HcmV?d00001 diff --git a/res/image/download.png b/res/image/download.png new file mode 100644 index 0000000000000000000000000000000000000000..1917f0dc8ecbcbd944de453ebc38e1b7559b95e5 GIT binary patch literal 568 zcmV-80>}M{P)PbXFR9HvtS3NHTQ52m;Pq3v#A+aPvBnp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Lg8SoMt;~IJwuN`R-G)C0k8KDFY{MVn$NC6%uYTTK zaO$K}khN|~>kT#@84KGW2{0NpO)7B;yQE5HaINLNUla%>{1Eg<8!Xi4-+crE`lEq$ zPAr7e4ePGGwUBTbo=CXqnQ_ot*fYXN3WdLR>wcyrEsjW7+gFnN1(f6t)vXC2Q04Q( zTzv2h<5-~*(0T{RTwqG$U`0;5Q)w1!*`F#=E0d*u-bE$;14Tx_67z{AH~;_u07*qo IM6N<$g7H^(p8x;= literal 0 HcmV?d00001 diff --git a/res/image/okay.png b/res/image/okay.png new file mode 100644 index 0000000000000000000000000000000000000000..9d8584be1772c728dea1270fb1c14e0020272d66 GIT binary patch literal 2267 zcmV<12qgE3P)`R=3bjy8%JPq!fA6@_tAt(8sjW z2TVKP{xL0m&fI(F-Wlrt{%XH_&iT$cbMO6L=L!N%DbYX@3UNp#B8Wi}4uk==PN}rvV4Bi*$~dmf793(|#5Hvi}d>KLKUD&fuEln1dt{2U&faduPP6u&D;3g5_6`xs8=r zX5;K?=zRw$tmbzXe255PV(3K14L^WX6E&-KRgL2X9f^&l8*sn zxH6nQQ}^)lBfeL?Q*2X+9un+$;s~ zNJ&)R91oK*L&i`tt%QV837X_Dw_fJvy_>#;1Ze;vo%{BajXD`9o*4E7jsyo2Lnmt1 zdFR4ANbd{4F8;uNq7)#BK%Ea5B;FR!4>Nwy75w`1>)BbeQ;Vpeoum*3>C12WsOjuW z*hiFA+q_I7SyR1+{A>BzJCVeNAc)oDm{nM@h1hVqoLX)(zetTpWzW<-*eo`6VZbF_ zWS`2``pb7Ch*f~6wa7+TBiJ6d9p`K(^(pn#rquHO)9*7>8LF>S6*8hTD4tfVIrvt0 zE1AbL^#>0ii4i!s0zBzU&StT3_)mw4xAlKo%InLS`^#L~+->?D=tI_{Sro(-s1ISC zG_^M|@7O%PZ}{HdE-x$4Jumh~?zr3mg9k{9NMqNOU7DTtM$Wh#cEs<{Y^T;$OKN4R zk>CMFDs&%Fdqr*XkrACiL0kdC&%xx~@$a%Fc8g~HU)z2qsUnFZO@#EhPqk&rrplY$Ixr$|{meDTTHCf#pyP53AvIo2t zY6<1Lso#+qovGQ&vbr+tm3A6jhDrejqE-RUX_4H0dpED0ehs%&KZX|wKAZd*i^neR z)gBfSh9kkDwqenWZ|{7|ykqld?r7Gmql0P%LA6%tqMJqJROhG*;(5l4;){4L@;Ly; z4jIdV=?9n@rhUV?=w=ZMk1sT3jxivTY71FJ0e{x2Q_)m`5Q3EO6jfP^u#gd*!KKzq z6vY=Y*`|HbnRhjhH_yI_+rPpOZ3zA$C?y&}4LVnZ8)G)|){}1q#IFyoe89T%>jGlC z2X3s4RXXeNPV3cPdu}ZSHwsMkNFrHtZcQ+P2RP1b&r1-5_tR5vNF=MyuHx%kUz_4h zhwNa*nHA(+%`?TGDuQz8-V^j=Ddll`*{Nle)Rh?W<(~T^?JXrdg?$P87^V!PxVD(Z#~0J;ZZ&Gw zUk+j?1<`veJK#l9bH~hO(bz?-IK2XwR4v>L6iyWWI8c$4_xXP*ix8MI&3o7g7Gmd{ zfi8J3DxSl?fX#vODTsXNEqG9?7Ps>@u)}1Bf`?=$Ke67Ii24%v2v+LZ!|$zFTJRsj z$y6(rR)eL>RWGjXGcpf~|e$_Ifd|HfTRKT&x6^Svcm_#Z);aHZU&);s_J002ovPDHLkV1mY;Jiq_| literal 0 HcmV?d00001 diff --git a/res/image/problem.png b/res/image/problem.png new file mode 100644 index 0000000000000000000000000000000000000000..f73ed2ddf448dea782f0b485f11c209b93702a39 GIT binary patch literal 1688 zcmV;J250$+P)J>ri(LpxLq>}dvtfKGY0V)t)YfUBZ4^mTL zJD|03&iTq#9|NHQLT%AnRswOWa)W8lRDXl3)FG{l|6mP{Z9q|e#D z0AZ5z40U!o7QWd(MF(~AyFqhPa%eQmc94-SQ`_@)`%56=u|zh6KJ$dDS8X?`>tKA{0fPNJ-WLxL&SSq|N>3qt zGa&O$;!0qufl`-c)+J5UDTpryY+Qaz*j6kxRZm2o|FR)o%1$bZ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