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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # CTRL + O to open a text file
  9. # UP and DOWN scrolling - (use cursor )
  10. ######
  11. import board
  12. import terminalio
  13. from adafruit_display_text import label
  14. from keyboard import Keyboard
  15. import busio
  16. import sdcardio
  17. import storage
  18. import os
  19. def save_file():
  20. save_text = "save file"
  21. save_group = label.Label(terminalio.FONT, text=save_text)
  22. save_group.x = 10
  23. save_group.y = 10
  24. board.DISPLAY.root_group = save_group
  25. def open_file():
  26. open_text = "open file"
  27. open_group = label.Label(terminalio.FONT, text=open_text)
  28. open_group.x = 10
  29. open_group.y = 10
  30. board.DISPLAY.root_group = open_group
  31. def editor(text):
  32. if page == "editor":
  33. ## WRAPPING
  34. ## calculate wrapping when entering a character
  35. lines = text.split("\n")
  36. current_line = lines[len(lines)-1]
  37. if len(current_line) > 35:
  38. text=text+"\n" # <- start a new line here
  39. ##############################################
  40. text = text+key
  41. text_area.text = text
  42. return text
  43. def esc():
  44. if page == "editor":
  45. pass
  46. elif page == "save" or page == "open":
  47. board.DISPLAY.root_group = intro_text_area
  48. ## SD Card set up for saving text to SD card
  49. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  50. cs = board.SD_CS
  51. try:
  52. sdcard = sdcardio.SDCard(spi, cs)
  53. vfs = storage.VfsFat(sdcard)
  54. storage.mount(vfs, "/sd") # access files on sd card here
  55. SD_status = "SD Card Mounted"
  56. except OSError:
  57. SD_status = "No SD card found"
  58. pass # SD card not inserted/found
  59. #########################################
  60. keyb = Keyboard()
  61. page = "editor"
  62. intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
  63. intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B")
  64. intro_text_area.x = 10
  65. intro_text_area.y = 10
  66. text = ""
  67. text_area = label.Label(terminalio.FONT, text=text)
  68. text_area.x = 0
  69. text_area.y = 40
  70. board.DISPLAY.root_group = intro_text_area
  71. intro_text_area.append(text_area)
  72. while True:
  73. key = keyb.scan()
  74. if key == "BACKSP":
  75. text = text[:-1]
  76. elif key == "CTRLD":
  77. text = ""
  78. elif key == "CTRLS":
  79. save_file()
  80. page = "save"
  81. elif key == "CTRLO":
  82. open_file()
  83. page="open"
  84. elif key == "ESC":
  85. esc()
  86. page = "editor"
  87. else:
  88. text = editor(text)