From 849bc0f8f6abf42567a152cf6e01bf7349902aac Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Sun, 26 Sep 2010 21:47:55 -0400 Subject: wirish reformatted and code-styled --- wirish/usb_serial.cpp | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'wirish/usb_serial.cpp') diff --git a/wirish/usb_serial.cpp b/wirish/usb_serial.cpp index fafdf49..405220a 100644 --- a/wirish/usb_serial.cpp +++ b/wirish/usb_serial.cpp @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 Perry Hung. @@ -20,10 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - * ****************************************************************************/ + *****************************************************************************/ /** - * @brief wirish usb class for easy goin communication, uses libmaple's + * @brief Wirish USB class for easy communication, uses libmaple's * virtual com port implementation */ @@ -49,8 +49,10 @@ void USBSerial::write(uint8 ch) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + uint16 status = 0; uint32 start = millis(); + while(status == 0 && (millis() - start <= USB_TIMEOUT)) { status = usbSendBytes(&ch, 1); } @@ -60,13 +62,15 @@ void USBSerial::write(const char *str) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + 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) + if(oldstatus != status) start = millis(); oldstatus = status; } @@ -76,52 +80,55 @@ void USBSerial::write(void *buf, uint32 size) { if(!(usbIsConnected() && usbIsConfigured())) { return; } + if (!buf) { return; } + 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) + if(oldstatus != status) start = millis(); oldstatus = status; } } uint32 USBSerial::available(void) { - return usbBytesAvailable(); + return usbBytesAvailable(); } uint32 USBSerial::read(void *buf, uint32 len) { - if (!buf) { - return 0; - } + if (!buf) { + return 0; + } - return usbReceiveBytes((uint8*)buf, len); + return usbReceiveBytes((uint8*)buf, len); } uint8 USBSerial::read(void) { - uint8 ch; - usbReceiveBytes(&ch, 1); - return ch; + uint8 ch; + usbReceiveBytes(&ch, 1); + return ch; } uint8 USBSerial::pending(void) { - return usbGetPending(); + return usbGetPending(); } uint8 USBSerial::getDTR(void) { - return usbGetDTR(); + return usbGetDTR(); } uint8 USBSerial::getRTS(void) { - return usbGetRTS(); + return usbGetRTS(); } uint8 USBSerial::isConnected(void) { - return (usbIsConnected() && usbIsConfigured()); + return (usbIsConnected() && usbIsConfigured()); } USBSerial SerialUSB; -- cgit v1.2.3