@@ -28,49 +28,38 @@ static uint8_t i2c_bus_count = 0;
2828static mp_lcd_i2c_bus_obj_t * * i2c_bus_objs ;
2929
3030
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+
3139void mp_lcd_i2c_bus_deinit_all (void )
3240{
3341 // we need to copy the existing array to a new one so the order doesn't
3442 // 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- }
4443}
4544
4645
4746static 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 )
4847{
4948 enum {
50- ARG_sda ,
51- ARG_scl ,
49+ ARG_i2c_bus ,
5250 ARG_addr ,
53- ARG_host ,
5451 ARG_control_phase_bytes ,
5552 ARG_dc_bit_offset ,
56- ARG_freq ,
5753 ARG_dc_low_on_data ,
58- ARG_sda_pullup ,
59- ARG_scl_pullup ,
6054 ARG_disable_control_phase
6155 };
6256
6357 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 },
6659 { 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 } },
6860 { MP_QSTR_control_phase_bytes , MP_ARG_INT | MP_ARG_KW_ONLY , {.u_int = 1 } },
6961 { 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 } },
7463 { MP_QSTR_disable_control_phase , MP_ARG_BOOL | MP_ARG_KW_ONLY , {.u_bool = false } }
7564 };
7665
@@ -90,17 +79,9 @@ static mp_obj_t mp_lcd_i2c_bus_make_new(const mp_obj_type_t *type, size_t n_args
9079
9180 self -> callback = mp_const_none ;
9281
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 );
10383
84+ self -> port = bus -> port ;
10485 self -> panel_io_config .dev_addr = (uint32_t )args [ARG_addr ].u_int ;
10586 self -> panel_io_config .on_color_trans_done = bus_trans_done_cb ;
10687 self -> panel_io_config .user_ctx = self ;
@@ -129,12 +110,6 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
129110 return ret ;
130111 }
131112
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-
138113 self -> panel_io_handle .panel_io = NULL ;
139114
140115 if (self -> view1 != NULL ) {
@@ -152,22 +127,7 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
152127 self -> view2 = NULL ;
153128 LCD_DEBUG_PRINT ("i2c_free_framebuffer(self, buf=1)\n" )
154129 }
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+
171131 return ret ;
172132 } else {
173133 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
188148 self -> panel_io_config .lcd_cmd_bits = (int )cmd_bits ;
189149 self -> panel_io_config .lcd_param_bits = (int )param_bits ;
190150
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+ mp_lcd_err_t ret = esp_lcd_new_panel_io_i2c (self -> port , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
204152
205153 if (ret != 0 ) {
206154 mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" ), ret );
207155 return ret ;
208156 }
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+
215158 return ret ;
216159}
217160
0 commit comments