Merge pull request #202 from birarda/master

bail on opening serial after two failed attempts
This commit is contained in:
birarda 2013-05-06 13:09:55 -07:00
commit 3676da8cb2
2 changed files with 28 additions and 17 deletions

View file

@ -34,6 +34,11 @@ const int GRAVITY_SAMPLES = 200; // Use the first samples to
const bool USING_INVENSENSE_MPU9150 = 1; const bool USING_INVENSENSE_MPU9150 = 1;
SerialInterface::SerialInterface() :
active(false),
_failedOpenAttempts(0) {
}
void SerialInterface::pair() { void SerialInterface::pair() {
#ifdef __APPLE__ #ifdef __APPLE__
@ -43,25 +48,29 @@ void SerialInterface::pair() {
int matchStatus; int matchStatus;
regex_t regex; regex_t regex;
// for now this only works on OS X, where the usb serial shows up as /dev/tty.usb* if (_failedOpenAttempts < 2) {
if((devDir = opendir("/dev"))) { // if we've already failed to open the detected interface twice then don't try again
while((entry = readdir(devDir))) {
regcomp(&regex, "tty\\.usb", REG_EXTENDED|REG_NOSUB); // for now this only works on OS X, where the usb serial shows up as /dev/tty.usb*
matchStatus = regexec(&regex, entry->d_name, (size_t) 0, NULL, 0); if((devDir = opendir("/dev"))) {
if (matchStatus == 0) { while((entry = readdir(devDir))) {
char *serialPortname = new char[100]; regcomp(&regex, "tty\\.usb", REG_EXTENDED|REG_NOSUB);
sprintf(serialPortname, "/dev/%s", entry->d_name); matchStatus = regexec(&regex, entry->d_name, (size_t) 0, NULL, 0);
if (matchStatus == 0) {
initializePort(serialPortname, 115200); char *serialPortname = new char[100];
sprintf(serialPortname, "/dev/%s", entry->d_name);
delete [] serialPortname;
initializePort(serialPortname, 115200);
delete [] serialPortname;
}
regfree(&regex);
} }
regfree(&regex); closedir(devDir);
} }
closedir(devDir);
} }
#endif
#endif
} }
// connect to the serial port // connect to the serial port
@ -73,6 +82,7 @@ int SerialInterface::initializePort(char* portname, int baud) {
if (serialFd == -1) { if (serialFd == -1) {
printLog("Failed.\n"); printLog("Failed.\n");
_failedOpenAttempts++;
return -1; // Failed to open port return -1; // Failed to open port
} }
struct termios options; struct termios options;

View file

@ -36,7 +36,7 @@ extern const bool USING_INVENSENSE_MPU9150;
class SerialInterface { class SerialInterface {
public: public:
SerialInterface() { active = false; }; SerialInterface();
void pair(); void pair();
void readData(); void readData();
@ -56,7 +56,7 @@ public:
glm::vec3 getGravity() {return gravity;}; glm::vec3 getGravity() {return gravity;};
private: private:
int initializePort(char * portname, int baud); int initializePort(char* portname, int baud);
void resetSerial(); void resetSerial();
int lastMeasured[NUM_CHANNELS]; int lastMeasured[NUM_CHANNELS];
float trailingAverage[NUM_CHANNELS]; float trailingAverage[NUM_CHANNELS];
@ -68,6 +68,7 @@ private:
int _lastYaw; int _lastYaw;
int _lastPitch; int _lastPitch;
int _lastRoll; int _lastRoll;
int _failedOpenAttempts;
}; };
#endif #endif