@@ -28,49 +28,38 @@ static uint8_t i2c_bus_count = 0;
28
28
static mp_lcd_i2c_bus_obj_t * * i2c_bus_objs ;
29
29
30
30
31
+ typedef struct _i2c_obj_t {
32
+ mp_obj_base_t base ;
33
+ i2c_port_t port : 8 ;
34
+ gpio_num_t scl : 8 ;
35
+ gpio_num_t sda : 8 ;
36
+ } _i2c_obj_t ;
37
+
38
+
31
39
void mp_lcd_i2c_bus_deinit_all (void )
32
40
{
33
41
// we need to copy the existing array to a new one so the order doesn't
34
42
// get all mucked up when objects get removed.
35
- mp_lcd_i2c_bus_obj_t * objs [i2c_bus_count ];
36
-
37
- for (uint8_t i = 0 ;i < i2c_bus_count ;i ++ ) {
38
- objs [i ] = i2c_bus_objs [i ];
39
- }
40
-
41
- for (uint8_t i = 0 ;i < i2c_bus_count ;i ++ ) {
42
- i2c_del (MP_OBJ_FROM_PTR (objs [i ]));
43
- }
44
43
}
45
44
46
45
47
46
static mp_obj_t mp_lcd_i2c_bus_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args )
48
47
{
49
48
enum {
50
- ARG_sda ,
51
- ARG_scl ,
49
+ ARG_i2c_bus ,
52
50
ARG_addr ,
53
- ARG_host ,
54
51
ARG_control_phase_bytes ,
55
52
ARG_dc_bit_offset ,
56
- ARG_freq ,
57
53
ARG_dc_low_on_data ,
58
- ARG_sda_pullup ,
59
- ARG_scl_pullup ,
60
54
ARG_disable_control_phase
61
55
};
62
56
63
57
const mp_arg_t make_new_args [] = {
64
- { MP_QSTR_sda , MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
65
- { MP_QSTR_scl , MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
58
+ { MP_QSTR_i2c_bus , MP_ARG_OBJ | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
66
59
{ MP_QSTR_addr , MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED },
67
- { MP_QSTR_host , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 0 } },
68
60
{ MP_QSTR_control_phase_bytes , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 1 } },
69
61
{ MP_QSTR_dc_bit_offset , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 6 } },
70
- { MP_QSTR_freq , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 100000 } },
71
- { MP_QSTR_dc_low_on_data , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = false } },
72
- { MP_QSTR_sda_pullup , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = true } },
73
- { MP_QSTR_scl_pullup , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = true } },
62
+ { MP_QSTR_dc_low_on_data , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = true } },
74
63
{ MP_QSTR_disable_control_phase , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = false } }
75
64
};
76
65
@@ -90,17 +79,9 @@ static mp_obj_t mp_lcd_i2c_bus_make_new(const mp_obj_type_t *type, size_t n_args
90
79
91
80
self -> callback = mp_const_none ;
92
81
93
- self -> host = args [ARG_host ].u_int ;
94
- self -> bus_handle = (esp_lcd_i2c_bus_handle_t )((uint32_t )self -> host );
95
-
96
- self -> bus_config .mode = I2C_MODE_MASTER ;
97
- self -> bus_config .sda_io_num = (int )args [ARG_sda ].u_int ;
98
- self -> bus_config .scl_io_num = (int )args [ARG_scl ].u_int ;
99
- self -> bus_config .sda_pullup_en = (bool )args [ARG_sda_pullup ].u_bool ;
100
- self -> bus_config .scl_pullup_en = (bool )args [ARG_scl_pullup ].u_bool ;
101
- self -> bus_config .master .clk_speed = (uint32_t )args [ARG_freq ].u_int ;
102
- self -> bus_config .clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL ;
82
+ _i2c_obj_t * bus = MP_OBJ_TO_PTR (args [ARG_i2c_bus ].u_obj );
103
83
84
+ self -> port = bus -> port ;
104
85
self -> panel_io_config .dev_addr = (uint32_t )args [ARG_addr ].u_int ;
105
86
self -> panel_io_config .on_color_trans_done = bus_trans_done_cb ;
106
87
self -> panel_io_config .user_ctx = self ;
@@ -129,12 +110,6 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
129
110
return ret ;
130
111
}
131
112
132
- ret = i2c_driver_delete (self -> host );
133
- if (ret != 0 ) {
134
- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_driver_delete)" ), ret );
135
- return ret ;
136
- }
137
-
138
113
self -> panel_io_handle .panel_io = NULL ;
139
114
140
115
if (self -> view1 != NULL ) {
@@ -152,22 +127,7 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
152
127
self -> view2 = NULL ;
153
128
LCD_DEBUG_PRINT ("i2c_free_framebuffer(self, buf=1)\n" )
154
129
}
155
-
156
- uint8_t i = 0 ;
157
- for (;i < i2c_bus_count ;i ++ ) {
158
- if (i2c_bus_objs [i ] == self ) {
159
- i2c_bus_objs [i ] = NULL ;
160
- break ;
161
- }
162
- }
163
-
164
- for (uint8_t j = i + 1 ;j < i2c_bus_count ;j ++ ) {
165
- i2c_bus_objs [j - i + 1 ] = i2c_bus_objs [j ];
166
- }
167
-
168
- i2c_bus_count -- ;
169
- i2c_bus_objs = m_realloc (i2c_bus_objs , i2c_bus_count * sizeof (mp_lcd_i2c_bus_obj_t * ));
170
-
130
+
171
131
return ret ;
172
132
} else {
173
133
return LCD_FAIL ;
@@ -188,30 +148,13 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
188
148
self -> panel_io_config .lcd_cmd_bits = (int )cmd_bits ;
189
149
self -> panel_io_config .lcd_param_bits = (int )param_bits ;
190
150
191
- mp_lcd_err_t ret = i2c_param_config (self -> host , & self -> bus_config );
192
- if (ret != 0 ) {
193
- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_param_config)" ), ret );
194
- return ret ;
195
- }
196
-
197
- ret = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
198
- if (ret != 0 ) {
199
- mp_raise_msg_varg (& mp_type_OSError , MP_ERROR_TEXT ("%d(i2c_driver_install)" ), ret );
200
- return ret ;
201
- }
202
-
203
- ret = esp_lcd_new_panel_io_i2c (self -> bus_handle , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
151
+ ret = esp_lcd_new_panel_io_i2c (self -> port , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
204
152
205
153
if (ret != 0 ) {
206
154
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" ), ret );
207
155
return ret ;
208
156
}
209
-
210
- // add the new bus ONLY after successfull initilization of the bus
211
- i2c_bus_count ++ ;
212
- i2c_bus_objs = m_realloc (i2c_bus_objs , i2c_bus_count * sizeof (mp_lcd_i2c_bus_obj_t * ));
213
- i2c_bus_objs [i2c_bus_count - 1 ] = self ;
214
-
157
+
215
158
return ret ;
216
159
}
217
160
0 commit comments