From 391fd5e2436baff6a37324ef5a4b04b3fd91d17c Mon Sep 17 00:00:00 2001 From: Laury Date: Sun, 19 Jun 2022 10:26:50 +0200 Subject: [PATCH] [python/ion] Error if brightness isn't between 0 and 240 in set_brightness --- python/port/mod/ion/modion.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/python/port/mod/ion/modion.cpp b/python/port/mod/ion/modion.cpp index f698c0a21..ab38f7c3e 100644 --- a/python/port/mod/ion/modion.cpp +++ b/python/port/mod/ion/modion.cpp @@ -107,11 +107,16 @@ mp_obj_t modion_get_keys() { } mp_obj_t modion_set_brightness(mp_obj_t brightness_mp){ - uint8_t brightness = static_cast(mp_obj_get_int(brightness_mp)); - GlobalPreferences::sharedGlobalPreferences()->setBrightnessLevel(brightness); - Ion::Backlight::setBrightness(brightness); - micropython_port_interrupt_if_needed(); - return mp_const_none; + int brightness = mp_obj_get_int(brightness_mp); + if (brightness < 0 || brightness > 240) { + mp_raise_ValueError("Brightness must be between 0 and 240"); + } else { + uint8_t unsignedBrightness = static_cast(brightness); + GlobalPreferences::sharedGlobalPreferences()->setBrightnessLevel(unsignedBrightness); + Ion::Backlight::setBrightness(unsignedBrightness); + micropython_port_interrupt_if_needed(); + return mp_const_none; + } } mp_obj_t modion_get_brightness(){