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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # GUIDE
  2. # type freely! CTRL+D clears the text.
  3. # backspace, space, and return function as expected.
  4. # keys not recognised WILL print "UNKNOWN 0x??", but this is useful for extra key scans
  5. #
  6. # WORK IN PROGRESS
  7. # CTRL + S to save text to the SD Card
  8. ######
  9. import board
  10. import terminalio
  11. from adafruit_display_text import label
  12. from keyboard import Keyboard
  13. import busio
  14. import sdcardio
  15. import storage
  16. import os
  17. ## SD Card set up for saving text to 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. SD_status = "SD Card Mounted"
  25. except OSError:
  26. SD_status = "No SD card found"
  27. pass # SD card not inserted/found
  28. #########################################
  29. keyb = Keyboard()
  30. text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
  31. text_area = label.Label(terminalio.FONT, text=text)
  32. text_area.x = 10
  33. text_area.y = 10
  34. board.DISPLAY.root_group = text_area
  35. while True:
  36. key = keyb.scan()
  37. if key == "BACKSP":
  38. text = text[:-1]
  39. elif key == "CTRLD":
  40. text = "Press CTRL+D to clear text\n\n"
  41. else:
  42. ## WRAPPING
  43. ## calculate wrapping when entering a character
  44. lines = text.split("\n")
  45. current_line = lines[len(lines)-1]
  46. if len(current_line) > 35:
  47. text=text+"\n" # <- start a new line here
  48. ##############################################
  49. text = text+key
  50. text_area.text = text