I have a lot of boards and usually need to re-use code even for different projects.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. from keyb import Keyboard # type: ignore
  2. import time, os, rtc, json # type: ignore
  3. import board, busio, sdcardio, storage, terminalio, displayio # type: ignore
  4. from adafruit_display_text import label # type: ignore
  5. from adafruit_display_shapes.rect import Rect # type: ignore
  6. ### PAGE FUNCTIONS # # # # # # # # # #
  7. def home():
  8. title_label.text = "Home"
  9. title_label.x = 95
  10. title_label.y = 14
  11. page_label.text = """
  12. ESC = DOWNTIME TAB = SET CLOCK
  13. BACKSP = HERA SP = RT17
  14. ENTER NUMBER FOR CHECK DIGITS
  15. """
  16. page_label.x = 5
  17. page_label.y = 30
  18. time_label.text = strf_time()
  19. input_label.text = ""
  20. input_label.x = 100
  21. input_label.y = 120
  22. # # # # # # # # # # # # # # # # # #
  23. def strf_time():
  24. r = rtc.RTC()
  25. current_time = r.datetime
  26. hour = current_time.tm_hour
  27. mint = current_time.tm_min
  28. return str(hour) + ":" + str(mint)
  29. def strf_date():
  30. current_time = r.datetime
  31. day = current_time.tm_mday
  32. month = current_time.tm_mon
  33. year = current_time.tm_year
  34. return str(day) + "/" + str(month) + "/" + str(year)
  35. def set_datetime():
  36. numbers = ["0","1","2","3","4","5","6","7","8","9"]
  37. cancel = False
  38. title_label.text = "Set Clock"
  39. title_label.x = 75
  40. page_label.text = "Enter Hour\n"
  41. page_label.y = 45
  42. hour = ""
  43. while not cancel:
  44. key = keyb.scan()
  45. if key in numbers:
  46. hour += key
  47. page_label.text += key
  48. elif key == "\n":
  49. break
  50. elif key == "ESC":
  51. cancel = True
  52. page_label.text += "\nEnter Minute\n"
  53. minute = ""
  54. while not cancel:
  55. key = keyb.scan()
  56. if key in numbers:
  57. minute += key
  58. page_label.text += key
  59. elif key == "\n":
  60. break
  61. elif key == "ESC":
  62. cancel = True
  63. page_label.text = "Enter Day\n"
  64. day = ""
  65. while not cancel:
  66. key = keyb.scan()
  67. if key in numbers:
  68. day += key
  69. page_label.text += key
  70. elif key == "\n":
  71. break
  72. elif key == "ESC":
  73. cancel = True
  74. page_label.text = "Enter Month\n"
  75. month = ""
  76. while not cancel:
  77. key = keyb.scan()
  78. if key in numbers:
  79. month += key
  80. page_label.text += key
  81. elif key == "\n":
  82. break
  83. elif key == "ESC":
  84. cancel = True
  85. page_label.text = "Enter Year\n"
  86. year = ""
  87. while not cancel:
  88. key = keyb.scan()
  89. if key in numbers:
  90. year += key
  91. page_label.text += key
  92. elif key == "\n":
  93. break
  94. elif key == "ESC":
  95. cancel = True
  96. if not cancel:
  97. h = int(hour)
  98. m = int(minute)
  99. d = int(day)
  100. mo = int(month)
  101. y = int(year)
  102. r.datetime = time.struct_time((y, mo, d, h, m, 0, 0, -1, -1))
  103. page_label.text = strf_time() + " @ " + strf_date() + "\n(Tab to return)"
  104. while True:
  105. key = keyb.scan()
  106. if key == " ":
  107. home()
  108. break
  109. def get_digits(number):
  110. cd = "NULL"
  111. codes = {"3":"ZMZ",
  112. "5":"HYT",
  113. "6":"MRC",
  114. "9":"DAH",
  115. "10":"ITQ",
  116. "14":"PIZ",
  117. "15":"OIO",
  118. "16":"GIH",
  119. "18":"HDS",
  120. "19":"AIA",
  121. "20":"ZIJ",
  122. "21":"FGE",
  123. "23":"DTX",
  124. "24":"QQK",
  125. "26":"KQG",
  126. "27":"VKW",
  127. "28":"VZJ",
  128. "29":"CTK",
  129. "30":"WMD",
  130. "32":"ZOR",
  131. "33":"RQR",
  132. "34":"LRR",
  133. "35":"SBA",
  134. "36":"XVQ",
  135. "38":"NRX",
  136. "39":"HCM",
  137. "40":"MQG"}
  138. if number in codes:
  139. cd = codes[number]
  140. title_label.text = cd
  141. title_label.x = 75
  142. page_label.text = "Check Digits for " + number
  143. page_label.y = 45
  144. input_label.text=""
  145. while True:
  146. key = keyb.scan()
  147. if key == "ESC":
  148. home()
  149. break
  150. else:
  151. home()
  152. def show_barcodes(code):
  153. if code == "RT17":
  154. bmp = displayio.OnDiskBitmap("/barcodes/RT17.bmp")
  155. else:
  156. bmp = displayio.OnDiskBitmap("/barcodes/HERA.bmp")
  157. title_label.text = ""
  158. page_label.text = ""
  159. tile_grid = displayio.TileGrid(bmp, pixel_shader=bmp.pixel_shader)
  160. tile_grid.x = 30
  161. tile_grid.y = 15
  162. display_group.append(tile_grid)
  163. while True:
  164. key = keyb.scan()
  165. if key == "ESC":
  166. display_group.remove(tile_grid)
  167. home()
  168. break
  169. def downtime_page():
  170. title_label.text = "Downtime"
  171. title_label.x = 75
  172. default_page_text = """
  173. ESC = QUIT OK = VIEW LOG
  174. 1) WAITING | 2) PROBLEM | 3) COMFORT
  175. 4) OTHER
  176. """
  177. default_page_y = 30
  178. page_label.text = default_page_text
  179. page_label.y = default_page_y
  180. while True:
  181. key = keyb.scan()
  182. if key == "ESC":
  183. home()
  184. break
  185. elif key == "\n":
  186. page_label.text = ""
  187. page_label.y = 40
  188. for entry in downtime:
  189. dt = entry[0]
  190. hour = dt.tm_hour
  191. mint = dt.tm_min
  192. dt_time = str(hour) + ":" + str(mint)
  193. reason = entry[1]
  194. page_label.text += dt_time + " - " + reason + "\n"
  195. while True:
  196. key = keyb.scan()
  197. if key == "ESC":
  198. page_label.text = default_page_text
  199. page_label.y = default_page_y
  200. break
  201. elif key == "1" or key == "2" or key == "3":
  202. if key == "1":
  203. reason = "WAITING"
  204. elif key == "2":
  205. reason = "PROBLEM"
  206. elif key == "3":
  207. reason = "COMFORT"
  208. dt = current_time = r.datetime
  209. entry = [dt, reason]
  210. downtime.append(entry)
  211. page_label.text += "\n\nDowntime Recorded"
  212. time.sleep(3)
  213. page_label.text = default_page_text
  214. # SET UP SD CARD
  215. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  216. cs = board.SD_CS
  217. try:
  218. sdcard = sdcardio.SDCard(spi, cs)
  219. vfs = storage.VfsFat(sdcard)
  220. storage.mount(vfs, "/sd") # access files on sd card here
  221. except OSError:
  222. pass # SD card not inserted/found
  223. # SET UP DISPLAY
  224. display = board.DISPLAY
  225. display_group = displayio.Group()
  226. display.root_group = display_group
  227. # Header Bar
  228. rect = Rect(0, 0, 238, 30, fill=0x1f4476)
  229. display.root_group.append(rect)
  230. # Page Title
  231. title_font = terminalio.FONT
  232. title_color = 0xffffff
  233. title_label = label.Label(title_font, text="Home", color=title_color, scale=2)
  234. display.root_group.append(title_label)
  235. # Time Label
  236. time_font = terminalio.FONT
  237. time_color = 0x89a0a8
  238. time_label = label.Label(time_font, text="00:00", color=time_color, scale=1)
  239. time_label.x = 200
  240. time_label.y = 24
  241. display.root_group.append(time_label)
  242. # IP Label
  243. # ip_font = terminalio.FONT
  244. # ip_color = 0x89a0a8
  245. # ip_label = label.Label(ip_font, text="0.0.0.0", color=ip_color, scale=1)
  246. # ip_label.x = 10
  247. # ip_label.y = 24
  248. # display.root_group.append(ip_label)
  249. # Page Text
  250. page_font = terminalio.FONT
  251. page_color = 0xffffff
  252. page_label = label.Label(page_font, text="page_text", color=page_color, scale=1)
  253. display.root_group.append(page_label)
  254. # Numbers Input Text
  255. input_font = terminalio.FONT
  256. input_color = 0xffffff
  257. input_label = label.Label(input_font, text="", color=input_color, scale=2)
  258. display.root_group.append(input_label)
  259. home()
  260. keyb = Keyboard()
  261. downtime = []
  262. r = rtc.RTC()
  263. #r.datetime = time.struct_time((2025, 10, 7, 23, 39, 0, 0, -1, -1)) # THIS LINE SETS THE RTC, first is year, then month, then day, then hour, then minute, (rest is unimportant)
  264. #current_time = r.datetime
  265. last_time = "00:00"
  266. ## PROGRAM MAIN LOOP (WORKS FROM HOME PAGE IF OTHER PAGES HAVE THEIR OWN LOOPS)
  267. numbers = ["0","1","2","3","4","5","6","7","8","9"]
  268. ninput = ""
  269. while True:
  270. time_label.text = strf_time()
  271. key = keyb.scan()
  272. if key == " ":
  273. set_datetime()
  274. elif key == "BACKSP":
  275. show_barcodes("HERA")
  276. elif key == " ":
  277. show_barcodes("RT17")
  278. elif key in numbers:
  279. input_label.text += key
  280. ninput += key
  281. elif key == "\n":
  282. get_digits(ninput)
  283. ninput = ""
  284. elif key == "ESC":
  285. downtime_page()
  286. # Homepage Clock
  287. # current_time = strf_time()
  288. # if last_time != current_time:
  289. # time_label.text = current_time
  290. # last_time = current_time
  291. #time_label.text = strf_time()
  292. # IDEA
  293. #
  294. #
  295. # current move -> move start time, array of intermediate move times like when replenning, end move time (last part of move like pallet store)
  296. # OR just button for every scan with time idk (strf and also actual time format so it can be calculated. )
  297. # then display diff between times in a menu
  298. #
  299. #