1
1
from micropython import const
2
+ import micropython
2
3
import machine
3
4
import pointer_framework
4
5
5
6
7
+ _INT_ON_PD0_BIT = const (0x01 )
8
+ _INT_OFF_PD0_BIT = const (0x00 )
9
+
10
+ _VREF_ON_PD1_BIT = const (0x02 )
11
+ _VREF_OFF_PD1_BIT = const (0x00 )
12
+
13
+
6
14
_CMD_X_READ = const (0xD0 )
7
15
_CMD_Y_READ = const (0x90 )
8
16
_MIN_RAW_COORD = const (10 )
9
17
_MAX_RAW_COORD = const (4090 )
10
18
19
+ _Z_VALUE_1 = const (0xB0 )
20
+ _Z_VALUE_2 = const (0xC0 )
21
+ _Y_POSITION = const (0x90 )
22
+ _X_POSITION = const (0xD0 )
23
+ _BATTERY = const (0xA6 )
24
+ _AUX_IN = const (0xE6 )
25
+ _TEMP0 = const (0x86 )
26
+ _TEMP1 = const (0xF6 )
27
+
11
28
12
29
class XPT2046 (pointer_framework .PointerDriver ):
13
30
@@ -19,15 +36,48 @@ def __init__(
19
36
cs ,
20
37
host ,
21
38
freq = 5000000 ,
39
+ interrupt = - 1 ,
40
+ vref_on = False ,
41
+ press_threshold = 10 ,
22
42
touch_cal = None
23
43
):
24
44
super ().__init__ (touch_cal = touch_cal )
25
- self ._trans_buf = bytearray (3 )
45
+ self ._trans_buf = bytearray (1 )
26
46
self ._trans_mv = memoryview (self ._trans_buf )
27
47
28
- self ._recv_buf = bytearray (3 )
48
+ self ._recv_buf = bytearray (2 )
29
49
self ._recv_mv = memoryview (self ._recv_buf )
30
50
51
+ self ._on_schedule_ref = self ._on_schedule
52
+ self ._press_threshold = press_threshold
53
+ if interrupt != - 1 :
54
+ PD0_BIT = _INT_ON_PD0_BIT
55
+ interrupt = machine .Pin (interrupt , machine .Pin .IN )
56
+ interrupt .irq (trigger = machine .Pin .IRQ_FALLING , handler = self ._on_interrupt )
57
+
58
+ self ._set_mode_event ()
59
+ else :
60
+ interrupt = None
61
+ PD0_BIT = _INT_OFF_PD0_BIT
62
+
63
+ self ._interrupt = interrupt
64
+
65
+ if vref_on :
66
+ PD1_BIT = _VREF_ON_PD1_BIT
67
+ else :
68
+ PD1_BIT = _VREF_OFF_PD1_BIT
69
+
70
+ PD_BITS = PD1_BIT | PD0_BIT
71
+
72
+ self ._Z_VALUE_1 = _Z_VALUE_1 | PD_BITS
73
+ self ._Z_VALUE_2 = _Z_VALUE_2 | PD_BITS
74
+ self ._Y_POSITION = _Y_POSITION | PD_BITS
75
+ self ._X_POSITION = _X_POSITION | PD_BITS
76
+ # self._BATTERY = const(0xA6)
77
+ # self._AUX_IN = const(0xE6)
78
+ # self._TEMP0 = const(0x86)
79
+ # self._TEMP1 = const(0xF6)
80
+
31
81
self ._spi = machine .SPI (
32
82
host + 1 ,
33
83
baudrate = freq ,
@@ -39,45 +89,58 @@ def __init__(
39
89
self .cs = machine .Pin (cs , machine .Pin .OUT )
40
90
self .cs .value (1 )
41
91
92
+ def _read_reg (self , reg ):
93
+ self .cs .value (0 )
94
+ self ._trans_buf [0 ] = reg
95
+ self ._recv_buf [0 ] = 0x00
96
+ self ._recv_buf [1 ] = 0x00
97
+ self ._spi .write_readinto (self ._trans_mv , self ._recv_mv )
98
+ self .cs .value (1 )
99
+
100
+ return (self ._recv_buf [0 ] << 8 ) | self ._recv_buf [1 ]
101
+
102
+ def _on_schedule (self , * _ ):
103
+ self .read ()
104
+
105
+ def _on_interrupt (self , _ ):
106
+ micropython .schedule (self ._on_schedule_ref , None )
107
+
42
108
def _get_coords (self ):
43
- x_vals = []
44
- y_vals = []
109
+ z1 = self ._read_reg (self ._Z_VALUE_1 )
110
+ z2 = self ._read_reg (self ._Z_VALUE_2 )
111
+
112
+ z = (z1 >> 3 ) + (_MAX_RAW_COORD - (z2 >> 3 ))
45
113
46
- for _ in range (0 , 3 ):
47
- self .cs .value (0 )
48
- self ._trans_buf [0 ] = _CMD_X_READ
49
- self ._recv_buf [0 ] = 0x00
50
- self ._recv_buf [1 ] = 0x00
51
- self ._recv_buf [2 ] = 0x00
114
+ if z < self ._press_threshold :
115
+ return None
52
116
53
- self . _spi . write_readinto ( self . _trans_mv , self . _recv_mv )
54
- x = ( self ._recv_buf [ 1 ] * 256 + self ._recv_buf [ 2 ]) >> 3
117
+ # dump first reading
118
+ self ._read_reg ( self ._X_POSITION )
55
119
56
- self ._trans_buf [0 ] = _CMD_Y_READ
57
- self ._recv_buf [0 ] = 0x00
58
- self ._recv_buf [1 ] = 0x00
59
- self ._recv_buf [2 ] = 0x00
120
+ x_points = []
121
+ y_points = []
60
122
61
- self ._spi .write_readinto (self ._trans_mv , self ._recv_mv )
62
- y = (self ._recv_buf [1 ] * 256 + self ._recv_buf [2 ]) >> 3
123
+ count = 0
124
+ while count != 3 :
125
+ x = self ._read_reg (self ._X_POSITION ) >> 3
126
+ y = self ._read_reg (self ._Y_POSITION ) >> 3
63
127
64
128
if (
65
129
(_MIN_RAW_COORD < x < _MAX_RAW_COORD ) or
66
130
(_MIN_RAW_COORD < y < _MAX_RAW_COORD )
67
131
):
68
- return None
132
+ continue
69
133
70
- x_vals .append (x )
71
- y_vals .append (y )
72
- self . cs . value ( 1 )
134
+ x_points .append (x )
135
+ y_points .append (y )
136
+ count += 1
73
137
74
- if 0 in x_vals or 0 in y_vals :
75
- return None
138
+ x = int ( sum ( x_points ) / len ( x_points ))
139
+ y = int ( sum ( y_points ) / len ( y_points ))
76
140
77
- x_vals . remove ( max ( x_vals ) )
78
- x_vals . remove ( min ( x_vals ) )
141
+ x = int (( x / _MAX_RAW_COORD ) * self . _orig_width )
142
+ y = int (( y / _MAX_RAW_COORD ) * self . _orig_height )
79
143
80
- y_vals .remove (max (y_vals ))
81
- y_vals .remove (min (y_vals ))
144
+ print (x , y )
82
145
83
- return self .PRESSED , x_vals [ 0 ], y_vals [ 0 ]
146
+ return self .PRESSED , x , y
0 commit comments