AceTMI  0.1.0
Unified interface for selecting different implementations for communicating with a TM1637 LED controller chip on Arduino platforms
SoftTmiInterface.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_TMI_SOFT_TMI_INTERFACE_H
26 #define ACE_TMI_SOFT_TMI_INTERFACE_H
27 
28 #include <Arduino.h>
29 
30 namespace ace_tmi {
31 
64  public:
78  explicit SoftTmiInterface(
79  uint8_t dioPin,
80  uint8_t clkPin,
81  uint8_t delayMicros
82  ) :
83  mDioPin(dioPin),
84  mClkPin(clkPin),
85  mDelayMicros(delayMicros)
86  {}
87 
97  void begin() const {
98  digitalWrite(mClkPin, LOW);
99  digitalWrite(mDioPin, LOW);
100 
101  // Begin with both lines at HIGH.
102  clockHigh();
103  dataHigh();
104  }
105 
107  void end() const {
108  clockHigh();
109  dataHigh();
110  }
111 
113  void startCondition() const {
114  clockHigh();
115  dataHigh();
116 
117  dataLow();
118  clockLow();
119  }
120 
122  void stopCondition() const {
123  // clock will always be LOW when this is called
124  dataLow();
125  clockHigh();
126  dataHigh();
127  }
128 
140  uint8_t sendByte(uint8_t data) const {
141  for (uint8_t i = 0; i < 8; ++i) {
142  if (data & 0x1) {
143  dataHigh();
144  } else {
145  dataLow();
146  }
147  clockHigh();
148  clockLow();
149  data >>= 1;
150  }
151 
152  return readAck();
153  }
154 
155  private:
160  uint8_t readAck() const {
161  // Go into INPUT mode, reusing dataHigh(), saving 10 flash bytes on AVR.
162  dataHigh();
163  uint8_t ack = digitalRead(mDioPin);
164 
165  // Device releases DIO upon falling edge of the 9th CLK.
166  clockHigh();
167  clockLow();
168  return ack;
169  }
170 
171  void bitDelay() const { delayMicroseconds(mDelayMicros); }
172 
173  void clockHigh() const { pinMode(mClkPin, INPUT); bitDelay(); }
174 
175  void clockLow() const { pinMode(mClkPin, OUTPUT); bitDelay(); }
176 
177  void dataHigh() const { pinMode(mDioPin, INPUT); bitDelay(); }
178 
179  void dataLow() const { pinMode(mDioPin, OUTPUT); bitDelay(); }
180 
181  private:
182  uint8_t const mDioPin;
183  uint8_t const mClkPin;
184  uint8_t const mDelayMicros;
185 };
186 
187 } // ace_tmi
188 
189 #endif
ace_tmi::SoftTmiInterface
Class that knows how to communicate with a TM1637 chip.
Definition: SoftTmiInterface.h:63
ace_tmi::SoftTmiInterface::stopCondition
void stopCondition() const
Generate the I2C stop condition.
Definition: SoftTmiInterface.h:122
ace_tmi::SoftTmiInterface::startCondition
void startCondition() const
Generate the I2C start condition.
Definition: SoftTmiInterface.h:113
ace_tmi::SoftTmiInterface::end
void end() const
Set dio and clk pins to INPUT mode.
Definition: SoftTmiInterface.h:107
ace_tmi::SoftTmiInterface::SoftTmiInterface
SoftTmiInterface(uint8_t dioPin, uint8_t clkPin, uint8_t delayMicros)
Constructor.
Definition: SoftTmiInterface.h:78
ace_tmi::SoftTmiInterface::begin
void begin() const
Initialize the dio and clk pins.
Definition: SoftTmiInterface.h:97
ace_tmi::SoftTmiInterface::sendByte
uint8_t sendByte(uint8_t data) const
Send the data byte on the data bus, with LSB first instead of the usual MSB first for I2C.
Definition: SoftTmiInterface.h:140