| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- # GUIDE
- # type freely! CTRL+D clears the text.
- # backspace, space, and return function as expected.
- # keys not recognised WILL print "UNKNOWN 0x??", but this is useful for extra key scans
- #
- # WORK IN PROGRESS
- # CTRL + S to save text to the SD Card
- # CTRL + O to open a text file
- # UP and DOWN scrolling - (use cursor )
- ######
-
- import board
- import terminalio
- from adafruit_display_text import label
- from keyboard import Keyboard
-
- import busio
- import sdcardio
- import storage
- import os
-
- ## SD Card set up for saving text to SD card
- spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
- cs = board.SD_CS
-
- try:
- sdcard = sdcardio.SDCard(spi, cs)
- vfs = storage.VfsFat(sdcard)
-
- storage.mount(vfs, "/sd", readonly=False) # access files on sd card here
- SD_status = "SD Card Mounted"
- except OSError:
- SD_status = "No SD card found"
- pass # SD card not inserted/found
- #########################################
-
- keyb = Keyboard()
-
- page = "editor"
-
- intro_text = "Press CTRL+D to clear text\n"+SD_status+"\n\n"
- intro_text_area = label.Label(terminalio.FONT, text=intro_text, color="C3B")
- intro_text_area.x = 10
- intro_text_area.y = 10
-
- text = ""
- text_area = label.Label(terminalio.FONT, text=text)
- text_area.x = 0
- text_area.y = 30
-
- board.DISPLAY.root_group = intro_text_area
- intro_text_area.append(text_area)
-
- filename_text = ""
- save_text = "Save File - Enter Filename\nOverwriting Enabled"
- save_group = label.Label(terminalio.FONT, text=save_text, color="C3B")
- save_group.x = 10
- save_group.y = 10
- filename_group = label.Label(terminalio.FONT, text=filename_text)
- filename_group.x = 0
- filename_group.y = 60
- save_group.append(filename_group)
-
- files_text = ""
- open_text = "Open File - Select using arrow keys"
- open_group = label.Label(terminalio.FONT, text=open_text, color="C3B")
- open_group.x = 10
- open_group.y = 10
- files_group = label.Label(terminalio.FONT, text=files_text)
- files_group.x = 0
- files_group.y = 30
- open_group.append(files_group)
-
- open_cursor = 0
-
- while True:
- key = keyb.scan()
-
- if key == "BACKSP":
- if page == "editor":
- text = text[:-1]
- text_area.text = text
- elif page == "save":
- filename_text = filename_text[:-1]
- filename_group.text = filename_text
-
- elif key == "CTRLD":
- if page == "editor":
- text = ""
- text_area.text = text
- elif page == "save":
- filename_text = ""
- filename_group.text = filename_text
-
- elif key == "CTRLS":
- board.DISPLAY.root_group = save_group
- page = "save"
- filename_group.text=filename_text
-
- elif key == "CTRLO":
- board.DISPLAY.root_group = open_group
- page="open"
- files_text = ""
- index = 0
- for file in os.listdir("/sd"):
- filename = str(file)
- if index == open_cursor:
- filename = "> " + filename
- files_text = files_text + filename+"\n"
- index += 1
- files_group.text = files_text
-
- elif key == "ESC":
- if page == "editor":
- pass
- elif page == "save" or page == "open":
- board.DISPLAY.root_group = intro_text_area
- page = "editor"
-
-
- else:
- ## The actual editor
- if page == "editor":
- ## WRAPPING
- ## calculate wrapping when entering a character
- lines = text.split("\n")
- current_line = lines[len(lines)-1]
- if len(current_line) > 35:
- text=text+"\n" # <- start a new line here
- ##############################################
- text = text+key
- text_area.text = text
-
- ## The save file menu - for typing the filename
- elif page == "save":
- if key == "DOWN":
- filename_text = filename_text + "."
- filename_group.text = filename_text
- elif key == "\n":
- with open("/sd/"+filename_text, "w") as f:
- f.write(text)
- f.close()
- board.DISPLAY.root_group = intro_text_area
- page = "editor"
- elif not keyb.is_alphanumeric(key):
- pass # dont allow special characters to be entered
- else:
- filename_text = filename_text+key
- filename_group.text=filename_text
-
- ## The open file menu - for selecting a file
- elif page == "open":
- pass
|