Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
TwoWireHspiImple.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 LumitronixIFlex library helper functions for DotStars using ESP32's alternate SPI (HSPI) (APA102/LPD8806).
3 
4 Written by Michael C. Miller.
5 Minor changes adapting TwoWireSpiImple to support HSPI by Louis Beaudoin (Pixelvation)
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 #include <SPI.h>
31 
32 template<typename T_SPISPEED> class TwoWireHspiImple
33 {
34 public:
35  typedef typename T_SPISPEED::SettingsObject SettingsObject;
36 
37  TwoWireHspiImple(uint8_t, uint8_t) // clock and data pins ignored for hardware SPI
38  {
39  _hspi = new SPIClass(HSPI);
40  }
41 
43  {
44  _hspi->end();
45  delete _hspi;
46  }
47 
48 #if defined(ARDUINO_ARCH_ESP32)
49  // for cases where hardware SPI can have pins changed
50  void begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
51  {
52  _hspi->begin(sck, miso, mosi, ss);
53  }
54 #endif
55 
56  void begin()
57  {
58  _hspi->begin();
59  }
60 
62  {
63  _hspi->beginTransaction(SPISettings(_speed.Clock, MSBFIRST, SPI_MODE0));
64  }
65 
67  {
68  _hspi->endTransaction();
69  }
70 
71  void transmitByte(uint8_t data)
72  {
73  _hspi->transfer(data);
74  }
75 
76  void transmitBytes(const uint8_t* data, size_t dataSize)
77  {
78  // ESPs have a method to write without inplace overwriting the send buffer
79  // since we don't care what gets received, use it for performance
80  // FIX: but for what ever reason on Esp32, its not const
81  _hspi->writeBytes(const_cast<uint8_t*>(data), dataSize);
82  }
83 
84  void applySettings([[maybe_unused]] const SettingsObject& settings)
85  {
86  _speed.applySettings(settings);
87  }
88 
89 private:
90  SPIClass * _hspi = NULL;
91  T_SPISPEED _speed;
92 };
Definition: TwoWireHspiImple.h:33
void transmitBytes(const uint8_t *data, size_t dataSize)
Definition: TwoWireHspiImple.h:76
void begin()
Definition: TwoWireHspiImple.h:56
~TwoWireHspiImple()
Definition: TwoWireHspiImple.h:42
void endTransaction()
Definition: TwoWireHspiImple.h:66
T_SPISPEED::SettingsObject SettingsObject
Definition: TwoWireHspiImple.h:35
void beginTransaction()
Definition: TwoWireHspiImple.h:61
void transmitByte(uint8_t data)
Definition: TwoWireHspiImple.h:71
void applySettings([[maybe_unused]] const SettingsObject &settings)
Definition: TwoWireHspiImple.h:84
TwoWireHspiImple(uint8_t, uint8_t)
Definition: TwoWireHspiImple.h:37