2929
3030#include "py/mphal.h"
3131#include "py/runtime.h"
32+ #include <dfs_posix.h>
33+ #include "py/mperrno.h"
3234
3335#if MICROPY_PY_MACHINE_LCD
3436
@@ -213,6 +215,98 @@ STATIC mp_obj_t machine_lcd_show_image(size_t n_args, const mp_obj_t *args) {
213215}
214216STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_lcd_show_image_obj , 6 , 6 , machine_lcd_show_image );
215217
218+ rt_uint16_t rgb888to565 (rt_uint32_t RGB )
219+ {
220+ int R , G , B ;
221+ R = (RGB >> 19 ) & 0x1F ;
222+ G = (RGB >> 10 ) & 0x3F ;
223+ B = (RGB >> 3 ) & 0x1F ;
224+ return (R << 11 ) | (G << 5 ) | B ;
225+ }
226+
227+ /// \method show_image array
228+ ///
229+ /// display the image on the lcd.
230+ /// @param x x position
231+ /// @param y y position
232+ /// @param file bmp file pathname
233+ STATIC mp_obj_t machine_lcd_show_bmp (size_t n_args , const mp_obj_t * args ) {
234+ #define BMP_INFO_SIZE 54
235+ rt_uint16_t x = mp_obj_get_int (args [1 ]);
236+ rt_uint16_t y = mp_obj_get_int (args [2 ]);
237+ const char * pathname = mp_obj_str_get_str (args [3 ]);
238+
239+ int fd , len ;
240+ fd = open (pathname , O_RDONLY , 0 );
241+ if (fd < 0 )
242+ {
243+ mp_raise_OSError (MP_EINVAL );
244+ }
245+
246+ void * bmp_info = rt_malloc (BMP_INFO_SIZE );
247+ if (bmp_info == RT_NULL )
248+ {
249+ mp_raise_OSError (MP_ENOMEM );
250+ }
251+
252+ len = read (fd , bmp_info , BMP_INFO_SIZE );
253+ if (len < 0 )
254+ {
255+ close (fd );
256+ mp_raise_OSError (MP_EINVAL );
257+ }
258+
259+ rt_uint32_t width = * (rt_uint32_t * )(bmp_info + 18 );
260+ rt_uint32_t heigth = * (rt_uint32_t * )(bmp_info + 22 );
261+
262+ void * image_buf = rt_malloc (2 * width );
263+ if (image_buf == RT_NULL )
264+ {
265+ mp_raise_OSError (MP_ENOMEM );
266+ }
267+
268+ void * row_buf = rt_malloc (4 * width );
269+ if (row_buf == RT_NULL )
270+ {
271+ mp_raise_OSError (MP_ENOMEM );
272+ }
273+
274+ int image_index , row_index ;
275+ rt_uint16_t rgb565_temp ;
276+
277+ for (int i = 0 ; i < heigth ; i ++ )
278+ {
279+ image_index = 0 ;
280+ row_index = 0 ;
281+
282+ len = read (fd , row_buf , 4 * width );
283+ if (len < 0 )
284+ {
285+ close (fd );
286+ mp_raise_OSError (MP_EINVAL );
287+ }
288+
289+ while (row_index < (4 * width ))
290+ {
291+ rgb565_temp = rgb888to565 (* (rt_uint32_t * )(row_buf + row_index ));
292+ * (rt_uint8_t * )(image_buf + image_index ) = (rgb565_temp >> 8 );
293+ * (rt_uint8_t * )(image_buf + image_index + 1 ) = rgb565_temp & 0xff ;
294+
295+ row_index += 4 ;
296+ image_index += 2 ;
297+ }
298+
299+ lcd_show_image ( x , y -- , width , 1 , (const rt_uint8_t * )image_buf );
300+ }
301+
302+ close (fd );
303+ rt_free (bmp_info );
304+ rt_free (image_buf );
305+ rt_free (row_buf );
306+ return mp_const_none ;
307+ }
308+ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_lcd_show_bmp_obj , 4 , 4 , machine_lcd_show_bmp );
309+
216310STATIC const mp_rom_map_elem_t machine_lcd_locals_dict_table [] = {
217311 // instance methods
218312 { MP_ROM_QSTR (MP_QSTR_light ), MP_ROM_PTR (& machine_lcd_light_obj ) },
@@ -224,6 +318,7 @@ STATIC const mp_rom_map_elem_t machine_lcd_locals_dict_table[] = {
224318 { MP_ROM_QSTR (MP_QSTR_circle ), MP_ROM_PTR (& machine_lcd_circle_obj ) },
225319 { MP_ROM_QSTR (MP_QSTR_set_color ), MP_ROM_PTR (& machine_lcd_set_color_obj ) },
226320 { MP_ROM_QSTR (MP_QSTR_show_image ), MP_ROM_PTR (& machine_lcd_show_image_obj ) },
321+ { MP_ROM_QSTR (MP_QSTR_show_bmp ), MP_ROM_PTR (& machine_lcd_show_bmp_obj ) },
227322 // color
228323 { MP_ROM_QSTR (MP_QSTR_WHITE ), MP_ROM_INT (WHITE ) },
229324 { MP_ROM_QSTR (MP_QSTR_BLACK ), MP_ROM_INT (BLACK ) },
0 commit comments