From cc43a2956efe238e82f4af4df8967723133af13c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 31 Dec 2019 00:29:15 -0800 Subject: [PATCH] initial (#326) --- keywords.txt | 5 ++ src/NeoPixelBus.h | 1 + src/internal/Ws2801GenericMethod.h | 130 +++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/internal/Ws2801GenericMethod.h diff --git a/keywords.txt b/keywords.txt index 85c3e3f..a6c19a1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -320,6 +320,11 @@ DotStarSpiMethod KEYWORD1 DotStarSpi20MhzMethod KEYWORD1 DotStarSpi10MhzMethod KEYWORD1 DotStarSpi2MhzMethod KEYWORD1 +NeoWs2801Method KEYWORD1 +NeoWs2801SpiMethod KEYWORD1 +NeoWs2801Spi20MhzMethod KEYWORD1 +NeoWs2801Spi10MhzMethod KEYWORD1 +NeoWs2801Spi2MhzMethod KEYWORD1 Lpd8806Method KEYWORD1 Lpd8806SpiMethod KEYWORD1 Lpd8806Spi20MhzMethod KEYWORD1 diff --git a/src/NeoPixelBus.h b/src/NeoPixelBus.h index 842a893..c716249 100644 --- a/src/NeoPixelBus.h +++ b/src/NeoPixelBus.h @@ -78,6 +78,7 @@ License along with NeoPixel. If not, see #include "internal/DotStarGenericMethod.h" #include "internal/Lpd8806GenericMethod.h" +#include "internal/Ws2801GenericMethod.h" #if defined(ARDUINO_ARCH_ESP8266) diff --git a/src/internal/Ws2801GenericMethod.h b/src/internal/Ws2801GenericMethod.h new file mode 100644 index 0000000..0dd4edc --- /dev/null +++ b/src/internal/Ws2801GenericMethod.h @@ -0,0 +1,130 @@ +/*------------------------------------------------------------------------- +NeoPixel library helper functions for WS2801 + +Written by Michael C. Miller. + +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 Ws2801MethodBase +{ +public: + Ws2801MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) : + _sizePixels(pixelCount * elementSize), + _wire(pinClock, pinData) + { + _pixels = (uint8_t*)malloc(_sizePixels); + memset(_pixels, 0, _sizePixels); + } + +#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny) + Ws2801MethodBase(uint16_t pixelCount, size_t elementSize) : + Ws2801MethodBase(SCK, MOSI, pixelCount, elementSize) + { + } +#endif + + ~Ws2801MethodBase() + { + free(_pixels); + } + + bool IsReadyToUpdate() const + { + uint32_t delta = micros() - _endTime; + + return (delta >= 500); + } + +#if defined(ARDUINO_ARCH_ESP32) + 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(); + + _endTime = micros(); + } + + void Update(bool) + { + while (!IsReadyToUpdate()) + { +#if !defined(ARDUINO_TEEONARDU_LEO) && !defined(ARDUINO_TEEONARDU_FLORA) + yield(); // allows for system yield if needed +#endif + } + + _wire.beginTransaction(); + + // data + _wire.transmitBytes(_pixels, _sizePixels); + + _wire.endTransaction(); + + // save EOD time for latch on next call + _endTime = micros(); + } + + uint8_t* getPixels() const + { + return _pixels; + }; + + size_t getPixelsSize() const + { + return _sizePixels; + }; + +private: + uint32_t _endTime; // Latch timing reference + const size_t _sizePixels; // Size of '_pixels' buffer below + + T_TWOWIRE _wire; + uint8_t* _pixels; // Holds LED color values +}; + +typedef Ws2801MethodBase NeoWs2801Method; + +#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny) +#include "TwoWireSpiImple.h" +typedef Ws2801MethodBase> NeoWs2801Spi20MhzMethod; +typedef Ws2801MethodBase> NeoWs2801Spi10MhzMethod; +typedef Ws2801MethodBase> NeoWs2801Spi2MhzMethod; +typedef NeoWs2801Spi10MhzMethod NeoWs2801SpiMethod; +#endif + + +