Skip to content

Commit 27b6b53

Browse files
Try to fix @micropython.viper issues on macOS/darwin
1 parent b55ff46 commit 27b6b53

File tree

2 files changed

+36
-16
lines changed
  • internal_filesystem/apps

2 files changed

+36
-16
lines changed

internal_filesystem/apps/com.micropythonos.draw/assets/draw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def draw_line(self, x, y):
6161
lv.draw_line(self.layer,dsc)
6262
self.canvas.finish_layer(self.layer)
6363

64-
@micropython.viper # make it with native compilation
64+
# @micropython.viper # "invalid micropython decorator" on macOS
6565
def draw_rect(self, x: int, y: int):
6666
draw_dsc = lv.draw_rect_dsc_t()
6767
lv.draw_rect_dsc_t.init(draw_dsc)

internal_filesystem/apps/com.micropythonos.musicplayer/assets/audio_player.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,29 @@ def play_wav(cls, filename, result_callback=None):
201201
print(f"Playing {data_size} original bytes (vol {cls._volume}%) ...")
202202
f.seek(data_start)
203203

204-
# ----- Viper volume scaler (16-bit only) -------------------------
205-
@micropython.viper
204+
# Fallback to non-viper and non-functional code on desktop, as macOS/darwin throws "invalid micropython decorator"
206205
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
207-
for i in range(0, num_bytes, 2):
208-
lo = int(buf[i])
209-
hi = int(buf[i+1])
210-
sample = (hi << 8) | lo
211-
if hi & 128:
212-
sample -= 65536
213-
sample = (sample * scale_fixed) // 32768
214-
if sample > 32767:
215-
sample = 32767
216-
elif sample < -32768:
217-
sample = -32768
218-
buf[i] = sample & 255
219-
buf[i+1] = (sample >> 8) & 255
206+
pass
207+
208+
try:
209+
# ----- Viper volume scaler (16-bit only) -------------------------
210+
@micropython.viper # throws "invalid micropython decorator" on macOS / darwin
211+
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
212+
for i in range(0, num_bytes, 2):
213+
lo = int(buf[i])
214+
hi = int(buf[i+1])
215+
sample = (hi << 8) | lo
216+
if hi & 128:
217+
sample -= 65536
218+
sample = (sample * scale_fixed) // 32768
219+
if sample > 32767:
220+
sample = 32767
221+
elif sample < -32768:
222+
sample = -32768
223+
buf[i] = sample & 255
224+
buf[i+1] = (sample >> 8) & 255
225+
except SyntaxError:
226+
print("Viper not supported (e.g., on desktop)—using plain Python.")
220227

221228
chunk_size = 4096
222229
bytes_per_original_sample = (bits_per_sample // 8) * channels
@@ -276,3 +283,16 @@ def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
276283
if cls._i2s:
277284
cls._i2s.deinit()
278285
cls._i2s = None
286+
287+
288+
289+
def optional_viper(func):
290+
"""Decorator to apply @micropython.viper if possible."""
291+
try:
292+
@micropython.viper
293+
@func # Wait, no—see below for proper chaining
294+
def wrapped(*args, **kwargs):
295+
return func(*args, **kwargs)
296+
return wrapped
297+
except SyntaxError:
298+
return func # Fallback to original

0 commit comments

Comments
 (0)