Skip to content

Commit 915b6aa

Browse files
committed
codal_port: Add "neopixel" module.
This module is frozen Python code, and backed by the low-level microbit.ws2821_write function. It should be compatible with micro:bit v1, and also includes support for RGBW LEDs.
1 parent e9bc1c8 commit 915b6aa

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

src/codal_port/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Makefile to build libmicropython.a
22

3+
FROZEN_MANIFEST ?= manifest.py
4+
35
CROSS_COMPILE = arm-none-eabi-
46
CFLAGS_EXTRA = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4
57

src/codal_port/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
freeze("modules", "neopixel.py", opt=3)

src/codal_port/modmicrobit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ STATIC mp_obj_t microbit_set_volume(mp_obj_t volume_in) {
8282
}
8383
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_set_volume_obj, microbit_set_volume);
8484

85+
STATIC mp_obj_t microbit_ws2812_write(mp_obj_t pin_in, mp_obj_t buf_in) {
86+
uint8_t pin = microbit_obj_get_pin(pin_in)->name;
87+
mp_buffer_info_t bufinfo;
88+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
89+
microbit_hal_pin_write_ws2812(pin, bufinfo.buf, bufinfo.len);
90+
return mp_const_none;
91+
}
92+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(microbit_ws2812_write_obj, microbit_ws2812_write);
93+
8594
STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = {
8695
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_microbit) },
8796

@@ -104,6 +113,7 @@ STATIC const mp_rom_map_elem_t microbit_module_globals_table[] = {
104113
{ MP_ROM_QSTR(MP_QSTR_panic), MP_ROM_PTR(&microbit_panic_obj) },
105114
{ MP_ROM_QSTR(MP_QSTR_temperature), MP_ROM_PTR(&microbit_temperature_obj) },
106115
{ MP_ROM_QSTR(MP_QSTR_set_volume), MP_ROM_PTR(&microbit_set_volume_obj) },
116+
{ MP_ROM_QSTR(MP_QSTR_ws2812_write), MP_ROM_PTR(&microbit_ws2812_write_obj) },
107117

108118
{ MP_ROM_QSTR(MP_QSTR_pin0), MP_ROM_PTR(&microbit_p0_obj) },
109119
{ MP_ROM_QSTR(MP_QSTR_pin1), MP_ROM_PTR(&microbit_p1_obj) },

src/codal_port/modules/neopixel.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# NeoPixel driver for MicroPython
2+
# MIT license; Copyright (c) 2016-2020 Damien P. George
3+
4+
from microbit import ws2812_write
5+
6+
7+
class NeoPixel:
8+
ORDER = (1, 0, 2, 3)
9+
10+
def __init__(self, pin, n, bpp=3):
11+
self.pin = pin
12+
self.n = n
13+
self.bpp = bpp
14+
self.buf = bytearray(n * bpp)
15+
16+
def __setitem__(self, index, val):
17+
offset = index * self.bpp
18+
for i in range(self.bpp):
19+
self.buf[offset + self.ORDER[i]] = val[i]
20+
21+
def __getitem__(self, index):
22+
offset = index * self.bpp
23+
return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp))
24+
25+
def fill(self, color):
26+
for i in range(self.n):
27+
self[i] = color
28+
29+
def write(self):
30+
ws2812_write(self.pin, self.buf)
31+
32+
# microbit v1 methods
33+
34+
def __len__(self):
35+
return self.n
36+
37+
def clear(self):
38+
for i in range(len(self.buf)):
39+
self.buf[i] = 0
40+
self.write()
41+
42+
show = write

src/codal_port/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
5555
#define MICROPY_MODULE_BUILTIN_INIT (1)
5656
#define MICROPY_MODULE_WEAK_LINKS (1)
57+
#define MICROPY_MODULE_FROZEN_MPY (1)
58+
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
5759
#define MICROPY_ENABLE_SCHEDULER (1)
5860

5961
// Fine control over Python builtins, classes, modules, etc

0 commit comments

Comments
 (0)