esp8266: Add APA102 serial individually controllable LEDs support.
APA102 is a new "smart LED", similar to WS2812 aka "Neopixel".
This commit is contained in:
parent
6fa60153ea
commit
a0a08b4be1
@ -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.apa102_write(clock_pin, data_pin, rgbi_buf)
|
||||
|
||||
WebREPL (web browser interactive prompt)
|
||||
----------------------------------------
|
||||
|
||||
|
@ -65,6 +65,7 @@ SRC_C = \
|
||||
esppwm.c \
|
||||
esponewire.c \
|
||||
espneopixel.c \
|
||||
espapa102.c \
|
||||
intr.c \
|
||||
modpyb.c \
|
||||
modpybpin.c \
|
||||
|
110
esp8266/espapa102.c
Normal file
110
esp8266/espapa102.c
Normal file
@ -0,0 +1,110 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include "c_types.h"
|
||||
#include "eagle_soc.h"
|
||||
#include "user_interface.h"
|
||||
#include "espapa102.h"
|
||||
|
||||
#define NOP asm volatile(" nop \n\t")
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
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++) {
|
||||
_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);
|
||||
}
|
||||
}
|
||||
|
||||
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 / 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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
27
esp8266/espapa102.h
Normal file
27
esp8266/espapa102.h
Normal file
@ -0,0 +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);
|
@ -43,6 +43,7 @@
|
||||
#include "spi_flash.h"
|
||||
#include "mem.h"
|
||||
#include "espneopixel.h"
|
||||
#include "espapa102.h"
|
||||
#include "modpyb.h"
|
||||
|
||||
#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)
|
||||
}
|
||||
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());
|
||||
}
|
||||
@ -679,6 +690,7 @@ 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
|
||||
|
28
esp8266/scripts/apa102.py
Normal file
28
esp8266/scripts/apa102.py
Normal file
@ -0,0 +1,28 @@
|
||||
# APA102 driver for MicroPython on ESP8266
|
||||
# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch
|
||||
|
||||
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)
|
Loading…
x
Reference in New Issue
Block a user