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

code.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. from keyb import Keyboard # type: ignore
  2. import time, os, rtc, json, wifi, supervisor # 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. #import pytumblr # type: ignore
  7. import tumblrapi # type: ignore
  8. ### DEFAULT HOME PAGE # # # # # # # # # #
  9. def home(pos = 0):
  10. title_label.text = "Home"
  11. title_label.x = 10
  12. title_label.y = 14
  13. ip_label.text = my_ip
  14. ip_label.x = 160
  15. ip_label.y = 20
  16. menu_options = ["SD Card", "Tumblr", "Web Server"]
  17. count = 0
  18. menu_labs = []
  19. for option in menu_options:
  20. if count == pos:
  21. menu_label = label.Label(terminalio.FONT, text = "> " + option, color=0xffffff, scale=1)
  22. else:
  23. menu_label = label.Label(terminalio.FONT, text = option, color=0xffffff, scale=1)
  24. menu_label.x = 10
  25. menu_label.y = 45 + count * 10
  26. display.root_group.append(menu_label)
  27. menu_labs.append(menu_label)
  28. count+=1
  29. while True:
  30. key = keyb.scan()
  31. if key == "UP":
  32. if pos == 0:
  33. pass
  34. else:
  35. menu_labs[pos].text = menu_options[pos]
  36. pos -= 1
  37. menu_labs[pos].text = "> " + menu_options[pos]
  38. elif key == "DOWN":
  39. if pos == len(menu_options)-1:
  40. pass
  41. else:
  42. menu_labs[pos].text = menu_options[pos]
  43. pos += 1
  44. menu_labs[pos].text = "> " + menu_options[pos]
  45. elif key == "\n":
  46. option = menu_options[pos]
  47. if option == "SD Card":
  48. for lab in menu_labs:
  49. display.root_group.remove(lab)
  50. sd_card()
  51. return
  52. elif option == "Tumblr":
  53. for lab in menu_labs:
  54. display.root_group.remove(lab)
  55. tumblr()
  56. return
  57. elif option == "Web Server":
  58. for lab in menu_labs:
  59. display.root_group.remove(lab)
  60. web_server()
  61. return
  62. # # # # # # # # # # # # # # # # # #
  63. def sd_card():
  64. title_label.text = "SD Card"
  65. fpos = 0
  66. filename_labels = []
  67. files = os.listdir("/sd")
  68. count = 0
  69. for filename in files:
  70. if count == pos:
  71. filename_label = label.Label(terminalio.FONT, text = "> " + filename, color=0xffffff, scale=1)
  72. else:
  73. filename_label = label.Label(terminalio.FONT, text = filename, color=0xffffff, scale=1)
  74. filename_label.x = 10
  75. filename_label.y = 45 + count * 10
  76. display.root_group.append(filename_label)
  77. filename_labels.append(filename_label)
  78. count+=1
  79. while True:
  80. key = keyb.scan()
  81. if key == "ESC":
  82. for lab in filename_labels:
  83. display.root_group.remove(lab)
  84. home(pos = 0)
  85. return
  86. elif key == "UP":
  87. if fpos == 0:
  88. pass
  89. else:
  90. filename_labels[fpos].text = files[fpos]
  91. fpos -= 1
  92. filename_labels[fpos].text = "> " + files[fpos]
  93. elif key == "DOWN":
  94. if fpos == len(files)-1:
  95. pass
  96. else:
  97. filename_labels[fpos].text = files[fpos]
  98. fpos += 1
  99. filename_labels[fpos].text = "> " + files[fpos]
  100. elif key == "\n":
  101. file = "/sd/" + files[fpos]
  102. for lab in filename_labels:
  103. display.root_group.remove(lab)
  104. file_label = label.Label(terminalio.FONT, text = "", color=0xffffff, scale=1)
  105. file_label.x = 10
  106. file_label.y = 45
  107. display.root_group.append(file_label)
  108. with open(file, "r") as f:
  109. file_label.text = f.read()
  110. while True:
  111. key = keyb.scan()
  112. if key == "ESC":
  113. display.root_group.remove(file_label)
  114. sd_card()
  115. return
  116. def tumblr():
  117. title_label.text = "Tumblr"
  118. while True:
  119. key = keyb.scan()
  120. if key == "ESC":
  121. home(pos = 1)
  122. return
  123. def web_server():
  124. title_label.text = "Web Server"
  125. while True:
  126. key = keyb.scan()
  127. if key == "ESC":
  128. home(pos = 2)
  129. return
  130. keyb = Keyboard()
  131. # SET UP DISPLAY & WIFI
  132. display = board.DISPLAY
  133. display_group = displayio.Group()
  134. display.root_group = display_group
  135. wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
  136. [os.getenv("WIFI2"),os.getenv("WIFIPW2")],
  137. [os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
  138. h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
  139. h1.x = 10
  140. h1.y = 15
  141. count = 0
  142. menu_labs = []
  143. for option in wifi_options:
  144. if count == 0:
  145. menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
  146. else:
  147. menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
  148. menu_lab.x = 10
  149. menu_lab.y = 35 + count * 10
  150. display.root_group.append(menu_lab)
  151. menu_labs.append(menu_lab)
  152. count+=1
  153. display.root_group.append(h1)
  154. pos = 0
  155. while True:
  156. key = keyb.scan()
  157. if key == "UP":
  158. if pos == 0:
  159. pass
  160. else:
  161. menu_labs[pos].text = wifi_options[pos][0]
  162. pos -= 1
  163. menu_labs[pos].text = "> " + wifi_options[pos][0]
  164. elif key == "DOWN":
  165. if pos == len(wifi_options)-1:
  166. pass
  167. else:
  168. menu_labs[pos].text = wifi_options[pos][0]
  169. pos += 1
  170. menu_labs[pos].text = "> " + wifi_options[pos][0]
  171. elif key == "\n":
  172. WIFI_SSID = wifi_options[pos][0]
  173. WIFI_PASS = wifi_options[pos][1]
  174. break
  175. try:
  176. for lab in menu_labs:
  177. display.root_group.remove(lab)
  178. txt = label.Label(terminalio.FONT, text = "", color=0x00ff00, scale=1)
  179. txt.text = "Connecting to " + WIFI_SSID + "..."
  180. txt.x = 10
  181. txt.y = 35
  182. display.root_group.append(txt)
  183. wifi.radio.connect(ssid=WIFI_SSID,
  184. password=WIFI_PASS)
  185. my_ip = str(wifi.radio.ipv4_address).strip()
  186. display.root_group.remove(h1)
  187. display.root_group.remove(txt)
  188. except ConnectionError as e:
  189. h1.text = "Aw..."
  190. txt.text = "Could not connect to " + WIFI_SSID
  191. txt.color = 0xff0000
  192. txt.x = 10
  193. txt.y = 35
  194. txt2 = label.Label(terminalio.FONT, text = "", color=0x89a0a8, scale=1)
  195. txt2.text = "Press CTRL + C followed by CTRL + D\nto soft-reboot"
  196. txt2.x = 10
  197. txt2.y = 100
  198. display.root_group.append(txt2)
  199. while True:
  200. pass
  201. # SET UP SD CARD
  202. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  203. cs = board.SD_CS
  204. try:
  205. sdcard = sdcardio.SDCard(spi, cs)
  206. vfs = storage.VfsFat(sdcard)
  207. storage.mount(vfs, "/sd") # access files on sd card here
  208. except OSError:
  209. pass # SD card not inserted/found
  210. # Header Bar
  211. header_bar = Rect(0, 0, 238, 30, fill=0x1f4476)
  212. display.root_group.append(header_bar)
  213. # Page Title
  214. title_label = label.Label(terminalio.FONT, text="Home", color=0xffffff, scale=2)
  215. display.root_group.append(title_label)
  216. # IP Label
  217. ip_label = label.Label(terminalio.FONT, text="0.0.0.0", color=0x89a0a8, scale=1)
  218. display.root_group.append(ip_label)
  219. home()