History

initial setup of polling for newly connected headset

Stephen Birarda committed 13 years ago

Commit d0203151 parent 50f1ba83 files · +94 −51Browse files

Showing the changes to interface/src/SerialInterface.cpp (1 of 3 files). Show all

Unified · Split

interface/src/SerialInterface.cpp+76−5
@@ -15,6 +15,12 @@ · 14 unmodified lines
1515 //
1616
1717 #include "SerialInterface.h"
18+#include <dirent.h>
19+#include <sys/time.h>
20+#ifdef __APPLE__
21+#include <regex.h>
22+#endif
23+
1824
1925 int serial_fd;
2026 const int MAX_BUFFER = 255;
@@ -23,12 +29,48 @@ int serial_buffer_pos = 0; · 2 unmodified lines
2329 int samples_total = 0;
2430
2531 const int ZERO_OFFSET = 2048;
32+const short NO_READ_MAXIMUM = 10;
33+
34+SerialInterface::SerialInterface() {
35+ active = false;
36+ noReadCount = 0;
37+}
2638
27−// Init the serial port to the specified values
28−int SerialInterface::init(char * portname, int baud)
39+void SerialInterface::pair() {
40+ // look for a matching gyro setup
41+ DIR *devDir;
42+ struct dirent *entry;
43+ int matchStatus;
44+ regex_t regex;
45+
46+#ifdef __APPLE__
47+ // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*
48+ if((devDir = opendir("/dev"))) {
49+ while((entry = readdir(devDir))) {
50+ regcomp(&regex, "tty\\.usb", REG_EXTENDED|REG_NOSUB);
51+ matchStatus = regexec(&regex, entry->d_name, (size_t) 0, NULL, 0);
52+ if (matchStatus == 0) {
53+ char *serialPortname = new char[100];
54+ sprintf(serialPortname, "/dev/%s", entry->d_name);
55+
56+ init(serialPortname, 115200);
57+
58+ delete [] serialPortname;
59+ }
60+ regfree(&regex);
61+ }
62+ closedir(devDir);
63+ }
64+#endif
65+}
66+
67+// Init the serial port to the specified values
68+int SerialInterface::init(char* portname, int baud)
2969 {
3070 serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
3171
72+ printf("Attemping to open serial interface: %s\n", portname);
73+
3274 if (serial_fd == -1) return -1; // Failed to open port
3375
3476 struct termios options;
@@ -68,9 +110,12 @@ int SerialInterface::init(char * portname, int baud) · 33 unmodified lines
68110 for (int i = 1; i < MAX_BUFFER; i++) {
69111 serial_buffer[i] = ' ';
70112 }
71−
72−
73− return 0; // Init serial port was a success
113+
114+ printf("Serial interface opened!\n");
115+
116+ active = true;
117+
118+ return 0;
74119 }
75120
76121 // Reset Trailing averages to the current measurement
@@ -133,8 +178,12 @@ void SerialInterface::readData() { · 56 unmodified lines
133178 const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
134179 char bufchar[1];
135180
181+ bool atLeastOneRead = false;
182+
136183 while (read(serial_fd, &bufchar, 1) > 0)
137184 {
185+ atLeastOneRead = true;
186+
138187 //std::cout << bufchar[0];
139188 serial_buffer[serial_buffer_pos] = bufchar[0];
140189 serial_buffer_pos++;
@@ -169,6 +218,28 @@ void SerialInterface::readData() { · 28 unmodified lines
169218 serial_buffer_pos = 0;
170219 }
171220 }
221+
222+ if (!atLeastOneRead) {
223+ noReadCount++;
224+ std::cout << "#" << noReadCount << " blank read from serial.\n";
225+ if (noReadCount >= NO_READ_MAXIMUM) {
226+ disconnectSerial();
227+ }
228+ }
229+}
230+
231+void SerialInterface::disconnectSerial() {
232+ std::cout << "Reached maximum blank read count. Shutting down serial.\n";
233+
234+ active = false;
235+ noReadCount = 0;
236+
237+ // Clear the measured and average channel data
238+ for (int i = 1; i < NUM_CHANNELS; i++)
239+ {
240+ lastMeasured[i] = 0;
241+ trailingAverage[i] = 0.0;
242+ }
172243 }
173244
174245