1
- from machine import Pin
1
+ from micropython import const
2
+
2
3
import espidf as esp
3
4
import lvgl as lv
4
5
5
6
# TODO: Viper/native emmitters don't behave well when module is frozen.
6
7
8
+ _STATE_PRESSED = lv .INDEV_STATE .PRESSED
9
+ _STATE_RELEASED = lv .INDEV_STATE .RELEASED
10
+
7
11
class xpt2046 :
8
12
9
13
# Command is 8 bit, but we add another bit as a space before xpt2046 stats sending the response, See Figure 12 on the datasheet
10
-
11
14
CMD_X_READ = const (0b100100000 )
12
15
CMD_Y_READ = const (0b110100000 )
13
16
CMD_Z1_READ = const (0b101100000 )
@@ -21,13 +24,14 @@ def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25,
21
24
transpose = True , samples = 3 ):
22
25
23
26
# Initializations
24
-
25
27
if not lv .is_initialized ():
26
28
lv .init ()
29
+ disp = lv .display_t ()
30
+ else :
31
+ disp = lv .display_get_default ()
27
32
28
- disp = lv .display_t ()
29
- self .screen_width = disp .get_hor_res ()
30
- self .screen_height = disp .get_ver_res ()
33
+ self .screen_width = disp .get_horizontal_resolution ()
34
+ self .screen_height = disp .get_vertical_resolution ()
31
35
self .miso = miso
32
36
self .mosi = mosi
33
37
self .clk = clk
@@ -43,69 +47,74 @@ def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25,
43
47
self .transpose = transpose
44
48
self .samples = samples
45
49
50
+ self .trans_result_ptr = esp .C_Pointer ()
51
+ self .start_time_ptr = esp .C_Pointer ()
52
+ self .end_time_ptr = esp .C_Pointer ()
53
+ self .cycles_in_ms = esp .esp_clk_cpu_freq () // 1000
54
+
46
55
self .touch_count = 0
47
56
self .touch_cycles = 0
48
57
49
58
self .spi_init ()
50
59
51
60
self .indev_drv = lv .indev_create ()
52
61
self .indev_drv .set_type (lv .INDEV_TYPE .POINTER )
62
+ self .indev_drv .set_display (disp )
53
63
self .indev_drv .set_read_cb (self .read )
54
-
64
+
55
65
def calibrate (self , x0 , y0 , x1 , y1 ):
56
66
self .cal_x0 = x0
57
67
self .cal_y0 = y0
58
68
self .cal_x1 = x1
59
69
self .cal_y1 = y1
60
70
61
71
def spi_init (self ):
62
- buscfg = esp .spi_bus_config_t ({
72
+ buscfg = esp .spi_bus_config_t ({
63
73
"miso_io_num" : self .miso ,
64
- "mosi_io_num" : self .mosi ,
65
- "sclk_io_num" : self .clk ,
66
- "quadwp_io_num" : - 1 ,
67
- "quadhd_io_num" : - 1 ,
68
- "max_transfer_sz" : 4 ,
69
- })
74
+ "mosi_io_num" : self .mosi ,
75
+ "sclk_io_num" : self .clk ,
76
+ "quadwp_io_num" : - 1 ,
77
+ "quadhd_io_num" : - 1 ,
78
+ "max_transfer_sz" : 4 ,
79
+ })
70
80
71
81
devcfg_flags = 0 # esp.SPI_DEVICE.NO_DUMMY
72
82
if self .half_duplex :
73
83
devcfg_flags |= esp .SPI_DEVICE .HALFDUPLEX
74
84
75
- devcfg = esp .spi_device_interface_config_t ({
76
- "command_bits" : 9 , # Actually 8, but need another cycle before xpt starts transmitting response, see Figure 12 on the datasheet.
77
- "clock_speed_hz" : self .mhz * 1000 * 1000 ,
78
- "mode" : 0 , # SPI mode 0
79
- "spics_io_num" : self .cs , # CS pin
80
- "queue_size" : self .max_cmds ,
81
- "flags" : devcfg_flags ,
82
- "duty_cycle_pos" : 128 ,
83
- })
85
+ devcfg = esp .spi_device_interface_config_t ({
86
+ "command_bits" : 9 , # Actually 8, but need another cycle before xpt starts transmitting response, see Figure 12 on the datasheet.
87
+ "clock_speed_hz" : self .mhz * 1000 * 1000 ,
88
+ "mode" : 0 , # SPI mode 0
89
+ "spics_io_num" : self .cs , # CS pin
90
+ "queue_size" : self .max_cmds ,
91
+ "flags" : devcfg_flags ,
92
+ "duty_cycle_pos" : 128 ,
93
+ })
84
94
85
95
esp .gpio_pad_select_gpio (self .cs )
86
96
87
- # Initialize the SPI bus, if needed
97
+ # Initialize the SPI bus, if needed
88
98
89
99
if buscfg .miso_io_num >= 0 and \
90
100
buscfg .mosi_io_num >= 0 and \
91
101
buscfg .sclk_io_num >= 0 :
92
102
93
- esp .gpio_pad_select_gpio (self .miso )
94
- esp .gpio_pad_select_gpio (self .mosi )
95
- esp .gpio_pad_select_gpio (self .clk )
96
-
97
- esp .gpio_set_direction (self .miso , esp .GPIO_MODE .INPUT )
98
- esp .gpio_set_pull_mode (self .miso , esp .GPIO .PULLUP_ONLY )
99
- esp .gpio_set_direction (self .mosi , esp .GPIO_MODE .OUTPUT )
100
- esp .gpio_set_direction (self .clk , esp .GPIO_MODE .OUTPUT )
103
+ esp .gpio_pad_select_gpio (self .miso )
104
+ esp .gpio_pad_select_gpio (self .mosi )
105
+ esp .gpio_pad_select_gpio (self .clk )
101
106
102
- ret = esp .spi_bus_initialize (self .spihost , buscfg , 1 )
103
- if ret != 0 : raise RuntimeError ("Failed initializing SPI bus" )
107
+ esp .gpio_set_direction (self .miso , esp .GPIO_MODE .INPUT )
108
+ esp .gpio_set_pull_mode (self .miso , esp .GPIO .PULLUP_ONLY )
109
+ esp .gpio_set_direction (self .mosi , esp .GPIO_MODE .OUTPUT )
110
+ esp .gpio_set_direction (self .clk , esp .GPIO_MODE .OUTPUT )
104
111
105
- # Attach the xpt2046 to the SPI bus
112
+ ret = esp .spi_bus_initialize (self .spihost , buscfg , 1 )
113
+ if ret != 0 : raise RuntimeError ("Failed initializing SPI bus" )
106
114
115
+ # Attach the xpt2046 to the SPI bus
107
116
ptr_to_spi = esp .C_Pointer ()
108
- ret = esp .spi_bus_add_device (self .spihost , devcfg , ptr_to_spi )
117
+ ret = esp .spi_bus_add_device (self .spihost , devcfg , ptr_to_spi )
109
118
if ret != 0 : raise RuntimeError ("Failed adding SPI device" )
110
119
self .spi = ptr_to_spi .ptr_val
111
120
@@ -117,7 +126,6 @@ def spi_init(self):
117
126
'rxlength' : 16
118
127
}) for i in range (0 , self .max_cmds )]
119
128
120
- trans_result_ptr = esp .C_Pointer ()
121
129
122
130
#
123
131
# Deinitalize SPI device and bus
@@ -193,9 +201,6 @@ def get_pressure(self, factor : int) -> int:
193
201
if int (z1 ) == 0 : return - 1
194
202
return ( (int (x )* factor ) / 4096 )* ( int (z2 )/ int (z1 ) - 1 )
195
203
196
- start_time_ptr = esp .C_Pointer ()
197
- end_time_ptr = esp .C_Pointer ()
198
- cycles_in_ms = esp .esp_clk_cpu_freq () // 1000
199
204
200
205
# @micropython.native
201
206
def read (self , indev_drv , data ) -> int :
@@ -210,9 +215,9 @@ def read(self, indev_drv, data) -> int:
210
215
211
216
if coords :
212
217
data .point .x ,data .point .y = coords
213
- data .state = lv . INDEV_STATE . PRESSED
218
+ data .state = _STATE_PRESSED
214
219
return False
215
- data .state = lv . INDEV_STATE . RELEASED
220
+ data .state = _STATE_RELEASED
216
221
return False
217
222
218
223
def stat (self ):
0 commit comments