diff --git a/components/badge23/display.c b/components/badge23/display.c index 93a5454b046ae0a4cc912f3d135869a0d10b6bc7..afaffd34acc23612c56551581d84250c5974e0f3 100644 --- a/components/badge23/display.c +++ b/components/badge23/display.c @@ -31,15 +31,9 @@ static void _display_init() { GC9A01_Init(); // GC9A01_Screen_Load(0,0,240,240,pixels); GC9A01_Update(); - - the_ctx = ctx_new_for_framebuffer( - ScreenBuff, - GC9A01_Width, - GC9A01_Height, - GC9A01_Width * 2, - CTX_FORMAT_RGB565_BYTESWAPPED - ); - + + + /* display_queue = xQueueCreate(1, sizeof(display_cfg_t)); TaskHandle_t handle; @@ -54,6 +48,20 @@ static void _display_init() { */ } + +void display_ctx_init() { + the_ctx = ctx_new_for_framebuffer( + ScreenBuff, + GC9A01_Width, + GC9A01_Height, + GC9A01_Width * 2, + CTX_FORMAT_RGB565_BYTESWAPPED + ); + + // rotate by 180 deg and translate x and y by 120 px to have (0,0) at the center of the screen + ctx_apply_transform(the_ctx,-1,0,120,0,-1,120,0,0,1); +} + void display_update(){ GC9A01_Update(); } @@ -97,5 +105,8 @@ static void display_task(TimerHandle_t aaaaa) { display_draw_scope(); } -void display_init() { _display_init(); } +void display_init() { + _display_init(); + display_ctx_init(); +} diff --git a/components/badge23/include/badge23/display.h b/components/badge23/include/badge23/display.h index 1fc0fca10b482bef8bbd870cb5d819249d3206cf..e2946a47c711e84e73982053045ea07ed903aae2 100644 --- a/components/badge23/include/badge23/display.h +++ b/components/badge23/include/badge23/display.h @@ -5,6 +5,7 @@ #include "../../usermodule/uctx/uctx/ctx.h" void display_init(); +void display_ctx_init(); void display_draw_scope(); void display_update(); void display_draw_pixel(uint8_t x, uint8_t y, uint16_t col); diff --git a/python_payload/demo_worms.py b/python_payload/demo_worms.py index 033d72a3349458d8c68d49305ca21a403ad3e73d..ebe97b991ce8d650a556e8e6e10c31fe85d63a45 100644 --- a/python_payload/demo_worms.py +++ b/python_payload/demo_worms.py @@ -37,10 +37,10 @@ ctx.text_baseline = ctx.MIDDLE #ctx.rgb() expects individual values for the channels, so unpack a list/tuple with * #operations on ctx can be chained #create a blue background -ctx.rgb(*BLUE).rectangle(0,0,WIDTH,HEIGHT).fill() +ctx.rgb(*BLUE).rectangle(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT).fill() #Write some text -ctx.move_to(WIDTH/2,HEIGHT/2).rgb(*WHITE).text("Hi :)") +ctx.move_to(0,0).rgb(*WHITE).text("Hi :)") hardware.display_update() @@ -48,7 +48,7 @@ class Worm(): def __init__(self): self.color = randrgb() self.direction = random.random()*math.pi*2 - self.size = random.randint(5,30) + self.size = 10 self.speed = self.size/5 (x,y) = xy_from_polar(40, self.direction+90) self.x = x @@ -59,26 +59,19 @@ class Worm(): def draw(self): ctx.rgb(*self.color) ctx.round_rectangle( - WIDTH/2+self.x-self.size/2, - HEIGHT/2+self.y-self.size/2, + self.x-self.size/2, + self.y-self.size/2, self.size,self.size,self.size//2 ).fill() def mutate(self): - self.size += random.randint(-3,4) - self.color = ([max(0,min(1,x+(random.random()*0.1-0.05))) for x in self.color]) - + self.color = ([max(0,min(1,x+((random.random()-0.5)*0.3))) for x in self.color]) def move(self): - if self.size < 3: - self.size+=1 - - if self.size > 30: - self.size-=1 - - self.speed = min(3,self.size/4) - #self.speed = self.size/5 + dist = math.sqrt(self.x**2+self.y**2) + self.size = (120-dist)/3 + self.speed = self.size/5 self.direction += (random.random()-0.5)*math.pi/4 @@ -86,7 +79,7 @@ class Worm(): self.x+=dx self.y+=dy - dist = math.sqrt(self.x**2+self.y**2) + if dist>110-self.size/2 and dist>self._lastdist: polar_position=math.atan2(self.y,self.x) dx=dx*-abs(math.cos(polar_position)) diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c index bcdcc81429b0d3bea201ab9654f6b63b91b4ce78..f204f921f8eb7811aba5343a91f235e2d7f2cf5d 100644 --- a/usermodule/mp_hardware.c +++ b/usermodule/mp_hardware.c @@ -128,6 +128,12 @@ STATIC mp_obj_t mp_get_ctx(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_get_ctx_obj, 0, 0, mp_get_ctx); +STATIC mp_obj_t mp_reset_ctx(size_t n_args, const mp_obj_t *args) { + display_ctx_init(); + mp_ctx = mp_ctx_from_ctx(the_ctx); + return mp_ctx; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_reset_ctx_obj, 0, 0, mp_reset_ctx); STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_badge_audio) }, @@ -145,6 +151,7 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_display_get_pixel), MP_ROM_PTR(&mp_display_get_pixel_obj) }, { MP_ROM_QSTR(MP_QSTR_display_fill), MP_ROM_PTR(&mp_display_fill_obj) }, { MP_ROM_QSTR(MP_QSTR_get_ctx), MP_ROM_PTR(&mp_get_ctx_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset_ctx), MP_ROM_PTR(&mp_reset_ctx_obj) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, mp_module_hardware_globals_table);