Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
DotStarGenericMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for DotStars using general Pins (APA102).
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 DotStarMethodBase
38 {
39 public:
40  typedef typename T_TWOWIRE::SettingsObject SettingsObject;
41 
42  DotStarMethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
43  _sizeData(pixelCount * elementSize + settingsSize),
44  _sizeEndFrame((pixelCount + 15) / 16), // 16 = div 2 (bit for every two pixels) div 8 (bits to bytes)
45  _wire(pinClock, pinData)
46  {
47  _data = static_cast<uint8_t*>(malloc(_sizeData));
48  // data cleared later in Begin()
49  }
50 
51 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
52  DotStarMethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
53  DotStarMethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
54  {
55  }
56 #endif
57 
59  {
60  free(_data);
61  }
62 
63  bool IsReadyToUpdate() const
64  {
65  return true; // dot stars don't have a required delay
66  }
67 
68 #if defined(ARDUINO_ARCH_ESP32)
69  void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
70  {
71  _wire.begin(sck, miso, mosi, ss);
72  }
73 #endif
74 
75  void Initialize()
76  {
77  _wire.begin();
78  }
79 
80  void Update(bool)
81  {
82  const uint8_t startFrame[4] = { 0x00 };
83  const uint8_t resetFrame[4] = { 0x00 };
84 
85  _wire.beginTransaction();
86 
87  // start frame
88  _wire.transmitBytes(startFrame, sizeof(startFrame));
89 
90  // data
91  _wire.transmitBytes(_data, _sizeData);
92 
93  // reset frame
94  _wire.transmitBytes(resetFrame, sizeof(resetFrame));
95 
96  // end frame
97 
98  // one bit for every two pixels with no less than 1 byte
99  for (size_t endFrameByte = 0; endFrameByte < _sizeEndFrame; endFrameByte++)
100  {
101  _wire.transmitByte(0x00);
102  }
103 
104  _wire.endTransaction();
105  }
106 
108  {
109  // this method requires update to be called only if changes to buffer
110  return false;
111  }
112 
113  uint8_t* getData() const
114  {
115  return _data;
116  };
117 
118  size_t getDataSize() const
119  {
120  return _sizeData;
121  };
122 
123  void applySettings([[maybe_unused]] const SettingsObject& settings)
124  {
125  _wire.applySettings(settings);
126  }
127 
128 private:
129  const size_t _sizeData; // Size of '_data' buffer below
130  const size_t _sizeEndFrame;
131 
132  T_TWOWIRE _wire;
133  uint8_t* _data; // Holds LED color values
134 };
135 
137 
138 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
139 #include "TwoWireSpiImple.h"
148 
150 #endif
151 
152 #if defined(ARDUINO_ARCH_ESP32)
153 // Give option to use Vspi alias of Spi class if wanting to specify which SPI peripheral is used on the ESP32
154 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed40Mhz>> DotStarEsp32Vspi40MhzMethod;
155 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed20Mhz>> DotStarEsp32Vspi20MhzMethod;
156 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed10Mhz>> DotStarEsp32Vspi10MhzMethod;
157 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed5Mhz>> DotStarEsp32Vspi5MhzMethod;
158 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed2Mhz>> DotStarEsp32Vspi2MhzMethod;
159 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed1Mhz>> DotStarEsp32Vspi1MhzMethod;
160 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed500Khz>> DotStarEsp32Vspi500KhzMethod;
161 typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeedHz>> DotStarEsp32VspiHzMethod;
162 
163 typedef DotStarSpi10MhzMethod DotStarEsp32VspiMethod;
164 
165 #include "TwoWireHspiImple.h"
166 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed40Mhz>> DotStarEsp32Hspi40MhzMethod;
167 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed20Mhz>> DotStarEsp32Hspi20MhzMethod;
168 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed10Mhz>> DotStarEsp32Hspi10MhzMethod;
169 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed5Mhz>> DotStarEsp32Hspi5MhzMethod;
170 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed2Mhz>> DotStarEsp32Hspi2MhzMethod;
171 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed1Mhz>> DotStarEsp32Hspi1MhzMethod;
172 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeed500Khz>> DotStarEsp32Hspi500KhzMethod;
173 typedef DotStarMethodBase<TwoWireHspiImple<SpiSpeedHz>> DotStarEsp32HspiHzMethod;
174 
175 typedef DotStarEsp32Hspi10MhzMethod DotStarEsp32HspiMethod;
176 #endif
DotStarMethodBase< TwoWireSpiImple< SpiSpeedHz > > DotStarSpiHzMethod
Definition: DotStarGenericMethod.h:147
DotStarMethodBase< TwoWireSpiImple< SpiSpeed40Mhz > > DotStarSpi40MhzMethod
Definition: DotStarGenericMethod.h:140
DotStarMethodBase< TwoWireSpiImple< SpiSpeed500Khz > > DotStarSpi500KhzMethod
Definition: DotStarGenericMethod.h:146
DotStarMethodBase< TwoWireSpiImple< SpiSpeed2Mhz > > DotStarSpi2MhzMethod
Definition: DotStarGenericMethod.h:144
DotStarSpi10MhzMethod DotStarSpiMethod
Definition: DotStarGenericMethod.h:149
DotStarMethodBase< TwoWireSpiImple< SpiSpeed10Mhz > > DotStarSpi10MhzMethod
Definition: DotStarGenericMethod.h:142
DotStarMethodBase< TwoWireBitBangImple > DotStarMethod
Definition: DotStarGenericMethod.h:136
DotStarMethodBase< TwoWireSpiImple< SpiSpeed20Mhz > > DotStarSpi20MhzMethod
Definition: DotStarGenericMethod.h:141
DotStarMethodBase< TwoWireSpiImple< SpiSpeed1Mhz > > DotStarSpi1MhzMethod
Definition: DotStarGenericMethod.h:145
DotStarMethodBase< TwoWireSpiImple< SpiSpeed5Mhz > > DotStarSpi5MhzMethod
Definition: DotStarGenericMethod.h:143
Definition: DotStarGenericMethod.h:38
bool AlwaysUpdate()
Definition: DotStarGenericMethod.h:107
size_t getDataSize() const
Definition: DotStarGenericMethod.h:118
void Update(bool)
Definition: DotStarGenericMethod.h:80
T_TWOWIRE::SettingsObject SettingsObject
Definition: DotStarGenericMethod.h:40
bool IsReadyToUpdate() const
Definition: DotStarGenericMethod.h:63
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: DotStarGenericMethod.h:123
DotStarMethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: DotStarGenericMethod.h:52
void Initialize()
Definition: DotStarGenericMethod.h:75
~DotStarMethodBase()
Definition: DotStarGenericMethod.h:58
DotStarMethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: DotStarGenericMethod.h:42
uint8_t * getData() const
Definition: DotStarGenericMethod.h:113