First version of the SYSTEM module

This commit is contained in:
Bo⋆˚✿˖° 2025-09-22 15:44:24 +02:00
parent fc2af7031e
commit 9f61b54f15
Signed by: FreedTapestry21
GPG key ID: 5E19D8C7E935C480
3 changed files with 132 additions and 16 deletions

34
main.py
View file

@ -2,9 +2,12 @@
# Petcard Bootloader - v1.0
# Copyright (c) 2025 FreedTapestry21
#
# Licensed under the MIT license
#
from machine import Pin
import network, urequest, machine
import machine
#import network, urequest, machine
# Define variables
CONSOLE_WELCOME = """
@ -37,12 +40,13 @@ def connect(ssid, psk):
pass
def boot():
import system
init()
print("Loading system...")
with open("petcard.py") as app:
exec(app.read())
# Check if button is pressed for recovery/update process
if __name__ == '__main__':
def main():
print(CONSOLE_WELCOME)
UPDATE = False
if ok.value() == 1:
UPDATE = True
@ -51,21 +55,29 @@ if __name__ == '__main__':
inp = input("Are you sure you want to enter the Petcard recovery process? (y/N)")
if inp == "" or inp == "n" or inp == "N":
print("Rebooting to resume boot process...")
machine.soft_reset()
if inp == "y" or inp == "Y":
print("Entering Petcard recovery process...")
print("Please enter your Wi-Fi credentials")
ssid = input("SSID: ")
psk = input("Password: ")
connect(ssid, psk)
#connect(ssid, psk)
print('Connected to network "' + str(ssid) + '"')
# Downloading system.py
download("system.py", "Petcard system")
download("petcard.py", "Petcard")
# Disabled due to being stuck with a Pico for now
#download("system.py", "Petcard system")
#download("petcard.py", "Petcard")
print("System recovery complete, restarting system...")
machine.reset()
machine.soft_reset()
else:
print("'" + inp + "' is not recognized as an option! Rebooting...")
machine.reset()
machine.soft_reset()
else:
boot()
boot()
# Check if button is pressed for recovery/update process
if __name__ == '__main__':
main()

View file

@ -1,4 +0,0 @@
#
# Petcard - v1.0
# Copyright (c) 2025 FreedTapestry21
#

110
system.py
View file

@ -1,4 +1,112 @@
#
# Petcard System - v1.0
# Copyright (c) 2025 FreedTapestry21
#
#
# Licensed under the MIT license
#
# Importing libraries
import sys, time, machine, framebuf, ssd1306, _thread
# Start Petcard frontend on core 1
with open("petcard.py") as app:
_thread.start_new_thread(exec(app.read()), ())
# Initialize global variables
CONFIG = json.loads(open("config.json", "r").read())
TEXT_HEIGHT = 8
PADDING = 4
# Initialize classes
class Display:
def __init__(self, disp, x, y) -> None:
self.x = x
self.y = y
self.type = disp
self.pages = y // 8
self.buffer = framebuf.FrameBuffer(bytearray(self.pages * x), x, y, framebuf.MONO_VLSB)
def log(self, msg, IGN=False): # IGN => IGNore to report to console (Only log)
if CONFIG["debug"] == 1:
date = time.localtime()
stamp = str(date[0]) + "-" + str(date[1]) + "-" + str(date[2]) + " " + str(date[3]) + ":" + str(date[4]) + ":" + str(date[5])
if IGN == False:
text = "[" + stamp + "] " + msg
print(text)
else:
if msg == "":
text = msg
else:
text = "[" + stamp + "] " + msg
if CONFIG["logging"] == 1: # Write log to log file
with open(CONFIG["logfile"], "a") as file:
file.write(text + '\n')
file.close()
def init(self):
if self.type == "SSD1306":
self.i2c = machine.I2C(0, sda=machine.Pin(4), scl=machine.Pin(5))
self.display = ssd1306.SSD1306_I2C(128, 64, self.i2c)
def update(self):
self.display.blit(self.buffer, 0, 0)
self.display.show()
class Controls:
def __init__(self):
pass
def waitfor(self):
while True:
for x in CONFIG["controls"]:
if machine.Pin(int(CONFIG["controls"][x]), machine.Pin.IN, machine.Pin.PULL_DOWN).value() == 1:
for y in CONFIG["controls"]:
if CONFIG["controls"][y] == x:
return y
else: pass
class UI:
def __init__(self):
self.selection = -1
def menu(self, display, controls, entries):
if self.selection == -1:
self.selection = 0
while True:
inp = controls.waitfor()
if inp == "down":
self.selection = self.selection + 1
if inp == "up":
self.selection = self.selection - 1
if self.selection >= len(entries):
self.selection = 0
if self.selection < 0:
self.slection = len(entries)
print(str(self.selection))
display.buffer.fill(0)
display.buffer.fill_rect(0, int(self.selection*TEXT_HEIGHT) + int(self.selection*PADDING) + 2, display.x, 10, 1)
i = PADDING
q = 0
for x in entries:
if i < display.y:
if q == self.selection:
display.buffer.text(x, 0, i, 0)
else:
display.buffer.text(x, 0, i, 1)
i = i + TEXT_HEIGHT + PADDING
q = q + 1
else:
pass
display.update()
time.sleep(0.2)
display.update()
return display
# Initialize functions
def quit(msg=""):
if msg == '':
sys.exit()
else:
print("[" + NAME + "] " + msg)