Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
Lpd6803GenericMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for LPD6803 using general Pins
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 Lpd6803MethodBase
38 {
39 public:
40  typedef typename T_TWOWIRE::SettingsObject SettingsObject;
41 
42  Lpd6803MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
43  _sizeData(pixelCount * elementSize + settingsSize),
44  _sizeFrame((pixelCount + 7) / 8), // bit for every pixel at least
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  Lpd6803MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
53  Lpd6803MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
54  {
55  }
56 #endif
57 
58 
60  {
61  free(_data);
62  }
63 
64  bool IsReadyToUpdate() const
65  {
66  return true; // dot stars don't have a required delay
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 #endif
75 
76  void Initialize()
77  {
78  _wire.begin();
79  }
80 
81  void Update(bool)
82  {
83  const uint8_t startFrame[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  // one bit for every pixel with no less than 1 byte
95  for (size_t frameByte = 0; frameByte < _sizeFrame; frameByte++)
96  {
97  _wire.transmitByte(0x00);
98  }
99 
100  _wire.endTransaction();
101  }
102 
104  {
105  // this method requires update to be called only if changes to buffer
106  return false;
107  }
108 
109  uint8_t* getData() const
110  {
111  return _data;
112  };
113 
114  size_t getDataSize() const
115  {
116  return _sizeData;
117  };
118 
119  void applySettings([[maybe_unused]] const SettingsObject& settings)
120  {
121  _wire.applySettings(settings);
122  }
123 
124 private:
125  const size_t _sizeData; // Size of '_data' buffer below
126  const size_t _sizeFrame;
127 
128  T_TWOWIRE _wire;
129  uint8_t* _data; // Holds LED color values
130 };
131 
133 
134 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
135 #include "TwoWireSpiImple.h"
142 
144 
146 #endif
147 
148 
149 
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed5Mhz > > Lpd6803Spi5MhzMethod
Definition: Lpd6803GenericMethod.h:138
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed500Khz > > Lpd6803Spi500KhzMethod
Definition: Lpd6803GenericMethod.h:141
Lpd6803MethodBase< TwoWireBitBangImple > Lpd6803Method
Definition: Lpd6803GenericMethod.h:132
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeedHz > > Lpd6803SpiHzMethod
Definition: Lpd6803GenericMethod.h:143
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed1Mhz > > Lpd6803Spi1MhzMethod
Definition: Lpd6803GenericMethod.h:140
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed20Mhz > > Lpd6803Spi20MhzMethod
Definition: Lpd6803GenericMethod.h:136
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed10Mhz > > Lpd6803Spi10MhzMethod
Definition: Lpd6803GenericMethod.h:137
Lpd6803Spi10MhzMethod Lpd6803SpiMethod
Definition: Lpd6803GenericMethod.h:145
Lpd6803MethodBase< TwoWireSpiImple< SpiSpeed2Mhz > > Lpd6803Spi2MhzMethod
Definition: Lpd6803GenericMethod.h:139
Definition: Lpd6803GenericMethod.h:38
Lpd6803MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Lpd6803GenericMethod.h:42
void Initialize()
Definition: Lpd6803GenericMethod.h:76
Lpd6803MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Lpd6803GenericMethod.h:52
bool IsReadyToUpdate() const
Definition: Lpd6803GenericMethod.h:64
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: Lpd6803GenericMethod.h:119
T_TWOWIRE::SettingsObject SettingsObject
Definition: Lpd6803GenericMethod.h:40
bool AlwaysUpdate()
Definition: Lpd6803GenericMethod.h:103
~Lpd6803MethodBase()
Definition: Lpd6803GenericMethod.h:59
uint8_t * getData() const
Definition: Lpd6803GenericMethod.h:109
void Update(bool)
Definition: Lpd6803GenericMethod.h:81
size_t getDataSize() const
Definition: Lpd6803GenericMethod.h:114