Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions cydr.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ def _touch_handler(self, x, y):

#print("Touch:", x, y)

def raw(self):
'''
Returns all touch-data readable

Return:
p: pressure of the touch; x * (z1 - z2) / z1 if z1>0 else None
x: x coordinate of finger 1
y: y coordinate of finger 1

z1: touchplate z1 position
z2: touchplate z2 position
t0: TEMP0 command result
t1: TEMP1 command result
b: GET_BATTERY command result
a: GET_AUX command result
d: number of milliseconds since this self.raw() routine last detected another touch
'''
return self._touch.raw_touch() # p, x, y, z1, z2, t0, t1, b, a, d = touch.raw_touch()

def touches(self):
'''
Returns last stored touch data.
Expand Down
38 changes: 27 additions & 11 deletions resources/xpt2046.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# https://github.com/rdagger/micropython-ili9341/

"""XPT2046 Touch module."""
from time import sleep
import time


class Touch(object):
Expand Down Expand Up @@ -51,6 +51,7 @@ def __init__(self, spi, cs, int_pin=None, int_handler=None,
self.x_max = x_max
self.y_min = y_min
self.y_max = y_max
self.last_touch=0
self.x_multiplier = width / (x_max - x_min)
self.x_add = x_min * -self.x_multiplier
self.y_multiplier = height / (y_max - y_min)
Expand Down Expand Up @@ -81,30 +82,30 @@ def get_touch(self):
if dev <= 50: # Deviation should be under margin of 50
return self.normalize(meanx, meany)
# get a new value
sample = self.raw_touch() # get a touch
sample = self.raw_touch(True) # get a touch
if sample is None:
nsamples = 0 # Invalidate buff
else:
buff[buffptr] = sample # put in buff
buffptr = (buffptr + 1) % buf_length # Incr, until rollover
nsamples = min(nsamples + 1, buf_length) # Incr. until max

sleep(.05)
time.sleep(.05)
timeout -= .05
return None

def int_press(self, pin):
"""Send X,Y values to passed interrupt handler."""
if not pin.value() and not self.int_locked:
self.int_locked = True # Lock Interrupt
buff = self.raw_touch()
buff = self.raw_touch(True)

if buff is not None:
x, y = self.normalize(*buff)
self.int_handler(x, y)
sleep(.1) # Debounce falling edge
time.sleep(.1) # Debounce falling edge
elif pin.value() and self.int_locked:
sleep(.1) # Debounce rising edge
time.sleep(.1) # Debounce rising edge
self.int_locked = False # Unlock interrupt

def normalize(self, x, y):
Expand All @@ -113,18 +114,33 @@ def normalize(self, x, y):
y = int(self.y_multiplier * y + self.y_add)
return x, y

def raw_touch(self):
def raw_touch(self,xyonly=False):
"""Read raw X,Y touch values.

Returns:
tuple(int, int): X, Y
"""
x = self.send_command(self.GET_X)
y = self.send_command(self.GET_Y)
if self.x_min <= x <= self.x_max and self.y_min <= y <= self.y_max:
return (x, y)
else:
return None
if xyonly:
if self.x_min <= x <= self.x_max and self.y_min <= y <= self.y_max:
return (x, y)
else:
return None
else: # some boards give spurious touch events, which can be filtered out by looking at pressure and timing
z1 = self.send_command(self.GET_Z1) # pressure
z2 = self.send_command(self.GET_Z2)
t0 = self.send_command(self.GET_TEMP0) # temperature
t1 = self.send_command(self.GET_TEMP1)
b = self.send_command(self.GET_BATTERY)
a = self.send_command(self.GET_AUX) # chip aux ADC input
p = x * (z1 - z2) / z1 if z1>0 else None # touch-pressure is a formula based on x
d = None
if p is not None:
now=time.ticks_ms()
d = time.ticks_diff(now, self.last_touch) # timediff since last (for debounce)
self.last_touch=now
return (p, x, y, z1, z2, t0, t1, b, a, d)

def send_command(self, command):
"""Write command to XT2046 (MicroPython).
Expand Down