AceWire  0.1.0
Unified interface for selecting different I2C implementations on Arduino platforms
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_WIRE_SIMPLE_WIRE_INTERFACE_H
26 #define ACE_WIRE_SIMPLE_WIRE_INTERFACE_H
27 
28 #include <stdint.h>
29 #include <Arduino.h> // pinMode(), digitalWrite()
30 
31 namespace ace_wire {
32 
44  public:
59  uint8_t dataPin, uint8_t clockPin, uint8_t delayMicros
60  ) :
61  mDataPin(dataPin),
62  mClockPin(clockPin),
63  mDelayMicros(delayMicros)
64  {}
65 
74  void begin() const {
75  digitalWrite(mClockPin, LOW);
76  digitalWrite(mDataPin, LOW);
77 
78  // Begin with both lines in INPUT mode to passively go HIGH.
79  clockHigh();
80  dataHigh();
81  }
82 
84  void end() const {
85  clockHigh();
86  dataHigh();
87  }
88 
93  void beginTransmission(uint8_t addr) const {
94  clockHigh();
95  dataHigh();
96 
97  dataLow();
98  clockLow();
99 
100  // Send I2C addr (7 bits) and R/W bit set to "write" (0x00).
101  uint8_t effectiveAddr = (addr << 1) | 0x00;
102  write(effectiveAddr);
103  }
104 
106  void endTransmission() const {
107  // clock will always be LOW when this is called
108  dataLow();
109  clockHigh();
110  dataHigh();
111  }
112 
123  uint8_t write(uint8_t data) const {
124  for (uint8_t i = 0; i < 8; ++i) {
125  if (data & 0x80) {
126  dataHigh();
127  } else {
128  dataLow();
129  }
130  clockHigh();
131  clockLow();
132  data <<= 1;
133  }
134 
135  return readAck();
136  }
137 
138  private:
143  uint8_t readAck() const {
144  // Go into INPUT mode, reusing dataHigh(), saving 10 flash bytes on AVR.
145  dataHigh();
146  uint8_t ack = digitalRead(mDataPin);
147 
148  // Device releases SDA upon falling edge of the 9th CLK.
149  clockHigh();
150  clockLow();
151  return ack;
152  }
153 
154  void bitDelay() const { delayMicroseconds(mDelayMicros); }
155 
156  void clockHigh() const { pinMode(mClockPin, INPUT); bitDelay(); }
157 
158  void clockLow() const { pinMode(mClockPin, OUTPUT); bitDelay(); }
159 
160  void dataHigh() const { pinMode(mDataPin, INPUT); bitDelay(); }
161 
162  void dataLow() const { pinMode(mDataPin, OUTPUT); bitDelay(); }
163 
164  private:
165  uint8_t const mDataPin;
166  uint8_t const mClockPin;
167  uint8_t const mDelayMicros;
168 };
169 
170 }
171 
172 #endif
ace_wire::SimpleWireInterface::end
void end() const
Set clock and data pins to INPUT mode.
Definition: SimpleWireInterface.h:84
ace_wire::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:123
ace_wire::SimpleWireInterface::beginTransmission
void beginTransmission(uint8_t addr) const
Send start condition.
Definition: SimpleWireInterface.h:93
ace_wire::SimpleWireInterface
A software I2C implementation for sending LED segment patterns over I2C.
Definition: SimpleWireInterface.h:43
ace_wire::SimpleWireInterface::endTransmission
void endTransmission() const
Send stop condition.
Definition: SimpleWireInterface.h:106
ace_wire::SimpleWireInterface::SimpleWireInterface
SimpleWireInterface(uint8_t dataPin, uint8_t clockPin, uint8_t delayMicros)
Constructor.
Definition: SimpleWireInterface.h:58
ace_wire::SimpleWireInterface::begin
void begin() const
Initialize the clock and data pins.
Definition: SimpleWireInterface.h:74