Skip to content

Commit f67613a

Browse files
committed
updates xpt2046 driver
1 parent cabd909 commit f67613a

File tree

2 files changed

+81
-82
lines changed

2 files changed

+81
-82
lines changed

api_drivers/common_api_drivers/indev/xpt2046.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import time
66

77

8-
_CMD_X_READ = const(0xD0) # 12 bit resolution
9-
_CMD_Y_READ = const(0x90) # 12 bit resolution
8+
_CMD_X_READ = const(0xD1) # 12 bit resolution
9+
_CMD_Y_READ = const(0x91) # 12 bit resolution
10+
_CMD_Z1_READ = const(0xB1) # 12 bit resolution
11+
_CMD_Z2_READ = const(0xC1) # 12 bit resolution
12+
1013

1114
_MIN_RAW_COORD = const(10)
1215
_MAX_RAW_COORD = const(4090)
@@ -15,64 +18,56 @@
1518
class XPT2046(pointer_framework.PointerDriver):
1619

1720
confidence = 5
18-
margin = 50
21+
z_thresh = 400
1922

2023
def __init__(
2124
self,
2225
spi_bus,
2326
touch_cal=None
2427
):
2528
super().__init__(touch_cal=touch_cal)
26-
self._trans_buf = bytearray(3)
29+
self._trans_buf = bytearray(2)
2730
self._trans_mv = memoryview(self._trans_buf)
2831

29-
self._recv_buf = bytearray(3)
32+
self._recv_buf = bytearray(2)
3033
self._recv_mv = memoryview(self._recv_buf)
3134

3235
self.__confidence = max(min(self.confidence, 25), 3)
3336
self.__points = [[0, 0] for _ in range(self.__confidence)]
3437

35-
margin = max(min(self.margin, 100), 1)
36-
self.__margin = margin * margin
37-
3838
self._spi = spi_bus
3939

4040
def _read_reg(self, reg):
4141
self._trans_buf[0] = reg
4242
self._recv_buf[0] = 0x00
4343
self._recv_buf[1] = 0x00
44-
self._recv_buf[2] = 0x00
4544

4645
self._spi.write_readinto(self._trans_mv, self._recv_mv)
4746

48-
return ((self._recv_buf[1] << 8) | self._recv_buf[2]) >> 3
47+
return ((self._recv_buf[0] << 8) | self._recv_buf[1]) >> 3
4948

5049
def _get_coords(self):
5150
points = self.__points
5251
count = 0
53-
timeout = 5000000
54-
start_time = time.ticks_ns()
55-
while timeout > 0:
56-
if count == self.__confidence:
57-
break
58-
59-
sample = self._get_raw() # get a touch
60-
if sample is not None:
61-
points[count][0], points[count][1] = sample # put in buff
62-
count += 1
63-
64-
end_time = time.ticks_ns()
65-
timeout -= time.ticks_diff(end_time, start_time)
66-
start_time = end_time
67-
68-
if count:
52+
53+
z1 = self._read_reg(_CMD_Z1_READ)
54+
z2 = self._read_reg(_CMD_Z2_READ)
55+
56+
z = z1 + (_MAX_RAW_COORD - z2)
57+
if z >= self.z_thresh:
58+
self._read_reg(_CMD_X_READ)
59+
60+
while count < self.__confidence:
61+
sample = self._get_raw() # get a touch
62+
if sample is not None:
63+
points[count][0], points[count][1] = sample # put in buff
64+
count += 1
65+
6966
meanx = sum([points[i][0] for i in range(count)]) // count
7067
meany = sum([points[i][1] for i in range(count)]) // count
71-
dev = sum([(points[i][0] - meanx) ** 2 + (points[i][1] - meany) ** 2 for i in range(count)]) / count
7268

73-
if dev <= self.__margin:
74-
x, y = self._normalize(meanx, meany)
75-
return self.PRESSED, x, y
69+
x, y = self._normalize(meanx, meany)
70+
return self.PRESSED, x, y
7671

7772
return None
7873

@@ -85,7 +80,7 @@ def _normalize(self, x, y):
8580
def _get_raw(self):
8681
x = self._read_reg(_CMD_X_READ)
8782
y = self._read_reg(_CMD_Y_READ)
88-
if x > _MIN_RAW_COORD and y < _MAX_RAW_COORD: # touch pressed?
83+
if _MAX_RAW_COORD >= x >= _MIN_RAW_COORD and _MAX_RAW_COORD >= y >= _MIN_RAW_COORD:
8984
return x, y
9085
else:
9186
return None

api_drivers/py_api_drivers/frozen/indev/pointer_framework.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import lvgl as lv # NOQA
22
import _indev_base
3+
import micropython # NOQA
34

45

5-
def _remap(value, old_min, old_max, new_min, new_max):
6-
return int((((value - old_min) * (new_max - new_min)) / (old_max - old_min)) + new_min)
6+
@micropython.viper
7+
def _remap(value: int, old_min: int, old_max: int, new_min: int, new_max: int) -> int:
8+
newrange: int = new_max - new_min
9+
oldrange: int = old_max - old_min
10+
step1: int = value - old_min
11+
step2: int = step1 * newrange
12+
step3: int = int(step2 / oldrange)
13+
14+
return step3 + new_min
715

816

917
class PointerDriver(_indev_base.IndevBase):
@@ -59,62 +67,58 @@ def _get_coords(self):
5967
# of (state, x, y) or None if no touch even has occured
6068
raise NotImplementedError
6169

70+
def _calc_coords(self, x, y):
71+
cal = self._cal
72+
rotation = self._rotation
73+
left = cal.left
74+
right = cal.right
75+
top = cal.top
76+
bottom = cal.bottom
77+
78+
if left is None:
79+
left = 0
80+
if right is None:
81+
right = self._orig_width
82+
if top is None:
83+
top = 0
84+
if bottom is None:
85+
bottom = self._orig_height
86+
87+
if rotation == lv.DISPLAY_ROTATION._90: # NOQA
88+
left, right, top, bottom = bottom, top, left, right
89+
x, y = y, x
90+
elif rotation == lv.DISPLAY_ROTATION._180: # NOQA
91+
left, right, top, bottom = right, left, bottom, top
92+
elif rotation == lv.DISPLAY_ROTATION._270: # NOQA
93+
left, right, top, bottom = top, bottom, right, left
94+
x, y = y, x
95+
96+
xpos = _remap(x, left, right, 0, self._width)
97+
ypos = _remap(y, top, bottom, 0, self._height)
98+
return xpos, ypos
99+
62100
def _read(self, drv, data): # NOQA
63101
coords = self._get_coords()
64102

65-
if coords is None: # ignore no touch & multi touch
66-
if self._current_state != self.RELEASED:
67-
self._current_state = self.RELEASED
68-
69-
data.point.x = self._last_x
70-
data.point.y = self._last_y
71-
data.state = self._current_state
72-
# print("raw(x={0}, y={1}) point(x={2} y={3})".format(-1, -1, data.point.x, data.point.y)) # NOQA
73-
data.continue_reading = False
74-
return
75-
76-
state, x, y = coords
103+
if coords is None:
104+
state = self.RELEASED
105+
x, y = self._last_x, self._last_y
106+
else:
107+
state, x, y = coords
77108

78109
if None not in (x, y):
79-
cal = self._cal
80-
rotation = self._rotation
81-
left = cal.left
82-
right = cal.right
83-
top = cal.top
84-
bottom = cal.bottom
85-
86-
if left is None:
87-
left = 0
88-
if right is None:
89-
right = self._orig_width
90-
if top is None:
91-
top = 0
92-
if bottom is None:
93-
bottom = self._orig_height
94-
95-
if rotation == lv.DISPLAY_ROTATION._0: # NOQA
96-
xpos = _remap(x, left, right, 0, self._orig_width)
97-
ypos = _remap(y, top, bottom, 0, self._orig_height)
98-
elif rotation == lv.DISPLAY_ROTATION._90: # NOQA
99-
xpos = _remap(y, bottom, top, 0, self._orig_height)
100-
ypos = _remap(x, left, right, 0, self._orig_width)
101-
elif rotation == lv.DISPLAY_ROTATION._180: # NOQA
102-
xpos = _remap(x, right, left, 0, self._orig_width)
103-
ypos = _remap(y, bottom, top, 0, self._orig_height)
104-
elif rotation == lv.DISPLAY_ROTATION._270: # NOQA
105-
xpos = _remap(y, top, bottom, 0, self._orig_height)
106-
ypos = _remap(x, right, left, 0, self._orig_width)
110+
self._last_x, self._last_y = x, y
111+
if coords is None:
112+
data.continue_reading = False
107113
else:
108-
raise RuntimeError
109-
110-
self._last_x = xpos
111-
self._last_y = ypos
112-
113-
data.continue_reading = True
114+
data.continue_reading = True
115+
else:
116+
data.continue_reading = False
114117

115-
data.state = self._current_state
116-
data.point.x = self._last_x
117-
data.point.y = self._last_y
118+
data.point.x, data.point.y = (
119+
self._calc_coords(self._last_x, self._last_y)
120+
)
121+
data.state = state
118122

119123
def get_vect(self, point):
120124
self._indev_drv.get_vect(point)

0 commit comments

Comments
 (0)