Skip to content

Commit 782efcb

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 f0fce10 commit 782efcb

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

cpp/arduino/Arduino.h

Lines changed: 1 addition & 0 deletions
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

1920
typedef bool boolean;
2021
typedef uint8_t byte;

cpp/arduino/Godmode.cpp

Lines changed: 4 additions & 0 deletions
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(&godmode.wire.dataIn, &godmode.wire.dataOut);

cpp/arduino/Godmode.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class GodmodeState {
5252
struct PortDef serialPort[NUM_SERIAL_PORTS];
5353
struct InterruptDef interrupt[MOCK_PINS_COUNT]; // not sure how to get actual number
5454
struct PortDef spi;
55+
struct PortDef wire;
5556

5657
void resetPins() {
5758
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {

cpp/arduino/Wire.h

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

0 commit comments

Comments
 (0)