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
15//15//
16 16
17#include "SerialInterface.h"17#include "SerialInterface.h"
18#include <dirent.h>
19#include <sys/time.h>
20#ifdef __APPLE__
21#include <regex.h>
22#endif
23
18 24
19int serial_fd;25int serial_fd;
20const int MAX_BUFFER = 255;26const int MAX_BUFFER = 255;
@@ -23,12 +29,48 @@ int serial_buffer_pos = 0; · 2 unmodified lines
23int samples_total = 0;29int samples_total = 0;
24 30
25const int ZERO_OFFSET = 2048;31const int ZERO_OFFSET = 2048;
32const short NO_READ_MAXIMUM = 10;
33
34SerialInterface::SerialInterface() {
35 active = false;
36 noReadCount = 0;
37}
26 38
27// Init the serial port to the specified values 39void SerialInterface::pair() {
28int SerialInterface::init(char * portname, int baud)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
68int SerialInterface::init(char* portname, int baud)
29{69{
30 serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);70 serial_fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
31 71
72 printf("Attemping to open serial interface: %s\n", portname);
73
32 if (serial_fd == -1) return -1; // Failed to open port74 if (serial_fd == -1) return -1; // Failed to open port
33 75
34 struct termios options;76 struct termios options;
@@ -68,9 +110,12 @@ int SerialInterface::init(char * portname, int baud) · 33 unmodified lines
68 for (int i = 1; i < MAX_BUFFER; i++) {110 for (int i = 1; i < MAX_BUFFER; i++) {
69 serial_buffer[i] = ' ';111 serial_buffer[i] = ' ';
70 }112 }
71 113
72 114 printf("Serial interface opened!\n");
73 return 0; // Init serial port was a success115
116 active = true;
117
118 return 0;
74}119}
75 120
76// Reset Trailing averages to the current measurement121// Reset Trailing averages to the current measurement
@@ -133,8 +178,12 @@ void SerialInterface::readData() { · 56 unmodified lines
133 const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};178 const float AVG_RATE[] = {0.01, 0.01, 0.01, 0.01, 0.01, 0.01};
134 char bufchar[1];179 char bufchar[1];
135 180
181 bool atLeastOneRead = false;
182
136 while (read(serial_fd, &bufchar, 1) > 0)183 while (read(serial_fd, &bufchar, 1) > 0)
137 {184 {
185 atLeastOneRead = true;
186
138 //std::cout << bufchar[0];187 //std::cout << bufchar[0];
139 serial_buffer[serial_buffer_pos] = bufchar[0];188 serial_buffer[serial_buffer_pos] = bufchar[0];
140 serial_buffer_pos++;189 serial_buffer_pos++;
@@ -169,6 +218,28 @@ void SerialInterface::readData() { · 28 unmodified lines
169 serial_buffer_pos = 0;218 serial_buffer_pos = 0;
170 }219 }
171 }220 }
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
231void 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 }
172}243}
173 244
174 245