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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
  31. intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B")
  32. intro_text_area.x = 10
  33. intro_text_area.y = 10
  34. text = ""
  35. text_area = label.Label(terminalio.FONT, text=text)
  36. text_area.x = 0
  37. text_area.y = 40
  38. board.DISPLAY.root_group = intro_text_area
  39. intro_text_area.append(text_area)
  40. while True:
  41. key = keyb.scan()
  42. if key == "BACKSP":
  43. text = text[:-1]
  44. elif key == "CTRLD":
  45. text = ""
  46. else:
  47. ## WRAPPING
  48. ## calculate wrapping when entering a character
  49. lines = text.split("\n")
  50. current_line = lines[len(lines)-1]
  51. if len(current_line) > 35:
  52. text=text+"\n" # <- start a new line here
  53. ##############################################
  54. text = text+key
  55. text_area.text = text