AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
HardSpiFastInterface.h
1 /*
2 MIT License
3 
4 Copyright (c) 2021 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_SEGMENT_HARD_SPI_FAST_INTERFACE_H
26 #define ACE_SEGMENT_HARD_SPI_FAST_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h>
30 #include <SPI.h>
31 
32 namespace ace_segment {
33 
59 template <typename T_SPI, uint8_t T_LATCH_PIN>
61  private:
62  // The following constants are defined without including <SPI.h> to avoid
63  // pulling in the global SPI instance into applications which don't use SPI.
64  // They may become template parameters in the future.
65 
67  static const uint32_t kClockSpeed = 8000000;
68 
70  #if defined(ARDUINO_ARCH_STM32)
71  static const BitOrder kBitOrder = MSBFIRST;
72  #else
73  static const uint8_t kBitOrder = MSBFIRST;
74  #endif
75 
77  static const uint8_t kSpiMode = SPI_MODE0;
78 
79  public:
80  HardSpiFastInterface(T_SPI& spi) : mSpi(spi) {}
81 
86  void begin() const {
87  // To use Hardware SPI on ESP8266, we must set the SCK and MOSI pins to
88  // 'SPECIAL' instead of 'OUTPUT'. This is performed by calling
89  // SPI.begin(). Also, unlike other Arduino platforms, the SPIClass on
90  // the ESP8266 defaults to controlling the SS/CS pin itself, instead of
91  // letting the application code control it. The setHwCs(false) let's
92  // HardSpiInterface control the CS/SS pin.
93  // https://www.esp8266.com/wiki/doku.php?id=esp8266_gpio_pin_allocations
94  #if defined(ESP8266)
95  mSpi.setHwCs(false);
96  #endif
97 
98  pinModeFast(T_LATCH_PIN, OUTPUT);
99  }
100 
101  void end() const {
102  pinModeFast(T_LATCH_PIN, INPUT);
103  }
104 
106  void send8(uint8_t value) const {
107  mSpi.beginTransaction(SPISettings(kClockSpeed, kBitOrder, kSpiMode));
108  digitalWriteFast(T_LATCH_PIN, LOW);
109  mSpi.transfer(value);
110  digitalWriteFast(T_LATCH_PIN, HIGH);
111  mSpi.endTransaction();
112  }
113 
115  void send16(uint16_t value) const {
116  mSpi.beginTransaction(SPISettings(kClockSpeed, kBitOrder, kSpiMode));
117  digitalWriteFast(T_LATCH_PIN, LOW);
118  mSpi.transfer16(value);
119  digitalWriteFast(T_LATCH_PIN, HIGH);
120  mSpi.endTransaction();
121  }
122 
123  void send16(uint8_t msb, uint8_t lsb) const {
124  uint16_t value = ((uint16_t) msb) << 8 | (uint16_t) lsb;
125  send16(value);
126  }
127 
128  private:
129  T_SPI& mSpi;
130 };
131 
132 } // ace_segment
133 
134 #endif
ace_segment::HardSpiFastInterface::send8
void send8(uint8_t value) const
Send 8 bits, including latching LOW and HIGH.
Definition: HardSpiFastInterface.h:106
ace_segment::HardSpiFastInterface
Hardware SPI interface to talk to one or two 74HC595 Shift Register chip(s), using the predefined SPI...
Definition: HardSpiFastInterface.h:60
ace_segment::HardSpiFastInterface::send16
void send16(uint16_t value) const
Send 16 bits, including latching LOW and HIGH.
Definition: HardSpiFastInterface.h:115
ace_segment::HardSpiFastInterface::begin
void begin() const
Initialize the HardSpiInterface.
Definition: HardSpiFastInterface.h:86