Skip to content

Commit

Permalink
chips/samd21: Accept a single list of three numbers to neopixel
Browse files Browse the repository at this point in the history
This makes boards with a single neopixel easier to use

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Mar 27, 2022
1 parent a27d492 commit 9bd4e47
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
53 changes: 35 additions & 18 deletions chips/samd21/snek-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,21 @@ snek_poly_to_inten(snek_poly_t a)
struct snek_neopixel *snek_neopixels;
static snek_offset_t snek_neopixel_count;

static snek_poly_t
set_neopixel(int p, snek_poly_t pixel)
{
if (snek_poly_type(pixel) != snek_list)
return snek_error_type_1(pixel);
snek_list_t *pixel_list = snek_poly_to_list(pixel);
if (snek_list_type(pixel_list) == snek_list_dict || pixel_list->size != 3)
return snek_error_type_1(pixel);
snek_poly_t *pixel_data = snek_list_data(pixel_list);
snek_neopixels[p].r = snek_poly_to_inten(pixel_data[0]);
snek_neopixels[p].g = snek_poly_to_inten(pixel_data[1]);
snek_neopixels[p].b = snek_poly_to_inten(pixel_data[2]);
return SNEK_NULL;
}

snek_poly_t
snek_builtin_neopixel(snek_poly_t pixels)
{
Expand All @@ -712,28 +727,30 @@ snek_builtin_neopixel(snek_poly_t pixels)
if (snek_list_type(pixels_list) == snek_list_dict)
return snek_error_type_1(pixels);

if (snek_neopixels == NULL || snek_neopixel_count < pixels_list->size) {
snek_neopixels = snek_alloc(pixels_list->size * sizeof (struct snek_neopixel));
snek_poly_t *pixels_data = snek_list_data(pixels_list);
snek_offset_t list_size = pixels_list->size;

/* Did the user pass a list of three elements, the first of which is a number? */
bool is_immediate = (list_size == 3 && snek_poly_type(pixels_data[0]) == snek_float);

if (is_immediate)
list_size = 1;

if (snek_neopixels == NULL || snek_neopixel_count < list_size) {
snek_neopixels = snek_alloc(list_size * sizeof (struct snek_neopixel));
if (!snek_neopixels)
return SNEK_NULL;
snek_neopixel_count = pixels_list->size;
snek_neopixel_count = list_size;
}

snek_poly_t *pixels_data = snek_list_data(pixels_list);
for (int p = 0; p < pixels_list->size; p++) {
snek_poly_t pixel = pixels_data[p];

if (snek_poly_type(pixel) != snek_list)
return snek_error_type_1(pixel);
snek_list_t *pixel_list = snek_poly_to_list(pixel);
if (snek_list_type(pixel_list) == snek_list_dict || pixel_list->size != 3)
return snek_error_type_1(pixel);
snek_poly_t *pixel_data = snek_list_data(pixel_list);
snek_neopixels[p].r = snek_poly_to_inten(pixel_data[0]);
snek_neopixels[p].g = snek_poly_to_inten(pixel_data[1]);
snek_neopixels[p].b = snek_poly_to_inten(pixel_data[2]);
if (snek_abort)
return SNEK_NULL;
if (is_immediate)
set_neopixel(0, pixels);
else {
for (int p = 0; p < pixels_list->size; p++) {
set_neopixel(p, pixels_data[p]);
if (snek_abort)
return SNEK_NULL;
}
}
if (power_pin == dir_pin)
ao_snek_neopixel_write(ao_snek_pin[power_pin].gpio, ao_snek_pin[power_pin].pin,
Expand Down
16 changes: 13 additions & 3 deletions doc/snek.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2150,9 +2150,19 @@ Programs either a set of neopixel devices connected to the current
Power pin (when Power and Direction are the same) or a set of APA102
devices connected to the current Power (used for APA102 Data) and
Direction (used for APA102 Clock) pins (when Power and Direction are
different). _pixels_ is a list or tuple, each element of which is a
list or tuple of three numbers ranging from 0 to 1 for the desired
red, green and blue intensity of the target neopixel.(((neopixel)))
different). _pixels_ is either a list/tuple of three numbers, or a
list/tuple, each element of which is a list/tuple of three numbers
ranging from 0 to 1 for the desired red, green and blue intensity of
the target neopixel.(((neopixel)))

[subs="verbatim,quotes"]
----
> *talkto(NEOPIXEL)*
> *neopixel((0, 0.5, 0))*
----

This example programs a single NeoPixel device, setting it to
half-intensity green.

[subs="verbatim,quotes"]
----
Expand Down

0 comments on commit 9bd4e47

Please sign in to comment.