AceWire  0.3.2
Unified interface for selecting different I2C implementations on Arduino platforms
TwoWireInterface.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_TWO_WIRE_INTERFACE_H
26 #define ACE_WIRE_TWO_WIRE_INTERFACE_H
27 
28 #include <stdint.h>
29 
30 namespace ace_wire {
31 
49 template <typename T_WIRE>
51  public:
58  explicit TwoWireInterface(T_WIRE& wire) : mWire(wire) {}
59 
61  void begin() {}
62 
64  void end() {}
65 
75  uint8_t beginTransmission(uint8_t addr) {
76  mWire.beginTransmission(addr);
77  return 0;
78  }
79 
88  uint8_t write(uint8_t data) {
89  return (uint8_t) mWire.write(data);
90  }
91 
106  uint8_t endTransmission(bool sendStop) {
107  return mWire.endTransmission(sendStop);
108  }
109 
111  uint8_t endTransmission() {
112  return mWire.endTransmission();
113  }
114 
124  uint8_t requestFrom(uint8_t addr, uint8_t quantity, bool sendStop) {
125  return mWire.requestFrom(addr, quantity, (uint8_t) sendStop);
126  }
127 
140  uint8_t requestFrom(uint8_t addr, uint8_t quantity) {
141  return mWire.requestFrom(addr, quantity);
142  }
143 
145  uint8_t read() {
146  return mWire.read();
147  }
148 
149  // Use default copy constructor and assignment operator.
150  TwoWireInterface(const TwoWireInterface&) = default;
151  TwoWireInterface& operator=(const TwoWireInterface&) = default;
152 
153  private:
154  T_WIRE& mWire;
155 };
156 
157 }
158 
159 #endif
ace_wire::TwoWireInterface::TwoWireInterface
TwoWireInterface(T_WIRE &wire)
Constructor.
Definition: TwoWireInterface.h:58
ace_wire::TwoWireInterface::endTransmission
uint8_t endTransmission()
Same as endTransmission(bool) but always send STOP condition.
Definition: TwoWireInterface.h:111
ace_wire::TwoWireInterface::read
uint8_t read()
Read byte from buffer.
Definition: TwoWireInterface.h:145
ace_wire::TwoWireInterface::end
void end()
End the interface.
Definition: TwoWireInterface.h:64
ace_wire::TwoWireInterface::endTransmission
uint8_t endTransmission(bool sendStop)
Send the data in the buffer, with a STOP condition if sendStop is true.
Definition: TwoWireInterface.h:106
ace_wire::TwoWireInterface
A thin wrapper around an I2C TwoWire class and its Wire object.
Definition: TwoWireInterface.h:50
ace_wire::TwoWireInterface::write
uint8_t write(uint8_t data)
Write data into the write buffer.
Definition: TwoWireInterface.h:88
ace_wire::TwoWireInterface::begin
void begin()
Initial the interface.
Definition: TwoWireInterface.h:61
ace_wire::TwoWireInterface::beginTransmission
uint8_t beginTransmission(uint8_t addr)
Prepare the write buffer to accept a sequence of data, and save the addr for transmission when endTra...
Definition: TwoWireInterface.h:75
ace_wire::TwoWireInterface::requestFrom
uint8_t requestFrom(uint8_t addr, uint8_t quantity)
Read bytes from the slave and store in buffer owned by T_WIRE, and always send a STOP condition.
Definition: TwoWireInterface.h:140
ace_wire::TwoWireInterface::requestFrom
uint8_t requestFrom(uint8_t addr, uint8_t quantity, bool sendStop)
Read bytes from the slave and store in buffer owned by T_WIRE and send a STOP condition if sendStop i...
Definition: TwoWireInterface.h:124