From 30ef58ab01be4ee5b7a312147236f6bcdf143996 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Mon, 28 Mar 2016 01:58:14 +0200 Subject: [PATCH 1/8] esp8266/apa102: Add APA102 support --- esp8266/Makefile | 1 + esp8266/espapa102.c | 63 ++++++++++++++++++++++++++++++++++++++ esp8266/espapa102.h | 1 + esp8266/modesp.c | 12 ++++++++ esp8266/tests/apa102.py | 67 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 esp8266/espapa102.c create mode 100644 esp8266/espapa102.h create mode 100644 esp8266/tests/apa102.py diff --git a/esp8266/Makefile b/esp8266/Makefile index d8786301a800..f9a4c49d6699 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -66,6 +66,7 @@ SRC_C = \ esponewire.c \ espneopixel.c \ intr.c \ + espapa102.c \ modpyb.c \ modpybpin.c \ modpybpwm.c \ diff --git a/esp8266/espapa102.c b/esp8266/espapa102.c new file mode 100644 index 000000000000..d535fc80bbdf --- /dev/null +++ b/esp8266/espapa102.c @@ -0,0 +1,63 @@ +// Original version from https://github.com/adafruit/Adafruit_NeoPixel +// Modifications by dpgeorge to support auto-CPU-frequency detection + +// This is a mash-up of the Due show() code + insights from Michael Miller's +// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus +// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution. + +#include +#include "c_types.h" +#include "eagle_soc.h" +#include "user_interface.h" +#include "espapa102.h" + +void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes) { + uint32_t clockPinMask, dataPinMask; + + clockPinMask = 1 << clockPin; + dataPinMask = 1 << dataPin; + + // start the frame + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); + + for (uint32_t i = 0; i < 32; i++) { + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + } + + // write pixels + for (uint32_t i = 0; i < numBytes / 4; i++) { + for (uint32_t j = 0; j < 4; j++) { + uint8_t byte = pixels[i * 4 + (3 - j)] | (j == 0 ? 0xE0 : 0x00); + + for (uint32_t k = 0; k < 8; k++) { + uint32_t dataHigh = (byte >> (7 - k)) & 0x01; + + if (dataHigh) { + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); + } + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + if (dataHigh) { + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); + } + } + } + } + + // end the frame + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); + + // we need to write some more clock cycles, because each led + // delays the data by one edge after inverting the clock + for (uint32_t i = 0; i < numBytes / 4 - 1; i++) { + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + } + + for (uint32_t i = 0; i < 32; i++) { + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + } +} diff --git a/esp8266/espapa102.h b/esp8266/espapa102.h new file mode 100644 index 000000000000..4e72667b3363 --- /dev/null +++ b/esp8266/espapa102.h @@ -0,0 +1 @@ +void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes); diff --git a/esp8266/modesp.c b/esp8266/modesp.c index 885e7ae95eeb..f28de424bf49 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -43,6 +43,7 @@ #include "spi_flash.h" #include "mem.h" #include "espneopixel.h" +#include "espapa102.h" #include "modpyb.h" #define MODESP_ESPCONN (0) @@ -663,6 +664,16 @@ STATIC mp_obj_t esp_esf_free_bufs(mp_obj_t idx_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_esf_free_bufs_obj, esp_esf_free_bufs); +STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + esp_apa102_write(mp_obj_get_pin_obj(clockPin)->phys_port, + mp_obj_get_pin_obj(dataPin)->phys_port, + (uint8_t*)bufinfo.buf, bufinfo.len); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_apa102_write_obj, esp_apa102_write_); + STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) }, @@ -685,6 +696,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_malloc), (mp_obj_t)&esp_malloc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_free), (mp_obj_t)&esp_free_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_esf_free_bufs), (mp_obj_t)&esp_esf_free_bufs_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_apa102_write), (mp_obj_t)&esp_apa102_write_obj }, #if MODESP_INCLUDE_CONSTANTS { MP_OBJ_NEW_QSTR(MP_QSTR_SLEEP_NONE), diff --git a/esp8266/tests/apa102.py b/esp8266/tests/apa102.py new file mode 100644 index 000000000000..83f18d0dbc67 --- /dev/null +++ b/esp8266/tests/apa102.py @@ -0,0 +1,67 @@ +import time +import machine +from esp import apa102_write + +class APA102: + def __init__(self, clock_pin, data_pin, n): + self.clock_pin = clock_pin + self.data_pin = data_pin + self.n = n + self.buf = bytearray(n * 4) + + def __setitem__(self, index, val): + r, g, b, brightness = val + self.buf[index * 4] = r + self.buf[index * 4 + 1] = g + self.buf[index * 4 + 2] = b + self.buf[index * 4 + 3] = brightness + + def __getitem__(self, index): + i = index * 4 + return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3] + + def write(self): + apa102_write(self.clock_pin, self.data_pin, self.buf) + +def test(): + # put a neopixel strip on GPIO14 as clock and GPIO13 as data pin + c = machine.Pin(14, machine.Pin.OUT) + d = machine.Pin(13, machine.Pin.OUT) + np = APA102(c, d, 8) + n = np.n + + # cycle + for i in range(4 * n): + for j in range(n): + np[j] = (0, 0, 0, 0) + np[i % n] = (255, 255, 255, 31) + np.write() + time.sleep_ms(25) + + # bounce + for i in range(4 * n): + for j in range(n): + np[j] = (0, 0, 128, 31) + if (i // n) % 2 == 0: + np[i % n] = (0, 0, 0, 0) + else: + np[n - 1 - (i % n)] = (0, 0, 0, 0) + np.write() + time.sleep_ms(60) + + # fade in/out + for i in range(0, 4 * 256, 8): + for j in range(n): + if (i // 256) % 2 == 0: + val = i & 0xff + else: + val = 255 - (i & 0xff) + np[j] = (val, 0, 0, 31) + np.write() + + # clear + for i in range(n): + np[i] = (0, 0, 0, 0) + np.write() + +test() From 1e59bb8b16f73aef66f0b9f891f53cc4ebc38d88 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Sun, 17 Apr 2016 23:53:33 +0200 Subject: [PATCH 2/8] esp8266/apa102: Rework code, especially make use of parts of pull request #1950 --- esp8266/espapa102.c | 97 +++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/esp8266/espapa102.c b/esp8266/espapa102.c index d535fc80bbdf..e3bb08ee3b7c 100644 --- a/esp8266/espapa102.c +++ b/esp8266/espapa102.c @@ -1,63 +1,84 @@ -// Original version from https://github.com/adafruit/Adafruit_NeoPixel -// Modifications by dpgeorge to support auto-CPU-frequency detection - -// This is a mash-up of the Due show() code + insights from Michael Miller's -// ESP8266 work for the NeoPixelBus library: github.com/Makuna/NeoPixelBus -// Needs to be a separate .c file to enforce ICACHE_RAM_ATTR execution. - #include #include "c_types.h" #include "eagle_soc.h" #include "user_interface.h" #include "espapa102.h" -void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes) { - uint32_t clockPinMask, dataPinMask; +#define NOP asm volatile(" nop \n\t") - clockPinMask = 1 << clockPin; - dataPinMask = 1 << dataPin; - - // start the frame - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); +static inline void _esp_apa102_send_byte(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t byte) { + for (uint32_t i = 0; i < 8; i++) { + if (byte & 0x80) { + // set data pin high + GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); + } else { + // set data pin low + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); + } - for (uint32_t i = 0; i < 32; i++) { + // set clock pin high GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + byte <<= 1; + NOP; + NOP; + + // set clock pin low GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + NOP; + NOP; } +} - // write pixels +static inline void _esp_apa102_send_colors(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t *pixels, uint32_t numBytes) { for (uint32_t i = 0; i < numBytes / 4; i++) { - for (uint32_t j = 0; j < 4; j++) { - uint8_t byte = pixels[i * 4 + (3 - j)] | (j == 0 ? 0xE0 : 0x00); - - for (uint32_t k = 0; k < 8; k++) { - uint32_t dataHigh = (byte >> (7 - k)) & 0x01; - - if (dataHigh) { - GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); - } - GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); - - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); - if (dataHigh) { - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask); - } - } - } + _esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 3] | 0xE0); + _esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 2]); + _esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 1]); + _esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4]); + } +} + +static inline void _esp_apa102_start_frame(uint32_t clockPinMask, uint32_t dataPinMask) { + for (uint32_t i = 0; i < 4; i++) { + _esp_apa102_send_byte(clockPinMask, dataPinMask, 0x00); } +} - // end the frame +static inline void _esp_apa102_append_additionial_cycles(uint32_t clockPinMask, uint32_t dataPinMask, uint32_t numBytes) { GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask); // we need to write some more clock cycles, because each led // delays the data by one edge after inverting the clock - for (uint32_t i = 0; i < numBytes / 4 - 1; i++) { + for (uint32_t i = 0; i < numBytes / 8 + ((numBytes / 4) % 2); i++) { GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); + NOP; + NOP; + GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); + NOP; + NOP; } +} - for (uint32_t i = 0; i < 32; i++) { - GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask); - GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask); +static inline void _esp_apa102_end_frame(uint32_t clockPinMask, uint32_t dataPinMask) { + for (uint32_t i = 0; i < 4; i++) { + _esp_apa102_send_byte(clockPinMask, dataPinMask, 0xFF); } } + +void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes) { + uint32_t clockPinMask, dataPinMask; + + clockPinMask = 1 << clockPin; + dataPinMask = 1 << dataPin; + + // start the frame + _esp_apa102_start_frame(clockPinMask, dataPinMask); + + // write pixels + _esp_apa102_send_colors(clockPinMask, dataPinMask, pixels, numBytes); + + // end the frame + _esp_apa102_append_additionial_cycles(clockPinMask, dataPinMask, numBytes); + _esp_apa102_end_frame(clockPinMask, dataPinMask); +} From 0c85f9333f7a311e30cd8de88a6d5dfe486e5661 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Mon, 18 Apr 2016 00:14:12 +0200 Subject: [PATCH 3/8] esp8266/apa102: Add docs --- docs/esp8266/quickref.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index d2d677dc7931..233d3a28fc6b 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -271,6 +271,26 @@ For low-level driving of a NeoPixel:: import esp esp.neopixel_write(pin, grb_buf, is800khz) +APA102 driver +--------------- + +Use the ``apa102`` module:: + + from machine import Pin + from apa102 import APA102 + + clock = Pin(14, Pin.OUT) # set GPIO14 to output to drive the clock + data = Pin(13, Pin.OUT) # set GPIO13 to output to drive the data + apa = APA102(clock, data, 8) # create APA102 driver on the clock and the data pin for 8 pixels + apa[0] = (255, 255, 255, 31) # set the first pixel to white with a maximum brightness of 31 + apa.write() # write data to all pixels + r, g, b, brightness = apa[0] # get first pixel colour + +For low-level driving of an APA102:: + + import esp + esp.neopixel_write(clock_pin, data_pin, rgbi_buf) + WebREPL (web browser interactive prompt) ---------------------------------------- From fcb53e8f83f80043bcedcf3b7ba39251ec8e4ca0 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Mon, 18 Apr 2016 00:15:17 +0200 Subject: [PATCH 4/8] esp8266/apa102: Rename and fix some things in the apa102 script --- docs/esp8266/quickref.rst | 2 +- esp8266/scripts/apa102.py | 27 +++++++++++++++++++++++++++ esp8266/tests/apa102.py | 27 ++++++++++++++------------- 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 esp8266/scripts/apa102.py diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index 233d3a28fc6b..d7c67548d3d3 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -272,7 +272,7 @@ For low-level driving of a NeoPixel:: esp.neopixel_write(pin, grb_buf, is800khz) APA102 driver ---------------- +------------- Use the ``apa102`` module:: diff --git a/esp8266/scripts/apa102.py b/esp8266/scripts/apa102.py new file mode 100644 index 000000000000..21bca3709d71 --- /dev/null +++ b/esp8266/scripts/apa102.py @@ -0,0 +1,27 @@ +# APA102 driver for MicroPython on ESP8266 + +from esp import apa102_write + +class APA102: + def __init__(self, clock_pin, data_pin, n): + self.clock_pin = clock_pin + self.data_pin = data_pin + self.n = n + self.buf = bytearray(n * 4) + + self.clock_pin.init(clock_pin.OUT) + self.data_pin.init(data_pin.OUT) + + def __setitem__(self, index, val): + r, g, b, brightness = val + self.buf[index * 4] = r + self.buf[index * 4 + 1] = g + self.buf[index * 4 + 2] = b + self.buf[index * 4 + 3] = brightness + + def __getitem__(self, index): + i = index * 4 + return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3] + + def write(self): + apa102_write(self.clock_pin, self.data_pin, self.buf) diff --git a/esp8266/tests/apa102.py b/esp8266/tests/apa102.py index 83f18d0dbc67..513622a8081d 100644 --- a/esp8266/tests/apa102.py +++ b/esp8266/tests/apa102.py @@ -27,26 +27,26 @@ def test(): # put a neopixel strip on GPIO14 as clock and GPIO13 as data pin c = machine.Pin(14, machine.Pin.OUT) d = machine.Pin(13, machine.Pin.OUT) - np = APA102(c, d, 8) - n = np.n + apa = APA102(c, d, 8) + n = apa.n # cycle for i in range(4 * n): for j in range(n): - np[j] = (0, 0, 0, 0) - np[i % n] = (255, 255, 255, 31) - np.write() + apa[j] = (0, 0, 0, 0) + apa[i % n] = (255, 255, 255, 31) + apa.write() time.sleep_ms(25) # bounce for i in range(4 * n): for j in range(n): - np[j] = (0, 0, 128, 31) + apa[j] = (0, 0, 128, 31) if (i // n) % 2 == 0: - np[i % n] = (0, 0, 0, 0) + apa[i % n] = (0, 0, 0, 0) else: - np[n - 1 - (i % n)] = (0, 0, 0, 0) - np.write() + apa[n - 1 - (i % n)] = (0, 0, 0, 0) + apa.write() time.sleep_ms(60) # fade in/out @@ -56,12 +56,13 @@ def test(): val = i & 0xff else: val = 255 - (i & 0xff) - np[j] = (val, 0, 0, 31) - np.write() + apa[j] = (val, 0, 0, 31) + apa.write() # clear for i in range(n): - np[i] = (0, 0, 0, 0) - np.write() + apa[i] = (0, 0, 0, 0) + + apa.write() test() From 0467fd055030c61de7144e75d06c4efa7a963fa1 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Fri, 6 May 2016 16:55:31 +0200 Subject: [PATCH 5/8] esp8266/apa102: Correct function name to apa102_write in the quickref doc --- docs/esp8266/quickref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/esp8266/quickref.rst b/docs/esp8266/quickref.rst index d7c67548d3d3..7b246a65e1be 100644 --- a/docs/esp8266/quickref.rst +++ b/docs/esp8266/quickref.rst @@ -289,7 +289,7 @@ Use the ``apa102`` module:: For low-level driving of an APA102:: import esp - esp.neopixel_write(clock_pin, data_pin, rgbi_buf) + esp.apa102_write(clock_pin, data_pin, rgbi_buf) WebREPL (web browser interactive prompt) ---------------------------------------- From 0fb0446a96d4afcc6f3e696ca36100ec170617f6 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Fri, 6 May 2016 19:20:07 +0200 Subject: [PATCH 6/8] esp8266/apa102: Remove apa102.py from tests --- esp8266/tests/apa102.py | 68 ----------------------------------------- 1 file changed, 68 deletions(-) delete mode 100644 esp8266/tests/apa102.py diff --git a/esp8266/tests/apa102.py b/esp8266/tests/apa102.py deleted file mode 100644 index 513622a8081d..000000000000 --- a/esp8266/tests/apa102.py +++ /dev/null @@ -1,68 +0,0 @@ -import time -import machine -from esp import apa102_write - -class APA102: - def __init__(self, clock_pin, data_pin, n): - self.clock_pin = clock_pin - self.data_pin = data_pin - self.n = n - self.buf = bytearray(n * 4) - - def __setitem__(self, index, val): - r, g, b, brightness = val - self.buf[index * 4] = r - self.buf[index * 4 + 1] = g - self.buf[index * 4 + 2] = b - self.buf[index * 4 + 3] = brightness - - def __getitem__(self, index): - i = index * 4 - return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3] - - def write(self): - apa102_write(self.clock_pin, self.data_pin, self.buf) - -def test(): - # put a neopixel strip on GPIO14 as clock and GPIO13 as data pin - c = machine.Pin(14, machine.Pin.OUT) - d = machine.Pin(13, machine.Pin.OUT) - apa = APA102(c, d, 8) - n = apa.n - - # cycle - for i in range(4 * n): - for j in range(n): - apa[j] = (0, 0, 0, 0) - apa[i % n] = (255, 255, 255, 31) - apa.write() - time.sleep_ms(25) - - # bounce - for i in range(4 * n): - for j in range(n): - apa[j] = (0, 0, 128, 31) - if (i // n) % 2 == 0: - apa[i % n] = (0, 0, 0, 0) - else: - apa[n - 1 - (i % n)] = (0, 0, 0, 0) - apa.write() - time.sleep_ms(60) - - # fade in/out - for i in range(0, 4 * 256, 8): - for j in range(n): - if (i // 256) % 2 == 0: - val = i & 0xff - else: - val = 255 - (i & 0xff) - apa[j] = (val, 0, 0, 31) - apa.write() - - # clear - for i in range(n): - apa[i] = (0, 0, 0, 0) - - apa.write() - -test() From 76a5bfe0eca178e168503a314cdc37458853f261 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Fri, 6 May 2016 19:22:01 +0200 Subject: [PATCH 7/8] esp8266/apa102: Move things to the correct places --- esp8266/Makefile | 2 +- esp8266/modesp.c | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/esp8266/Makefile b/esp8266/Makefile index f9a4c49d6699..2d65b4bd6dae 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -65,8 +65,8 @@ SRC_C = \ esppwm.c \ esponewire.c \ espneopixel.c \ - intr.c \ espapa102.c \ + intr.c \ modpyb.c \ modpybpin.c \ modpybpwm.c \ diff --git a/esp8266/modesp.c b/esp8266/modesp.c index f28de424bf49..4b0c0e4668b1 100644 --- a/esp8266/modesp.c +++ b/esp8266/modesp.c @@ -637,6 +637,16 @@ STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t is800k) } STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_); +STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + esp_apa102_write(mp_obj_get_pin_obj(clockPin)->phys_port, + mp_obj_get_pin_obj(dataPin)->phys_port, + (uint8_t*)bufinfo.buf, bufinfo.len); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_apa102_write_obj, esp_apa102_write_); + STATIC mp_obj_t esp_freemem() { return MP_OBJ_NEW_SMALL_INT(system_get_free_heap_size()); } @@ -664,16 +674,6 @@ STATIC mp_obj_t esp_esf_free_bufs(mp_obj_t idx_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_esf_free_bufs_obj, esp_esf_free_bufs); -STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) { - mp_buffer_info_t bufinfo; - mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); - esp_apa102_write(mp_obj_get_pin_obj(clockPin)->phys_port, - mp_obj_get_pin_obj(dataPin)->phys_port, - (uint8_t*)bufinfo.buf, bufinfo.len); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_apa102_write_obj, esp_apa102_write_); - STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) }, @@ -690,13 +690,13 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_getaddrinfo_obj }, #endif { MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write), (mp_obj_t)&esp_neopixel_write_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_apa102_write), (mp_obj_t)&esp_apa102_write_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_freemem), (mp_obj_t)&esp_freemem_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_meminfo), (mp_obj_t)&esp_meminfo_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, // TODO delete/rename/move elsewhere { MP_OBJ_NEW_QSTR(MP_QSTR_malloc), (mp_obj_t)&esp_malloc_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_free), (mp_obj_t)&esp_free_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_esf_free_bufs), (mp_obj_t)&esp_esf_free_bufs_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_apa102_write), (mp_obj_t)&esp_apa102_write_obj }, #if MODESP_INCLUDE_CONSTANTS { MP_OBJ_NEW_QSTR(MP_QSTR_SLEEP_NONE), From 66dc7b9e514c49950a88a9ee214267631b6c7304 Mon Sep 17 00:00:00 2001 From: misterdanb Date: Sun, 8 May 2016 20:52:18 +0200 Subject: [PATCH 8/8] esp8266/apa102: Add license information --- esp8266/espapa102.c | 26 ++++++++++++++++++++++++++ esp8266/espapa102.h | 26 ++++++++++++++++++++++++++ esp8266/scripts/apa102.py | 1 + 3 files changed, 53 insertions(+) diff --git a/esp8266/espapa102.c b/esp8266/espapa102.c index e3bb08ee3b7c..e5f3024b1071 100644 --- a/esp8266/espapa102.c +++ b/esp8266/espapa102.c @@ -1,3 +1,29 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Robert Foss, Daniel Busch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + #include #include "c_types.h" #include "eagle_soc.h" diff --git a/esp8266/espapa102.h b/esp8266/espapa102.h index 4e72667b3363..82c92025d389 100644 --- a/esp8266/espapa102.h +++ b/esp8266/espapa102.h @@ -1 +1,27 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Robert Foss, Daniel Busch + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes); diff --git a/esp8266/scripts/apa102.py b/esp8266/scripts/apa102.py index 21bca3709d71..126448cc2059 100644 --- a/esp8266/scripts/apa102.py +++ b/esp8266/scripts/apa102.py @@ -1,4 +1,5 @@ # APA102 driver for MicroPython on ESP8266 +# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch from esp import apa102_write