Skip to content
Snippets Groups Projects
Commit 5f869a93 authored by dx's avatar dx Committed by q3k
Browse files

leds: accept floats (0.0 to 1.0) for rgb, with backwards compat

parent 4601348c
No related branches found
No related tags found
No related merge requests found
...@@ -60,9 +60,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_slew_rate_obj, ...@@ -60,9 +60,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_leds_get_slew_rate_obj,
STATIC mp_obj_t mp_led_set_rgb(size_t n_args, const mp_obj_t *args) { STATIC mp_obj_t mp_led_set_rgb(size_t n_args, const mp_obj_t *args) {
uint8_t index = mp_obj_get_int(args[0]); uint8_t index = mp_obj_get_int(args[0]);
uint8_t red = mp_obj_get_int(args[1]); float red = mp_obj_get_float(args[1]);
uint8_t green = mp_obj_get_int(args[2]); float green = mp_obj_get_float(args[2]);
uint8_t blue = mp_obj_get_int(args[3]); float blue = mp_obj_get_float(args[3]);
st3m_leds_set_single_rgb(index, red, green, blue); st3m_leds_set_single_rgb(index, red, green, blue);
return mp_const_none; return mp_const_none;
} }
...@@ -81,9 +82,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_hsv_obj, 4, 4, ...@@ -81,9 +82,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_led_set_hsv_obj, 4, 4,
mp_led_set_hsv); mp_led_set_hsv);
STATIC mp_obj_t mp_led_set_all_rgb(mp_obj_t r, mp_obj_t g, mp_obj_t b) { STATIC mp_obj_t mp_led_set_all_rgb(mp_obj_t r, mp_obj_t g, mp_obj_t b) {
uint8_t red = mp_obj_get_int(r); float red = mp_obj_get_float(r);
uint8_t green = mp_obj_get_int(g); float green = mp_obj_get_float(g);
uint8_t blue = mp_obj_get_int(b); float blue = mp_obj_get_float(b);
st3m_leds_set_all_rgb(red, green, blue); st3m_leds_set_all_rgb(red, green, blue);
return mp_const_none; return mp_const_none;
} }
......
...@@ -105,12 +105,17 @@ void st3m_leds_update_hardware() { ...@@ -105,12 +105,17 @@ void st3m_leds_update_hardware() {
} }
} }
void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, void st3m_leds_set_single_rgb(uint8_t index, float red, float green,
uint8_t blue) { float blue) {
if (red > 1.0) red /= 255.0;
if (green > 1.0) green /= 255.0;
if (blue > 1.0) blue /= 255.0;
LOCK_INCOMING; LOCK_INCOMING;
state.target_buffer[index].r = red; state.target_buffer[index].r = (uint8_t) (red * 255);
state.target_buffer[index].g = green; state.target_buffer[index].g = (uint8_t) (green * 255);
state.target_buffer[index].b = blue; state.target_buffer[index].b = (uint8_t) (blue * 255);
UNLOCK_INCOMING; UNLOCK_INCOMING;
} }
...@@ -125,7 +130,7 @@ void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float val) { ...@@ -125,7 +130,7 @@ void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float val) {
UNLOCK_INCOMING; UNLOCK_INCOMING;
} }
void st3m_leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue) { void st3m_leds_set_all_rgb(float red, float green, float blue) {
for (int i = 0; i < 40; i++) { for (int i = 0; i < 40; i++) {
st3m_leds_set_single_rgb(i, red, green, blue); st3m_leds_set_single_rgb(i, red, green, blue);
} }
......
...@@ -23,10 +23,10 @@ void st3m_leds_init(); ...@@ -23,10 +23,10 @@ void st3m_leds_init();
// //
// After you're ready setting up your blink, call st3m_leds_update, or enable // After you're ready setting up your blink, call st3m_leds_update, or enable
// autoupdates. // autoupdates.
void st3m_leds_set_single_rgb(uint8_t index, uint8_t red, uint8_t green, void st3m_leds_set_single_rgb(uint8_t index, float red, float green,
uint8_t blue); float blue);
void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float value); void st3m_leds_set_single_hsv(uint8_t index, float hue, float sat, float value);
void st3m_leds_set_all_rgb(uint8_t red, uint8_t green, uint8_t blue); void st3m_leds_set_all_rgb(float red, float green, float blue);
void st3m_leds_set_all_hsv(float hue, float sat, float value); void st3m_leds_set_all_hsv(float hue, float sat, float value);
// Set/get global LED brightness, 0-255. Default 69. // Set/get global LED brightness, 0-255. Default 69.
......
...@@ -8,13 +8,13 @@ There are 8 LEDs per top petal, or 4 LEDs per petal. ...@@ -8,13 +8,13 @@ There are 8 LEDs per top petal, or 4 LEDs per petal.
After you're ready setting up your blink, call update(), or enable autoupdates. After you're ready setting up your blink, call update(), or enable autoupdates.
""" """
def set_rgb(ix: int, r: int, g: int, b: int) -> None: def set_rgb(ix: int, r: float, g: float, b: float) -> None:
"""Set LED `ix` to rgb value r, g, b """Set LED `ix` to rgb value r, g, b
:param ix: LED index, from 0 to 39 :param ix: LED index, from 0 to 39
:param r: Red value, from 0 to 255 :param r: Red value, from 0.0 to 1.0
:param g: Green value, from 0 to 255 :param g: Green value, from 0.0 to 1.0
:param b: Blue value, from 0 to 255 :param b: Blue value, from 0.0 to 1.0
""" """
def set_hsv(ix: int, hue: float, sat: float, val: float) -> None: def set_hsv(ix: int, hue: float, sat: float, val: float) -> None:
...@@ -26,12 +26,12 @@ def set_hsv(ix: int, hue: float, sat: float, val: float) -> None: ...@@ -26,12 +26,12 @@ def set_hsv(ix: int, hue: float, sat: float, val: float) -> None:
:param val: Value, from 0.0 to 1.0 :param val: Value, from 0.0 to 1.0
""" """
def set_all_rgb(r: int, g: int, b: int) -> None: def set_all_rgb(r: float, g: float, b: float) -> None:
"""Set all LEDs to rgb value r, g, b """Set all LEDs to rgb value r, g, b
:param r: Red value, from 0 to 255 :param r: Red value, from 0.0 to 1.0
:param g: Green value, from 0 to 255 :param g: Green value, from 0.0 to 1.0
:param b: Blue value, from 0 to 255 :param b: Blue value, from 0.0 to 1.0
""" """
def set_all_hsv(h: float, s: float, v: float) -> None: def set_all_hsv(h: float, s: float, v: float) -> None:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment