@@ -86,22 +86,21 @@ unittest(pin_history)
86
86
digitalWrite(myPin, HIGH);
87
87
digitalWrite(myPin, HIGH);
88
88
89
+ // pin history is queued in case we want to analyze it later.
90
+ // we expect 6 values in that queue.
89
91
assertEqual(6, state->digitalPin[1].size());
90
92
bool expected[6] = {LOW, HIGH, LOW, LOW, HIGH, HIGH};
91
93
bool actual[6];
92
94
93
- // move history queue into an array because at the moment, reading
94
- // the history is destructive -- it's a linked-list queue. this
95
- // means that if toArray or hasElements fails, the queue will be in
96
- // an unknown state and you should reset it before continuing with
97
- // other tests
95
+ // convert history queue into an array so we can verify it
98
96
int numMoved = state->digitalPin[myPin].toArray(actual, 6);
99
97
assertEqual(6, numMoved);
100
98
101
99
// verify each element
102
100
for (int i = 0; i < 6; ++i) {
103
101
assertEqual(expected[i], actual[i]);
104
102
}
103
+ }
105
104
` ` `
106
105
107
106
@@ -141,6 +140,43 @@ unittest(pin_read_history)
141
140
142
141
# ### Serial Data
143
142
143
+ Basic input and output verification of serial port data can be done as follows :
144
+
145
+ ` ` ` c++
146
+ unittest(reading_writing_serial)
147
+ {
148
+ GodmodeState* state = GODMODE();
149
+ state->serialPort[0].dataIn = ""; // the queue of data waiting to be read
150
+ state->serialPort[0].dataOut = ""; // the history of data written
151
+
152
+ // When there is no data, nothing happens
153
+ assertEqual(-1, Serial.peek());
154
+ assertEqual("", state->serialPort[0].dataIn);
155
+ assertEqual("", state->serialPort[0].dataOut);
156
+
157
+ // if we put data on the input and peek at it, we see the value and it's not consumed
158
+ state->serialPort[0].dataIn = "a";
159
+ assertEqual('a', Serial.peek());
160
+ assertEqual("a", state->serialPort[0].dataIn);
161
+ assertEqual("", state->serialPort[0].dataOut);
162
+
163
+ // if we read the input, we see the value and it's consumed
164
+ assertEqual('a', Serial.read());
165
+ assertEqual("", state->serialPort[0].dataIn);
166
+ assertEqual("", state->serialPort[0].dataOut);
167
+
168
+ // when we write data, it shows up in the history -- the output buffer
169
+ Serial.write('b');
170
+ assertEqual("", state->serialPort[0].dataIn);
171
+ assertEqual("b", state->serialPort[0].dataOut);
172
+
173
+ // when we print more data, note that the history
174
+ // still contains the first thing we wrote
175
+ Serial.print("cdefg");
176
+ assertEqual("", state->serialPort[0].dataIn);
177
+ assertEqual("bcdefg", state->serialPort[0].dataOut);
178
+ }
179
+ ` ` `
144
180
145
181
A more complicated example : working with serial port IO. Let's say I have the following function:
146
182
0 commit comments