Skip to content

Commit d29f0fa

Browse files
committed
adds spi polarity, phase, mode converter functions to lcd_utils
1 parent 03f3746 commit d29f0fa

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

ext_mod/lcd_utils/src/lcd_utils.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,38 @@
55
#include "py/runtime.h"
66

77

8+
static mp_obj_t spi_mode_to_polarity_phase(mp_obj_t mode)
9+
{
10+
uint8_t spi_mode = (uint8_t)mp_obj_get_int_truncated(mode);
11+
uint8_t polarity = (spi_mode >> 1) & 0x1;
12+
uint8_t phase = spi_mode & 0x01;
13+
mp_obj_t tuple[2] = {
14+
mp_obj_new_int_from_uint(polarity),
15+
mp_obj_new_int_from_uint(phase),
16+
};
17+
return mp_obj_new_tuple(2, tuple);
18+
}
19+
20+
static MP_DEFINE_CONST_FUN_OBJ_1(spi_mode_to_polarity_phase_obj, spi_mode_to_polarity_phase);
21+
22+
23+
static mp_obj_t spi_polarity_phase_to_mode(mp_obj_t polarity, mp_obj_t phase)
24+
{
25+
uint8_t spi_polarity = (uint8_t)mp_obj_get_int_truncated(polarity);
26+
uint8_t spi_phase = (uint8_t)mp_obj_get_int_truncated(phase);
27+
return mp_obj_new_int_from_uint(spi_mode);
28+
}
29+
30+
static MP_DEFINE_CONST_FUN_OBJ_2(spi_polarity_phase_to_mode_obj, spi_polarity_phase_to_mode);
31+
32+
833
static const mp_rom_map_elem_t mp_lcd_utils_module_globals_table[] = {
934
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_lcd_utils) },
1035
{ MP_ROM_QSTR(MP_QSTR_remap), MP_ROM_PTR(&mp_lcd_utils_remap_obj) },
1136
{ MP_ROM_QSTR(MP_QSTR_int_float_converter), MP_ROM_PTR(&mp_lcd_utils_int_float_converter_obj) },
37+
{ MP_ROM_QSTR(MP_QSTR_spi_mode_to_polarity_phase), MP_ROM_PTR(&spi_mode_to_polarity_phase_obj) },
38+
{ MP_ROM_QSTR(MP_QSTR_spi_polarity_phase_to_mode), MP_ROM_PTR(&spi_polarity_phase_to_mode_obj) },
39+
1240
};
1341

1442
static MP_DEFINE_CONST_DICT(mp_lcd_utils_module_globals, mp_lcd_utils_module_globals_table);

lcd_utils.pyi

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,26 @@ def remap(
2525

2626
def int_float_converter(value: Union[float, int], /) -> Union[float, int]:
2727
...
28+
29+
30+
def spi_mode_to_polarity_phase(mode: int, /) -> tuple[int]:
31+
"""
32+
33+
:param mode: spi mode, can be 0, 1, 2 or 3
34+
:type mode: `int`
35+
36+
:returns: the 2 integer values as `(polarity, phase)`
37+
:rtype: `tuple`
38+
"""
39+
40+
def spi_polarity_phase_to_mode(polarity: int, phase: int, /) -> int:
41+
"""
42+
:param polarity: 1 or 0
43+
:type polarity: `int`
44+
45+
:param phase: 1 or 0
46+
:type phase: `int`
47+
48+
:returns: 0, 1, 2 or 3
49+
:rtype: `int`
50+
"""

micropy_updates/esp32/machine_i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@
417417
if (ret < 0) {
418418
mp_raise_OSError(-ret);
419419
}
420-
420+
421421
return mp_const_none;
422422
}
423423

0 commit comments

Comments
 (0)