Skip to content

Commit c8375a6

Browse files
authored
docs: Document @micropython.native usage policy. (#406)
* docs: Document @micropython.native usage policy. Closes #405. Add a guideline to CONTRIBUTING.md: @micropython.native must not be used in driver code (I2C/SPI bus time dominates, native compilation gains nothing) but may be used in examples on rendering hot-paths where a measurable speedup is expected. Remove the unnecessary @micropython.native from compute_display() in radar_screen.py — the function does 3 comparisons with no loop, so native compilation provides no benefit. Also remove the now-unused `import micropython`. * 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 * perf(ssd1327): Add @micropython.native to draw_cube() in 3D cube example. draw_cube() runs 12 math.sin/cos calls + floating-point arithmetic per frame in a tight animation loop. Native compilation reduces the per-frame overhead significantly on the STM32WB55.
1 parent a3ddb36 commit c8375a6

6 files changed

Lines changed: 9 additions & 2 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ lib/<component>/
3535
- **Time**: use `from time import sleep_ms` (not `utime`, not `sleep()` with float seconds).
3636
- **Exceptions**: use `except Exception:` instead of bare `except:`. Enforced by ruff (E722).
3737
- **No debug `print()`** in production driver code. Enforced by ruff (T20, examples and tests excluded).
38+
- **`@micropython.native`**: do **not** use in driver code (`device.py`) — methods are dominated by I2C/SPI bus time, so native compilation gains nothing and reduces debuggability. In **examples**, it may be used on rendering hot-paths (tight pixel loops, math-heavy drawing functions) where a measurable speedup is expected. Do not apply it to functions with only a few comparisons or I/O calls. Note: the native emitter does not support generators, closures, or `with` statements.
3839

3940
## Driver API conventions
4041

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):

lib/ssd1327/examples/rotating_3d_cube.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import math
22

3+
import micropython
34
import ssd1327
45
from machine import SPI, Pin
56

@@ -27,6 +28,7 @@
2728
r = [0, 0, 0]
2829

2930

31+
@micropython.native
3032
def draw_cube():
3133
r[0] = r[0] + pi / 180.0
3234
r[1] = r[1] + pi / 180.0

lib/vl53l1x/examples/radar_screen.py

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

88
from time import sleep_ms
99

10-
import micropython
1110
import ssd1327
1211
from machine import I2C, SPI, Pin
1312
from steami_screen import DARK, GRAY, LIGHT, RED, Screen, SSD1327Display
@@ -29,7 +28,6 @@
2928
tof = VL53L1X(i2c)
3029

3130

32-
@micropython.native
3331
def compute_display(distance):
3432
"""Compute proximity and color from distance value.
3533

0 commit comments

Comments
 (0)