Skip to content

Commit edf63d2

Browse files
committed
update README with serial port stuff
1 parent 502bbec commit edf63d2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,62 @@ unittest(example_godmode_stuff)
6868
}
6969
```
7070

71+
A more complicated example: working with serial port IO. Let's say I have the following function:
72+
73+
```C++
74+
void smartLightswitchSerialHandler(int pin) {
75+
if (Serial.available() > 0) {
76+
int incomingByte = Serial.read();
77+
int val = incomingByte == '0' ? LOW : HIGH;
78+
Serial.print("Ack ");
79+
digitalWrite(pin, val);
80+
Serial.print(String(pin));
81+
Serial.print(" ");
82+
Serial.print((char)incomingByte);
83+
}
84+
}
85+
```
86+
87+
This function has 3 side effects: it drains the serial port's receive buffer, affects a pin, and puts data in the serial port's send buffer. Or, if the receive buffer is empty, it does nothing at all.
88+
89+
```C++
90+
unittest(does_nothing_if_no_data)
91+
{
92+
// configure initial state
93+
GodmodeState* state = GODMODE();
94+
int myPin = 3;
95+
state->serialPort[0].dataIn = "";
96+
state->serialPort[0].dataOut = "";
97+
state->digitalPin[myPin] = LOW;
98+
99+
// execute action
100+
smartLightswitchSerialHandler(myPin);
101+
102+
// assess final state
103+
assertEqual(LOW, state->digitalPin[myPin]);
104+
assertEqual("", state->serialPort[0].dataIn);
105+
assertEqual("", state->serialPort[0].dataOut);
106+
}
107+
108+
unittest(two_flips)
109+
{
110+
GodmodeState* state = GODMODE();
111+
int myPin = 3;
112+
state->serialPort[0].dataIn = "10junk";
113+
state->serialPort[0].dataOut = "";
114+
state->digitalPin[myPin] = LOW;
115+
smartLightswitchSerialHandler(myPin);
116+
assertEqual(HIGH, state->digitalPin[myPin]);
117+
assertEqual("0junk", state->serialPort[0].dataIn);
118+
assertEqual("Ack 3 1", state->serialPort[0].dataOut);
119+
120+
state->serialPort[0].dataOut = "";
121+
smartLightswitchSerialHandler(myPin);
122+
assertEqual(LOW, state->digitalPin[myPin]);
123+
assertEqual("junk", state->serialPort[0].dataIn);
124+
assertEqual("Ack 3 0", state->serialPort[0].dataOut);
125+
}
126+
```
71127

72128
## More Documentation
73129

0 commit comments

Comments
 (0)