Update 'CARDPUTER CircuitPython/projects/text_editor.py'

This commit is contained in:
2025-03-05 12:25:29 +00:00
parent 1fddfb37cc
commit f116dfc4c2

View File

@@ -0,0 +1,65 @@
# 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
######
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") # 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()
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 = 40
board.DISPLAY.root_group = intro_text_area
intro_text_area.append(text_area)
while True:
key = keyb.scan()
if key == "BACKSP":
text = text[:-1]
elif key == "CTRLD":
text = ""
else:
## 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