diff --git a/keywords.txt b/keywords.txt index 6c442ba2..b38278ba 100644 --- a/keywords.txt +++ b/keywords.txt @@ -699,6 +699,7 @@ Tlc5947Spi15MhzMethod KEYWORD1 Tlc5947Spi15MhzMethod16Bit KEYWORD1 Tlc5947SpiMethod KEYWORD1 Tlc5947SpiMethod16Bit KEYWORD1 +Sm16716Method KEYWORD1 NeoPixelAnimator KEYWORD1 AnimUpdateCallback KEYWORD1 AnimationParam KEYWORD1 diff --git a/library.json b/library.json index 4a24c799..84940e1e 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "NeoPixelBus", - "keywords": "NeoPixel, WS2811, WS2812, WS2813, WS2821, SK6812, DotStar, APA102, SK9822, APA106, LPD8806, LPD6803, P9813, TM1829, TM1814, TM1914, TX1812, WS2801, SM16803, SM16823, SM16804, SM16824, DMX512, RGB, RGBW", + "keywords": "NeoPixel, WS2811, WS2812, WS2813, WS2821, SK6812, DotStar, APA102, SK9822, APA106, LPD8806, LPD6803, P9813, TM1829, TM1814, TM1914, TX1812, WS2801, SM16803, SM16823, SM16804, SM16824, SM16716, DMX512, RGB, RGBW", "description": "A library that makes controlling NeoPixels (WS2812x and many others) and DotStars (SK6812 and many others) easy. Supports most Arduino platforms, including async hardware support for Esp8266, Esp32, and Nrf52 (Nano 33 BLE). Support for RGBW pixels and 7 Segment LED direct driven. Includes seperate RgbColor, RgbwColor, Rgb16Color, Rgb48Color, HslColor, and HsbColor objects. Includes an animator class that helps create asyncronous animations. For all platforms; there are two methods of sending DotStar data, hardware SPI and software SPI.", "homepage": "https://github.com/Makuna/NeoPixelBus/wiki", "repository": { diff --git a/src/NeoPixelBus.h b/src/NeoPixelBus.h index a3f708a0..dc48e612 100644 --- a/src/NeoPixelBus.h +++ b/src/NeoPixelBus.h @@ -48,6 +48,7 @@ const uint16_t PixelIndex_OutOfBounds = 0xffff; #include "internal/Ws2801GenericMethod.h" #include "internal/P9813GenericMethod.h" #include "internal/Tlc5947GenericMethod.h" +#include "internal/Sm16716GenericMethod.h" #if defined(ARDUINO_ARCH_ESP8266) diff --git a/src/internal/Sm16716GenericMethod.h b/src/internal/Sm16716GenericMethod.h new file mode 100644 index 00000000..882b9a74 --- /dev/null +++ b/src/internal/Sm16716GenericMethod.h @@ -0,0 +1,138 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for SM16716 using general Pins + +Written by Michael C. Miller. +Contributed by Ivo H (ivoh95) + +I invest time and resources providing this open source code, +please support me by dontating (see https://github.com/Makuna/NeoPixelBus) + +------------------------------------------------------------------------- +This file is part of the Makuna/NeoPixelBus library. + +NeoPixelBus is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as +published by the Free Software Foundation, either version 3 of +the License, or (at your option) any later version. + +NeoPixelBus is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with NeoPixel. If not, see +. +-------------------------------------------------------------------------*/ + +#pragma once + +// must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set +#if defined(ARDUINO_ARCH_AVR) && !defined(__arm__) +#include "TwoWireBitBangImpleAvr.h" +#else +#include "TwoWireBitBangImple.h" +#endif + + +template class Sm16716MethodBase +{ +public: + typedef typename T_TWOWIRE::SettingsObject SettingsObject; + + Sm16716MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) : + _sizeData(pixelCount* elementSize + settingsSize), + _sizeFrame(6), // 48 bits + _wire(pinClock, pinData) + { + _data = static_cast(malloc(_sizeData)); + memset(_data, 0, _sizeData); + } + +#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny) + Sm16716MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) : + Sm16716MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize) + { + } +#endif + + ~Sm16716MethodBase() + { + free(_data); + } + + bool IsReadyToUpdate() const + { + return true; // dot stars don't have a required delay + } + +#if defined(ARDUINO_ARCH_ESP32) + // can't support hardware SPI due to weird extra bits + //void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) + //{ + // _wire.begin(sck, miso, mosi, ss); + //} +#endif + + void Initialize() + { + _wire.begin(); + } + + void Update(bool) + { + _wire.beginTransaction(); + + // start frame + for (size_t frameBytes = 0; frameBytes < _sizeFrame; frameBytes++) + { + _wire.transmitByte(0x00); + } + _wire.transmitBit(LOW); + _wire.transmitBit(LOW); // two extra 0s to make the 50 0 header + _wire.transmitBit(HIGH); // one to start the led frame + + for (size_t pixel = 0; pixel < (_sizeData / 3); pixel++) + { + _wire.transmitByte(_data[pixel]); + _wire.transmitByte(_data[pixel + 1]); + _wire.transmitByte(_data[pixel + 2]); + _wire.transmitBit(HIGH); //show the color and start the next frame + } + + _wire.endTransaction(); + } + + bool AlwaysUpdate() + { + // this method requires update to be called only if changes to buffer + return false; + } + + uint8_t* getData() const + { + return _data; + }; + + size_t getDataSize() const + { + return _sizeData; + }; + + void applySettings([[maybe_unused]] const SettingsObject& settings) + { + _wire.applySettings(settings); + } + +private: + const size_t _sizeData; // Size of '_data' buffer below + const size_t _sizeFrame; + + T_TWOWIRE _wire; + uint8_t* _data; // Holds LED color values +}; + +// can ONLY support our bitbang for wire due to requirement for custom transmitBit method +// to handle not byte oriented data stream +// +typedef Sm16716MethodBase Sm16716Method; diff --git a/src/internal/TwoWireBitBangImple.h b/src/internal/TwoWireBitBangImple.h index 92627a87..1c8627a4 100644 --- a/src/internal/TwoWireBitBangImple.h +++ b/src/internal/TwoWireBitBangImple.h @@ -62,6 +62,18 @@ class TwoWireBitBangImple digitalWrite(_pinData, LOW); } + void transmitBit(uint8_t bit) + { + // set data bit on pin + digitalWrite(_pinData, bit); + + // set clock high as data is ready + digitalWrite(_pinClock, HIGH); + + // set clock low as data pin is changed + digitalWrite(_pinClock, LOW); + } + void transmitByte(uint8_t data) { for (int bit = 7; bit >= 0; bit--) diff --git a/src/internal/TwoWireBitBangImpleAvr.h b/src/internal/TwoWireBitBangImpleAvr.h index 80c324f8..e3b9e320 100644 --- a/src/internal/TwoWireBitBangImpleAvr.h +++ b/src/internal/TwoWireBitBangImpleAvr.h @@ -67,6 +67,28 @@ class TwoWireBitBangImple digitalWrite(_pinData, LOW); } + void transmitBit(uint8_t bit) + { + // set data bit on pin + // digitalWrite(_pinData, bit); // HIGH : LOW + if (bit) + { + *_portData |= _pinMaskData; + } + else + { + *_portData &= ~_pinMaskData; + } + + // set clock high as data is ready + // digitalWrite(_pinClock, HIGH); + *_portClock |= _pinMaskClock; + + // set clock low as data pin is changed + // digitalWrite(_pinClock, LOW); + *_portClock &= ~_pinMaskClock; + } + void transmitByte(uint8_t data) { for (int bit = 7; bit >= 0; bit--)