Skip to content
Snippets Groups Projects
Commit 949d7514 authored by iggy's avatar iggy
Browse files

reset ctx (exposed to mp), rotate and translate ctx by default

parent 073588d5
No related branches found
No related tags found
No related merge requests found
...@@ -32,13 +32,7 @@ static void _display_init() { ...@@ -32,13 +32,7 @@ static void _display_init() {
// GC9A01_Screen_Load(0,0,240,240,pixels); // GC9A01_Screen_Load(0,0,240,240,pixels);
GC9A01_Update(); 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)); display_queue = xQueueCreate(1, sizeof(display_cfg_t));
...@@ -54,6 +48,20 @@ static void _display_init() { ...@@ -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(){ void display_update(){
GC9A01_Update(); GC9A01_Update();
} }
...@@ -97,5 +105,8 @@ static void display_task(TimerHandle_t aaaaa) { ...@@ -97,5 +105,8 @@ static void display_task(TimerHandle_t aaaaa) {
display_draw_scope(); display_draw_scope();
} }
void display_init() { _display_init(); } void display_init() {
_display_init();
display_ctx_init();
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "../../usermodule/uctx/uctx/ctx.h" #include "../../usermodule/uctx/uctx/ctx.h"
void display_init(); void display_init();
void display_ctx_init();
void display_draw_scope(); void display_draw_scope();
void display_update(); void display_update();
void display_draw_pixel(uint8_t x, uint8_t y, uint16_t col); void display_draw_pixel(uint8_t x, uint8_t y, uint16_t col);
......
...@@ -37,10 +37,10 @@ ctx.text_baseline = ctx.MIDDLE ...@@ -37,10 +37,10 @@ ctx.text_baseline = ctx.MIDDLE
#ctx.rgb() expects individual values for the channels, so unpack a list/tuple with * #ctx.rgb() expects individual values for the channels, so unpack a list/tuple with *
#operations on ctx can be chained #operations on ctx can be chained
#create a blue background #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 #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() hardware.display_update()
...@@ -48,7 +48,7 @@ class Worm(): ...@@ -48,7 +48,7 @@ class Worm():
def __init__(self): def __init__(self):
self.color = randrgb() self.color = randrgb()
self.direction = random.random()*math.pi*2 self.direction = random.random()*math.pi*2
self.size = random.randint(5,30) self.size = 10
self.speed = self.size/5 self.speed = self.size/5
(x,y) = xy_from_polar(40, self.direction+90) (x,y) = xy_from_polar(40, self.direction+90)
self.x = x self.x = x
...@@ -59,26 +59,19 @@ class Worm(): ...@@ -59,26 +59,19 @@ class Worm():
def draw(self): def draw(self):
ctx.rgb(*self.color) ctx.rgb(*self.color)
ctx.round_rectangle( ctx.round_rectangle(
WIDTH/2+self.x-self.size/2, self.x-self.size/2,
HEIGHT/2+self.y-self.size/2, self.y-self.size/2,
self.size,self.size,self.size//2 self.size,self.size,self.size//2
).fill() ).fill()
def mutate(self): def mutate(self):
self.size += random.randint(-3,4) self.color = ([max(0,min(1,x+((random.random()-0.5)*0.3))) for x in self.color])
self.color = ([max(0,min(1,x+(random.random()*0.1-0.05))) for x in self.color])
def move(self): def move(self):
if self.size < 3: dist = math.sqrt(self.x**2+self.y**2)
self.size+=1 self.size = (120-dist)/3
self.speed = self.size/5
if self.size > 30:
self.size-=1
self.speed = min(3,self.size/4)
#self.speed = self.size/5
self.direction += (random.random()-0.5)*math.pi/4 self.direction += (random.random()-0.5)*math.pi/4
...@@ -86,7 +79,7 @@ class Worm(): ...@@ -86,7 +79,7 @@ class Worm():
self.x+=dx self.x+=dx
self.y+=dy self.y+=dy
dist = math.sqrt(self.x**2+self.y**2)
if dist>110-self.size/2 and dist>self._lastdist: if dist>110-self.size/2 and dist>self._lastdist:
polar_position=math.atan2(self.y,self.x) polar_position=math.atan2(self.y,self.x)
dx=dx*-abs(math.cos(polar_position)) dx=dx*-abs(math.cos(polar_position))
......
...@@ -128,6 +128,12 @@ STATIC mp_obj_t mp_get_ctx(size_t n_args, const mp_obj_t *args) { ...@@ -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_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[] = { 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) }, { 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[] = { ...@@ -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_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_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_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); STATIC MP_DEFINE_CONST_DICT(mp_module_hardware_globals, mp_module_hardware_globals_table);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment