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

code.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. ### DEFAULT HOME PAGE # # # # # # # # # #
  7. def home():
  8. title_label.text = "Home"
  9. title_label.x = 95
  10. title_label.y = 14
  11. page_label.text = """
  12. hello :)
  13. """
  14. page_label.x = 5
  15. page_label.y = 30
  16. # # # # # # # # # # # # # # # # # #
  17. # SET UP SD CARD
  18. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  19. cs = board.SD_CS
  20. try:
  21. sdcard = sdcardio.SDCard(spi, cs)
  22. vfs = storage.VfsFat(sdcard)
  23. storage.mount(vfs, "/sd") # access files on sd card here
  24. except OSError:
  25. pass # SD card not inserted/found
  26. # SET UP DISPLAY
  27. display = board.DISPLAY
  28. display_group = displayio.Group()
  29. display.root_group = display_group
  30. # Header Bar
  31. rect = Rect(0, 0, 238, 30, fill=0x1f4476)
  32. display.root_group.append(rect)
  33. # Page Title
  34. title_font = terminalio.FONT
  35. title_color = 0xffffff
  36. title_label = label.Label(title_font, text="Home", color=title_color, scale=2)
  37. display.root_group.append(title_label)
  38. # Page Text
  39. page_font = terminalio.FONT
  40. page_color = 0xffffff
  41. page_label = label.Label(page_font, text="page_text", color=page_color, scale=1)
  42. display.root_group.append(page_label)
  43. home()
  44. keyb = Keyboard()
  45. ## PROGRAM MAIN LOOP (WORKS FROM HOME PAGE IF OTHER PAGES HAVE THEIR OWN LOOPS)
  46. while True:
  47. key = keyb.scan()