Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
Sm16716GenericMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for SM16716 using general Pins
3 
4 Written by Michael C. Miller.
5 Contributed by Ivo H (ivoh95)
6 
7 I invest time and resources providing this open source code,
8 please support me by dontating (see https://github.com/Makuna)
9 
10 -------------------------------------------------------------------------
11 This file is part of the LUMITRONIX_iFlex_Workshop library.
12 
13 LumitronixIFlexBus is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation, either version 3 of
16 the License, or (at your option) any later version.
17 
18 LumitronixIFlexBus is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public
24 License along with LumitronixIFlex. If not, see
25 <http://www.gnu.org/licenses/>.
26 -------------------------------------------------------------------------*/
27 
28 #pragma once
29 
30 // must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set
31 #if defined(ARDUINO_ARCH_AVR) && !defined(__arm__)
32 #include "TwoWireBitBangImpleAvr.h"
33 #else
34 #include "TwoWireBitBangImple.h"
35 #endif
36 
37 
38 template<typename T_TWOWIRE> class Sm16716MethodBase
39 {
40 public:
41  typedef typename T_TWOWIRE::SettingsObject SettingsObject;
42 
43  Sm16716MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
44  _sizeData(pixelCount* elementSize + settingsSize),
45  _sizeFrame(6), // 48 bits
46  _wire(pinClock, pinData)
47  {
48  _data = static_cast<uint8_t*>(malloc(_sizeData));
49  memset(_data, 0, _sizeData);
50  }
51 
52 #if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
53  Sm16716MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
54  Sm16716MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
55  {
56  }
57 #endif
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  // can't support hardware SPI due to weird extra bits
71  //void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
72  //{
73  // _wire.begin(sck, miso, mosi, ss);
74  //}
75 #endif
76 
77  void Initialize()
78  {
79  _wire.begin();
80  }
81 
82  void Update(bool)
83  {
84  _wire.beginTransaction();
85 
86  // start frame
87  for (size_t frameBytes = 0; frameBytes < _sizeFrame; frameBytes++)
88  {
89  _wire.transmitByte(0x00);
90  }
91  _wire.transmitBit(LOW);
92  _wire.transmitBit(LOW); // two extra 0s to make the 50 0 header
93  _wire.transmitBit(HIGH); // one to start the led frame
94 
95  for (size_t pixel = 0; pixel < (_sizeData / 3); pixel++)
96  {
97  _wire.transmitByte(_data[pixel]);
98  _wire.transmitByte(_data[pixel + 1]);
99  _wire.transmitByte(_data[pixel + 2]);
100  _wire.transmitBit(HIGH); //show the color and start the next frame
101  }
102 
103  _wire.endTransaction();
104  }
105 
107  {
108  // this method requires update to be called only if changes to buffer
109  return false;
110  }
111 
112  uint8_t* getData() const
113  {
114  return _data;
115  };
116 
117  size_t getDataSize() const
118  {
119  return _sizeData;
120  };
121 
122  void applySettings([[maybe_unused]] const SettingsObject& settings)
123  {
124  _wire.applySettings(settings);
125  }
126 
127 private:
128  const size_t _sizeData; // Size of '_data' buffer below
129  const size_t _sizeFrame;
130 
131  T_TWOWIRE _wire;
132  uint8_t* _data; // Holds LED color values
133 };
134 
135 // can ONLY support our bitbang for wire due to requirement for custom transmitBit method
136 // to handle not byte oriented data stream
137 //
Sm16716MethodBase< TwoWireBitBangImple > Sm16716Method
Definition: Sm16716GenericMethod.h:138
Definition: Sm16716GenericMethod.h:39
void Update(bool)
Definition: Sm16716GenericMethod.h:82
bool AlwaysUpdate()
Definition: Sm16716GenericMethod.h:106
uint8_t * getData() const
Definition: Sm16716GenericMethod.h:112
Sm16716MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Sm16716GenericMethod.h:43
T_TWOWIRE::SettingsObject SettingsObject
Definition: Sm16716GenericMethod.h:41
Sm16716MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize)
Definition: Sm16716GenericMethod.h:53
bool IsReadyToUpdate() const
Definition: Sm16716GenericMethod.h:64
void Initialize()
Definition: Sm16716GenericMethod.h:77
size_t getDataSize() const
Definition: Sm16716GenericMethod.h:117
~Sm16716MethodBase()
Definition: Sm16716GenericMethod.h:59
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: Sm16716GenericMethod.h:122