Revert "Update Adafruit_NeoPixel library to commit fd74287"

This commit is contained in:
Daniel Öster 2025-03-26 23:55:28 +02:00 committed by GitHub
parent 6824f0633a
commit 6f59924b4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 3945 additions and 4326 deletions

View file

@ -33,390 +33,380 @@
* *
*/ */
#ifndef ADAFRUIT_NEOPIXEL_H #ifndef ADAFRUIT_NEOPIXEL_H
#define ADAFRUIT_NEOPIXEL_H #define ADAFRUIT_NEOPIXEL_H
#ifdef ARDUINO #ifdef ARDUINO
#if (ARDUINO >= 100) #if (ARDUINO >= 100)
#include <Arduino.h> #include <Arduino.h>
#else #else
#include <WProgram.h> #include <WProgram.h>
#include <pins_arduino.h> #include <pins_arduino.h>
#endif #endif
#ifdef USE_TINYUSB // For Serial when selecting TinyUSB #ifdef USE_TINYUSB // For Serial when selecting TinyUSB
#include <Adafruit_TinyUSB.h> #include <Adafruit_TinyUSB.h>
#endif #endif
#endif #endif
#ifdef TARGET_LPC1768 #ifdef TARGET_LPC1768
#include <Arduino.h> #include <Arduino.h>
#endif #endif
#if defined(ARDUINO_ARCH_RP2040) #if defined(ARDUINO_ARCH_RP2040)
#include <stdlib.h> #include <stdlib.h>
#include "hardware/pio.h" #include "hardware/pio.h"
#include "hardware/clocks.h" #include "hardware/clocks.h"
#include "rp2040_pio.h" #include "rp2040_pio.h"
#endif #endif
// The order of primary colors in the NeoPixel data stream can vary among // The order of primary colors in the NeoPixel data stream can vary among
// device types, manufacturers and even different revisions of the same // device types, manufacturers and even different revisions of the same
// item. The third parameter to the Adafruit_NeoPixel constructor encodes // item. The third parameter to the Adafruit_NeoPixel constructor encodes
// the per-pixel byte offsets of the red, green and blue primaries (plus // the per-pixel byte offsets of the red, green and blue primaries (plus
// white, if present) in the data stream -- the following #defines provide // white, if present) in the data stream -- the following #defines provide
// an easier-to-use named version for each permutation. e.g. NEO_GRB // an easier-to-use named version for each permutation. e.g. NEO_GRB
// indicates a NeoPixel-compatible device expecting three bytes per pixel, // indicates a NeoPixel-compatible device expecting three bytes per pixel,
// with the first byte transmitted containing the green value, second // with the first byte transmitted containing the green value, second
// containing red and third containing blue. The in-memory representation // containing red and third containing blue. The in-memory representation
// of a chain of NeoPixels is the same as the data-stream order; no // of a chain of NeoPixels is the same as the data-stream order; no
// re-ordering of bytes is required when issuing data to the chain. // re-ordering of bytes is required when issuing data to the chain.
// Most of these values won't exist in real-world devices, but it's done // Most of these values won't exist in real-world devices, but it's done
// this way so we're ready for it (also, if using the WS2811 driver IC, // this way so we're ready for it (also, if using the WS2811 driver IC,
// one might have their pixels set up in any weird permutation). // one might have their pixels set up in any weird permutation).
// Bits 5,4 of this value are the offset (0-3) from the first byte of a // Bits 5,4 of this value are the offset (0-3) from the first byte of a
// pixel to the location of the red color byte. Bits 3,2 are the green // pixel to the location of the red color byte. Bits 3,2 are the green
// offset and 1,0 are the blue offset. If it is an RGBW-type device // offset and 1,0 are the blue offset. If it is an RGBW-type device
// (supporting a white primary in addition to R,G,B), bits 7,6 are the // (supporting a white primary in addition to R,G,B), bits 7,6 are the
// offset to the white byte...otherwise, bits 7,6 are set to the same value // offset to the white byte...otherwise, bits 7,6 are set to the same value
// as 5,4 (red) to indicate an RGB (not RGBW) device. // as 5,4 (red) to indicate an RGB (not RGBW) device.
// i.e. binary representation: // i.e. binary representation:
// 0bWWRRGGBB for RGBW devices // 0bWWRRGGBB for RGBW devices
// 0bRRRRGGBB for RGB // 0bRRRRGGBB for RGB
// RGB NeoPixel permutations; white and red offsets are always same // RGB NeoPixel permutations; white and red offsets are always same
// Offset: W R G B // Offset: W R G B
#define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) ///< Transmit as R,G,B #define NEO_RGB ((0 << 6) | (0 << 4) | (1 << 2) | (2)) ///< Transmit as R,G,B
#define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) ///< Transmit as R,B,G #define NEO_RBG ((0 << 6) | (0 << 4) | (2 << 2) | (1)) ///< Transmit as R,B,G
#define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) ///< Transmit as G,R,B #define NEO_GRB ((1 << 6) | (1 << 4) | (0 << 2) | (2)) ///< Transmit as G,R,B
#define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,R #define NEO_GBR ((2 << 6) | (2 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,R
#define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) ///< Transmit as B,R,G #define NEO_BRG ((1 << 6) | (1 << 4) | (2 << 2) | (0)) ///< Transmit as B,R,G
#define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,R #define NEO_BGR ((2 << 6) | (2 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,R
// RGBW NeoPixel permutations; all 4 offsets are distinct // RGBW NeoPixel permutations; all 4 offsets are distinct
// Offset: W R G B // Offset: W R G B
#define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) ///< Transmit as W,R,G,B #define NEO_WRGB ((0 << 6) | (1 << 4) | (2 << 2) | (3)) ///< Transmit as W,R,G,B
#define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) ///< Transmit as W,R,B,G #define NEO_WRBG ((0 << 6) | (1 << 4) | (3 << 2) | (2)) ///< Transmit as W,R,B,G
#define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) ///< Transmit as W,G,R,B #define NEO_WGRB ((0 << 6) | (2 << 4) | (1 << 2) | (3)) ///< Transmit as W,G,R,B
#define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) ///< Transmit as W,G,B,R #define NEO_WGBR ((0 << 6) | (3 << 4) | (1 << 2) | (2)) ///< Transmit as W,G,B,R
#define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) ///< Transmit as W,B,R,G #define NEO_WBRG ((0 << 6) | (2 << 4) | (3 << 2) | (1)) ///< Transmit as W,B,R,G
#define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) ///< Transmit as W,B,G,R #define NEO_WBGR ((0 << 6) | (3 << 4) | (2 << 2) | (1)) ///< Transmit as W,B,G,R
#define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) ///< Transmit as R,W,G,B #define NEO_RWGB ((1 << 6) | (0 << 4) | (2 << 2) | (3)) ///< Transmit as R,W,G,B
#define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) ///< Transmit as R,W,B,G #define NEO_RWBG ((1 << 6) | (0 << 4) | (3 << 2) | (2)) ///< Transmit as R,W,B,G
#define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) ///< Transmit as R,G,W,B #define NEO_RGWB ((2 << 6) | (0 << 4) | (1 << 2) | (3)) ///< Transmit as R,G,W,B
#define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) ///< Transmit as R,G,B,W #define NEO_RGBW ((3 << 6) | (0 << 4) | (1 << 2) | (2)) ///< Transmit as R,G,B,W
#define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) ///< Transmit as R,B,W,G #define NEO_RBWG ((2 << 6) | (0 << 4) | (3 << 2) | (1)) ///< Transmit as R,B,W,G
#define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) ///< Transmit as R,B,G,W #define NEO_RBGW ((3 << 6) | (0 << 4) | (2 << 2) | (1)) ///< Transmit as R,B,G,W
#define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) ///< Transmit as G,W,R,B #define NEO_GWRB ((1 << 6) | (2 << 4) | (0 << 2) | (3)) ///< Transmit as G,W,R,B
#define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) ///< Transmit as G,W,B,R #define NEO_GWBR ((1 << 6) | (3 << 4) | (0 << 2) | (2)) ///< Transmit as G,W,B,R
#define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) ///< Transmit as G,R,W,B #define NEO_GRWB ((2 << 6) | (1 << 4) | (0 << 2) | (3)) ///< Transmit as G,R,W,B
#define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) ///< Transmit as G,R,B,W #define NEO_GRBW ((3 << 6) | (1 << 4) | (0 << 2) | (2)) ///< Transmit as G,R,B,W
#define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,W,R #define NEO_GBWR ((2 << 6) | (3 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,W,R
#define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,R,W #define NEO_GBRW ((3 << 6) | (2 << 4) | (0 << 2) | (1)) ///< Transmit as G,B,R,W
#define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) ///< Transmit as B,W,R,G #define NEO_BWRG ((1 << 6) | (2 << 4) | (3 << 2) | (0)) ///< Transmit as B,W,R,G
#define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) ///< Transmit as B,W,G,R #define NEO_BWGR ((1 << 6) | (3 << 4) | (2 << 2) | (0)) ///< Transmit as B,W,G,R
#define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) ///< Transmit as B,R,W,G #define NEO_BRWG ((2 << 6) | (1 << 4) | (3 << 2) | (0)) ///< Transmit as B,R,W,G
#define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) ///< Transmit as B,R,G,W #define NEO_BRGW ((3 << 6) | (1 << 4) | (2 << 2) | (0)) ///< Transmit as B,R,G,W
#define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,W,R #define NEO_BGWR ((2 << 6) | (3 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,W,R
#define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,R,W #define NEO_BGRW ((3 << 6) | (2 << 4) | (1 << 2) | (0)) ///< Transmit as B,G,R,W
// Add NEO_KHZ400 to the color order value to indicate a 400 KHz device. // Add NEO_KHZ400 to the color order value to indicate a 400 KHz device.
// All but the earliest v1 NeoPixels expect an 800 KHz data stream, this is // All but the earliest v1 NeoPixels expect an 800 KHz data stream, this is
// the default if unspecified. Because flash space is very limited on ATtiny // the default if unspecified. Because flash space is very limited on ATtiny
// devices (e.g. Trinket, Gemma), v1 NeoPixels aren't handled by default on // devices (e.g. Trinket, Gemma), v1 NeoPixels aren't handled by default on
// those chips, though it can be enabled by removing the ifndef/endif below, // those chips, though it can be enabled by removing the ifndef/endif below,
// but code will be bigger. Conversely, can disable the NEO_KHZ400 line on // but code will be bigger. Conversely, can disable the NEO_KHZ400 line on
// other MCUs to remove v1 support and save a little space. // other MCUs to remove v1 support and save a little space.
#define NEO_KHZ800 0x0000 ///< 800 KHz data transmission #define NEO_KHZ800 0x0000 ///< 800 KHz data transmission
#ifndef __AVR_ATtiny85__ #ifndef __AVR_ATtiny85__
#define NEO_KHZ400 0x0100 ///< 400 KHz data transmission #define NEO_KHZ400 0x0100 ///< 400 KHz data transmission
#endif #endif
// If 400 KHz support is enabled, the third parameter to the constructor // If 400 KHz support is enabled, the third parameter to the constructor
// requires a 16-bit value (in order to select 400 vs 800 KHz speed). // requires a 16-bit value (in order to select 400 vs 800 KHz speed).
// If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value // If only 800 KHz is enabled (as is default on ATtiny), an 8-bit value
// is sufficient to encode pixel color order, saving some space. // is sufficient to encode pixel color order, saving some space.
#ifdef NEO_KHZ400 #ifdef NEO_KHZ400
typedef uint16_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor typedef uint16_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor
#else #else
typedef uint8_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor typedef uint8_t neoPixelType; ///< 3rd arg to Adafruit_NeoPixel constructor
#endif #endif
// These two tables are declared outside the Adafruit_NeoPixel class // These two tables are declared outside the Adafruit_NeoPixel class
// because some boards may require oldschool compilers that don't // because some boards may require oldschool compilers that don't
// handle the C++11 constexpr keyword. // handle the C++11 constexpr keyword.
/* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255). /* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255).
Copy & paste this snippet into a Python REPL to regenerate: Copy & paste this snippet into a Python REPL to regenerate:
import math import math
for x in range(256): for x in range(256):
print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))), print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))),
if x&15 == 15: print if x&15 == 15: print
*/ */
static const uint8_t PROGMEM _NeoPixelSineTable[256] = { static const uint8_t PROGMEM _NeoPixelSineTable[256] = {
128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170,
173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, 173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211,
213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240,
241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254,
254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251,
250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, 250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232,
230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, 230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198,
196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155, 196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155,
152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109,
106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65,
62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29, 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29,
27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6, 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6,
5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11,
12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37,
40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121,
124}; 124};
/* Similar to above, but for an 8-bit gamma-correction table. /* Similar to above, but for an 8-bit gamma-correction table.
Copy & paste this snippet into a Python REPL to regenerate: Copy & paste this snippet into a Python REPL to regenerate:
import math import math
gamma=2.6 gamma=2.6
for x in range(256): for x in range(256):
print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))), print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))),
if x&15 == 15: print if x&15 == 15: print
*/ */
static const uint8_t PROGMEM _NeoPixelGammaTable[256] = { static const uint8_t PROGMEM _NeoPixelGammaTable[256] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17,
17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35, 25, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35,
36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 80, 81, 64, 65, 66, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 80, 81,
82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, 97, 99, 100, 102, 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, 97, 99, 100, 102,
103, 105, 106, 108, 109, 111, 112, 114, 115, 117, 119, 120, 122, 124, 125, 103, 105, 106, 108, 109, 111, 112, 114, 115, 117, 119, 120, 122, 124, 125,
127, 129, 130, 132, 134, 136, 137, 139, 141, 143, 145, 146, 148, 150, 152, 127, 129, 130, 132, 134, 136, 137, 139, 141, 143, 145, 146, 148, 150, 152,
154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182,
184, 186, 188, 191, 193, 195, 197, 199, 202, 204, 206, 209, 211, 213, 215, 184, 186, 188, 191, 193, 195, 197, 199, 202, 204, 206, 209, 211, 213, 215,
218, 220, 223, 225, 227, 230, 232, 235, 237, 240, 242, 245, 247, 250, 252, 218, 220, 223, 225, 227, 230, 232, 235, 237, 240, 242, 245, 247, 250, 252,
255}; 255};
/* Declare external methods required by the Adafruit_NeoPixel implementation /*!
for specific hardware/library versions @brief Class that stores state and functions for interacting with
*/ Adafruit NeoPixels and compatible devices.
#if defined(ESP32) */
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) class Adafruit_NeoPixel {
extern "C" void espInit();
#endif public:
#endif // Constructor: number of LEDs, pin number, LED type
Adafruit_NeoPixel(uint16_t n, int16_t pin = 6,
/*! neoPixelType type = NEO_GRB + NEO_KHZ800);
@brief Class that stores state and functions for interacting with Adafruit_NeoPixel(void);
Adafruit NeoPixels and compatible devices. ~Adafruit_NeoPixel();
*/
class Adafruit_NeoPixel { void begin(void);
void show(void);
public: void setPin(int16_t p);
// Constructor: number of LEDs, pin number, LED type void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
Adafruit_NeoPixel(uint16_t n, int16_t pin = 6, void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w);
neoPixelType type = NEO_GRB + NEO_KHZ800); void setPixelColor(uint16_t n, uint32_t c);
Adafruit_NeoPixel(void); void fill(uint32_t c = 0, uint16_t first = 0, uint16_t count = 0);
~Adafruit_NeoPixel(); void setBrightness(uint8_t);
void clear(void);
void begin(void); void updateLength(uint16_t n);
void show(void); void updateType(neoPixelType t);
void setPin(int16_t p); /*!
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b); @brief Check whether a call to show() will start sending data
void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w); immediately or will 'block' for a required interval. NeoPixels
void setPixelColor(uint16_t n, uint32_t c); require a short quiet time (about 300 microseconds) after the
void fill(uint32_t c = 0, uint16_t first = 0, uint16_t count = 0); last bit is received before the data 'latches' and new data can
void setBrightness(uint8_t); start being received. Usually one's sketch is implicitly using
void clear(void); this time to generate a new frame of animation...but if it
void updateLength(uint16_t n); finishes very quickly, this function could be used to see if
void updateType(neoPixelType t); there's some idle time available for some low-priority
/*! concurrent task.
@brief Check whether a call to show() will start sending data @return 1 or true if show() will start sending immediately, 0 or false
immediately or will 'block' for a required interval. NeoPixels if show() would block (meaning some idle time is available).
require a short quiet time (about 300 microseconds) after the */
last bit is received before the data 'latches' and new data can bool canShow(void) {
start being received. Usually one's sketch is implicitly using // It's normal and possible for endTime to exceed micros() if the
this time to generate a new frame of animation...but if it // 32-bit clock counter has rolled over (about every 70 minutes).
finishes very quickly, this function could be used to see if // Since both are uint32_t, a negative delta correctly maps back to
there's some idle time available for some low-priority // positive space, and it would seem like the subtraction below would
concurrent task. // suffice. But a problem arises if code invokes show() very
@return 1 or true if show() will start sending immediately, 0 or false // infrequently...the micros() counter may roll over MULTIPLE times in
if show() would block (meaning some idle time is available). // that interval, the delta calculation is no longer correct and the
*/ // next update may stall for a very long time. The check below resets
bool canShow(void) { // the latch counter if a rollover has occurred. This can cause an
// It's normal and possible for endTime to exceed micros() if the // extra delay of up to 300 microseconds in the rare case where a
// 32-bit clock counter has rolled over (about every 70 minutes). // show() call happens precisely around the rollover, but that's
// Since both are uint32_t, a negative delta correctly maps back to // neither likely nor especially harmful, vs. other code that might
// positive space, and it would seem like the subtraction below would // stall for 30+ minutes, or having to document and frequently remind
// suffice. But a problem arises if code invokes show() very // and/or provide tech support explaining an unintuitive need for
// infrequently...the micros() counter may roll over MULTIPLE times in // show() calls at least once an hour.
// that interval, the delta calculation is no longer correct and the uint32_t now = micros();
// next update may stall for a very long time. The check below resets if (endTime > now) {
// the latch counter if a rollover has occurred. This can cause an endTime = now;
// extra delay of up to 300 microseconds in the rare case where a }
// show() call happens precisely around the rollover, but that's return (now - endTime) >= 300L;
// neither likely nor especially harmful, vs. other code that might }
// stall for 30+ minutes, or having to document and frequently remind /*!
// and/or provide tech support explaining an unintuitive need for @brief Get a pointer directly to the NeoPixel data buffer in RAM.
// show() calls at least once an hour. Pixel data is stored in a device-native format (a la the NEO_*
uint32_t now = micros(); constants) and is not translated here. Applications that access
if (endTime > now) { this buffer will need to be aware of the specific data format
endTime = now; and handle colors appropriately.
} @return Pointer to NeoPixel buffer (uint8_t* array).
return (now - endTime) >= 300L; @note This is for high-performance applications where calling
} setPixelColor() on every single pixel would be too slow (e.g.
/*! POV or light-painting projects). There is no bounds checking
@brief Get a pointer directly to the NeoPixel data buffer in RAM. on the array, creating tremendous potential for mayhem if one
Pixel data is stored in a device-native format (a la the NEO_* writes past the ends of the buffer. Great power, great
constants) and is not translated here. Applications that access responsibility and all that.
this buffer will need to be aware of the specific data format */
and handle colors appropriately. uint8_t *getPixels(void) const { return pixels; };
@return Pointer to NeoPixel buffer (uint8_t* array). uint8_t getBrightness(void) const;
@note This is for high-performance applications where calling /*!
setPixelColor() on every single pixel would be too slow (e.g. @brief Retrieve the pin number used for NeoPixel data output.
POV or light-painting projects). There is no bounds checking @return Arduino pin number (-1 if not set).
on the array, creating tremendous potential for mayhem if one */
writes past the ends of the buffer. Great power, great int16_t getPin(void) const { return pin; };
responsibility and all that. /*!
*/ @brief Return the number of pixels in an Adafruit_NeoPixel strip object.
uint8_t *getPixels(void) const { return pixels; }; @return Pixel count (0 if not set).
uint8_t getBrightness(void) const; */
/*! uint16_t numPixels(void) const { return numLEDs; }
@brief Retrieve the pin number used for NeoPixel data output. uint32_t getPixelColor(uint16_t n) const;
@return Arduino pin number (-1 if not set). /*!
*/ @brief An 8-bit integer sine wave function, not directly compatible
int16_t getPin(void) const { return pin; }; with standard trigonometric units like radians or degrees.
/*! @param x Input angle, 0-255; 256 would loop back to zero, completing
@brief Return the number of pixels in an Adafruit_NeoPixel strip object. the circle (equivalent to 360 degrees or 2 pi radians).
@return Pixel count (0 if not set). One can therefore use an unsigned 8-bit variable and simply
*/ add or subtract, allowing it to overflow/underflow and it
uint16_t numPixels(void) const { return numLEDs; } still does the expected contiguous thing.
uint32_t getPixelColor(uint16_t n) const; @return Sine result, 0 to 255, or -128 to +127 if type-converted to
/*! a signed int8_t, but you'll most likely want unsigned as this
@brief An 8-bit integer sine wave function, not directly compatible output is often used for pixel brightness in animation effects.
with standard trigonometric units like radians or degrees. */
@param x Input angle, 0-255; 256 would loop back to zero, completing static uint8_t sine8(uint8_t x) {
the circle (equivalent to 360 degrees or 2 pi radians). return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out
One can therefore use an unsigned 8-bit variable and simply }
add or subtract, allowing it to overflow/underflow and it /*!
still does the expected contiguous thing. @brief An 8-bit gamma-correction function for basic pixel brightness
@return Sine result, 0 to 255, or -128 to +127 if type-converted to adjustment. Makes color transitions appear more perceptially
a signed int8_t, but you'll most likely want unsigned as this correct.
output is often used for pixel brightness in animation effects. @param x Input brightness, 0 (minimum or off/black) to 255 (maximum).
*/ @return Gamma-adjusted brightness, can then be passed to one of the
static uint8_t sine8(uint8_t x) { setPixelColor() functions. This uses a fixed gamma correction
return pgm_read_byte(&_NeoPixelSineTable[x]); // 0-255 in, 0-255 out exponent of 2.6, which seems reasonably okay for average
} NeoPixels in average tasks. If you need finer control you'll
/*! need to provide your own gamma-correction function instead.
@brief An 8-bit gamma-correction function for basic pixel brightness */
adjustment. Makes color transitions appear more perceptially static uint8_t gamma8(uint8_t x) {
correct. return pgm_read_byte(&_NeoPixelGammaTable[x]); // 0-255 in, 0-255 out
@param x Input brightness, 0 (minimum or off/black) to 255 (maximum). }
@return Gamma-adjusted brightness, can then be passed to one of the /*!
setPixelColor() functions. This uses a fixed gamma correction @brief Convert separate red, green and blue values into a single
exponent of 2.6, which seems reasonably okay for average "packed" 32-bit RGB color.
NeoPixels in average tasks. If you need finer control you'll @param r Red brightness, 0 to 255.
need to provide your own gamma-correction function instead. @param g Green brightness, 0 to 255.
*/ @param b Blue brightness, 0 to 255.
static uint8_t gamma8(uint8_t x) { @return 32-bit packed RGB value, which can then be assigned to a
return pgm_read_byte(&_NeoPixelGammaTable[x]); // 0-255 in, 0-255 out variable for later use or passed to the setPixelColor()
} function. Packed RGB format is predictable, regardless of
/*! LED strand color order.
@brief Convert separate red, green and blue values into a single */
"packed" 32-bit RGB color. static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
@param r Red brightness, 0 to 255. return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
@param g Green brightness, 0 to 255. }
@param b Blue brightness, 0 to 255. /*!
@return 32-bit packed RGB value, which can then be assigned to a @brief Convert separate red, green, blue and white values into a
variable for later use or passed to the setPixelColor() single "packed" 32-bit WRGB color.
function. Packed RGB format is predictable, regardless of @param r Red brightness, 0 to 255.
LED strand color order. @param g Green brightness, 0 to 255.
*/ @param b Blue brightness, 0 to 255.
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { @param w White brightness, 0 to 255.
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; @return 32-bit packed WRGB value, which can then be assigned to a
} variable for later use or passed to the setPixelColor()
/*! function. Packed WRGB format is predictable, regardless of
@brief Convert separate red, green, blue and white values into a LED strand color order.
single "packed" 32-bit WRGB color. */
@param r Red brightness, 0 to 255. static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
@param g Green brightness, 0 to 255. return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
@param b Blue brightness, 0 to 255. }
@param w White brightness, 0 to 255. static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255);
@return 32-bit packed WRGB value, which can then be assigned to a /*!
variable for later use or passed to the setPixelColor() @brief A gamma-correction function for 32-bit packed RGB or WRGB
function. Packed WRGB format is predictable, regardless of colors. Makes color transitions appear more perceptially
LED strand color order. correct.
*/ @param x 32-bit packed RGB or WRGB color.
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { @return Gamma-adjusted packed color, can then be passed in one of the
return ((uint32_t)w << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; setPixelColor() functions. Like gamma8(), this uses a fixed
} gamma correction exponent of 2.6, which seems reasonably okay
static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255); for average NeoPixels in average tasks. If you need finer
/*! control you'll need to provide your own gamma-correction
@brief A gamma-correction function for 32-bit packed RGB or WRGB function instead.
colors. Makes color transitions appear more perceptially */
correct. static uint32_t gamma32(uint32_t x);
@param x 32-bit packed RGB or WRGB color.
@return Gamma-adjusted packed color, can then be passed in one of the void rainbow(uint16_t first_hue = 0, int8_t reps = 1,
setPixelColor() functions. Like gamma8(), this uses a fixed uint8_t saturation = 255, uint8_t brightness = 255,
gamma correction exponent of 2.6, which seems reasonably okay bool gammify = true);
for average NeoPixels in average tasks. If you need finer
control you'll need to provide your own gamma-correction static neoPixelType str2order(const char *v);
function instead.
*/ private:
static uint32_t gamma32(uint32_t x); #if defined(ARDUINO_ARCH_RP2040)
void rp2040Init(uint8_t pin, bool is800KHz);
void rainbow(uint16_t first_hue = 0, int8_t reps = 1, void rp2040Show(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz);
uint8_t saturation = 255, uint8_t brightness = 255, #endif
bool gammify = true);
protected:
static neoPixelType str2order(const char *v); #ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled...
bool is800KHz; ///< true if 800 KHz pixels
private: #endif
#if defined(ARDUINO_ARCH_RP2040) bool begun; ///< true if begin() previously called
void rp2040Init(uint8_t pin, bool is800KHz); uint16_t numLEDs; ///< Number of RGB LEDs in strip
void rp2040Show(uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz); uint16_t numBytes; ///< Size of 'pixels' buffer below
#endif int16_t pin; ///< Output pin number (-1 if not yet set)
uint8_t brightness; ///< Strip brightness 0-255 (stored as +1)
protected: uint8_t *pixels; ///< Holds LED color values (3 or 4 bytes each)
#ifdef NEO_KHZ400 // If 400 KHz NeoPixel support enabled... uint8_t rOffset; ///< Red index within each 3- or 4-byte pixel
bool is800KHz; ///< true if 800 KHz pixels uint8_t gOffset; ///< Index of green byte
#endif uint8_t bOffset; ///< Index of blue byte
bool begun; ///< true if begin() previously called uint8_t wOffset; ///< Index of white (==rOffset if no white)
uint16_t numLEDs; ///< Number of RGB LEDs in strip uint32_t endTime; ///< Latch timing reference
uint16_t numBytes; ///< Size of 'pixels' buffer below #ifdef __AVR__
int16_t pin; ///< Output pin number (-1 if not yet set) volatile uint8_t *port; ///< Output PORT register
uint8_t brightness; ///< Strip brightness 0-255 (stored as +1) uint8_t pinMask; ///< Output PORT bitmask
uint8_t *pixels; ///< Holds LED color values (3 or 4 bytes each) #endif
uint8_t rOffset; ///< Red index within each 3- or 4-byte pixel #if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32)
uint8_t gOffset; ///< Index of green byte GPIO_TypeDef *gpioPort; ///< Output GPIO PORT
uint8_t bOffset; ///< Index of blue byte uint32_t gpioPin; ///< Output GPIO PIN
uint8_t wOffset; ///< Index of white (==rOffset if no white) #endif
uint32_t endTime; ///< Latch timing reference #if defined(ARDUINO_ARCH_RP2040)
#ifdef __AVR__ PIO pio = pio0;
volatile uint8_t *port; ///< Output PORT register int sm = 0;
uint8_t pinMask; ///< Output PORT bitmask bool init = true;
#endif #endif
#if defined(ARDUINO_ARCH_STM32) || defined(ARDUINO_ARCH_ARDUINO_CORE_STM32) || defined(ARDUINO_ARCH_CH32) };
GPIO_TypeDef *gpioPort; ///< Output GPIO PORT
uint32_t gpioPin; ///< Output GPIO PIN #endif // ADAFRUIT_NEOPIXEL_H
#endif
#if defined(ARDUINO_ARCH_RP2040)
PIO pio = pio0;
int sm = 0;
bool init = true;
#endif
};
#endif // ADAFRUIT_NEOPIXEL_H

View file

@ -17,256 +17,147 @@
* limitations under the License. * limitations under the License.
*/ */
#if defined(ESP32) #if defined(ESP32)
#include <Arduino.h> #include <Arduino.h>
#include "driver/rmt.h"
#if defined(ESP_IDF_VERSION)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
#define HAS_ESP_IDF_4
#endif
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#define HAS_ESP_IDF_5
#endif
#endif
#ifdef HAS_ESP_IDF_5
static SemaphoreHandle_t show_mutex = NULL;
void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) {
// Note: Because rmtPin is shared between all instances, we will
// end up releasing/initializing the RMT channels each time we
// invoke on different pins. This is probably ok, just not
// efficient. led_data is shared between all instances but will
// be allocated with enough space for the largest instance; data
// is not used beyond the mutex lock so this should be fine.
#define SEMAPHORE_TIMEOUT_MS 50
static rmt_data_t *led_data = NULL;
static uint32_t led_data_size = 0;
static int rmtPin = -1;
if (show_mutex && xSemaphoreTake(show_mutex, SEMAPHORE_TIMEOUT_MS / portTICK_PERIOD_MS) == pdTRUE) {
uint32_t requiredSize = numBytes * 8;
if (requiredSize > led_data_size) {
free(led_data);
if (led_data = (rmt_data_t *)malloc(requiredSize * sizeof(rmt_data_t))) {
led_data_size = requiredSize;
} else {
led_data_size = 0;
}
} else if (requiredSize == 0) {
// To release RMT resources (RMT channels and led_data), call
// .updateLength(0) to set number of pixels/bytes to zero,
// then call .show() to invoke this code and free resources.
free(led_data);
led_data = NULL;
if (rmtPin >= 0) {
rmtDeinit(rmtPin);
rmtPin = -1;
}
led_data_size = 0;
}
if (led_data_size > 0 && requiredSize <= led_data_size) {
if (pin != rmtPin) {
if (rmtPin >= 0) {
rmtDeinit(rmtPin);
rmtPin = -1;
}
if (!rmtInit(pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
log_e("Failed to init RMT TX mode on pin %d", pin);
return;
}
rmtPin = pin;
}
if (rmtPin >= 0) {
int i=0;
for (int b=0; b < numBytes; b++) {
for (int bit=0; bit<8; bit++){
if ( pixels[b] & (1<<(7-bit)) ) {
led_data[i].level0 = 1;
led_data[i].duration0 = 8;
led_data[i].level1 = 0;
led_data[i].duration1 = 4;
} else {
led_data[i].level0 = 1;
led_data[i].duration0 = 4;
led_data[i].level1 = 0;
led_data[i].duration1 = 8;
}
i++;
}
}
rmtWrite(pin, led_data, numBytes * 8, RMT_WAIT_FOR_EVER);
}
}
xSemaphoreGive(show_mutex);
}
}
// To avoid race condition initializing the mutex, all instances of
// Adafruit_NeoPixel must be constructed before launching and child threads
void espInit() {
if (!show_mutex) {
show_mutex = xSemaphoreCreateMutex();
}
}
#else
#include "driver/rmt.h"
// This code is adapted from the ESP-IDF v3.4 RMT "led_strip" example, altered
// to work with the Arduino version of the ESP-IDF (3.2)
#define WS2812_T0H_NS (400)
#define WS2812_T0L_NS (850)
#define WS2812_T1H_NS (800)
#define WS2812_T1L_NS (450)
#define WS2811_T0H_NS (500)
#define WS2811_T0L_NS (2000)
#define WS2811_T1H_NS (1200)
#define WS2811_T1L_NS (1300)
static uint32_t t0h_ticks = 0;
static uint32_t t1h_ticks = 0;
static uint32_t t0l_ticks = 0;
static uint32_t t1l_ticks = 0;
// Limit the number of RMT channels available for the Neopixels. Defaults to all
// channels (8 on ESP32, 4 on ESP32-S2 and S3). Redefining this value will free
// any channels with a higher number for other uses, such as IR send-and-recieve
// libraries. Redefine as 1 to restrict Neopixels to only a single channel.
#define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
#define RMT_LL_HW_BASE (&RMT)
bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];
static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
size_t wanted_num, size_t *translated_size, size_t *item_num)
{
if (src == NULL || dest == NULL) {
*translated_size = 0;
*item_num = 0;
return;
}
const rmt_item32_t bit0 = {{{ t0h_ticks, 1, t0l_ticks, 0 }}}; //Logical 0
const rmt_item32_t bit1 = {{{ t1h_ticks, 1, t1l_ticks, 0 }}}; //Logical 1
size_t size = 0;
size_t num = 0;
uint8_t *psrc = (uint8_t *)src;
rmt_item32_t *pdest = dest;
while (size < src_size && num < wanted_num) {
for (int i = 0; i < 8; i++) {
// MSB first
if (*psrc & (1 << (7 - i))) {
pdest->val = bit1.val;
} else {
pdest->val = bit0.val;
}
num++;
pdest++;
}
size++;
psrc++;
}
*translated_size = size;
*item_num = num;
}
static bool rmt_initialized = false; #if defined(ESP_IDF_VERSION)
static bool rmt_adapter_initialized = false; #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
#define HAS_ESP_IDF_4
#endif
#endif
void espShow(uint8_t pin, uint8_t *pixels, uint32_t numBytes, boolean is800KHz) { // This code is adapted from the ESP-IDF v3.4 RMT "led_strip" example, altered
if (rmt_initialized == false) { // to work with the Arduino version of the ESP-IDF (3.2)
// Reserve channel
rmt_channel_t channel = 0;
#if defined(HAS_ESP_IDF_4)
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, channel);
config.clk_div = 2;
#else
// Match default TX config from ESP-IDF version 3.4
rmt_config_t config = {
.rmt_mode = RMT_MODE_TX,
.channel = channel,
.gpio_num = pin,
.clk_div = 2,
.mem_block_num = 1,
.tx_config = {
.carrier_freq_hz = 38000,
.carrier_level = RMT_CARRIER_LEVEL_HIGH,
.idle_level = RMT_IDLE_LEVEL_LOW,
.carrier_duty_percent = 33,
.carrier_en = false,
.loop_en = false,
.idle_output_en = true,
}
};
#endif
rmt_config(&config);
rmt_driver_install(config.channel, 0, 0);
// Convert NS timings to ticks
uint32_t counter_clk_hz = 0;
#if defined(HAS_ESP_IDF_4)
rmt_get_counter_clock(channel, &counter_clk_hz);
#else
// this emulates the rmt_get_counter_clock() function from ESP-IDF 3.4
if (RMT_LL_HW_BASE->conf_ch[config.channel].conf1.ref_always_on == RMT_BASECLK_REF) {
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
counter_clk_hz = REF_CLK_FREQ / (div);
} else {
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
counter_clk_hz = APB_CLK_FREQ / (div);
}
#endif
// NS to tick converter
float ratio = (float)counter_clk_hz / 1e9;
if (is800KHz) {
t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
} else {
t0h_ticks = (uint32_t)(ratio * WS2811_T0H_NS);
t0l_ticks = (uint32_t)(ratio * WS2811_T0L_NS);
t1h_ticks = (uint32_t)(ratio * WS2811_T1H_NS);
t1l_ticks = (uint32_t)(ratio * WS2811_T1L_NS);
}
// Initialize automatic timing translator
rmt_translator_init(0, ws2812_rmt_adapter);
rmt_initialized = true;
}
// Write and wait to finish #define WS2812_T0H_NS (400)
rmt_write_sample(0, pixels, (size_t)numBytes, false); #define WS2812_T0L_NS (850)
//rmt_wait_tx_done(config.channel, pdMS_TO_TICKS(100)); #define WS2812_T1H_NS (800)
#define WS2812_T1L_NS (450)
// Free channel again
//rmt_driver_uninstall(config.channel); #define WS2811_T0H_NS (500)
//rmt_reserved_channels[channel] = false; #define WS2811_T0L_NS (2000)
#define WS2811_T1H_NS (1200)
//gpio_set_direction(pin, GPIO_MODE_OUTPUT); #define WS2811_T1L_NS (1300)
}
static uint32_t t0h_ticks = 0;
#endif // ifndef IDF5 static uint32_t t1h_ticks = 0;
static uint32_t t0l_ticks = 0;
static uint32_t t1l_ticks = 0;
#endif // ifdef(ESP32)
// Limit the number of RMT channels available for the Neopixels. Defaults to all
// channels (8 on ESP32, 4 on ESP32-S2 and S3). Redefining this value will free
// any channels with a higher number for other uses, such as IR send-and-recieve
// libraries. Redefine as 1 to restrict Neopixels to only a single channel.
#define ADAFRUIT_RMT_CHANNEL_MAX RMT_CHANNEL_MAX
#define RMT_LL_HW_BASE (&RMT)
bool rmt_reserved_channels[ADAFRUIT_RMT_CHANNEL_MAX];
static void IRAM_ATTR ws2812_rmt_adapter(const void* src, rmt_item32_t* dest, size_t src_size, size_t wanted_num,
size_t* translated_size, size_t* item_num) {
if (src == NULL || dest == NULL) {
*translated_size = 0;
*item_num = 0;
return;
}
const rmt_item32_t bit0 = {{{t0h_ticks, 1, t0l_ticks, 0}}}; //Logical 0
const rmt_item32_t bit1 = {{{t1h_ticks, 1, t1l_ticks, 0}}}; //Logical 1
size_t size = 0;
size_t num = 0;
uint8_t* psrc = (uint8_t*)src;
rmt_item32_t* pdest = dest;
while (size < src_size && num < wanted_num) {
for (int i = 0; i < 8; i++) {
// MSB first
if (*psrc & (1 << (7 - i))) {
pdest->val = bit1.val;
} else {
pdest->val = bit0.val;
}
num++;
pdest++;
}
size++;
psrc++;
}
*translated_size = size;
*item_num = num;
}
static bool rmt_initialized = false;
static bool rmt_adapter_initialized = false;
void espShow(uint8_t pin, uint8_t* pixels, uint32_t numBytes, boolean is800KHz) {
if (rmt_initialized == false) {
// Reserve channel
rmt_channel_t channel = 0;
#if defined(HAS_ESP_IDF_4)
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin, channel);
config.clk_div = 2;
#else
// Match default TX config from ESP-IDF version 3.4
rmt_config_t config = {.rmt_mode = RMT_MODE_TX,
.channel = channel,
.gpio_num = pin,
.clk_div = 2,
.mem_block_num = 1,
.tx_config = {
.carrier_freq_hz = 38000,
.carrier_level = RMT_CARRIER_LEVEL_HIGH,
.idle_level = RMT_IDLE_LEVEL_LOW,
.carrier_duty_percent = 33,
.carrier_en = false,
.loop_en = false,
.idle_output_en = true,
}};
#endif
rmt_config(&config);
rmt_driver_install(config.channel, 0, 0);
// Convert NS timings to ticks
uint32_t counter_clk_hz = 0;
#if defined(HAS_ESP_IDF_4)
rmt_get_counter_clock(channel, &counter_clk_hz);
#else
// this emulates the rmt_get_counter_clock() function from ESP-IDF 3.4
if (RMT_LL_HW_BASE->conf_ch[config.channel].conf1.ref_always_on == RMT_BASECLK_REF) {
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
counter_clk_hz = REF_CLK_FREQ / (div);
} else {
uint32_t div_cnt = RMT_LL_HW_BASE->conf_ch[config.channel].conf0.div_cnt;
uint32_t div = div_cnt == 0 ? 256 : div_cnt;
counter_clk_hz = APB_CLK_FREQ / (div);
}
#endif
// NS to tick converter
float ratio = (float)counter_clk_hz / 1e9;
if (is800KHz) {
t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
} else {
t0h_ticks = (uint32_t)(ratio * WS2811_T0H_NS);
t0l_ticks = (uint32_t)(ratio * WS2811_T0L_NS);
t1h_ticks = (uint32_t)(ratio * WS2811_T1H_NS);
t1l_ticks = (uint32_t)(ratio * WS2811_T1L_NS);
}
// Initialize automatic timing translator
rmt_translator_init(0, ws2812_rmt_adapter);
rmt_initialized = true;
}
// Write and wait to finish
rmt_write_sample(0, pixels, (size_t)numBytes, false);
}
#endif