Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plasma: Add WS2812 W suppot for #220 #226

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions drivers/plasma/ws2812.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void WS2812::clear() {
}
}

void WS2812::set_hsv(uint32_t index, float h, float s, float v) {
void WS2812::set_hsv(uint32_t index, float h, float s, float v, uint8_t w) {
float i = floor(h * 6.0f);
float f = h * 6.0f - i;
v *= 255.0f;
Expand All @@ -76,12 +76,12 @@ void WS2812::set_hsv(uint32_t index, float h, float s, float v) {
uint8_t t = v * (1.0f - (1.0f - f) * s);

switch (int(i) % 6) {
case 0: set_rgb(index, v, t, p); break;
case 1: set_rgb(index, q, v, p); break;
case 2: set_rgb(index, p, v, t); break;
case 3: set_rgb(index, p, q, v); break;
case 4: set_rgb(index, t, p, v); break;
case 5: set_rgb(index, v, p, q); break;
case 0: set_rgb(index, v, t, p, w); break;
case 1: set_rgb(index, q, v, p, w); break;
case 2: set_rgb(index, p, v, t, w); break;
case 3: set_rgb(index, p, q, v, w); break;
case 4: set_rgb(index, t, p, v, w); break;
case 5: set_rgb(index, v, p, q, w); break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/plasma/ws2812.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace plasma {
bool stop();
void update(bool blocking=false);
void clear();
void set_hsv(uint32_t index, float h, float s, float v);
void set_hsv(uint32_t index, float h, float s, float v, uint8_t w=0);
void set_rgb(uint32_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w=0, bool gamma=true);
void set_brightness(uint8_t b);
RGB get(uint32_t index) {return buffer[index];};
Expand Down
18 changes: 12 additions & 6 deletions micropython/modules/plasma/plasma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ mp_obj_t PlasmaWS2812_start(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k


mp_obj_t PlasmaWS2812_set_rgb(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_index, ARG_r, ARG_g, ARG_b };
enum { ARG_self, ARG_index, ARG_r, ARG_g, ARG_b, ARG_w };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_r, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_g, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_b, MP_ARG_REQUIRED | MP_ARG_INT }
{ MP_QSTR_b, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_w, MP_ARG_INT, {.u_int = 0} }
};

// Parse args.
Expand All @@ -150,21 +151,24 @@ mp_obj_t PlasmaWS2812_set_rgb(size_t n_args, const mp_obj_t *pos_args, mp_map_t
int r = args[ARG_r].u_int;
int g = args[ARG_g].u_int;
int b = args[ARG_b].u_int;
int w = args[ARG_w].u_int;

_PlasmaWS2812_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _PlasmaWS2812_obj_t);
self->ws2812->set_rgb(index, r, g, b);

self->ws2812->set_rgb(index, r, g, b, w);

return mp_const_none;
}

mp_obj_t PlasmaWS2812_set_hsv(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_self, ARG_index, ARG_h, ARG_s, ARG_v };
enum { ARG_self, ARG_index, ARG_h, ARG_s, ARG_v, ARG_w };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_hue, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_sat, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&const_float_1)} },
{ MP_QSTR_val, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&const_float_1)} }
{ MP_QSTR_val, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&const_float_1)} },
{ MP_QSTR_w, MP_ARG_INT, {.u_int = 0} }
};

// Parse args.
Expand All @@ -175,9 +179,11 @@ mp_obj_t PlasmaWS2812_set_hsv(size_t n_args, const mp_obj_t *pos_args, mp_map_t
float h = mp_obj_get_float(args[ARG_h].u_obj);
float s = mp_obj_get_float(args[ARG_s].u_obj);
float v = mp_obj_get_float(args[ARG_v].u_obj);
int w = args[ARG_w].u_int;

_PlasmaWS2812_obj_t *self = MP_OBJ_TO_PTR2(args[ARG_self].u_obj, _PlasmaWS2812_obj_t);
self->ws2812->set_hsv(index, h, s, v);

self->ws2812->set_hsv(index, h, s, v, w);

return mp_const_none;
}
Expand Down