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

BasicReachMoveTracker.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. from keyb import Keyboard # type: ignore
  2. import time, os, rtc, json # type: ignore
  3. import board, busio, sdcardio, storage # type: ignore
  4. def count_moves_hour(moves, hour):
  5. count = 0
  6. for move in moves:
  7. if move[0] == hour:
  8. count += 1
  9. return count
  10. def diffs(moves):
  11. for move in moves:
  12. h = move[2].split(":")[0]
  13. if int(h) >= 5:
  14. line = str(move[0]) + ":" + str(move[1]) + " / " + str(move[2])
  15. print(line)
  16. def write_file(moves, downtime):
  17. data = {"Moves": moves, "Downtime": downtime}
  18. filename = "/sd/data.json"
  19. with open(filename, "w") as f:
  20. json.dump(data, f)
  21. def read_file():
  22. print("Reading data")
  23. filename = "/sd/data.json"
  24. with open(filename, "r") as f:
  25. data = json.load(f)
  26. moves = data["Moves"]
  27. downtime = data["Downtime"]
  28. return moves, downtime
  29. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  30. cs = board.SD_CS
  31. try:
  32. sdcard = sdcardio.SDCard(spi, cs)
  33. vfs = storage.VfsFat(sdcard)
  34. storage.mount(vfs, "/sd") # access files on sd card here
  35. except OSError:
  36. pass # SD card not inserted/found
  37. last = None
  38. moves = []
  39. downtime = []
  40. r = rtc.RTC()
  41. #r.datetime = time.struct_time((2025, 10, 7, 23, 39, 0, 0, -1, -1))
  42. current_time = r.datetime
  43. print(str(current_time.tm_hour) + ":" + str(current_time.tm_min))
  44. keyb = Keyboard()
  45. while True:
  46. key = keyb.scan()
  47. if key == "\n":
  48. if not last:
  49. last = time.time()
  50. print("Starting!")
  51. else:
  52. time2 = time.time()
  53. diff = time2 - last
  54. m, s = divmod(diff, 60)
  55. diff_delta = f'{m:02d}:{s:02d}'
  56. print(diff_delta)
  57. last = time2
  58. current_time = r.datetime
  59. hour = current_time.tm_hour
  60. mint = current_time.tm_min
  61. moves.append([hour, mint, diff_delta])
  62. print("Moves this hour:", count_moves_hour(moves, hour))
  63. write_file(moves, downtime)
  64. elif key == "b":
  65. print("Battery change")
  66. ct = r.datetime
  67. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  68. dt = ["Battery change", ct_str]
  69. downtime.append(dt)
  70. elif key == "n":
  71. print("Battery change finish")
  72. ct = r.datetime
  73. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  74. dt = ["Battery change finish", ct_str]
  75. downtime.append(dt)
  76. elif key == "p":
  77. print("Issue with pallet")
  78. ct = r.datetime
  79. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  80. dt = ["Issue with pallet", ct_str]
  81. downtime.append(dt)
  82. elif key == "q":
  83. print("Queue/Busy Area/Leaving Space")
  84. ct = r.datetime
  85. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  86. dt = ["Queue/Busy Area/Leaving Space", ct_str]
  87. downtime.append(dt)
  88. elif key == "s":
  89. print("Staff Reason")
  90. ct = r.datetime
  91. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  92. dt = ["Staff Reason", ct_str]
  93. downtime.append(dt)
  94. elif key == " ":
  95. print("Break")
  96. ct = r.datetime
  97. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  98. dt = ["Break", ct_str]
  99. downtime.append(dt)
  100. elif key == "BACKSP":
  101. print("Break Finish")
  102. ct = r.datetime
  103. ct_str = str(ct.tm_hour) + ":" + str(ct.tm_min)
  104. dt = ["Break Finish", ct_str]
  105. downtime.append(dt)
  106. elif key == "CTRLD":
  107. print("====== INFO =====")
  108. if downtime:
  109. for item in downtime:
  110. print(item[0], ":", item[1])
  111. print("====== ======== =====")
  112. diffs(moves)
  113. print("====== ======== =====")
  114. current_time = r.datetime
  115. hour = current_time.tm_hour
  116. print("Moves this hour:", count_moves_hour(moves, hour))
  117. print("Total Moves:", len(moves))
  118. print("====== ======== =====")
  119. elif key == "CTRLO":
  120. print("Overwrite Data From File?")
  121. while True:
  122. key = keyb.scan()
  123. if key == "\n":
  124. data = read_file()
  125. moves = data[0]
  126. downtime = data[1]
  127. print("DATA LOADED")
  128. break
  129. else:
  130. print("DATA UNCHANGED")
  131. break
  132. elif key == "CTRLS":
  133. print("Set Time")
  134. print("ENTER HOUR")
  135. hour = ""
  136. while True:
  137. key = keyb.scan()
  138. if key == "1":
  139. hour += key
  140. elif key == "2":
  141. hour += key
  142. elif key == "3":
  143. hour += key
  144. elif key == "4":
  145. hour += key
  146. elif key == "5":
  147. hour += key
  148. elif key == "6":
  149. hour += key
  150. elif key == "7":
  151. hour += key
  152. elif key == "8":
  153. hour += key
  154. elif key == "9":
  155. hour += key
  156. elif key == "0":
  157. hour += key
  158. elif key == "\n":
  159. print("HOUR:", hour)
  160. break
  161. print("ENTER MINUTE")
  162. minute = ""
  163. while True:
  164. key = keyb.scan()
  165. if key == "1":
  166. minute += key
  167. elif key == "2":
  168. minute += key
  169. elif key == "3":
  170. minute += key
  171. elif key == "4":
  172. minute += key
  173. elif key == "5":
  174. minute += key
  175. elif key == "6":
  176. minute += key
  177. elif key == "7":
  178. minute += key
  179. elif key == "8":
  180. minute += key
  181. elif key == "9":
  182. minute += key
  183. elif key == "0":
  184. minute += key
  185. elif key == "\n":
  186. print("MINUTE:", minute)
  187. break
  188. h = int(hour)
  189. m = int(minute)
  190. r.datetime = time.struct_time((2025, 10, 8, h, m, 0, 0, -1, -1))
  191. current_time = r.datetime
  192. print(str(current_time.tm_hour) + ":" + str(current_time.tm_min))