Skip to content

Commit 0864e70

Browse files
committed
perf: Add @micropython.native to rendering hot-paths in examples.
Apply the policy documented in this PR to the three examples with tight pixel loops: - tamagotchi.py: draw_character() — 4 nested loops, pixel by pixel - spirit_level.py: fill_circle() — double loop with x*x + y*y test - maze_game.py: draw_maze() — double loop over 11x11 grid
1 parent 8262532 commit 0864e70

3 files changed

Lines changed: 6 additions & 0 deletions

File tree

lib/bq27441/examples/tamagotchi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import random
1414
from time import sleep_ms, ticks_diff, ticks_ms
1515

16+
import micropython
1617
import ssd1327
1718
from bq27441 import BQ27441
1819
from machine import I2C, SPI, Pin
@@ -227,6 +228,7 @@ def wait_for_button():
227228
return None
228229

229230

231+
@micropython.native
230232
def draw_character(cx, cy, scale, sprite):
231233
"""Draw a scaled pixel-art sprite on the display framebuf."""
232234
fb = display.framebuf

lib/ism330dl/examples/maze_game.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import random
1919
from time import sleep_ms
2020

21+
import micropython
2122
import ssd1327
2223
from ism330dl import ISM330DL
2324
from machine import I2C, SPI, Pin
@@ -183,6 +184,7 @@ def cell_to_pixel(row, col):
183184
return SAFE_X + col * CELL_SIZE, SAFE_Y + row * CELL_SIZE
184185

185186

187+
@micropython.native
186188
def draw_maze(maze):
187189
"""Draw all maze walls as filled rectangles."""
188190
for row in range(len(maze)):

lib/ism330dl/examples/spirit_level.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from time import sleep_ms
1111

12+
import micropython
1213
import ssd1327
1314
from ism330dl import ISM330DL
1415
from machine import I2C, SPI, Pin
@@ -38,6 +39,7 @@
3839
POLL_RATE_MS = 20
3940

4041

42+
@micropython.native
4143
def fill_circle(fbuf, x0, y0, r, c):
4244
"""Helper to draw a filled circle since framebuf lacks it natively."""
4345
for y in range(-r, r + 1):

0 commit comments

Comments
 (0)