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

code.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ### DEFAULT HOME PAGE # # # # # # # # # #
  7. def home():
  8. title_label.text = "Home"
  9. title_label.x = 10
  10. title_label.y = 14
  11. ip_label.text = my_ip
  12. ip_label.x = 160
  13. ip_label.y = 20
  14. page_label.text = """
  15. 1 - User Info
  16. 2 - Make Post
  17. """
  18. page_label.x = 5
  19. page_label.y = 30
  20. # # # # # # # # # # # # # # # # # #
  21. keyb = Keyboard()
  22. # SET UP DISPLAY & WIFI
  23. display = board.DISPLAY
  24. display_group = displayio.Group()
  25. display.root_group = display_group
  26. wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
  27. [os.getenv("WIFI2"),os.getenv("WIFIPW2")],
  28. [os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
  29. h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
  30. h1.x = 10
  31. h1.y = 15
  32. count = 0
  33. menu_labs = []
  34. for option in wifi_options:
  35. if count == 0:
  36. menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
  37. else:
  38. menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
  39. menu_lab.x = 10
  40. menu_lab.y = 35 + count * 10
  41. display.root_group.append(menu_lab)
  42. menu_labs.append(menu_lab)
  43. count+=1
  44. display.root_group.append(h1)
  45. pos = 0
  46. while True:
  47. key = keyb.scan()
  48. if key == "UP":
  49. if pos == 0:
  50. pass
  51. else:
  52. menu_labs[pos].text = wifi_options[pos][0]
  53. pos -= 1
  54. menu_labs[pos].text = "> " + wifi_options[pos][0]
  55. elif key == "DOWN":
  56. if pos == len(wifi_options)-1:
  57. pass
  58. else:
  59. menu_labs[pos].text = wifi_options[pos][0]
  60. pos += 1
  61. menu_labs[pos].text = "> " + wifi_options[pos][0]
  62. elif key == "\n":
  63. WIFI_SSID = wifi_options[pos][0]
  64. WIFI_PASS = wifi_options[pos][1]
  65. break
  66. try:
  67. for lab in menu_labs:
  68. display.root_group.remove(lab)
  69. txt = label.Label(terminalio.FONT, text = "", color=0x00ff00, scale=1)
  70. txt.text = "Connecting to " + WIFI_SSID + "..."
  71. txt.x = 10
  72. txt.y = 35
  73. display.root_group.append(txt)
  74. wifi.radio.connect(ssid=WIFI_SSID,
  75. password=WIFI_PASS)
  76. my_ip = str(wifi.radio.ipv4_address).strip()
  77. display.root_group.remove(h1)
  78. display.root_group.remove(txt)
  79. except ConnectionError as e:
  80. h1.text = "Aw..."
  81. txt.text = "Could not connect to " + WIFI_SSID
  82. txt.color = 0xff0000
  83. txt.x = 10
  84. txt.y = 35
  85. txt2 = label.Label(terminalio.FONT, text = "", color=0x89a0a8, scale=1)
  86. txt2.text = "Press CTRL + C followed by CTRL + D\nto soft-reboot"
  87. txt2.x = 10
  88. txt2.y = 100
  89. display.root_group.append(txt2)
  90. while True:
  91. pass
  92. # SET UP SD CARD
  93. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  94. cs = board.SD_CS
  95. try:
  96. sdcard = sdcardio.SDCard(spi, cs)
  97. vfs = storage.VfsFat(sdcard)
  98. storage.mount(vfs, "/sd") # access files on sd card here
  99. except OSError:
  100. pass # SD card not inserted/found
  101. # Header Bar
  102. rect = Rect(0, 0, 238, 30, fill=0x1f4476)
  103. display.root_group.append(rect)
  104. # Page Title
  105. title_font = terminalio.FONT
  106. title_color = 0xffffff
  107. title_label = label.Label(title_font, text="Home", color=title_color, scale=2)
  108. display.root_group.append(title_label)
  109. # Page Text
  110. page_font = terminalio.FONT
  111. page_color = 0xffffff
  112. page_label = label.Label(page_font, text="page_text", color=page_color, scale=1)
  113. display.root_group.append(page_label)
  114. # IP Label
  115. ip_font = terminalio.FONT
  116. ip_color = 0x89a0a8
  117. ip_label = label.Label(ip_font, text="0.0.0.0", color=ip_color, scale=1)
  118. display.root_group.append(ip_label)
  119. home()
  120. ## PROGRAM MAIN LOOP (WORKS FROM HOME PAGE IF OTHER PAGES HAVE THEIR OWN LOOPS)
  121. while True:
  122. key = keyb.scan()