Skip to content

Commit

Permalink
This might fix the xpt2046 driver
Browse files Browse the repository at this point in the history
kdschlosser committed May 21, 2024
1 parent 567096d commit 2e06069
Showing 6 changed files with 104 additions and 38 deletions.
16 changes: 9 additions & 7 deletions api_drivers/common_api_drivers/display/st7789.py
Original file line number Diff line number Diff line change
@@ -101,10 +101,9 @@ def init(self):
self.set_params(_LCMCTRL, param_mv[:1])

param_buf[0] = 0x01
param_buf[1] = 0xFF
self.set_params(_VDVVRHEN, param_mv[:2])
self.set_params(_VDVVRHEN, param_mv[:1])

param_buf[0] = 0x10
param_buf[0] = 0x13
self.set_params(_VRHS, param_mv[:1])

param_buf[0] = 0x20
@@ -153,14 +152,17 @@ def init(self):

param_buf[0] = 0x00
param_buf[1] = 0x00
param_buf[2] = 0x00
param_buf[3] = 0xEF
param_buf[2] = (self.display_width >> 8) & 0xFF
param_buf[3] = self.display_width & 0xFF

self.set_params(_CASET, param_mv[:4])

# Page addresses
param_buf[0] = 0x00
param_buf[1] = 0x00
param_buf[2] = 0x01
param_buf[3] = 0x3F
param_buf[2] = (self.display_height >> 8) & 0xFF
param_buf[3] = self.display_height & 0xFF

self.set_params(_RASET, param_mv[:4])

self.set_params(_DISPON)
82 changes: 54 additions & 28 deletions api_drivers/common_api_drivers/indev/xpt2046.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
import micropython
import machine
import pointer_framework
import lcd_bus


_INT_ON_PD0_BIT = const(0x01)
@@ -35,10 +36,10 @@ def __init__(
clk,
cs,
host,
freq=5000000,
freq=1000000,
interrupt=-1,
vref_on=False,
press_threshold=10,
press_threshold=400,
touch_cal=None
):
super().__init__(touch_cal=touch_cal)
@@ -78,24 +79,50 @@ def __init__(
# self._TEMP0 = const(0x86)
# self._TEMP1 = const(0xF6)

self._spi = machine.SPI(
host + 1,
baudrate=freq,
sck=machine.Pin(clk, machine.Pin.OUT),
mosi=machine.Pin(mosi, machine.Pin.OUT),
miso=machine.Pin(miso, machine.Pin.IN, pull=machine.Pin.PULL_UP)
)
if (
isinstance(self._py_disp_drv._data_bus, lcd_bus.SPIBus) and
self._py_disp_drv._data_bus.get_host() == host
):
self.__shared_bus = True
self._spi = lcd_bus.SPIBus(
mosi=mosi,
miso=miso,
sclk=clk,
cs=cs,
freq=freq,
dc=-1,
host=host
)

self._spi.init(self._orig_width, self._orig_height, 4, False)

self.cs = machine.Pin(cs, machine.Pin.OUT)
self.cs.value(1)
else:
self.__shared_bus = False

self._spi = machine.SPI(
host + 1,
baudrate=freq,
sck=machine.Pin(clk, machine.Pin.OUT),
mosi=machine.Pin(mosi, machine.Pin.OUT),
miso=machine.Pin(miso, machine.Pin.IN, pull=machine.Pin.PULL_UP)
)

self.cs = machine.Pin(cs, machine.Pin.OUT)
self.cs.value(1)

def _read_reg(self, reg):
self.cs.value(0)
self._trans_buf[0] = reg
self._recv_buf[0] = 0x00
self._recv_buf[1] = 0x00
self._spi.write_readinto(self._trans_mv, self._recv_mv)
self.cs.value(1)

if self.__shared_bus:
self._spi.rx_param(reg, self._recv_mv)
else:
self.cs.value(0)
self._trans_buf[0] = reg
self._recv_buf[0] = 0x00
self._recv_buf[1] = 0x00
self._spi.write_readinto(self._trans_mv, self._recv_mv)
self.cs.value(1)

return (self._recv_buf[0] << 8) | self._recv_buf[1]

@@ -120,25 +147,24 @@ def _get_coords(self):
x_points = []
y_points = []

count = 0
while count != 3:
for _ in range(3):
x = self._read_reg(self._X_POSITION) >> 3
y = self._read_reg(self._Y_POSITION) >> 3

if (
(_MIN_RAW_COORD < x < _MAX_RAW_COORD) or
(_MIN_RAW_COORD < y < _MAX_RAW_COORD)
(_MIN_RAW_COORD <= x <= _MAX_RAW_COORD) or
(_MIN_RAW_COORD <= y <= _MAX_RAW_COORD)
):
continue
x = int((x / float(_MAX_RAW_COORD)) * self._orig_width)
y = int((y / float(_MAX_RAW_COORD)) * self._orig_height)

x_points.append(x)
y_points.append(y)
count += 1
x_points.append(x)
y_points.append(y)

x = int(sum(x_points) / len(x_points))
y = int(sum(y_points) / len(y_points))
if x_points and y_points:
x = int(sum(x_points) / len(x_points))
y = int(sum(y_points) / len(y_points))

x = int((x / _MAX_RAW_COORD) * self._orig_width)
y = int((y / _MAX_RAW_COORD) * self._orig_height)
return self.PRESSED, x, y

return self.PRESSED, x, y
return None
30 changes: 29 additions & 1 deletion ext_mod/lcd_bus/common_src/spi_bus.c
Original file line number Diff line number Diff line change
@@ -27,6 +27,10 @@
mp_raise_msg(&mp_type_NotImplementedError, MP_ERROR_TEXT("SPI display bus is not supported"));
return mp_const_none;
}


STATIC MP_DEFINE_CONST_DICT(mp_lcd_spi_bus_locals_dict, mp_lcd_bus_locals_dict_table);

Check failure on line 32 in ext_mod/lcd_bus/common_src/spi_bus.c

GitHub Actions / build

'sizeof (mp_lcd_bus_locals_dict_table)' will return the size of the pointer, not the array itself [-Werror,-Wsizeof-pointer-div]

Check failure on line 32 in ext_mod/lcd_bus/common_src/spi_bus.c

GitHub Actions / build

'sizeof (mp_lcd_bus_locals_dict_table)' will return the size of the pointer, not the array itself [-Werror,-Wsizeof-pointer-div]

Check failure on line 32 in ext_mod/lcd_bus/common_src/spi_bus.c

GitHub Actions / build

initializer element is not a compile-time constant

#else
#ifdef MICROPY_PY_NRF
#include "modules/machine/spi.h"
@@ -282,6 +286,13 @@
return LCD_OK;
}

mp_obj_t s_spi_bus_get_host(mp_obj_t obj)
{
mp_lcd_spi_bus_obj_t *self = (mp_lcd_spi_bus_obj_t *)obj;
return mp_obj_new_int(self->host);
}

MP_DEFINE_CONST_FUN_OBJ_1(s_spi_bus_get_host_obj, s_spi_bus_get_host);

/* sending functions
* These functions are here so a constant checking of the bit depth doesn't
@@ -336,14 +347,31 @@
/* end sending functions */

/* end function definitions */
STATIC const mp_rom_map_elem_t mp_lcd_spi_bus_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get_host), MP_ROM_PTR(&s_spi_bus_get_host_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_lane_count), MP_ROM_PTR(&mp_lcd_bus_get_lane_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_allocate_framebuffer), MP_ROM_PTR(&mp_lcd_bus_allocate_framebuffer_obj) },
{ MP_ROM_QSTR(MP_QSTR_free_framebuffer), MP_ROM_PTR(&mp_lcd_bus_free_framebuffer_obj) },
{ MP_ROM_QSTR(MP_QSTR_register_callback), MP_ROM_PTR(&mp_lcd_bus_register_callback_obj) },
{ MP_ROM_QSTR(MP_QSTR_tx_param), MP_ROM_PTR(&mp_lcd_bus_tx_param_obj) },
{ MP_ROM_QSTR(MP_QSTR_tx_color), MP_ROM_PTR(&mp_lcd_bus_tx_color_obj) },
{ MP_ROM_QSTR(MP_QSTR_rx_param), MP_ROM_PTR(&mp_lcd_bus_rx_param_obj) },
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mp_lcd_bus_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&mp_lcd_bus_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_lcd_bus_deinit_obj) },
};

STATIC MP_DEFINE_CONST_DICT(mp_lcd_spi_bus_locals_dict, mp_lcd_spi_bus_locals_dict_table);

#endif

/* create micropython class */
/* create micropython class */
MP_DEFINE_CONST_OBJ_TYPE(
mp_lcd_spi_bus_type,
MP_QSTR_SPI_Bus,
MP_TYPE_FLAG_NONE,
make_new, mp_lcd_spi_bus_make_new,
locals_dict, (mp_obj_dict_t *)&mp_lcd_bus_locals_dict
locals_dict, (mp_obj_dict_t *)&mp_lcd_spi_bus_locals_dict
);
/* end create micropython class */
11 changes: 10 additions & 1 deletion ext_mod/lcd_bus/esp32_src/spi_bus.c
Original file line number Diff line number Diff line change
@@ -277,7 +277,17 @@ mp_lcd_err_t spi_get_lane_count(mp_obj_t obj, uint8_t *lane_count)
}


mp_obj_t mp_spi_bus_get_host(mp_obj_t obj)
{
mp_lcd_spi_bus_obj_t *self = (mp_lcd_spi_bus_obj_t *)obj;
return mp_obj_new_int(self->host);
}

MP_DEFINE_CONST_FUN_OBJ_1(mp_spi_bus_get_host_obj, mp_spi_bus_get_host);


STATIC const mp_rom_map_elem_t mp_lcd_spi_bus_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get_host), MP_ROM_PTR(&mp_spi_bus_get_host_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_lane_count), MP_ROM_PTR(&mp_lcd_bus_get_lane_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_allocate_framebuffer), MP_ROM_PTR(&mp_lcd_bus_allocate_framebuffer_obj) },
{ MP_ROM_QSTR(MP_QSTR_free_framebuffer), MP_ROM_PTR(&mp_lcd_bus_free_framebuffer_obj) },
@@ -288,7 +298,6 @@ STATIC const mp_rom_map_elem_t mp_lcd_spi_bus_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&mp_lcd_bus_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&mp_lcd_bus_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&mp_lcd_bus_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_SPI_MAXIMUM_BUFFER_SIZE), MP_ROM_INT(SOC_SPI_MAXIMUM_BUFFER_SIZE) },
};

STATIC MP_DEFINE_CONST_DICT(mp_lcd_spi_bus_locals_dict, mp_lcd_spi_bus_locals_dict_table);
2 changes: 1 addition & 1 deletion ext_mod/lcd_bus/modlcd_bus.c
Original file line number Diff line number Diff line change
@@ -242,7 +242,7 @@ mp_obj_t mp_lcd_bus_register_callback(size_t n_args, const mp_obj_t *pos_args, m
MP_DEFINE_CONST_FUN_OBJ_KW(mp_lcd_bus_register_callback_obj, 2, mp_lcd_bus_register_callback);


STATIC const mp_rom_map_elem_t mp_lcd_bus_locals_dict_table[] = {
const mp_rom_map_elem_t mp_lcd_bus_locals_dict_table[] = {

Check failure on line 245 in ext_mod/lcd_bus/modlcd_bus.c

GitHub Actions / build

conflicting types for 'mp_lcd_bus_locals_dict_table'; have 'const mp_rom_map_elem_t[]' {aka 'const struct _mp_rom_map_elem_t[]'}

Check failure on line 245 in ext_mod/lcd_bus/modlcd_bus.c

GitHub Actions / build

conflicting types for 'mp_lcd_bus_locals_dict_table'; have 'const mp_rom_map_elem_t[]' {aka 'const struct _mp_rom_map_elem_t[]'}

Check failure on line 245 in ext_mod/lcd_bus/modlcd_bus.c

GitHub Actions / build

redefinition of 'mp_lcd_bus_locals_dict_table' with a different type: 'const mp_rom_map_elem_t[]' (aka 'const struct _mp_rom_map_elem_t[]') vs 'const mp_rom_map_elem_t *' (aka 'const struct _mp_rom_map_elem_t *')
{ MP_ROM_QSTR(MP_QSTR_get_lane_count), MP_ROM_PTR(&mp_lcd_bus_get_lane_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_allocate_framebuffer), MP_ROM_PTR(&mp_lcd_bus_allocate_framebuffer_obj) },
{ MP_ROM_QSTR(MP_QSTR_free_framebuffer), MP_ROM_PTR(&mp_lcd_bus_free_framebuffer_obj) },
1 change: 1 addition & 0 deletions ext_mod/lcd_bus/modlcd_bus.h
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
extern const mp_obj_fun_builtin_var_t mp_lcd_bus_allocate_framebuffer_obj;

extern const mp_obj_dict_t mp_lcd_bus_locals_dict;
extern const mp_rom_map_elem_t *mp_lcd_bus_locals_dict_table;

#endif /* _MODLCD_BUS_H_ */

0 comments on commit 2e06069

Please sign in to comment.