AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
SimpleWireFastInterface.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_SIMPLE_WIRE_FAST_INTERFACE_H
26 #define ACE_SEGMENT_SIMPLE_WIRE_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 
47 template <
48  uint8_t T_DATA_PIN,
49  uint8_t T_CLOCK_PIN,
50  uint8_t T_DELAY_MICROS
51 >
52 class SimpleWireFastInterface {
53  public:
58  SimpleWireFastInterface(uint8_t addr) : mAddr(addr) {}
59 
61  void begin() const {
62  // These are open-drain lines, with a pull-up resistor. We must not drive
63  // them HIGH actively since that could damage the transitor at the other
64  // end of the line pulling LOW. Instead, we go into INPUT mode to let the
65  // line to HIGH through the pullup resistor, then go to OUTPUT mode only
66  // to pull down.
67  digitalWriteFast(T_CLOCK_PIN, LOW);
68  digitalWriteFast(T_DATA_PIN, LOW);
69 
70  // Begin with both lines at HIGH.
71  clockHigh();
72  dataHigh();
73  }
74 
76  void end() const {
77  clockHigh();
78  dataHigh();
79  }
80 
82  void beginTransmission() const {
83  clockHigh();
84  dataHigh();
85 
86  dataLow();
87  clockLow();
88 
89  // Send I2C addr (7 bits) and R/W bit set to "write" (0x00).
90  uint8_t effectiveAddr = (mAddr << 1) | 0x00;
91  write(effectiveAddr);
92  }
93 
95  void endTransmission() const {
96  dataLow();
97  clockHigh();
98  dataHigh();
99  }
100 
106  uint8_t write(uint8_t data) const {
107  for (uint8_t i = 0; i < 8; ++i) {
108  if (data & 0x80) {
109  dataHigh();
110  } else {
111  dataLow();
112  }
113  clockHigh();
114  clockLow();
115  data <<= 1;
116  }
117 
118  return readAck();
119  }
120 
121  private:
126  uint8_t readAck() const {
127  // Go into INPUT mode, reusing dataHigh(), saving 10 flash bytes on AVR.
128  dataHigh();
129  uint8_t ack = digitalReadFast(T_DATA_PIN);
130 
131  // Device releases SDA upon falling edge of the 9th CLK.
132  clockHigh();
133  clockLow();
134  return ack;
135  }
136 
137  // The following methods use compile-time constants from the template
138  // parameters. The compiler will optimize away the 'this' pointer so that
139  // these methods become identical to calling static functions.
140 
141  void bitDelay() const { delayMicroseconds(T_DELAY_MICROS); }
142 
143  void clockHigh() const { pinModeFast(T_CLOCK_PIN, INPUT); bitDelay(); }
144 
145  void clockLow() const { pinModeFast(T_CLOCK_PIN, OUTPUT); bitDelay(); }
146 
147  void dataHigh() const { pinModeFast(T_DATA_PIN, INPUT); bitDelay(); }
148 
149  void dataLow() const { pinModeFast(T_DATA_PIN, OUTPUT); bitDelay(); }
150 
151  private:
152  uint8_t const mAddr;
153 };
154 
155 }
156 
157 #endif // defined(ARDUINO_ARCH_AVR)
158 
159 #endif