Skip to content

Commit a0a08b4

Browse files
heyjuvipfalcon
authored andcommitted
esp8266: Add APA102 serial individually controllable LEDs support.
APA102 is a new "smart LED", similar to WS2812 aka "Neopixel".
1 parent 6fa6015 commit a0a08b4

6 files changed

Lines changed: 198 additions & 0 deletions

File tree

docs/esp8266/quickref.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ For low-level driving of a NeoPixel::
271271
import esp
272272
esp.neopixel_write(pin, grb_buf, is800khz)
273273

274+
APA102 driver
275+
-------------
276+
277+
Use the ``apa102`` module::
278+
279+
from machine import Pin
280+
from apa102 import APA102
281+
282+
clock = Pin(14, Pin.OUT) # set GPIO14 to output to drive the clock
283+
data = Pin(13, Pin.OUT) # set GPIO13 to output to drive the data
284+
apa = APA102(clock, data, 8) # create APA102 driver on the clock and the data pin for 8 pixels
285+
apa[0] = (255, 255, 255, 31) # set the first pixel to white with a maximum brightness of 31
286+
apa.write() # write data to all pixels
287+
r, g, b, brightness = apa[0] # get first pixel colour
288+
289+
For low-level driving of an APA102::
290+
291+
import esp
292+
esp.apa102_write(clock_pin, data_pin, rgbi_buf)
293+
274294
WebREPL (web browser interactive prompt)
275295
----------------------------------------
276296

esp8266/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ SRC_C = \
6565
esppwm.c \
6666
esponewire.c \
6767
espneopixel.c \
68+
espapa102.c \
6869
intr.c \
6970
modpyb.c \
7071
modpybpin.c \

esp8266/espapa102.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Robert Foss, Daniel Busch
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include "c_types.h"
29+
#include "eagle_soc.h"
30+
#include "user_interface.h"
31+
#include "espapa102.h"
32+
33+
#define NOP asm volatile(" nop \n\t")
34+
35+
static inline void _esp_apa102_send_byte(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t byte) {
36+
for (uint32_t i = 0; i < 8; i++) {
37+
if (byte & 0x80) {
38+
// set data pin high
39+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask);
40+
} else {
41+
// set data pin low
42+
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, dataPinMask);
43+
}
44+
45+
// set clock pin high
46+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask);
47+
byte <<= 1;
48+
NOP;
49+
NOP;
50+
51+
// set clock pin low
52+
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask);
53+
NOP;
54+
NOP;
55+
}
56+
}
57+
58+
static inline void _esp_apa102_send_colors(uint32_t clockPinMask, uint32_t dataPinMask, uint8_t *pixels, uint32_t numBytes) {
59+
for (uint32_t i = 0; i < numBytes / 4; i++) {
60+
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 3] | 0xE0);
61+
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 2]);
62+
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4 + 1]);
63+
_esp_apa102_send_byte(clockPinMask, dataPinMask, pixels[i * 4]);
64+
}
65+
}
66+
67+
static inline void _esp_apa102_start_frame(uint32_t clockPinMask, uint32_t dataPinMask) {
68+
for (uint32_t i = 0; i < 4; i++) {
69+
_esp_apa102_send_byte(clockPinMask, dataPinMask, 0x00);
70+
}
71+
}
72+
73+
static inline void _esp_apa102_append_additionial_cycles(uint32_t clockPinMask, uint32_t dataPinMask, uint32_t numBytes) {
74+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, dataPinMask);
75+
76+
// we need to write some more clock cycles, because each led
77+
// delays the data by one edge after inverting the clock
78+
for (uint32_t i = 0; i < numBytes / 8 + ((numBytes / 4) % 2); i++) {
79+
GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, clockPinMask);
80+
NOP;
81+
NOP;
82+
83+
GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, clockPinMask);
84+
NOP;
85+
NOP;
86+
}
87+
}
88+
89+
static inline void _esp_apa102_end_frame(uint32_t clockPinMask, uint32_t dataPinMask) {
90+
for (uint32_t i = 0; i < 4; i++) {
91+
_esp_apa102_send_byte(clockPinMask, dataPinMask, 0xFF);
92+
}
93+
}
94+
95+
void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes) {
96+
uint32_t clockPinMask, dataPinMask;
97+
98+
clockPinMask = 1 << clockPin;
99+
dataPinMask = 1 << dataPin;
100+
101+
// start the frame
102+
_esp_apa102_start_frame(clockPinMask, dataPinMask);
103+
104+
// write pixels
105+
_esp_apa102_send_colors(clockPinMask, dataPinMask, pixels, numBytes);
106+
107+
// end the frame
108+
_esp_apa102_append_additionial_cycles(clockPinMask, dataPinMask, numBytes);
109+
_esp_apa102_end_frame(clockPinMask, dataPinMask);
110+
}

esp8266/espapa102.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Robert Foss, Daniel Busch
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
void esp_apa102_write(uint8_t clockPin, uint8_t dataPin, uint8_t *pixels, uint32_t numBytes);

esp8266/modesp.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "spi_flash.h"
4444
#include "mem.h"
4545
#include "espneopixel.h"
46+
#include "espapa102.h"
4647
#include "modpyb.h"
4748

4849
#define MODESP_ESPCONN (0)
@@ -636,6 +637,16 @@ STATIC mp_obj_t esp_neopixel_write_(mp_obj_t pin, mp_obj_t buf, mp_obj_t is800k)
636637
}
637638
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_neopixel_write_obj, esp_neopixel_write_);
638639

640+
STATIC mp_obj_t esp_apa102_write_(mp_obj_t clockPin, mp_obj_t dataPin, mp_obj_t buf) {
641+
mp_buffer_info_t bufinfo;
642+
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
643+
esp_apa102_write(mp_obj_get_pin_obj(clockPin)->phys_port,
644+
mp_obj_get_pin_obj(dataPin)->phys_port,
645+
(uint8_t*)bufinfo.buf, bufinfo.len);
646+
return mp_const_none;
647+
}
648+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(esp_apa102_write_obj, esp_apa102_write_);
649+
639650
STATIC mp_obj_t esp_freemem() {
640651
return MP_OBJ_NEW_SMALL_INT(system_get_free_heap_size());
641652
}
@@ -679,6 +690,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = {
679690
{ MP_OBJ_NEW_QSTR(MP_QSTR_getaddrinfo), (mp_obj_t)&esp_getaddrinfo_obj },
680691
#endif
681692
{ MP_OBJ_NEW_QSTR(MP_QSTR_neopixel_write), (mp_obj_t)&esp_neopixel_write_obj },
693+
{ MP_OBJ_NEW_QSTR(MP_QSTR_apa102_write), (mp_obj_t)&esp_apa102_write_obj },
682694
{ MP_OBJ_NEW_QSTR(MP_QSTR_freemem), (mp_obj_t)&esp_freemem_obj },
683695
{ MP_OBJ_NEW_QSTR(MP_QSTR_meminfo), (mp_obj_t)&esp_meminfo_obj },
684696
{ MP_OBJ_NEW_QSTR(MP_QSTR_info), (mp_obj_t)&pyb_info_obj }, // TODO delete/rename/move elsewhere

esp8266/scripts/apa102.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# APA102 driver for MicroPython on ESP8266
2+
# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch
3+
4+
from esp import apa102_write
5+
6+
class APA102:
7+
def __init__(self, clock_pin, data_pin, n):
8+
self.clock_pin = clock_pin
9+
self.data_pin = data_pin
10+
self.n = n
11+
self.buf = bytearray(n * 4)
12+
13+
self.clock_pin.init(clock_pin.OUT)
14+
self.data_pin.init(data_pin.OUT)
15+
16+
def __setitem__(self, index, val):
17+
r, g, b, brightness = val
18+
self.buf[index * 4] = r
19+
self.buf[index * 4 + 1] = g
20+
self.buf[index * 4 + 2] = b
21+
self.buf[index * 4 + 3] = brightness
22+
23+
def __getitem__(self, index):
24+
i = index * 4
25+
return self.buf[i], self.buf[i + 1], self.buf[i + 2], self.buf[i + 3]
26+
27+
def write(self):
28+
apa102_write(self.clock_pin, self.data_pin, self.buf)

0 commit comments

Comments
 (0)