aboutsummaryrefslogtreecommitdiffstats
path: root/wirish
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2010-07-19 00:24:31 -0400
committerbnewbold <bnewbold@robocracy.org>2010-07-19 00:38:36 -0400
commit52cbd2f1a1557002f46355e0095400a09c267ff9 (patch)
treeb2d8a95eb2cb2ac0c081fc0459a495baa75319c8 /wirish
parent7ae1ce63259ef440bc13d7493e9dcee43df83591 (diff)
downloadlibrambutan-52cbd2f1a1557002f46355e0095400a09c267ff9.tar.gz
librambutan-52cbd2f1a1557002f46355e0095400a09c267ff9.zip
working serialUSB with timeout
as a temporary workaround for the fact that SerialUSB is often blocking, this crude implementation makes the low-level C usbSendBytes function non-blocking (with a return code of bytes sent) and implements a 2ms timeout in the wirish write() function. also adds begin(), end(), getDTR(), getRTS(), pending(). device is still initialized the old fashioned way during init() so that, eg, autoreset will work. includes a simple multi-test program.
Diffstat (limited to 'wirish')
-rw-r--r--wirish/usb_serial.cpp43
-rw-r--r--wirish/usb_serial.h7
2 files changed, 45 insertions, 5 deletions
diff --git a/wirish/usb_serial.cpp b/wirish/usb_serial.cpp
index f186dea..2effa3d 100644
--- a/wirish/usb_serial.cpp
+++ b/wirish/usb_serial.cpp
@@ -32,24 +32,53 @@
#include "wirish.h"
#include "usb.h"
+#define USB_TIMEOUT 2
+
USBSerial :: USBSerial(void) {
}
+void USBSerial::begin(void) {
+ setupUSB();
+}
+
+void USBSerial::end(void) {
+ disableUSB();
+}
+
void USBSerial::write(uint8 ch) {
- usbSendBytes(&ch, 1);
+ uint16 status = 0;
+ uint32 start = millis();
+ while(status == 0 && (millis() - start <= USB_TIMEOUT)) {
+ status = usbSendBytes(&ch, 1);
+ }
}
void USBSerial::write(const char *str) {
- uint32 len = strlen(str);
-
- usbSendBytes((uint8*)str, len);
+ uint32 len = strlen(str);
+ uint16 status = 0;
+ uint16 oldstatus = 0;
+ uint32 start = millis();
+ while(status < len && (millis() - start < USB_TIMEOUT)) {
+ status += usbSendBytes((uint8*)str+status, len-status);
+ if(oldstatus != status)
+ start = millis();
+ oldstatus = status;
+ }
}
void USBSerial::write(void *buf, uint32 size) {
if (!buf) {
return;
}
- usbSendBytes((uint8*)buf, size);
+ uint16 status = 0;
+ uint16 oldstatus = 0;
+ uint32 start = millis();
+ while(status < size && (millis() - start < USB_TIMEOUT)) {
+ status += usbSendBytes((uint8*)buf+status, size-status);
+ if(oldstatus != status)
+ start = millis();
+ oldstatus = status;
+ }
}
uint32 USBSerial::available(void) {
@@ -70,5 +99,9 @@ uint8 USBSerial::read(void) {
return ch;
}
+uint8 USBSerial::pending(void) {
+ return usbGetPending();
+}
+
USBSerial SerialUSB;
diff --git a/wirish/usb_serial.h b/wirish/usb_serial.h
index e1c61ff..abb362b 100644
--- a/wirish/usb_serial.h
+++ b/wirish/usb_serial.h
@@ -36,6 +36,9 @@ class USBSerial : public Print {
public:
USBSerial(void);
+ void begin(void);
+ void end(void);
+
uint32 available(void);
uint32 read(void *buf, uint32 len);
@@ -44,6 +47,10 @@ class USBSerial : public Print {
void write(uint8);
void write(const char *str);
void write(void *, uint32);
+
+ uint8 getRTS();
+ uint8 getDTR();
+ uint8 pending();
};
extern USBSerial SerialUSB;