From ad00890d12ef5105c05f3438e71cff08e2c5e35c Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Mon, 5 Jun 2023 00:44:44 +0200
Subject: [PATCH] python: fully migrate over to ctx

This removes some leftover Python code that still used the old display_*
calls for drawing instead of ctx.

We are now left only with display_update, which we will replace by the
new graphics stack soon.
---
 python_payload/cap_touch_demo.py | 12 ++++++-----
 python_payload/utils.py          | 16 ---------------
 sim/fakes/hardware.py            | 25 +----------------------
 usermodule/mp_hardware.c         | 34 --------------------------------
 4 files changed, 8 insertions(+), 79 deletions(-)

diff --git a/python_payload/cap_touch_demo.py b/python_payload/cap_touch_demo.py
index 6b64786a11..3bbac30b5e 100644
--- a/python_payload/cap_touch_demo.py
+++ b/python_payload/cap_touch_demo.py
@@ -4,23 +4,25 @@ import cmath
 import math
 import time
 
+ctx = hardware.get_ctx()
+
 def init():
     pass
 
 def run():
-    hardware.display_fill(0)
+    ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
     time.sleep_ms(30)
     for i in range(10):
         size = (hardware.get_captouch(i) * 4) + 4
         size += int(max(0, sum([hardware.captouch_get_petal_pad(i, x) for x in range(0, 3+1)]) / 8000))
         x = 70 + (hardware.captouch_get_petal_rad(i)/1000)
         x += (hardware.captouch_get_petal_phi(i)/600)*1j
-        rot =  cmath.exp(2j *math.pi * i / 10)
+        rot = cmath.exp(2j * math.pi * i / 10)
         x = x * rot
-        col = 0b1111100000011111
+        col = (1.0, 0.0, 1.0)
         if i%2:
-            col = 0b0000011111111111
-        utils.draw_rect(int(x.imag+120-(size/2)),int(x.real+120-(size/2)),size,size,col)
+            col = (0.0, 1.0, 1.0)
+        ctx.rgb(*col).rectangle(-int(x.imag-(size/2)), -int(x.real-(size/2)), size, size).fill()
     hardware.display_update()
 
 def foreground():
diff --git a/python_payload/utils.py b/python_payload/utils.py
index 8ad43e97cb..2341cf9a0c 100644
--- a/python_payload/utils.py
+++ b/python_payload/utils.py
@@ -10,17 +10,6 @@ def clear_all_leds():
         set_led_rgb(i, 0, 0, 0)
     update_leds()
 
-def draw_text_big(text, x, y):
-    ypos = 120+int(len(text)) + int(y)
-    xpos = 120+int(len(text[0])) + int(x)
-    for l, line in enumerate(text):
-        for p, pixel in enumerate(line):
-            if(pixel == '#'):
-                display_draw_pixel(xpos - 2*p, ypos - 2*l, RED)
-                display_draw_pixel(xpos - 2*p, ypos - 2*l-1, BLUE)
-                display_draw_pixel(xpos - 2*p-1, ypos - 2*l, BLUE)
-                display_draw_pixel(xpos - 2*p-1, ypos - 2*l-1, RED)
-
 def highlight_bottom_petal(num, RED, GREEN, BLUE):
     start = 4 + 8*num
     for i in range(7):
@@ -34,11 +23,6 @@ def long_bottom_petal_captouch_blocking(num, ms):
             return True
     return False
 
-def draw_rect(x,y,w,h,col):
-    for j in range(w):
-        for k in range(h):
-            display_draw_pixel(x+j,y+k,col)
-
 def draw_volume_slider(ctx, volume):
     length = 96 + ((volume - 20) * 1.6)
     if length > 96:
diff --git a/sim/fakes/hardware.py b/sim/fakes/hardware.py
index 5d5128dd3d..3afd56558c 100644
--- a/sim/fakes/hardware.py
+++ b/sim/fakes/hardware.py
@@ -285,16 +285,6 @@ class Simulation:
 _sim = Simulation()
 
 
-_deprecated_notified = set()
-def _deprecated(f):
-    def wrapper(*args, **kwargs):
-        if f not in _deprecated_notified:
-            print(f'{f.__name__} is deprecated!')
-            _deprecated_notified.add(f)
-        return f(*args, **kwargs)
-    return wrapper
-
-
 def init_done():
     return True
 
@@ -317,19 +307,6 @@ def get_ctx():
     return _global_ctx
 
 
-@_deprecated
-def display_fill(color):
-    """
-    display_fill is deprecated as it doesn't work well with ctx's framebuffer
-    ownership / state diffing. Instead, callers should use plain ctx functions
-    to fill the screen.
-    """
-    r = (color >> 11) & 0b11111
-    g = (color >> 5 ) & 0b111111
-    b =  color        & 0b11111
-    get_ctx().rgb(r << 2, g << 3, b <<2).rectangle(-120, -120, 240, 240).fill()
-
-
 def display_update():
     _sim.process_events()
     _sim.render_display()
@@ -402,4 +379,4 @@ def captouch_get_petal_rad(a):
     return 0
 
 def captouch_get_petal_phi(a):
-    return 0
\ No newline at end of file
+    return 0
diff --git a/usermodule/mp_hardware.c b/usermodule/mp_hardware.c
index 37d9994d4b..81fcb90697 100644
--- a/usermodule/mp_hardware.c
+++ b/usermodule/mp_hardware.c
@@ -37,29 +37,6 @@ STATIC mp_obj_t mp_display_update(void) {
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_display_update_obj, mp_display_update);
 
-STATIC mp_obj_t mp_display_draw_pixel(size_t n_args, const mp_obj_t *args) {
-    uint16_t x = mp_obj_get_int(args[0]);
-    uint16_t y = mp_obj_get_int(args[1]);
-    uint16_t col = mp_obj_get_int(args[2]);
-    display_draw_pixel(x, y, col);
-    return mp_const_none;
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_display_draw_pixel_obj, 3, 4, mp_display_draw_pixel);
-
-STATIC mp_obj_t mp_display_get_pixel(size_t n_args, const mp_obj_t *args) {
-    uint16_t x = mp_obj_get_int(args[0]);
-    uint16_t y = mp_obj_get_int(args[1]);
-    return mp_obj_new_int(display_get_pixel(x, y));
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_display_get_pixel_obj, 2, 3, mp_display_get_pixel);
-
-STATIC mp_obj_t mp_display_fill(size_t n_args, const mp_obj_t *args) {
-    uint16_t col = mp_obj_get_int(args[0]);
-    display_fill(col);
-    return mp_const_none;
-}
-STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_display_fill_obj, 1, 2, mp_display_fill);
-
 STATIC mp_obj_t mp_get_captouch(size_t n_args, const mp_obj_t *args) {
     uint16_t captouch = read_captouch();
     uint16_t pad = mp_obj_get_int(args[0]);
@@ -199,13 +176,6 @@ 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) },
     { MP_ROM_QSTR(MP_QSTR_init_done), MP_ROM_PTR(&mp_init_done_obj) },
@@ -226,12 +196,8 @@ STATIC const mp_rom_map_elem_t mp_module_hardware_globals_table[] = {
     { MP_ROM_QSTR(MP_QSTR_set_led_hsv), MP_ROM_PTR(&mp_set_led_hsv_obj) },
     { MP_ROM_QSTR(MP_QSTR_update_leds), MP_ROM_PTR(&mp_update_leds_obj) },
     { MP_ROM_QSTR(MP_QSTR_display_update), MP_ROM_PTR(&mp_display_update_obj) },
-    { MP_ROM_QSTR(MP_QSTR_display_draw_pixel), MP_ROM_PTR(&mp_display_draw_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_version), MP_ROM_PTR(&mp_version_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);
-- 
GitLab