Try to fix @micropython.viper issues on macOS/darwin

This commit is contained in:
Thomas Farstrike
2025-11-22 16:32:43 +01:00
parent b55ff46e36
commit 27b6b53106
2 changed files with 36 additions and 16 deletions
@@ -61,7 +61,7 @@ class Draw(Activity):
lv.draw_line(self.layer,dsc)
self.canvas.finish_layer(self.layer)
@micropython.viper # make it with native compilation
# @micropython.viper # "invalid micropython decorator" on macOS
def draw_rect(self, x: int, y: int):
draw_dsc = lv.draw_rect_dsc_t()
lv.draw_rect_dsc_t.init(draw_dsc)
@@ -201,22 +201,29 @@ class AudioPlayer:
print(f"Playing {data_size} original bytes (vol {cls._volume}%) ...")
f.seek(data_start)
# ----- Viper volume scaler (16-bit only) -------------------------
@micropython.viper
# Fallback to non-viper and non-functional code on desktop, as macOS/darwin throws "invalid micropython decorator"
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
for i in range(0, num_bytes, 2):
lo = int(buf[i])
hi = int(buf[i+1])
sample = (hi << 8) | lo
if hi & 128:
sample -= 65536
sample = (sample * scale_fixed) // 32768
if sample > 32767:
sample = 32767
elif sample < -32768:
sample = -32768
buf[i] = sample & 255
buf[i+1] = (sample >> 8) & 255
pass
try:
# ----- Viper volume scaler (16-bit only) -------------------------
@micropython.viper # throws "invalid micropython decorator" on macOS / darwin
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
for i in range(0, num_bytes, 2):
lo = int(buf[i])
hi = int(buf[i+1])
sample = (hi << 8) | lo
if hi & 128:
sample -= 65536
sample = (sample * scale_fixed) // 32768
if sample > 32767:
sample = 32767
elif sample < -32768:
sample = -32768
buf[i] = sample & 255
buf[i+1] = (sample >> 8) & 255
except SyntaxError:
print("Viper not supported (e.g., on desktop)—using plain Python.")
chunk_size = 4096
bytes_per_original_sample = (bits_per_sample // 8) * channels
@@ -276,3 +283,16 @@ class AudioPlayer:
if cls._i2s:
cls._i2s.deinit()
cls._i2s = None
def optional_viper(func):
"""Decorator to apply @micropython.viper if possible."""
try:
@micropython.viper
@func # Wait, no—see below for proper chaining
def wrapped(*args, **kwargs):
return func(*args, **kwargs)
return wrapped
except SyntaxError:
return func # Fallback to original