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

text_editor.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ## SD Card set up for saving text to SD card
  20. spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
  21. cs = board.SD_CS
  22. try:
  23. sdcard = sdcardio.SDCard(spi, cs)
  24. vfs = storage.VfsFat(sdcard)
  25. storage.mount(vfs, "/sd", readonly=False) # access files on sd card here
  26. SD_status = "SD Card Mounted"
  27. except OSError:
  28. SD_status = "No SD card found"
  29. pass # SD card not inserted/found
  30. #########################################
  31. keyb = Keyboard()
  32. page = "editor"
  33. intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
  34. intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B")
  35. intro_text_area.x = 10
  36. intro_text_area.y = 10
  37. text = ""
  38. text_area = label.Label(terminalio.FONT, text=text)
  39. text_area.x = 0
  40. text_area.y = 30
  41. board.DISPLAY.root_group = intro_text_area
  42. intro_text_area.append(text_area)
  43. filename_text = ""
  44. save_text = "Save File - Enter Filename\nOverwriting Enabled"
  45. save_group = label.Label(terminalio.FONT, text=save_text, color="C3B")
  46. save_group.x = 10
  47. save_group.y = 10
  48. filename_group = label.Label(terminalio.FONT, text=filename_text)
  49. filename_group.x = 0
  50. filename_group.y = 60
  51. save_group.append(filename_group)
  52. files_text = ""
  53. open_text = "Open File - Select using arrow keys"
  54. open_group = label.Label(terminalio.FONT, text=open_text, color="C3B")
  55. open_group.x = 10
  56. open_group.y = 10
  57. files_group = label.Label(terminalio.FONT, text=files_text)
  58. files_group.x = 0
  59. files_group.y = 30
  60. open_group.append(files_group)
  61. open_cursor = 0
  62. while True:
  63. key = keyb.scan()
  64. if key == "BACKSP":
  65. if page == "editor":
  66. text = text[:-1]
  67. text_area.text = text
  68. elif page == "save":
  69. filename_text = filename_text[:-1]
  70. filename_group.text = filename_text
  71. elif key == "CTRLD":
  72. if page == "editor":
  73. text = ""
  74. text_area.text = text
  75. elif page == "save":
  76. filename_text = ""
  77. filename_group.text = filename_text
  78. elif key == "CTRLS":
  79. board.DISPLAY.root_group = save_group
  80. page = "save"
  81. filename_group.text=filename_text
  82. elif key == "CTRLO":
  83. board.DISPLAY.root_group = open_group
  84. page="open"
  85. files_text = ""
  86. index = 0
  87. for file in os.listdir("/sd"):
  88. filename = str(file)
  89. if index == open_cursor:
  90. filename = "> " + filename
  91. files_text = files_text + filename+"\n"
  92. index += 1
  93. files_group.text = files_text
  94. elif key == "ESC":
  95. if page == "editor":
  96. pass
  97. elif page == "save" or page == "open":
  98. board.DISPLAY.root_group = intro_text_area
  99. page = "editor"
  100. else:
  101. ## The actual editor
  102. if page == "editor":
  103. ## WRAPPING
  104. ## calculate wrapping when entering a character
  105. lines = text.split("\n")
  106. current_line = lines[len(lines)-1]
  107. if len(current_line) > 35:
  108. text=text+"\n" # <- start a new line here
  109. ##############################################
  110. text = text+key
  111. text_area.text = text
  112. ## The save file menu - for typing the filename
  113. elif page == "save":
  114. if key == "DOWN":
  115. filename_text = filename_text + "."
  116. filename_group.text = filename_text
  117. elif key == "\n":
  118. with open("/sd/"+filename_text, "w") as f:
  119. f.write(text)
  120. f.close()
  121. board.DISPLAY.root_group = intro_text_area
  122. page = "editor"
  123. elif not keyb.is_alphanumeric(key):
  124. pass # dont allow special characters to be entered
  125. else:
  126. filename_text = filename_text+key
  127. filename_group.text=filename_text
  128. ## The open file menu - for selecting a file
  129. elif page == "open":
  130. pass