Created
December 16, 2023 17:57
-
-
Save FoamyGuy/73593af39b6157510d34dc3f257d126c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2023 DJDevon3 | |
# SPDX-License-Identifier: MIT | |
# ESP32-S3 Feather Weather MQTT Touchscreen | |
# Coded for Circuit Python 8.2.x | |
import traceback | |
import time | |
import board | |
import displayio | |
import terminalio | |
from adafruit_display_text import label | |
from adafruit_displayio_layout.layouts.grid_layout import GridLayout | |
from adafruit_bitmap_font import bitmap_font | |
import adafruit_touchscreen | |
ASCII_CHARS = ( | |
"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", | |
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "", | |
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", ",", ".", "/", "\\", "'", | |
"[", "]", ";" | |
) | |
_now = time.monotonic() | |
DISPLAY_WIDTH = 480 | |
DISPLAY_HEIGHT = 320 | |
_touch_flip = (False, True) | |
# Initialize 3.5" TFT Featherwing Touchscreen | |
# ts_cs_pin = digitalio.DigitalInOut(board.D6) | |
# touchscreen = adafruit_stmpe610.Adafruit_STMPE610_SPI( | |
# board.SPI(), | |
# ts_cs_pin, | |
# calibration=((231, 3703), (287, 3787)), | |
# size=(display.width, display.height), | |
# disp_rotation=display.rotation, | |
# touch_flip=_touch_flip, | |
# ) | |
print("Init touchscreen") | |
# pylint: disable=no-member | |
touchscreen = adafruit_touchscreen.Touchscreen( | |
board.TOUCH_XL, | |
board.TOUCH_XR, | |
board.TOUCH_YD, | |
board.TOUCH_YU, | |
calibration=((6856, 60565), (9337, 56924)), | |
size=(board.DISPLAY.width, board.DISPLAY.height), | |
) | |
def _format_date(datetime): | |
return "{:02}/{:02}/{:02}".format( | |
datetime.tm_year, | |
datetime.tm_mon, | |
datetime.tm_mday, | |
) | |
def _format_time(datetime): | |
return "{:02}:{:02}".format( | |
datetime.tm_hour, | |
datetime.tm_min, | |
# datetime.tm_sec, | |
) | |
# Quick Colors for Labels | |
TEXT_BLACK = 0x000000 | |
TEXT_BLUE = 0x0000FF | |
TEXT_CYAN = 0x00FFFF | |
TEXT_GRAY = 0x8B8B8B | |
TEXT_GREEN = 0x00FF00 | |
TEXT_LIGHTBLUE = 0x90C7FF | |
TEXT_MAGENTA = 0xFF00FF | |
TEXT_ORANGE = 0xFFA500 | |
TEXT_PURPLE = 0x800080 | |
TEXT_RED = 0xFF0000 | |
TEXT_WHITE = 0xFFFFFF | |
TEXT_YELLOW = 0xFFFF00 | |
forkawesome_font = bitmap_font.load_font("/fonts/forkawesome-12.pcf") | |
# Function for minimizing labels to 1 liners | |
# Attribution: Anecdata (thanks!) | |
def make_my_label(font, anchor_point, anchored_position, scale, color): | |
func_label = label.Label(font) | |
func_label.anchor_point = anchor_point | |
func_label.anchored_position = anchored_position | |
func_label.scale = scale | |
func_label.color = color | |
return func_label | |
input_lbl = label.Label(terminalio.FONT, scale=2, text="", color=0xffffff, background_color=0x00000) | |
input_lbl.x = 10 | |
input_lbl.y = 10 | |
layout = GridLayout( | |
x=2, # layout x | |
y=100, # layout y | |
width=DISPLAY_WIDTH - 2, | |
height=DISPLAY_HEIGHT - 100, | |
grid_size=(14, 5), # Grid Layout width,height | |
cell_padding=2, | |
divider_lines=True, # divider lines around every cell | |
cell_anchor_point=(0.5, 0.5) | |
) | |
# Grid Layout Labels. Cell invisible with no text label | |
_labels = [] | |
keyboard_input = [] | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="`")) | |
layout.add_content(_labels[0], grid_position=(0, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="1")) | |
layout.add_content(_labels[1], grid_position=(1, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="2")) | |
layout.add_content(_labels[2], grid_position=(2, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="3")) | |
layout.add_content(_labels[3], grid_position=(3, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="4")) | |
layout.add_content(_labels[4], grid_position=(4, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="5")) | |
layout.add_content(_labels[5], grid_position=(5, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="6")) | |
layout.add_content(_labels[6], grid_position=(6, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="7")) | |
layout.add_content(_labels[7], grid_position=(7, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="8")) | |
layout.add_content(_labels[8], grid_position=(8, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="9")) | |
layout.add_content(_labels[9], grid_position=(9, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="0")) | |
layout.add_content(_labels[10], grid_position=(10, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="-")) | |
layout.add_content(_labels[11], grid_position=(11, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="=")) | |
layout.add_content(_labels[12], grid_position=(12, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(forkawesome_font, scale=1, x=0, y=0, text="\uf0e2")) | |
layout.add_content(_labels[13], grid_position=(13, 0), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Q")) | |
layout.add_content(_labels[14], grid_position=(0, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="W")) | |
layout.add_content(_labels[15], grid_position=(1, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="E")) | |
layout.add_content(_labels[16], grid_position=(2, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="R")) | |
layout.add_content(_labels[17], grid_position=(3, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="T")) | |
layout.add_content(_labels[18], grid_position=(4, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Y")) | |
layout.add_content(_labels[19], grid_position=(5, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="U")) | |
layout.add_content(_labels[20], grid_position=(6, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="I")) | |
layout.add_content(_labels[21], grid_position=(7, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="O")) | |
layout.add_content(_labels[22], grid_position=(8, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="P")) | |
layout.add_content(_labels[23], grid_position=(9, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="[")) | |
layout.add_content(_labels[24], grid_position=(10, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="]")) | |
layout.add_content(_labels[25], grid_position=(11, 1), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="\\")) | |
layout.add_content(_labels[26], grid_position=(12, 1), cell_size=(2, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="A")) | |
layout.add_content(_labels[27], grid_position=(0, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="S")) | |
layout.add_content(_labels[28], grid_position=(1, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="D")) | |
layout.add_content(_labels[29], grid_position=(2, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="F")) | |
layout.add_content(_labels[30], grid_position=(3, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="G")) | |
layout.add_content(_labels[31], grid_position=(4, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="H")) | |
layout.add_content(_labels[32], grid_position=(5, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="J")) | |
layout.add_content(_labels[33], grid_position=(6, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="K")) | |
layout.add_content(_labels[34], grid_position=(7, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="L")) | |
layout.add_content(_labels[35], grid_position=(8, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text=";")) | |
layout.add_content(_labels[36], grid_position=(9, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="'")) | |
layout.add_content(_labels[37], grid_position=(10, 2), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="ENTER")) | |
layout.add_content(_labels[38], grid_position=(11, 2), cell_size=(3, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="Z")) | |
layout.add_content(_labels[39], grid_position=(0, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="X")) | |
layout.add_content(_labels[40], grid_position=(1, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="C")) | |
layout.add_content(_labels[41], grid_position=(2, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="V")) | |
layout.add_content(_labels[42], grid_position=(3, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="B")) | |
layout.add_content(_labels[43], grid_position=(4, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="N")) | |
layout.add_content(_labels[44], grid_position=(5, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="M")) | |
layout.add_content(_labels[45], grid_position=(6, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text=",")) | |
layout.add_content(_labels[46], grid_position=(7, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text=",")) | |
layout.add_content(_labels[47], grid_position=(8, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text=".")) | |
layout.add_content(_labels[48], grid_position=(9, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="/")) | |
layout.add_content(_labels[49], grid_position=(10, 3), cell_size=(1, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="SHIFT")) | |
layout.add_content(_labels[50], grid_position=(11, 3), cell_size=(3, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="CTRL")) | |
layout.add_content(_labels[51], grid_position=(0, 4), cell_size=(2, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="ALT")) | |
layout.add_content(_labels[52], grid_position=(2, 4), cell_size=(2, 1)) | |
_labels.append(label.Label(terminalio.FONT, scale=2, x=0, y=0, text="SPACE")) | |
layout.add_content(_labels[53], grid_position=(4, 4), cell_size=(6, 1)) | |
_labels.append(label.Label(forkawesome_font, scale=1, x=0, y=0, text="\uf060")) | |
layout.add_content(_labels[54], grid_position=(10, 4), cell_size=(1, 1)) | |
_labels.append(label.Label(forkawesome_font, scale=1, x=0, y=0, text="\uf061")) | |
layout.add_content(_labels[55], grid_position=(11, 4), cell_size=(1, 1)) | |
_labels.append(label.Label(forkawesome_font, scale=1, x=0, y=0, text="\uf062")) | |
layout.add_content(_labels[56], grid_position=(12, 4), cell_size=(1, 1)) | |
_labels.append(label.Label(forkawesome_font, scale=1, x=0, y=0, text="\uf063")) | |
layout.add_content(_labels[57], grid_position=(13, 4), cell_size=(1, 1)) | |
# Create subgroups | |
splash = displayio.Group() | |
text_group = displayio.Group() | |
main_group = displayio.Group() | |
main_group.append(layout) | |
main_group.append(input_lbl) | |
board.DISPLAY.root_group = main_group | |
print(layout.get_cell((9, 4)).text) | |
print(layout.get_cell((10, 3)).text) | |
print(f"size: {layout.width}, {layout.height}") | |
print(f"cell size: {layout.cell_size_pixels}") | |
while True: | |
p = touchscreen.touch_point | |
if p: | |
#print(p) | |
touched_cell = layout.which_cell_contains(p) | |
if touched_cell: | |
touched_cell_view = layout.get_cell(touched_cell) | |
key_text = touched_cell_view.text | |
print(f"key_text: {key_text}") | |
if key_text in ASCII_CHARS: | |
input_lbl.text += key_text | |
elif key_text == "SPACE": | |
input_lbl.text += " " | |
touched_cell_view.background_color = 0x00ff00 | |
touched_cell_view.color = 0x000000 | |
time.sleep(0.2) | |
touched_cell_view.color = 0xffffff | |
layout.get_cell(touched_cell).background_color = 0x000000 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took the bits and pieces needed to merge into my script successfully. which_cell_contains is exactly what was needed. Thank you!