@@ -54,19 +54,52 @@ struct wireData_t {
54
54
class TwoWire : public ObservableDataStream {
55
55
private:
56
56
bool _didBegin = false ;
57
- wireData_t * in = nullptr ; // pointer to current slave for writing
58
- wireData_t * out = nullptr ; // pointer to current slave for reading
57
+ wireData_t* in = nullptr ; // pointer to current slave for writing
58
+ wireData_t* out = nullptr ; // pointer to current slave for reading
59
59
wireData_t slaves[SLAVE_COUNT];
60
60
61
61
public:
62
- // constructor initializes internal data
63
- TwoWire () {
62
+
63
+ // ////////////////////////////////////////////////////////////////////////////////////////////
64
+ // testing methods
65
+ // ////////////////////////////////////////////////////////////////////////////////////////////
66
+
67
+ // initialize all the mocks
68
+ void resetMocks () {
69
+ _didBegin = false ;
70
+ in = nullptr ; // pointer to current slave for writing
71
+ out = nullptr ; // pointer to current slave for reading
64
72
for (int i = 0 ; i < SLAVE_COUNT; ++i) {
65
73
slaves[i].misoSize = 0 ;
66
74
slaves[i].mosiSize = 0 ;
75
+ slaves[i].misoBuffer .clear ();
76
+ slaves[i].mosiBuffer .clear ();
67
77
}
68
78
}
69
79
80
+ // to verify that Wire.begin() was called at some point
81
+ bool didBegin () { return _didBegin; }
82
+
83
+ // to access the MISO buffer, which allows you to mock what the master will read in a request
84
+ deque<uint8_t >* getMiso (uint8_t address) {
85
+ return &slaves[address].misoBuffer ;
86
+ }
87
+
88
+ // to access the MOSI buffer, which records what the master sends during a write
89
+ deque<uint8_t >* getMosi (uint8_t address) {
90
+ return &slaves[address].mosiBuffer ;
91
+ }
92
+
93
+
94
+ // ////////////////////////////////////////////////////////////////////////////////////////////
95
+ // mock implementation
96
+ // ////////////////////////////////////////////////////////////////////////////////////////////
97
+
98
+ // constructor initializes internal data
99
+ TwoWire () {
100
+ resetMocks ();
101
+ }
102
+
70
103
// https://www.arduino.cc/en/Reference/WireBegin
71
104
// Initiate the Wire library and join the I2C bus as a master or slave. This
72
105
// should normally be called only once.
@@ -220,15 +253,6 @@ class TwoWire : public ObservableDataStream {
220
253
// We don't (yet) support the slave role in the mock
221
254
void onRequest (void (*callback)(void )) { assert (false ); }
222
255
223
- // testing methods
224
- bool didBegin () { return _didBegin; }
225
-
226
- deque<uint8_t > *getMiso (uint8_t address) {
227
- return &slaves[address].misoBuffer ;
228
- }
229
- deque<uint8_t > *getMosi (uint8_t address) {
230
- return &slaves[address].mosiBuffer ;
231
- }
232
256
};
233
257
234
258
extern TwoWire Wire;
0 commit comments