From f46ab997dd715027dfe8b0ac14b135211cdd5c4e Mon Sep 17 00:00:00 2001 From: cube Date: Wed, 4 Feb 2026 23:59:44 +0000 Subject: [PATCH] cardpal having recursion issues (try going in and out of a menu repeatedly you will see what i mean) --- .../projects/CardPal/code.py | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 CARDPUTER CircuitPython/projects/CardPal/code.py diff --git a/CARDPUTER CircuitPython/projects/CardPal/code.py b/CARDPUTER CircuitPython/projects/CardPal/code.py new file mode 100644 index 0000000..0d427f3 --- /dev/null +++ b/CARDPUTER CircuitPython/projects/CardPal/code.py @@ -0,0 +1,283 @@ +from keyb import Keyboard # type: ignore +import time, os, rtc, json, wifi, supervisor # type: ignore +import board, busio, sdcardio, storage, terminalio, displayio # type: ignore +from adafruit_display_text import label # type: ignore +from adafruit_display_shapes.rect import Rect # type: ignore +#import pytumblr # type: ignore +import tumblrapi # type: ignore + +### DEFAULT HOME PAGE # # # # # # # # # # +def home(pos = 0): + title_label.text = "Home" + title_label.x = 10 + title_label.y = 14 + + ip_label.text = my_ip + ip_label.x = 160 + ip_label.y = 20 + + menu_options = ["SD Card", "Tumblr", "Web Server"] + count = 0 + menu_labs = [] + for option in menu_options: + if count == pos: + menu_label = label.Label(terminalio.FONT, text = "> " + option, color=0xffffff, scale=1) + else: + menu_label = label.Label(terminalio.FONT, text = option, color=0xffffff, scale=1) + menu_label.x = 10 + menu_label.y = 45 + count * 10 + display.root_group.append(menu_label) + menu_labs.append(menu_label) + count+=1 + + while True: + key = keyb.scan() + if key == "UP": + if pos == 0: + pass + else: + menu_labs[pos].text = menu_options[pos] + pos -= 1 + menu_labs[pos].text = "> " + menu_options[pos] + + elif key == "DOWN": + if pos == len(menu_options)-1: + pass + else: + menu_labs[pos].text = menu_options[pos] + pos += 1 + menu_labs[pos].text = "> " + menu_options[pos] + + elif key == "\n": + option = menu_options[pos] + if option == "SD Card": + for lab in menu_labs: + display.root_group.remove(lab) + sd_card() + return + elif option == "Tumblr": + for lab in menu_labs: + display.root_group.remove(lab) + tumblr() + return + elif option == "Web Server": + for lab in menu_labs: + display.root_group.remove(lab) + web_server() + return + +# # # # # # # # # # # # # # # # # # + +def sd_card(): + title_label.text = "SD Card" + + fpos = 0 + + filename_labels = [] + files = os.listdir("/sd") + count = 0 + for filename in files: + if count == pos: + filename_label = label.Label(terminalio.FONT, text = "> " + filename, color=0xffffff, scale=1) + else: + filename_label = label.Label(terminalio.FONT, text = filename, color=0xffffff, scale=1) + filename_label.x = 10 + filename_label.y = 45 + count * 10 + display.root_group.append(filename_label) + filename_labels.append(filename_label) + count+=1 + + while True: + key = keyb.scan() + if key == "ESC": + for lab in filename_labels: + display.root_group.remove(lab) + home(pos = 0) + return + + + elif key == "UP": + if fpos == 0: + pass + else: + filename_labels[fpos].text = files[fpos] + fpos -= 1 + filename_labels[fpos].text = "> " + files[fpos] + + elif key == "DOWN": + if fpos == len(files)-1: + pass + else: + filename_labels[fpos].text = files[fpos] + fpos += 1 + filename_labels[fpos].text = "> " + files[fpos] + + elif key == "\n": + file = "/sd/" + files[fpos] + for lab in filename_labels: + display.root_group.remove(lab) + file_label = label.Label(terminalio.FONT, text = "", color=0xffffff, scale=1) + file_label.x = 10 + file_label.y = 45 + display.root_group.append(file_label) + + with open(file, "r") as f: + file_label.text = f.read() + + while True: + key = keyb.scan() + if key == "ESC": + display.root_group.remove(file_label) + sd_card() + return + + +def tumblr(): + title_label.text = "Tumblr" + + while True: + key = keyb.scan() + if key == "ESC": + home(pos = 1) + return + + +def web_server(): + title_label.text = "Web Server" + + while True: + key = keyb.scan() + if key == "ESC": + home(pos = 2) + return + + + + + + + + + + +keyb = Keyboard() + + +# SET UP DISPLAY & WIFI +display = board.DISPLAY +display_group = displayio.Group() +display.root_group = display_group + +wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")], + [os.getenv("WIFI2"),os.getenv("WIFIPW2")], + [os.getenv("WIFI3"),os.getenv("WIFIPW3")]] + + +h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2) +h1.x = 10 +h1.y = 15 + +count = 0 +menu_labs = [] +for option in wifi_options: + if count == 0: + menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1) + else: + menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1) + menu_lab.x = 10 + menu_lab.y = 35 + count * 10 + display.root_group.append(menu_lab) + menu_labs.append(menu_lab) + count+=1 + +display.root_group.append(h1) + +pos = 0 +while True: + key = keyb.scan() + if key == "UP": + if pos == 0: + pass + else: + menu_labs[pos].text = wifi_options[pos][0] + pos -= 1 + menu_labs[pos].text = "> " + wifi_options[pos][0] + elif key == "DOWN": + if pos == len(wifi_options)-1: + pass + else: + menu_labs[pos].text = wifi_options[pos][0] + pos += 1 + menu_labs[pos].text = "> " + wifi_options[pos][0] + elif key == "\n": + WIFI_SSID = wifi_options[pos][0] + WIFI_PASS = wifi_options[pos][1] + break + +try: + for lab in menu_labs: + display.root_group.remove(lab) + + txt = label.Label(terminalio.FONT, text = "", color=0x00ff00, scale=1) + txt.text = "Connecting to " + WIFI_SSID + "..." + txt.x = 10 + txt.y = 35 + display.root_group.append(txt) + + wifi.radio.connect(ssid=WIFI_SSID, + password=WIFI_PASS) + my_ip = str(wifi.radio.ipv4_address).strip() + + display.root_group.remove(h1) + display.root_group.remove(txt) +except ConnectionError as e: + h1.text = "Aw..." + + txt.text = "Could not connect to " + WIFI_SSID + txt.color = 0xff0000 + txt.x = 10 + txt.y = 35 + + txt2 = label.Label(terminalio.FONT, text = "", color=0x89a0a8, scale=1) + txt2.text = "Press CTRL + C followed by CTRL + D\nto soft-reboot" + txt2.x = 10 + txt2.y = 100 + + display.root_group.append(txt2) + + while True: + pass + + + + +# SET UP SD CARD +spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO) +cs = board.SD_CS + +try: + sdcard = sdcardio.SDCard(spi, cs) + vfs = storage.VfsFat(sdcard) + + storage.mount(vfs, "/sd") # access files on sd card here +except OSError: + pass # SD card not inserted/found + + + + +# Header Bar +header_bar = Rect(0, 0, 238, 30, fill=0x1f4476) +display.root_group.append(header_bar) + +# Page Title +title_label = label.Label(terminalio.FONT, text="Home", color=0xffffff, scale=2) +display.root_group.append(title_label) + +# IP Label +ip_label = label.Label(terminalio.FONT, text="0.0.0.0", color=0x89a0a8, scale=1) +display.root_group.append(ip_label) + +home() + +