From d74a223f7efd4d1c355007fcde18f00084a9edbc Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Oct 2011 12:40:54 +1100 Subject: [PATCH] make desktop serial more responsive --- libraries/Desktop/support/FastSerial.cpp | 17 +++++++++++++++++ libraries/Desktop/support/main.cpp | 19 +++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/libraries/Desktop/support/FastSerial.cpp b/libraries/Desktop/support/FastSerial.cpp index fb66de60af..04623fa8d0 100644 --- a/libraries/Desktop/support/FastSerial.cpp +++ b/libraries/Desktop/support/FastSerial.cpp @@ -45,6 +45,7 @@ #include #include #include +#include "desktop.h" #define LISTEN_BASE_PORT 5760 #define BUFFER_SIZE 128 @@ -305,3 +306,19 @@ void FastSerial::_freeBuffer(Buffer *buffer) { } +/* + return true if any bytes are pending + */ +void desktop_serial_select_setup(fd_set *fds, int *fd_high) +{ + int i; + + for (i=0; i *fd_high) { + *fd_high = tcp_state[i].fd; + } + } + } +} diff --git a/libraries/Desktop/support/main.cpp b/libraries/Desktop/support/main.cpp index b6eef2e63b..9e16c1ff70 100644 --- a/libraries/Desktop/support/main.cpp +++ b/libraries/Desktop/support/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "desktop.h" void setup(void); void loop(void); @@ -16,15 +17,17 @@ int main(void) setup(); while (true) { struct timeval tv; - unsigned long t1, t2; - t1 = micros(); + fd_set fds; + int fd_high = 0; + + FD_ZERO(&fds); loop(); - t2 = micros(); - if (t2 - t1 < 2) { - tv.tv_sec = 0; - tv.tv_usec = 1; - select(0, NULL, NULL, NULL, &tv); - } + + desktop_serial_select_setup(&fds, &fd_high); + tv.tv_sec = 0; + tv.tv_usec = 100; + + select(fd_high+1, &fds, NULL, NULL, &tv); } return 0; }