Skip to content

Commit 8e0f4d1

Browse files
authored
fix(drivers): fix xpt2046.py esp32 specific touch driver (lvgl#333)
1 parent d4889b9 commit 8e0f4d1

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

driver/esp32/xpt2046.py

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
from machine import Pin
1+
from micropython import const
2+
23
import espidf as esp
34
import lvgl as lv
45

56
# TODO: Viper/native emmitters don't behave well when module is frozen.
67

8+
_STATE_PRESSED = lv.INDEV_STATE.PRESSED
9+
_STATE_RELEASED = lv.INDEV_STATE.RELEASED
10+
711
class xpt2046:
812

913
# 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-
1114
CMD_X_READ = const(0b100100000)
1215
CMD_Y_READ = const(0b110100000)
1316
CMD_Z1_READ = const(0b101100000)
@@ -21,13 +24,14 @@ def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25,
2124
transpose = True, samples = 3):
2225

2326
# Initializations
24-
2527
if not lv.is_initialized():
2628
lv.init()
29+
disp = lv.display_t()
30+
else:
31+
disp = lv.display_get_default()
2732

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()
3135
self.miso = miso
3236
self.mosi = mosi
3337
self.clk = clk
@@ -43,69 +47,74 @@ def __init__(self, miso=-1, mosi=-1, clk=-1, cs=25,
4347
self.transpose = transpose
4448
self.samples = samples
4549

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+
4655
self.touch_count = 0
4756
self.touch_cycles = 0
4857

4958
self.spi_init()
5059

5160
self.indev_drv = lv.indev_create()
5261
self.indev_drv.set_type(lv.INDEV_TYPE.POINTER)
62+
self.indev_drv.set_display(disp)
5363
self.indev_drv.set_read_cb(self.read)
54-
64+
5565
def calibrate(self, x0, y0, x1, y1):
5666
self.cal_x0 = x0
5767
self.cal_y0 = y0
5868
self.cal_x1 = x1
5969
self.cal_y1 = y1
6070

6171
def spi_init(self):
62-
buscfg = esp.spi_bus_config_t({
72+
buscfg = esp.spi_bus_config_t({
6373
"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+
})
7080

7181
devcfg_flags = 0 # esp.SPI_DEVICE.NO_DUMMY
7282
if self.half_duplex:
7383
devcfg_flags |= esp.SPI_DEVICE.HALFDUPLEX
7484

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+
})
8494

8595
esp.gpio_pad_select_gpio(self.cs)
8696

87-
# Initialize the SPI bus, if needed
97+
# Initialize the SPI bus, if needed
8898

8999
if buscfg.miso_io_num >= 0 and \
90100
buscfg.mosi_io_num >= 0 and \
91101
buscfg.sclk_io_num >= 0:
92102

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)
101106

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)
104111

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")
106114

115+
# Attach the xpt2046 to the SPI bus
107116
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)
109118
if ret != 0: raise RuntimeError("Failed adding SPI device")
110119
self.spi = ptr_to_spi.ptr_val
111120

@@ -117,7 +126,6 @@ def spi_init(self):
117126
'rxlength': 16
118127
}) for i in range(0, self.max_cmds)]
119128

120-
trans_result_ptr = esp.C_Pointer()
121129

122130
#
123131
# Deinitalize SPI device and bus
@@ -193,9 +201,6 @@ def get_pressure(self, factor : int) -> int:
193201
if int(z1) == 0: return -1
194202
return ( (int(x)*factor) / 4096)*( int(z2)/int(z1) - 1)
195203

196-
start_time_ptr = esp.C_Pointer()
197-
end_time_ptr = esp.C_Pointer()
198-
cycles_in_ms = esp.esp_clk_cpu_freq() // 1000
199204

200205
# @micropython.native
201206
def read(self, indev_drv, data) -> int:
@@ -210,9 +215,9 @@ def read(self, indev_drv, data) -> int:
210215

211216
if coords:
212217
data.point.x ,data.point.y = coords
213-
data.state = lv.INDEV_STATE.PRESSED
218+
data.state = _STATE_PRESSED
214219
return False
215-
data.state = lv.INDEV_STATE.RELEASED
220+
data.state = _STATE_RELEASED
216221
return False
217222

218223
def stat(self):

0 commit comments

Comments
 (0)