# SPDX-FileCopyrightText: 2022 Paulus Schulinck
#
# SPDX-License-Identifier: MIT
##############################
# 2022-10-22. Last update: 2023-07-26
# by @PaulskPt
#
# touch test results on an Espressif ESP32-S3-BOX
#
# 319 X 0
# | |
# ---+--------------------+--- 0
# | |
# | |
# | |
# Y | | Middle: x: 159, y: 119
# | |
# | |
# | |
# ---+--------------------+--- 239
# | |
#
# Conclusion: instead of having 0,0 as the top-left corner
# the top-right corner is 0,0
# and the lower-left is 319,239
# NOTE: touching the "red" button on the middle of the lower part of the display
# causes the touch functionality to halt. Only a reset can restart the
# touch test again. This problem I will discuss with the Adafruit Circuitpython team.
#
"""
Example output (part):
Circuitpython ESP32-S3-Box touch test.
Please touch the screen,
except the bottom line with the red button dot.
main():
Waiting for your touches...
ck_touch(): nr of touches= 1
ck_touched(): screen touched at: (319,0). Pressure: 13
main(): Added touch data: id= 0, at x,y = 319,0. Pressure= 13
[...]
There are 10 records in the touch dictionary:
dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
key: 0, x,y = 0, 0. id: 0. pressure: 0
key: 1, x,y = 221,129. id: 0. pressure: 26
key: 2, x,y = 221,129. id: 0. pressure: 34
key: 3, x,y = 152, 94. id: 0. pressure: 32
key: 4, x,y = 238, 36. id: 0. pressure: 26
key: 5, x,y = 280, 56. id: 0. pressure: 30
key: 6, x,y = 179, 95. id: 0. pressure: 47
key: 7, x,y = 25,191. id: 0. pressure: 25
key: 8, x,y = 57, 45. id: 0. pressure: 25
key: 9, x,y = 319, 0. id: 0. pressure: 13
Done!
"""
####
import time
import sys
touch_dict = {0: {'x':0,
'y': 0,
'id': 0,
'pressure': 0}}
import board
import busio
import adafruit_tt21100
my_debug = False
i2c = busio.I2C(board.SCL, board.SDA)
# Create library object (named "tt") using a Bus I2C port
# Note: GPIO3 is connected to board.CPT_INT
# See schematic -> Strapping
tt = adafruit_tt21100.TT21100(i2c) # , irq_pin=3)
# print(f"dir(tt)= {dir(tt)}")
if tt:
if my_debug:
print(f"\ndir(tt)= {dir(tt)}")
print(f"Global: tt._i2c= {tt._i2c}, irq_pin= {tt._irq_pin}")
touch_x = 0
touch_y = 0
touch_id = 0 # this is used in case of multi-touch
touch_pr = 0
def clr_touch():
global touch_x, touch_y, touch_id, touch_pr, tt
touch_x = 0
touch_y = 0
touch_id = 0
touch_pr = 0
# Clear the touch buffer
TAG="clr_touch(): "
cnt = 0
ts = []
while True:
ts = tt.touches
if my_debug:
print(TAG+f"ts= {ts}, type(ts)= {type(ts)}")
if isinstance(ts, list):
if len(ts) == 0:
break
if cnt == 0:
time.sleep(0.2)
cnt += 1
if cnt >= 10:
if not my_debug:
print(TAG+f"touch buffer read {cnt} times.")
break
def ck_touch():
TAG = "ck_touch(): "
# if the screen is being touched print the touches
ts = None
n = tt.touched
if n > 0:
print(TAG+f"nr of touches= {n}")
ts = tt.touches
if my_debug:
print(f"type(ts)= {type(ts)}")
print(f"ck_touched(): screen touched at: ({ts[0]['x']},{ts[0]['y']}). Pressure: {ts[0]['pressure']}")
return ts
def main():
global touch_x, touch_y, touch_id, touch_pr
TAG = "main(): "
cnt = 0 # index to touch_dict
ts = None
print("\nCircuitpython ESP32-S3-Box touch test.")
print("Please touch the screen,")
print("except the bottom line with the red button dot.")
print(TAG+"\nWaiting for your touches...\n")
clr_touch()
while True:
try:
while ts is None:
ts = ck_touch() # added just for trial
if ts is None:
time.sleep(1)
t = type(ts)
if isinstance(ts, list):
ts_dict = ts[0]
t = type(ts_dict)
# print(TAG+f"type(ts_dict)= {t}")
if isinstance(ts_dict, dict):
touch_y = ts_dict['y']
touch_x = ts_dict['x']
touch_id = ts_dict['id']
touch_pr = ts_dict['pressure']
if cnt in touch_dict.keys():
cnt += 1 # we don't want overwrite existing touch data
touch_dict[cnt] = {'x': touch_x, 'y': touch_y, 'id': touch_id, 'pressure': touch_pr}
print(TAG+f"Added touch data: id= {touch_id}, at x,y = {touch_x},{touch_y}. Pressure= {touch_pr}")
# cleanup for next round
clr_touch()
ts = None
# increase dict key index
cnt += 1
else:
print(TAG+f"expected type dict but received type: {t}")
raise TypeError
else:
print(TAG+f"expected type list but received type: {t}")
raise TypeError
except KeyboardInterrupt:
print("User interrupted. Exiting...")
break
time.sleep(1)
if cnt >= 10:
break
le = len(touch_dict)
if le > 0:
print(f"\nThere are {le} records in the touch dictionary:")
print(touch_dict.keys())
try:
for k,v in touch_dict.items():
print("key: {:2d}, x,y = {:3d},{:3d}. id: {:3d}. pressure: {}".format(k, v['x'],
v['y'], v['id'], v['pressure']))
except IndexError as e:
print(f"IndexError occurred at key= {k}")
else:
print("touch dictionary is empty")
print("Done!\n")
sys.exit()
if __name__ == '__main__':
main()
REPL output of board:
>>> import board
>>> dir(board)
['__class__', '__name__', 'BOOT', 'CLK', 'CS', 'CTP_INT', 'DISPLAY', 'G10', 'G11', 'G12', 'G13', 'G14', 'G21', 'G38', 'G39', 'G40', 'G41', 'G42', 'G43', 'G44', 'G9', 'I2S_ADC_SDOUT', 'I2S_CODEC_DSDIN', 'I2S_LRCK', 'I2S_MCLK', 'I2S_SCLK', 'LCD_CS', 'LCD_CTRL', 'LCD_DC', 'LCD_MOSI', 'LCD_RST', 'LCD_SCK', 'MISO', 'MOSI', 'MUTE_STATUS', 'PA_CTRL', 'SCL', 'SCL2', 'SDA', 'SDA2', 'U0RXD', 'U0TXD', 'board_id']
The script above runs fine until the "red" circle button in the middle on the bottom of the frame around the display is touched (see image in ´Additional information´ below.
Only a reset of the device can restore the touch functionality.
In documation of Espressif the "red" button is referred as "home" button.
The Espressif demo firmware makes use of the "red" circle button.
In circuitpython 8.1.0-beta.2 in the board module there is no "board.HOME" button defined.