5
5
6
6
#include < wiringSerial.h>
7
7
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 ) {
12
10
return false ;
13
11
}
14
12
return true ;
15
13
}
16
14
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 " );
19
27
}
20
28
21
29
#else
22
30
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) {
24
40
return EOF;
25
41
}
26
42
27
43
#endif
28
44
29
45
TEST (CommandInterpreterTest, CreateCommandInterpreter) {
30
46
testing::internal::CaptureStdout ();
47
+ int serial = -1 ;
48
+ initializeGPIO (&serial);
31
49
32
50
auto pinNumbers = std::vector<int >{4 , 5 , 2 , 3 , 9 , 7 , 8 , 6 };
33
51
@@ -53,8 +71,11 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
53
71
expectedOutput.append (" PWM 1500\n " );
54
72
}
55
73
74
+ int charRead = EOF;
56
75
std::string serialOutput;
57
- while (getSerialChar () != EOF) {}
76
+ while ((charRead = getSerialChar (&serial)) != EOF) {
77
+ serialOutput.push_back ((char )charRead);
78
+ }
58
79
59
80
ASSERT_EQ (pinStatus.size (), 8 );
60
81
ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 }));
@@ -68,6 +89,8 @@ TEST(CommandInterpreterTest, CreateCommandInterpreter) {
68
89
69
90
TEST (CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
70
91
testing::internal::CaptureStdout ();
92
+ int serial = -1 ;
93
+ initializeGPIO (&serial);
71
94
72
95
auto pinNumbers = std::vector<int >{4 , 5 , 2 , 3 , 9 , 7 , 8 , 6 };
73
96
@@ -99,8 +122,11 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
99
122
expectedOutput.append (" Configure 8 Digital\n Set 8 Digital High\n " );
100
123
expectedOutput.append (" Configure 9 Digital\n Set 9 Digital Low\n " );
101
124
125
+ int charRead = EOF;
102
126
std::string serialOutput;
103
- while (getSerialChar () != EOF) {}
127
+ while ((charRead = getSerialChar (&serial)) != EOF) {
128
+ serialOutput.push_back ((char )charRead);
129
+ }
104
130
ASSERT_EQ (pinStatus.size (), 10 );
105
131
ASSERT_EQ (pinStatus, (std::vector<int >{1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1500 , 1 , 0 }));
106
132
if (serialOutput.empty ()) {
@@ -113,6 +139,8 @@ TEST(CommandInterpreterTest, CreateCommandInterpreterWithDigitalPins) {
113
139
114
140
TEST (CommandInterpreterTest, BlindExecuteHardwarePwm) {
115
141
testing::internal::CaptureStdout ();
142
+ int serial = -1 ;
143
+ initializeGPIO (&serial);
116
144
117
145
const CommandComponent acceleration = {1900 , 1900 , 1100 ,
118
146
1250 , 1300 , 1464 , 1535 ,
@@ -155,8 +183,11 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
155
183
expectedOutput.append (" Set 8 PWM 1535\n " );
156
184
expectedOutput.append (" Set 6 PWM 1536\n " );
157
185
186
+ int charRead = EOF;
158
187
std::string serialOutput;
159
- while (getSerialChar () != EOF) {}
188
+ while ((charRead = getSerialChar (&serial)) != EOF) {
189
+ serialOutput.push_back ((char )charRead);
190
+ }
160
191
ASSERT_NEAR ((endTime - startTime) / std::chrono::milliseconds (1 ), std::chrono::milliseconds (2000 ) /
161
192
std::chrono::milliseconds (1 ), std::chrono::milliseconds (10 ) / std::chrono::milliseconds (1 ));
162
193
ASSERT_EQ (pinStatus, (std::vector<int >{1900 , 1900 , 1100 , 1250 , 1300 , 1464 , 1535 , 1536 }));
@@ -170,6 +201,8 @@ TEST(CommandInterpreterTest, BlindExecuteHardwarePwm) {
170
201
171
202
TEST (CommandInterpreterTest, BlindExecuteSoftwarePwm) {
172
203
testing::internal::CaptureStdout ();
204
+ int serial = -1 ;
205
+ initializeGPIO (&serial);
173
206
174
207
const CommandComponent acceleration = {1100 , 1900 , 1100 ,
175
208
1250 , 1300 , 1464 , 1535 ,
@@ -212,8 +245,11 @@ TEST(CommandInterpreterTest, BlindExecuteSoftwarePwm) {
212
245
expectedOutput.append (" Set 8 PWM 1535\n " );
213
246
expectedOutput.append (" Set 6 PWM 1536\n " );
214
247
248
+ int charRead = EOF;
215
249
std::string serialOutput;
216
- while (getSerialChar () != EOF) {}
250
+ while ((charRead = getSerialChar (&serial)) != EOF) {
251
+ serialOutput.push_back ((char )charRead);
252
+ }
217
253
ASSERT_NEAR ((endTime - startTime) / std::chrono::milliseconds (1 ), std::chrono::milliseconds (2000 ) /
218
254
std::chrono::milliseconds (1 ), std::chrono::milliseconds (10 ) / std::chrono::milliseconds (1 ));
219
255
0 commit comments