Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
Ws2801GenericMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for WS2801
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 
27 #pragma once
28 
29 // must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set
30 #if defined(ARDUINO_ARCH_AVR) && !defined(__arm__)
31 #include "TwoWireBitBangImpleAvr.h"
32 #else
33 #include "TwoWireBitBangImple.h"
34 #endif
35 
36 
37 template<typename T_TWOWIRE> class Ws2801MethodBase
38 {
39 public:
40  typedef typename T_TWOWIRE::SettingsObject SettingsObject;
41 
42  Ws2801MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
43  _sizeData(pixelCount * elementSize + settingsSize),
44  _wire(pinClock, pinData)
45  {
46  _data = static_cast<uint8_t*>(malloc(_sizeData));
47  // data cleared later in Begin()
48  }
49 
50 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
51  Ws2801MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
52  Ws2801MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
53  {
54  }
55 #endif
56 
58  {
59  free(_data);
60  }
61 
62  bool IsReadyToUpdate() const
63  {
64  uint32_t delta = micros() - _endTime;
65 
66  return (delta >= 500);
67  }
68 
69 #if defined(ARDUINO_ARCH_ESP32)
70  void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
71  {
72  _wire.begin(sck, miso, mosi, ss);
73 
74  _endTime = micros();
75  }
76 #endif
77 
78  void Initialize()
79  {
80  _wire.begin();
81 
82  _endTime = micros();
83  }
84 
85  void Update(bool)
86  {
87  while (!IsReadyToUpdate())
88  {
89 #if !defined(ARDUINO_TEEONARDU_LEO) && !defined(ARDUINO_TEEONARDU_FLORA)
90  yield(); // allows for system yield if needed
91 #endif
92  }
93 
94  _wire.beginTransaction();
95 
96  // data
97  _wire.transmitBytes(_data, _sizeData);
98 
99  _wire.endTransaction();
100 
101  // save EOD time for latch on next call
102  _endTime = micros();
103  }
104 
106  {
107  // this method requires update to be called only if changes to buffer
108  return false;
109  }
110 
111  uint8_t* getData() const
112  {
113  return _data;
114  };
115 
116  size_t getDataSize() const
117  {
118  return _sizeData;
119  };
120 
121  void applySettings([[maybe_unused]] const SettingsObject& settings)
122  {
123  _wire.applySettings(settings);
124  }
125 
126 private:
127  const size_t _sizeData; // Size of '_data' buffer below
128 
129  uint32_t _endTime; // Latch timing reference
130  T_TWOWIRE _wire;
131  uint8_t* _data; // Holds LED color values
132 };
133 
135 
136 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
137 #include "TwoWireSpiImple.h"
144 
146 
148 #endif
149 
150 
151 
Ws2801MethodBase< TwoWireSpiImple< SpiSpeedHz > > Ws2801SpiHzMethod
Definition: Ws2801GenericMethod.h:145
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed2Mhz > > Ws2801Spi2MhzMethod
Definition: Ws2801GenericMethod.h:141
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed20Mhz > > Ws2801Spi20MhzMethod
Definition: Ws2801GenericMethod.h:138
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed5Mhz > > Ws2801Spi5MhzMethod
Definition: Ws2801GenericMethod.h:140
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed500Khz > > Ws2801Spi500KhzMethod
Definition: Ws2801GenericMethod.h:143
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed10Mhz > > Ws2801Spi10MhzMethod
Definition: Ws2801GenericMethod.h:139
Ws2801MethodBase< TwoWireBitBangImple > Ws2801Method
Definition: Ws2801GenericMethod.h:134
Ws2801MethodBase< TwoWireSpiImple< SpiSpeed1Mhz > > Ws2801Spi1MhzMethod
Definition: Ws2801GenericMethod.h:142
Ws2801Spi10MhzMethod Ws2801SpiMethod
Definition: Ws2801GenericMethod.h:147
Definition: Ws2801GenericMethod.h:38
~Ws2801MethodBase()
Definition: Ws2801GenericMethod.h:57
T_TWOWIRE::SettingsObject SettingsObject
Definition: Ws2801GenericMethod.h:40
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: Ws2801GenericMethod.h:121
void Initialize()
Definition: Ws2801GenericMethod.h:78
Ws2801MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Ws2801GenericMethod.h:51
uint8_t * getData() const
Definition: Ws2801GenericMethod.h:111
void Update(bool)
Definition: Ws2801GenericMethod.h:85
bool AlwaysUpdate()
Definition: Ws2801GenericMethod.h:105
Ws2801MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Ws2801GenericMethod.h:42
size_t getDataSize() const
Definition: Ws2801GenericMethod.h:116
bool IsReadyToUpdate() const
Definition: Ws2801GenericMethod.h:62