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

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