Skip to content

Commit 643e441

Browse files
committed
Update NeoPixel API to be more Pythonic.
Can create multiple drivers (each one is an independent object). Uses subscription to read and write colours.
1 parent 9621875 commit 643e441

File tree

4 files changed

+89
-29
lines changed

4 files changed

+89
-29
lines changed

inc/genhdr/qstrdefs.generated.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,4 +609,4 @@ QDEF(MP_QSTR_readinto, (const byte*)"\x4b\x08" "readinto")
609609
QDEF(MP_QSTR_ODD, (const byte*)"\x6a\x03" "ODD")
610610
QDEF(MP_QSTR_EVEN, (const byte*)"\xdd\x04" "EVEN")
611611
QDEF(MP_QSTR_neopixel, (const byte*)"\x69\x08" "neopixel")
612-
QDEF(MP_QSTR_set_colour, (const byte*)"\x10\x0a" "set_colour")
612+
QDEF(MP_QSTR_NeoPixel, (const byte*)"\x69\x08" "NeoPixel")

inc/microbit/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ extern const struct _mp_obj_module_t neopixel_module;
110110
const char *readline_hist[8]; \
111111
mp_obj_t keyboard_interrupt_obj; \
112112
void *async_data[3]; \
113-
struct _neopixel_strip_t *neopixel_strip; \
114113

115114
// We need to provide a declaration/definition of alloca()
116115
#include <alloca.h>

inc/microbit/qstrdefsport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ Q(ODD)
362362
Q(EVEN)
363363

364364
Q(neopixel)
365-
Q(init)
365+
Q(NeoPixel)
366366
Q(clear)
367367
Q(show)
368-
Q(set_colour)

source/microbit/modneopixel.cpp

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
/*
3-
* This file is part of the Micro Python project, http://micropython.org/
2+
* This file is part of the MicroPython project, http://micropython.org/
43
*
54
* The MIT License (MIT)
65
*
7-
* Copyright (c) 2015 Damien P. George
6+
* Copyright (c) 2015-2016 Damien P. George
87
*
98
* Permission is hereby granted, free of charge, to any person obtaining a copy
109
* of this software and associated documentation files (the "Software"), to deal
@@ -30,43 +29,106 @@
3029

3130
extern "C" {
3231

33-
#include "py/mpstate.h"
32+
#include "py/runtime.h"
3433
#include "lib/neopixel.h"
3534
#include "microbitobj.h"
3635

37-
STATIC mp_obj_t mod_neopixel_init(mp_obj_t pin_in, mp_obj_t num_leds_in) {
38-
PinName pin = microbit_obj_get_pin(pin_in)->name;
39-
MP_STATE_PORT(neopixel_strip) = m_new_obj(neopixel_strip_t);
40-
neopixel_init(MP_STATE_PORT(neopixel_strip), pin, mp_obj_get_int(num_leds_in));
41-
return mp_const_none;
36+
extern const mp_obj_type_t neopixel_type;
37+
38+
typedef struct _neopixel_obj_t {
39+
mp_obj_base_t base;
40+
neopixel_strip_t strip;
41+
} neopixel_obj_t;
42+
43+
STATIC mp_obj_t neopixel_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
44+
(void)type_in;
45+
mp_arg_check_num(n_args, n_kw, 2, 2, false);
46+
47+
PinName pin = microbit_obj_get_pin(args[0])->name;
48+
mp_int_t num_pixels = mp_obj_get_int(args[1]);
49+
50+
if (num_pixels <= 0) {
51+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid number of pixels"));
52+
}
53+
54+
neopixel_obj_t *self = m_new_obj(neopixel_obj_t);
55+
self->base.type = &neopixel_type;
56+
neopixel_init(&self->strip, pin, num_pixels);
57+
58+
return self;
4259
}
43-
MP_DEFINE_CONST_FUN_OBJ_2(mod_neopixel_init_obj, mod_neopixel_init);
4460

45-
STATIC mp_obj_t mod_neopixel_clear(void) {
46-
neopixel_clear(MP_STATE_PORT(neopixel_strip));
47-
return mp_const_none;
61+
STATIC mp_obj_t neopixel_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
62+
neopixel_obj_t *self = (neopixel_obj_t*)self_in;
63+
mp_uint_t index = mp_get_index(self->base.type, self->strip.num_leds, index_in, false);
64+
if (value == MP_OBJ_NULL) {
65+
// delete item
66+
return MP_OBJ_NULL; // op not supported
67+
} else if (value == MP_OBJ_SENTINEL) {
68+
// load
69+
mp_obj_t rgb[3] = {
70+
MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.r),
71+
MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.g),
72+
MP_OBJ_NEW_SMALL_INT(self->strip.leds[index].simple.b),
73+
};
74+
return mp_obj_new_tuple(3, rgb);
75+
} else {
76+
// store
77+
mp_obj_t *rgb;
78+
mp_obj_get_array_fixed_n(value, 3, &rgb);
79+
mp_int_t r = mp_obj_get_int(rgb[0]);
80+
mp_int_t g = mp_obj_get_int(rgb[1]);
81+
mp_int_t b = mp_obj_get_int(rgb[2]);
82+
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
83+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid colour"));
84+
}
85+
neopixel_set_color(&self->strip, index, r, g, b);
86+
return mp_const_none;
87+
}
4888
}
49-
MP_DEFINE_CONST_FUN_OBJ_0(mod_neopixel_clear_obj, mod_neopixel_clear);
5089

51-
STATIC mp_obj_t mod_neopixel_show(void) {
52-
neopixel_show(MP_STATE_PORT(neopixel_strip));
90+
STATIC mp_obj_t neopixel_clear_(mp_obj_t self_in) {
91+
neopixel_obj_t *self = (neopixel_obj_t*)self_in;
92+
neopixel_clear(&self->strip);
5393
return mp_const_none;
5494
}
55-
MP_DEFINE_CONST_FUN_OBJ_0(mod_neopixel_show_obj, mod_neopixel_show);
95+
MP_DEFINE_CONST_FUN_OBJ_1(neopixel_clear_obj, neopixel_clear_);
5696

57-
STATIC mp_obj_t mod_neopixel_set_colour(mp_uint_t n_args, const mp_obj_t *args) {
58-
(void)n_args;
59-
neopixel_set_color(MP_STATE_PORT(neopixel_strip), mp_obj_get_int(args[0]), mp_obj_get_int(args[1]), mp_obj_get_int(args[2]), mp_obj_get_int(args[3]));
97+
STATIC mp_obj_t neopixel_show_(mp_obj_t self_in) {
98+
neopixel_obj_t *self = (neopixel_obj_t*)self_in;
99+
neopixel_show(&self->strip);
60100
return mp_const_none;
61101
}
62-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_neopixel_set_colour_obj, 4, 4, mod_neopixel_set_colour);
102+
MP_DEFINE_CONST_FUN_OBJ_1(neopixel_show_obj, neopixel_show_);
103+
104+
STATIC const mp_map_elem_t neopixel_locals_dict_table[] = {
105+
{ MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&neopixel_clear_obj },
106+
{ MP_OBJ_NEW_QSTR(MP_QSTR_show), (mp_obj_t)&neopixel_show_obj },
107+
};
108+
109+
STATIC MP_DEFINE_CONST_DICT(neopixel_locals_dict, neopixel_locals_dict_table);
110+
111+
const mp_obj_type_t neopixel_type = {
112+
{ &mp_type_type },
113+
.name = MP_QSTR_NeoPixel,
114+
.print = NULL,
115+
.make_new = neopixel_make_new,
116+
.call = NULL,
117+
.unary_op = NULL,
118+
.binary_op = NULL,
119+
.attr = NULL,
120+
.subscr = neopixel_subscr,
121+
.getiter = NULL,
122+
.iternext = NULL,
123+
.buffer_p = {NULL},
124+
.stream_p = NULL,
125+
.bases_tuple = MP_OBJ_NULL,
126+
.locals_dict = (mp_obj_t)&neopixel_locals_dict,
127+
};
63128

64129
STATIC const mp_map_elem_t neopixel_module_globals_table[] = {
65130
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_neopixel) },
66-
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&mod_neopixel_init_obj },
67-
{ MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&mod_neopixel_clear_obj },
68-
{ MP_OBJ_NEW_QSTR(MP_QSTR_show), (mp_obj_t)&mod_neopixel_show_obj },
69-
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_colour), (mp_obj_t)&mod_neopixel_set_colour_obj },
131+
{ MP_OBJ_NEW_QSTR(MP_QSTR_NeoPixel), (mp_obj_t)&neopixel_type },
70132
};
71133

72134
STATIC MP_DEFINE_CONST_DICT(neopixel_module_globals, neopixel_module_globals_table);

0 commit comments

Comments
 (0)