3535#include <stdbool.h>
3636#include <stdio.h>
3737#include <stdint.h>
38+ #include <stdbool.h>
3839#include <string.h>
3940
4041#include <badge_eink.h>
5859#define imgSize (BADGE_EINK_WIDTH*BADGE_EINK_HEIGHT)
5960static uint8_t * img = 0 ;
6061
61- #define PXb (x ,y ) img[((x)+(y)*BADGE_EINK_WIDTH)] = 0x00
62- #define PXw (x ,y ) img[((x)+(y)*BADGE_EINK_WIDTH)] = 0xff
62+ #define PX (x ,y ,v ) img[((x)+(y)*BADGE_EINK_WIDTH)] = (v)
6363#define ABS (x ) (((x)<0)?-(x):(x))
6464
6565static void gfx_input_poll (uint32_t btn );
6666
6767STATIC mp_obj_t gfx_init (void ) {
6868 img = freedomgfxInit ();
69- for (int i = 0 ; i < imgSize ; i ++ )
70- img [i ] = 0xff ;
69+ memset (img , 0xff , imgSize );
7170 freedomgfxDraw ();
7271 return mp_const_none ;
7372}
@@ -93,7 +92,7 @@ STATIC mp_obj_t gfx_line(mp_uint_t n_args, const mp_obj_t *args) {
9392 int y1 = mp_obj_get_int (args [3 ]);
9493 int col = mp_obj_get_int (args [4 ]);
9594
96- PXb (x0 ,y0 );
95+ PX (x0 ,y0 , col );
9796
9897 // algorithm: https://de.wikipedia.org/wiki/Bresenham-Algorithmus
9998 int dx = ABS (x1 - x0 );
@@ -104,7 +103,7 @@ STATIC mp_obj_t gfx_line(mp_uint_t n_args, const mp_obj_t *args) {
104103 int err2 ;
105104
106105 while (1 ){
107- if ( col == 0 ) PXb ( x0 ,y0 ); else PXw ( x0 , y0 );
106+ PX ( x0 ,y0 , col );
108107 if ((x0 == x1 ) && (y0 == y1 ))
109108 break ;
110109 err2 = 2 * err ;
@@ -134,12 +133,7 @@ STATIC mp_obj_t gfx_area(mp_uint_t n_args, const mp_obj_t *args) {
134133 for (int i = 0 ; i < a ; i ++ )
135134 {
136135 for (int j = 0 ; j < b ; j ++ )
137- {
138- if (col == 0 )
139- PXb (x0 + i ,y0 + j );
140- else
141- PXw (x0 + i ,y0 + j );
142- }
136+ PX (x0 + i ,y0 + j ,col );
143137 }
144138
145139 return mp_const_none ;
@@ -171,12 +165,7 @@ STATIC mp_obj_t gfx_string(mp_uint_t n_args, const mp_obj_t *args) {
171165 for (int j = 0 ; j < cwidth ; j ++ ) // TODO: only works for single byte rows
172166 {
173167 if ( font [4 * 8 + (int )(* data )* clen + i ] & (1 <<(7 - j )))
174- {
175- if (col == 0 )
176- PXb (x0 + j + xoffs ,y0 + i );
177- else
178- PXw (x0 + j + xoffs ,y0 + i );
179- }
168+ PX (x0 + j + xoffs ,y0 + i ,col );
180169 }
181170 }
182171 data ++ ;
@@ -204,7 +193,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gfx_flush_obj, 0, 1, gfx_flush);
204193
205194// callback system
206195
207- STATIC uint8_t button_init_done = 0 ;
196+ STATIC bool button_init_done = false ;
208197STATIC mp_obj_t button_callbacks [1 + BADGE_BUTTONS ];
209198
210199static void gfx_input_poll (uint32_t btn )
@@ -220,14 +209,15 @@ STATIC mp_obj_t gfx_input_init(mp_uint_t n_args, const mp_obj_t *args) {
220209 for (size_t i = 0 ; i <= BADGE_BUTTONS ; i ++ ){
221210 button_callbacks [i ] = mp_const_none ;
222211 }
223- button_init_done = 1 ;
212+ button_init_done = true ;
224213 return mp_const_none ;
225214}
226215STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (gfx_input_init_obj , 0 , 1 , gfx_input_init );
227216
228217STATIC mp_obj_t gfx_input_attach (mp_uint_t n_args , const mp_obj_t * args ) {
229- uint8_t button = mp_obj_get_int (args [0 ]);
230- button_callbacks [button ] = args [1 ];
218+ int button = mp_obj_get_int (args [0 ]);
219+ if (button > 0 && button <= BADGE_BUTTONS )
220+ button_callbacks [button ] = args [1 ];
231221 return mp_const_none ;
232222}
233223STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (gfx_input_attach_obj , 2 , 2 , gfx_input_attach );
@@ -250,8 +240,8 @@ STATIC const mp_rom_map_elem_t freedomgfx_module_globals_table[] = {
250240 {MP_OBJ_NEW_QSTR (MP_QSTR_input_init ), (mp_obj_t )& gfx_input_init_obj },
251241 {MP_OBJ_NEW_QSTR (MP_QSTR_input_attach ), (mp_obj_t )& gfx_input_attach_obj },
252242
253- {MP_OBJ_NEW_QSTR (MP_QSTR_BLACK ), MP_OBJ_NEW_SMALL_INT (0 )},
254- {MP_OBJ_NEW_QSTR (MP_QSTR_WHITE ), MP_OBJ_NEW_SMALL_INT (1 )},
243+ {MP_OBJ_NEW_QSTR (MP_QSTR_BLACK ), MP_OBJ_NEW_SMALL_INT (0x00 )},
244+ {MP_OBJ_NEW_QSTR (MP_QSTR_WHITE ), MP_OBJ_NEW_SMALL_INT (0xff )},
255245
256246 {MP_OBJ_NEW_QSTR (MP_QSTR_JOY_UP ), MP_OBJ_NEW_SMALL_INT (BADGE_BUTTON_UP )},
257247 {MP_OBJ_NEW_QSTR (MP_QSTR_JOY_DOWN ), MP_OBJ_NEW_SMALL_INT (BADGE_BUTTON_DOWN )},
0 commit comments