Skip to content

Commit ea1ab74

Browse files
committed
documentation
1 parent 358381b commit ea1ab74

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,21 @@ unittest(pin_history)
8686
digitalWrite(myPin, HIGH);
8787
digitalWrite(myPin, HIGH);
8888
89+
// pin history is queued in case we want to analyze it later.
90+
// we expect 6 values in that queue.
8991
assertEqual(6, state->digitalPin[1].size());
9092
bool expected[6] = {LOW, HIGH, LOW, LOW, HIGH, HIGH};
9193
bool actual[6];
9294
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
9896
int numMoved = state->digitalPin[myPin].toArray(actual, 6);
9997
assertEqual(6, numMoved);
10098
10199
// verify each element
102100
for (int i = 0; i < 6; ++i) {
103101
assertEqual(expected[i], actual[i]);
104102
}
103+
}
105104
```
106105

107106

@@ -141,6 +140,43 @@ unittest(pin_read_history)
141140

142141
#### Serial Data
143142

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+
```
144180

145181
A more complicated example: working with serial port IO. Let's say I have the following function:
146182

SampleProjects/TestSomething/test/serial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unittest(serial_ports)
4141
Serial.print("cdefg");
4242
assertEqual("", state->serialPort[0].dataIn);
4343
assertEqual("bcdefg", state->serialPort[0].dataOut);
44-
}
44+
}
4545

4646
#endif
4747

0 commit comments

Comments
 (0)