from keyb import Keyboard # type: ignore import time, os, rtc, json # type: ignore import board, busio, sdcardio, storage # type: ignore def count_moves_hour(moves, hour): count = 0 for move in moves: if move[0] == hour: count += 1 return count def diffs(moves): for move in moves: h = move[2].split(":")[0] if int(h) >= 5: line = str(move[0]) + ":" + str(move[1]) + " / " + str(move[2]) print(line) def write_file(moves, downtime): data = {"Moves": moves, "Downtime": downtime} filename = "/sd/data.json" with open(filename, "w") as f: json.dump(data, f) def read_file(): print("Reading data") filename = "/sd/data.json" with open(filename, "r") as f: data = json.load(f) moves = data["Moves"] downtime = data["Downtime"] return moves, downtime 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 last = None moves = [] downtime = [] r = rtc.RTC() #r.datetime = time.struct_time((2025, 10, 7, 23, 39, 0, 0, -1, -1)) current_time = r.datetime print(str(current_time.tm_hour) + ":" + str(current_time.tm_min)) keyb = Keyboard() while True: key = keyb.scan() if key == "\n": if not last: last = time.time() print("Starting!") else: time2 = time.time() diff = time2 - last m, s = divmod(diff, 60) diff_delta = f'{m:02d}:{s:02d}' print(diff_delta) last = time2 current_time = r.datetime hour = current_time.tm_hour mint = current_time.tm_min moves.append([hour, mint, diff_delta]) print("Moves this hour:", count_moves_hour(moves, hour)) write_file(moves, downtime) elif key == "b": print("Battery change") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Battery change", ct_str] downtime.append(dt) elif key == "n": print("Battery change finish") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Battery change finish", ct_str] downtime.append(dt) elif key == "p": print("Issue with pallet") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Issue with pallet", ct_str] downtime.append(dt) elif key == "q": print("Queue/Busy Area/Leaving Space") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Queue/Busy Area/Leaving Space", ct_str] downtime.append(dt) elif key == "s": print("Staff Reason") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Staff Reason", ct_str] downtime.append(dt) elif key == " ": print("Break") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Break", ct_str] downtime.append(dt) elif key == "BACKSP": print("Break Finish") ct = r.datetime ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min) dt = ["Break Finish", ct_str] downtime.append(dt) elif key == "CTRLD": print("====== INFO =====") if downtime: for item in downtime: print(item[0], ":", item[1]) print("====== ======== =====") diffs(moves) print("====== ======== =====") current_time = r.datetime hour = current_time.tm_hour print("Moves this hour:", count_moves_hour(moves, hour)) print("Total Moves:", len(moves)) print("====== ======== =====") elif key == "CTRLO": print("Overwrite Data From File?") while True: key = keyb.scan() if key == "\n": data = read_file() moves = data[0] downtime = data[1] print("DATA LOADED") break else: print("DATA UNCHANGED") break elif key == "CTRLS": print("Set Time") print("ENTER HOUR") hour = "" while True: key = keyb.scan() if key == "1": hour += key elif key == "2": hour += key elif key == "3": hour += key elif key == "4": hour += key elif key == "5": hour += key elif key == "6": hour += key elif key == "7": hour += key elif key == "8": hour += key elif key == "9": hour += key elif key == "0": hour += key elif key == "\n": print("HOUR:", hour) break print("ENTER MINUTE") minute = "" while True: key = keyb.scan() if key == "1": minute += key elif key == "2": minute += key elif key == "3": minute += key elif key == "4": minute += key elif key == "5": minute += key elif key == "6": minute += key elif key == "7": minute += key elif key == "8": minute += key elif key == "9": minute += key elif key == "0": minute += key elif key == "\n": print("MINUTE:", minute) break h = int(hour) m = int(minute) r.datetime = time.struct_time((2025, 10, 8, h, m, 0, 0, -1, -1)) current_time = r.datetime print(str(current_time.tm_hour) + ":" + str(current_time.tm_min))