AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
SimpleWireInterface.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_INTERFACE_H
26 #define ACE_SEGMENT_SIMPLE_WIRE_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pinMode(), digitalWrite()
30 
31 namespace ace_segment {
32 
44  public:
54  uint8_t addr, uint8_t dataPin, uint8_t clockPin, uint8_t delayMicros
55  ) :
56  mAddr(addr),
57  mDataPin(dataPin),
58  mClockPin(clockPin),
59  mDelayMicros(delayMicros)
60  {}
61 
63  void begin() const {
64  // These are open-drain lines, with a pull-up resistor. We must not drive
65  // them HIGH actively since that could damage the transitor at the other
66  // end of the line pulling LOW. Instead, we go into INPUT mode to let the
67  // line to HIGH through the pullup resistor, then go to OUTPUT mode only
68  // to pull down.
69  digitalWrite(mClockPin, LOW);
70  digitalWrite(mDataPin, LOW);
71 
72  // Begin with both lines at HIGH.
73  clockHigh();
74  dataHigh();
75  }
76 
78  void end() const {
79  clockHigh();
80  dataHigh();
81  }
82 
84  void beginTransmission() const {
85  clockHigh();
86  dataHigh();
87 
88  dataLow();
89  clockLow();
90 
91  // Send I2C addr (7 bits) and R/W bit set to "write" (0x00).
92  uint8_t effectiveAddr = (mAddr << 1) | 0x00;
93  write(effectiveAddr);
94  }
95 
97  void endTransmission() const {
98  dataLow();
99  clockHigh();
100  dataHigh();
101  }
102 
108  uint8_t write(uint8_t data) const {
109  for (uint8_t i = 0; i < 8; ++i) {
110  if (data & 0x80) {
111  dataHigh();
112  } else {
113  dataLow();
114  }
115  clockHigh();
116  clockLow();
117  data <<= 1;
118  }
119 
120  return readAck();
121  }
122 
123  private:
128  uint8_t readAck() const {
129  // Go into INPUT mode, reusing dataHigh(), saving 10 flash bytes on AVR.
130  dataHigh();
131  uint8_t ack = digitalRead(mDataPin);
132 
133  // Device releases SDA upon falling edge of the 9th CLK.
134  clockHigh();
135  clockLow();
136  return ack;
137  }
138 
139  void bitDelay() const { delayMicroseconds(mDelayMicros); }
140 
141  void clockHigh() const { pinMode(mClockPin, INPUT); bitDelay(); }
142 
143  void clockLow() const { pinMode(mClockPin, OUTPUT); bitDelay(); }
144 
145  void dataHigh() const { pinMode(mDataPin, INPUT); bitDelay(); }
146 
147  void dataLow() const { pinMode(mDataPin, OUTPUT); bitDelay(); }
148 
149  private:
150  uint8_t const mAddr;
151  uint8_t const mDataPin;
152  uint8_t const mClockPin;
153  uint8_t const mDelayMicros;
154 };
155 
156 }
157 
158 #endif
ace_segment::SimpleWireInterface::write
uint8_t write(uint8_t data) const
Send the data byte on the data bus, with MSB first as specified by I2C.
Definition: SimpleWireInterface.h:108
ace_segment::SimpleWireInterface
A software I2C implementation for sending LED segment patterns over I2C.
Definition: SimpleWireInterface.h:43
ace_segment::SimpleWireInterface::end
void end() const
Set clock and data pins to INPUT mode.
Definition: SimpleWireInterface.h:78
ace_segment::SimpleWireInterface::beginTransmission
void beginTransmission() const
Send start condition.
Definition: SimpleWireInterface.h:84
ace_segment::SimpleWireInterface::begin
void begin() const
Initialize the clock and data pins.
Definition: SimpleWireInterface.h:63
ace_segment::SimpleWireInterface::endTransmission
void endTransmission() const
Send stop condition.
Definition: SimpleWireInterface.h:97
ace_segment::SimpleWireInterface::SimpleWireInterface
SimpleWireInterface(uint8_t addr, uint8_t dataPin, uint8_t clockPin, uint8_t delayMicros)
Constructor.
Definition: SimpleWireInterface.h:53