Skip to content

Commit b6f5803

Browse files
committed
Work on serial verification
Issues with /r/n vs /n. Need to fix Pico code probably.
1 parent 9de8f15 commit b6f5803

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

testing/Command_Interpreter_Testing.cpp

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,47 @@
55

66
#include <wiringSerial.h>
77

8-
serial;
9-
10-
bool WiringControl::initializeGPIO() {
11-
if ((serial = serialOpen("/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00", 115200)) < 0) {
8+
bool initializeGPIO(int* serial) {
9+
if ((*serial = serialOpen("/dev/serial/by-id/usb-MicroPython_Board_in_FS_mode_e6614864d3798738-if00", 115200)) < 0) {
1210
return false;
1311
}
1412
return true;
1513
}
1614

17-
int getSerialChar() {
18-
return serialGetchar(serial); // from wiringSerial
15+
int getSerialChar(int* serial) {
16+
if (*serial == -1) {
17+
if (!initializeGPIO(serial)) {
18+
std::cerr << "Unable to open serial port! Exiting." << std::endl;
19+
exit(42);
20+
}
21+
}
22+
return serialGetchar(*serial); // from wiringSerial
23+
}
24+
25+
void echoOn(int serial) {
26+
serialPuts(serial, "echo on\n");
1927
}
2028

2129
#else
2230

23-
int getSerialChar() {
31+
bool initializeGPIO(int* serial) {
32+
return true;
33+
}
34+
35+
void echoOn(int serial) {
36+
return;
37+
}
38+
39+
int getSerialChar(int* serial) {
2440
return EOF;
2541
}
2642

2743
#endif
2844

2945
TEST(CommandInterpreterTest, CreateCommandInterpreter) {
3046
testing::internal::CaptureStdout();
47+
int serial = -1;
48+
initializeGPIO(&serial);
3149

3250
auto pinNumbers = std::vector<int>{4, 5, 2, 3, 9, 7, 8, 6};
3351

@@ -53,8 +71,11 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
5371
expectedOutput.append(" PWM 1500\n");
5472
}
5573

74+
int charRead = EOF;
5675
std::string serialOutput;
57-
while (getSerialChar() != EOF) {}
76+
while ((charRead = getSerialChar(&serial)) != EOF) {
77+
serialOutput.push_back((char)charRead);
78+
}
5879

5980
ASSERT_EQ(pinStatus.size(), 8);
6081
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500}));
@@ -68,6 +89,8 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
6889

6990
TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
7091
testing::internal::CaptureStdout();
92+
int serial = -1;
93+
initializeGPIO(&serial);
7194

7295
auto pinNumbers = std::vector<int>{4, 5, 2, 3, 9, 7, 8, 6};
7396

@@ -99,8 +122,11 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
99122
expectedOutput.append("Configure 8 Digital\nSet 8 Digital High\n");
100123
expectedOutput.append("Configure 9 Digital\nSet 9 Digital Low\n");
101124

125+
int charRead = EOF;
102126
std::string serialOutput;
103-
while (getSerialChar() != EOF) {}
127+
while ((charRead = getSerialChar(&serial)) != EOF) {
128+
serialOutput.push_back((char)charRead);
129+
}
104130
ASSERT_EQ(pinStatus.size(), 10);
105131
ASSERT_EQ(pinStatus, (std::vector<int>{1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1, 0}));
106132
if (serialOutput.empty()) {
@@ -113,6 +139,8 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
113139

114140
TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
115141
testing::internal::CaptureStdout();
142+
int serial = -1;
143+
initializeGPIO(&serial);
116144

117145
const CommandComponent acceleration = {1900, 1900, 1100,
118146
1250, 1300, 1464, 1535,
@@ -155,8 +183,11 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
155183
expectedOutput.append("Set 8 PWM 1535\n");
156184
expectedOutput.append("Set 6 PWM 1536\n");
157185

186+
int charRead = EOF;
158187
std::string serialOutput;
159-
while (getSerialChar() != EOF) {}
188+
while ((charRead = getSerialChar(&serial)) != EOF) {
189+
serialOutput.push_back((char)charRead);
190+
}
160191
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
161192
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
162193
ASSERT_EQ(pinStatus, (std::vector<int>{1900, 1900, 1100, 1250, 1300, 1464, 1535, 1536}));
@@ -170,6 +201,8 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
170201

171202
TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
172203
testing::internal::CaptureStdout();
204+
int serial = -1;
205+
initializeGPIO(&serial);
173206

174207
const CommandComponent acceleration = {1100, 1900, 1100,
175208
1250, 1300, 1464, 1535,
@@ -212,8 +245,11 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
212245
expectedOutput.append("Set 8 PWM 1535\n");
213246
expectedOutput.append("Set 6 PWM 1536\n");
214247

248+
int charRead = EOF;
215249
std::string serialOutput;
216-
while (getSerialChar() != EOF) {}
250+
while ((charRead = getSerialChar(&serial)) != EOF) {
251+
serialOutput.push_back((char)charRead);
252+
}
217253
ASSERT_NEAR((endTime - startTime) / std::chrono::milliseconds(1), std::chrono::milliseconds(2000) /
218254
std::chrono::milliseconds(1), std::chrono::milliseconds(10) / std::chrono::milliseconds(1));
219255

0 commit comments

Comments
 (0)