@@ -23,6 +23,26 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
2323mp_lcd_err_t i2c_get_lane_count (mp_obj_t obj , uint8_t * lane_count );
2424
2525
26+ static uint8_t i2c_bus_count = 0 ;
27+ static mp_lcd_i2c_bus_obj_t * * i2c_bus_objs ;
28+
29+
30+ void mp_lcd_i2c_bus_deinit_all (void )
31+ {
32+ // we need to copy the existing array to a new one so the order doesn't
33+ // get all mucked up when objects get removed.
34+ mp_lcd_i2c_bus_obj_t * objs [i2c_bus_count ];
35+
36+ for (uint8_t i = 0 ;i < i2c_bus_count ;i ++ ) {
37+ objs [i ] = i2c_bus_objs [i ];
38+ }
39+
40+ for (uint8_t i = 0 ;i < i2c_bus_count ;i ++ ) {
41+ spi_del (MP_OBJ_FROM_PTR (objs [i ]));
42+ }
43+ }
44+
45+
2646static 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 )
2747{
2848 enum {
@@ -100,16 +120,57 @@ mp_lcd_err_t i2c_del(mp_obj_t obj)
100120{
101121 mp_lcd_i2c_bus_obj_t * self = (mp_lcd_i2c_bus_obj_t * )obj ;
102122
103- mp_lcd_err_t ret = esp_lcd_panel_io_del (self -> panel_io_handle .panel_io );
104- if (ret != 0 ) {
105- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_panel_io_del)" ), ret );
106- }
107- ret = i2c_driver_delete (self -> host );
108- if (ret != 0 ) {
109- mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_driver_delete)" ), ret );
123+ if (self -> panel_io_handle .panel_io != NULL ) {
124+
125+ mp_lcd_err_t ret = esp_lcd_panel_io_del (self -> panel_io_handle .panel_io );
126+ if (ret != 0 ) {
127+ mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_panel_io_del)" ), ret );
128+ return ret ;
129+ }
130+
131+ ret = i2c_driver_delete (self -> host );
132+ if (ret != 0 ) {
133+ mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_driver_delete)" ), ret );
134+ return ret ;
135+ }
136+
137+ self -> panel_io_handle .panel_io = NULL ;
138+
139+ if (self -> view1 != NULL ) {
140+ heap_caps_free (self -> view1 -> items );
141+ self -> view1 -> items = NULL ;
142+ self -> view1 -> len = 0
143+ self -> view1 = NULL ;
144+ LCD_DEBUG_PRINT ("i2c_free_framebuffer(self, buf=1)\n" )
145+ }
146+
147+ if (self -> view2 != NULL ) {
148+ heap_caps_free (self -> view2 -> items );
149+ self -> view2 -> items = NULL ;
150+ self -> view2 -> len = 0
151+ self -> view2 = NULL ;
152+ LCD_DEBUG_PRINT ("i2c_free_framebuffer(self, buf=1)\n" )
153+ }
154+
155+ uint8_t i = 0 ;
156+ for (;i < i2c_bus_count ;i ++ ) {
157+ if (i2c_bus_objs [i ] == self ) {
158+ i2c_bus_objs [i ] = NULL ;
159+ break ;
160+ }
161+ }
162+
163+ for (uint8_t j = i + 1 ;j < i2c_bus_count ;j ++ ) {
164+ i2c_bus_objs [j - i + 1 ] = i2c_bus_objs [j ];
165+ }
166+
167+ i2c_bus_count -- ;
168+ i2c_bus_objs = m_realloc (i2c_bus_objs , i2c_bus_count * sizeof (mp_lcd_i2c_bus_obj_t * ));
169+
170+ return ret ;
171+ } else {
172+ return LCD_FAIL ;
110173 }
111-
112- return ret ;
113174}
114175
115176
@@ -129,19 +190,27 @@ mp_lcd_err_t i2c_init(mp_obj_t obj, uint16_t width, uint16_t height, uint8_t bpp
129190 mp_lcd_err_t ret = i2c_param_config (self -> host , & self -> bus_config );
130191 if (ret != 0 ) {
131192 mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(i2c_param_config)" ), ret );
193+ return ret ;
132194 }
133195
134196 ret = i2c_driver_install (self -> host , I2C_MODE_MASTER , 0 , 0 , 0 );
135197 if (ret != 0 ) {
136198 mp_raise_msg_varg (& mp_type_OSError , MP_ERROR_TEXT ("%d(i2c_driver_install)" ), ret );
199+ return ret ;
137200 }
138201
139202 ret = esp_lcd_new_panel_io_i2c (self -> bus_handle , & self -> panel_io_config , & self -> panel_io_handle .panel_io );
140203
141204 if (ret != 0 ) {
142205 mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("%d(esp_lcd_new_panel_io_i2c)" ), ret );
206+ return ret ;
143207 }
144208
209+ // add the new bus ONLY after successfull initilization of the bus
210+ i2c_bus_count ++ ;
211+ m_realloc (i2c_bus_objs , i2c_bus_count * sizeof (mp_lcd_i2c_bus_obj_t * ));
212+ spi_bus_objs [i2c_bus_count - 1 ] = self ;
213+
145214 return ret ;
146215}
147216
0 commit comments