Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
P9813GenericMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for P9813s 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 P9813MethodBase
38 {
39 public:
40  typedef typename T_TWOWIRE::SettingsObject SettingsObject;
41 
42  P9813MethodBase(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  P9813MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
53  P9813MethodBase(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 endFrame[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  // end frame
94  _wire.transmitBytes(endFrame, sizeof(endFrame));
95 
96  _wire.endTransaction();
97  }
98 
99  bool AlwaysUpdate()
100  {
101  // this method requires update to be called only if changes to buffer
102  return false;
103  }
104 
105  uint8_t* getData() const
106  {
107  return _data;
108  };
109 
110  size_t getDataSize() const
111  {
112  return _sizeData;
113  };
114 
115  void applySettings([[maybe_unused]] const SettingsObject& settings)
116  {
117  _wire.applySettings(settings);
118  }
119 
120 private:
121  const size_t _sizeData; // Size of '_data' buffer below
122  const size_t _sizeEndFrame;
123 
124  T_TWOWIRE _wire;
125  uint8_t* _data; // Holds LED color values
126 };
127 
129 
130 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
131 #include "TwoWireSpiImple.h"
138 
140 
142 #endif
143 
144 
145 
P9813Spi10MhzMethod P9813SpiMethod
Definition: P9813GenericMethod.h:141
P9813MethodBase< TwoWireSpiImple< SpiSpeed500Khz > > P9813Spi500KhzMethod
Definition: P9813GenericMethod.h:137
P9813MethodBase< TwoWireSpiImple< SpiSpeed10Mhz > > P9813Spi10MhzMethod
Definition: P9813GenericMethod.h:133
P9813MethodBase< TwoWireSpiImple< SpiSpeed1Mhz > > P9813Spi1MhzMethod
Definition: P9813GenericMethod.h:136
P9813MethodBase< TwoWireBitBangImple > P9813Method
Definition: P9813GenericMethod.h:128
P9813MethodBase< TwoWireSpiImple< SpiSpeed20Mhz > > P9813Spi20MhzMethod
Definition: P9813GenericMethod.h:132
P9813MethodBase< TwoWireSpiImple< SpiSpeed5Mhz > > P9813Spi5MhzMethod
Definition: P9813GenericMethod.h:134
P9813MethodBase< TwoWireSpiImple< SpiSpeed2Mhz > > P9813Spi2MhzMethod
Definition: P9813GenericMethod.h:135
P9813MethodBase< TwoWireSpiImple< SpiSpeedHz > > P9813SpiHzMethod
Definition: P9813GenericMethod.h:139
Definition: P9813GenericMethod.h:38
T_TWOWIRE::SettingsObject SettingsObject
Definition: P9813GenericMethod.h:40
size_t getDataSize() const
Definition: P9813GenericMethod.h:110
uint8_t * getData() const
Definition: P9813GenericMethod.h:105
~P9813MethodBase()
Definition: P9813GenericMethod.h:58
bool AlwaysUpdate()
Definition: P9813GenericMethod.h:99
void Update(bool)
Definition: P9813GenericMethod.h:80
void Initialize()
Definition: P9813GenericMethod.h:75
bool IsReadyToUpdate() const
Definition: P9813GenericMethod.h:63
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: P9813GenericMethod.h:115
P9813MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: P9813GenericMethod.h:52
P9813MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: P9813GenericMethod.h:42