Skip to content

Commit 2030ce0

Browse files
committed
Minimal Wire.h mock
Most functions are empty and unfinished, so you will not (yet) be able to unit test I2C communication. But it will allow you to unit test code that is using libraries that uses I2C under the hood (e.g. LCD, RTC, etc).
1 parent 1e3f6c4 commit 2030ce0

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

cpp/arduino/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Where possible, variable names from the Arduino library are used to avoid confli
1515
#include "Stream.h"
1616
#include "HardwareSerial.h"
1717
#include "SPI.h"
18+
#include "Wire.h"
1819
#include "Nullptr.h"
1920

2021
typedef bool boolean;

cpp/arduino/Godmode.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Godmode.h"
22
#include "HardwareSerial.h"
33
#include "SPI.h"
4+
#include "Wire.h"
45

56
GodmodeState godmode = GodmodeState();
67

@@ -101,3 +102,6 @@ inline std::ostream& operator << ( std::ostream& out, const PinHistory<T>& ph )
101102

102103
// defined in SPI.h
103104
SPIClass SPI = SPIClass(&godmode.spi.dataIn, &godmode.spi.dataOut);
105+
106+
// defined in Wire.h
107+
TwoWire Wire = TwoWire();

cpp/arduino/Wire.h

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
2+
#pragma once
3+
4+
#include <inttypes.h>
5+
#include "Stream.h"
6+
7+
class TwoWire : public ObservableDataStream
8+
{
9+
public:
10+
TwoWire() {
11+
}
12+
13+
// https://www.arduino.cc/en/Reference/WireBegin
14+
// Initiate the Wire library and join the I2C bus as a master or slave. This should normally be called only once.
15+
void begin() {
16+
isMaster = true;
17+
}
18+
void begin(int address) {
19+
i2cAddress = address;
20+
isMaster = false;
21+
}
22+
void begin(uint8_t address) {
23+
begin((int)address);
24+
}
25+
void end() {
26+
// TODO: implement
27+
}
28+
29+
// https://www.arduino.cc/en/Reference/WireSetClock
30+
// This function modifies the clock frequency for I2C communication. I2C slave devices have no minimum working
31+
// clock frequency, however 100KHz is usually the baseline.
32+
void setClock(uint32_t) {
33+
// TODO: implement?
34+
}
35+
36+
// https://www.arduino.cc/en/Reference/WireBeginTransmission
37+
// Begin a transmission to the I2C slave device with the given address. Subsequently, queue bytes for
38+
// transmission with the write() function and transmit them by calling endTransmission().
39+
void beginTransmission(int address) {
40+
// TODO: implement
41+
}
42+
void beginTransmission(uint8_t address) {
43+
beginTransmission((int)address);
44+
}
45+
46+
// https://www.arduino.cc/en/Reference/WireEndTransmission
47+
// Ends a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were
48+
// queued by write().
49+
uint8_t endTransmission(uint8_t sendStop) {
50+
// TODO: implement
51+
return 0; // success
52+
}
53+
uint8_t endTransmission(void) {
54+
return endTransmission((uint8_t)true);
55+
}
56+
57+
// https://www.arduino.cc/en/Reference/WireRequestFrom
58+
// Used by the master to request bytes from a slave device. The bytes may then be retrieved with the
59+
// available() and read() functions.
60+
uint8_t requestFrom(int address, int quantity, int stop) {
61+
// TODO: implement
62+
return 0; // number of bytes returned from the slave device
63+
}
64+
uint8_t requestFrom(int address, int quantity) {
65+
int stop = true;
66+
return requestFrom(address, quantity, stop);
67+
}
68+
uint8_t requestFrom(uint8_t address, uint8_t quantity) {
69+
return requestFrom((int)address, (int)quantity);
70+
}
71+
uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t stop) {
72+
return requestFrom((int)address, (int)quantity, (int)stop);
73+
}
74+
uint8_t requestFrom(uint8_t, uint8_t, uint32_t, uint8_t, uint8_t) {
75+
// TODO: implement
76+
return 0;
77+
}
78+
79+
// https://www.arduino.cc/en/Reference/WireWrite
80+
// Writes data from a slave device in response to a request from a master, or queues bytes for transmission from a
81+
// master to slave device (in-between calls to beginTransmission() and endTransmission()).
82+
size_t write(uint8_t value) {
83+
// TODO: implement
84+
return 0; // number of bytes written
85+
}
86+
size_t write(const char *str) { return str == NULL ? 0 : write((const uint8_t *)str, String(str).length()); }
87+
size_t write(const uint8_t *buffer, size_t size) {
88+
size_t n;
89+
for (n = 0; size && write(*buffer++) && ++n; --size);
90+
return n;
91+
}
92+
size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); }
93+
size_t write(unsigned long n) { return write((uint8_t)n); }
94+
size_t write(long n) { return write((uint8_t)n); }
95+
size_t write(unsigned int n) { return write((uint8_t)n); }
96+
size_t write(int n) { return write((uint8_t)n); }
97+
98+
// https://www.arduino.cc/en/Reference/WireAvailable
99+
// Returns the number of bytes available for retrieval with read(). This should be called on a master device after a
100+
// call to requestFrom() or on a slave inside the onReceive() handler.
101+
int available(void) {
102+
// TODO: implement
103+
return 0; // number of bytes available for reading
104+
}
105+
106+
// https://www.arduino.cc/en/Reference/WireRead
107+
// Reads a byte that was transmitted from a slave device to a master after a call to requestFrom() or was transmitted
108+
// from a master to a slave. read() inherits from the Stream utility class.
109+
int read(void) {
110+
// TODO: implement
111+
return '\0'; // The next byte received
112+
}
113+
int peek(void) {
114+
// TODO: implement
115+
return 0;
116+
}
117+
void flush(void) {
118+
// TODO: implement
119+
}
120+
121+
// https://www.arduino.cc/en/Reference/WireOnReceive
122+
// Registers a function to be called when a slave device receives a transmission from a master.
123+
void onReceive( void (*callback)(int) ) {
124+
// TODO: implement
125+
}
126+
127+
// https://www.arduino.cc/en/Reference/WireOnRequest
128+
// Register a function to be called when a master requests data from this slave device.
129+
void onRequest( void (*callback)(void) ) {
130+
// TODO: implement
131+
}
132+
133+
private:
134+
int i2cAddress;
135+
bool isMaster = false;
136+
};
137+
138+
extern TwoWire Wire;

0 commit comments

Comments
 (0)