AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
SoftTmiFastInterface.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_SOFT_TMI_FAST_INTERFACE_H
26 #define ACE_SEGMENT_SOFT_TMI_FAST_INTERFACE_H
27 
28 // This header file requires the digitalWriteFast library on AVR, or the
29 // EpoxyMockDigitalWriteFast library on EpoxyDuino.
30 #if defined(ARDUINO_ARCH_AVR) || defined(EPOXY_DUINO)
31 
32 #include <stdint.h>
33 #include <Arduino.h> // delayMicroseconds()
34 
35 namespace ace_segment {
36 
72 template <
73  uint8_t T_DIO_PIN,
74  uint8_t T_CLK_PIN,
75  uint8_t T_DELAY_MICROS
76 >
77 class SoftTmiFastInterface {
78  public:
79  explicit SoftTmiFastInterface() = default;
80 
82  void begin() const {
83  // These are open-drain lines, with a pull-up resistor. We must not drive
84  // them HIGH actively since that could damage the transitor at the other
85  // end of the line pulling LOW. Instead, we go into INPUT mode to let the
86  // line to HIGH through the pullup resistor, then go to OUTPUT mode only
87  // to pull down.
88  digitalWriteFast(T_CLK_PIN, LOW);
89  digitalWriteFast(T_DIO_PIN, LOW);
90 
91  // Begin with both lines at HIGH.
92  clockHigh();
93  dataHigh();
94  }
95 
97  void end() const {
98  clockHigh();
99  dataHigh();
100  }
101 
103  void startCondition() const {
104  clockHigh();
105  dataHigh();
106 
107  dataLow();
108  clockLow();
109  }
110 
112  void stopCondition() const {
113  dataLow();
114  clockHigh();
115  dataHigh();
116  }
117 
124  uint8_t sendByte(uint8_t data) const {
125  for (uint8_t i = 0; i < 8; ++i) {
126  if (data & 0x1) {
127  dataHigh();
128  } else {
129  dataLow();
130  }
131  clockHigh();
132  clockLow();
133  data >>= 1;
134  }
135 
136  return readAck();
137  }
138 
139  private:
144  uint8_t readAck() const {
145  // Go into INPUT mode, reusing dataHigh(), saving 6 flash bytes on AVR.
146  dataHigh();
147  uint8_t ack = digitalReadFast(T_DIO_PIN);
148 
149  // Device releases DIO upon falling edge of the 9th CLK.
150  clockHigh();
151  clockLow();
152  return ack;
153  }
154 
155  // The following methods use compile-time constants from the template
156  // parameters. The compiler will optimize away the 'this' pointer so that
157  // these methods become identical to calling static functions.
158 
159  void bitDelay() const { delayMicroseconds(T_DELAY_MICROS); }
160 
161  void clockHigh() const { pinModeFast(T_CLK_PIN, INPUT); bitDelay(); }
162 
163  void clockLow() const { pinModeFast(T_CLK_PIN, OUTPUT); bitDelay(); }
164 
165  void dataHigh() const { pinModeFast(T_DIO_PIN, INPUT); bitDelay(); }
166 
167  void dataLow() const { pinModeFast(T_DIO_PIN, OUTPUT); bitDelay(); }
168 };
169 
170 } // ace_segment
171 
172 #endif // defined(ARDUINO_ARCH_AVR)
173 
174 #endif