From 88d4b095e4590ab9bbafcf76e134d168f66c41b1 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Fri, 4 Jun 2010 00:43:21 -0400 Subject: Preliminary wirish USBSerial implementation. -updated examples -removed HardwareUSB -cleaned up a handful of includes --- wirish/Print.cpp | 8 +++-- wirish/Print.h | 2 +- wirish/WProgram.h | 3 -- wirish/comm/HardwareUsb.cpp | 60 ------------------------------------- wirish/comm/HardwareUsb.h | 48 ------------------------------ wirish/usb_serial.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++ wirish/usb_serial.h | 52 ++++++++++++++++++++++++++++++++ wirish/wirish.h | 6 ++++ 8 files changed, 136 insertions(+), 115 deletions(-) delete mode 100644 wirish/comm/HardwareUsb.cpp delete mode 100644 wirish/comm/HardwareUsb.h create mode 100644 wirish/usb_serial.cpp create mode 100644 wirish/usb_serial.h (limited to 'wirish') diff --git a/wirish/Print.cpp b/wirish/Print.cpp index 5a1bc93..9baa757 100644 --- a/wirish/Print.cpp +++ b/wirish/Print.cpp @@ -32,10 +32,12 @@ void Print::write(const char *str) } /* default implementation: may be overridden */ -void Print::write(const uint8 *buffer, uint32 size) +void Print::write(void *buffer, uint32 size) { - while (size--) - write(*buffer++); + uint8 *ch = (uint8*)buffer; + while (size--) { + write(*ch++); + } } void Print::print(uint8 b) diff --git a/wirish/Print.h b/wirish/Print.h index b2f8647..72208b4 100644 --- a/wirish/Print.h +++ b/wirish/Print.h @@ -36,7 +36,7 @@ class Print public: virtual void write(uint8) = 0; virtual void write(const char *str); - virtual void write(const uint8 *buffer, uint32 size); + virtual void write(void *, uint32); void print(char); void print(const char[]); void print(uint8); diff --git a/wirish/WProgram.h b/wirish/WProgram.h index 889219e..9143991 100644 --- a/wirish/WProgram.h +++ b/wirish/WProgram.h @@ -23,9 +23,6 @@ * ****************************************************************************/ #include "wirish.h" -#include "HardwareSerial.h" -#include "HardwareUsb.h" -#include "math.h" void setup(); void loop(); diff --git a/wirish/comm/HardwareUsb.cpp b/wirish/comm/HardwareUsb.cpp deleted file mode 100644 index 064bcc8..0000000 --- a/wirish/comm/HardwareUsb.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Andrew Meyer. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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 Wiring like serial api to USB virtual COM - */ - -#include "wirish.h" -#include "HardwareUsb.h" -#include "usb.h" - -HardwareUsb::HardwareUsb(void) { -} - -uint8 HardwareUsb::read(void) { - uint8 outVal; - usbReceiveBytes(&outVal,1); - return outVal; -} - -uint8 HardwareUsb::available(void) { - return usbBytesAvailable(); -} - -void HardwareUsb::flush(void) { - uint8 totalBytes = usbBytesAvailable(); - uint8 recvBuf[totalBytes]; - usbReceiveBytes(recvBuf,totalBytes); -} - -void HardwareUsb::write(unsigned char ch) { - while (usbSendBytes(&ch, 1) == 0); -} - -void HardwareUsb::begin(void) { - /* placeholder for usb<->uart linking */ -} - -HardwareUsb Usb; diff --git a/wirish/comm/HardwareUsb.h b/wirish/comm/HardwareUsb.h deleted file mode 100644 index e2af2e2..0000000 --- a/wirish/comm/HardwareUsb.h +++ /dev/null @@ -1,48 +0,0 @@ -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Andrew Meyer. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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 Wiring like serial api to USB virtual COM. Dummy implmentation wrapping printChar() around usb.h for now. Todo: something better. - */ - -#ifndef _HARDWAREUSB_H_ -#define _HARDWAREUSB_H_ - -#include "Print.h" -#include "usb.h" - -class HardwareUsb : public Print { - public: - HardwareUsb(void); - void begin(); - uint8 available(void); - uint8 read(void); - void flush(void); - virtual void write(unsigned char); - using Print::write; -}; - -extern HardwareUsb Usb; - -#endif //_HARDWAREUSB_H/* ***************************************************************************** diff --git a/wirish/usb_serial.cpp b/wirish/usb_serial.cpp new file mode 100644 index 0000000..9476e99 --- /dev/null +++ b/wirish/usb_serial.cpp @@ -0,0 +1,72 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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 + * virtual com port implementation + */ + +#include + +#include "wirish.h" +#include "usb.h" + +USBSerial :: USBSerial(void) { +} + +void USBSerial::write(uint8 ch) { + usbSendBytes(&ch, 1); +} + +void USBSerial::write(const char *str) { + uint32 len = strlen(str); + usbSendBytes((uint8*)str, len); +} + +void USBSerial::write(void *buf, uint32 size) { + if (!buf) { + return; + } + usbSendBytes((uint8*)buf, size); +} + +uint32 USBSerial::available(void) { + return usbBytesAvailable(); +} + +uint32 USBSerial::read(void *buf, uint32 len) { + if (!buf) { + return 0; + } + + return usbReceiveBytes((uint8*)buf, len); +} + +uint8 USBSerial::read(void) { + uint8 ch; + return usbReceiveBytes(&ch, 1); +} + +USBSerial SerialUSB; + diff --git a/wirish/usb_serial.h b/wirish/usb_serial.h new file mode 100644 index 0000000..e1c61ff --- /dev/null +++ b/wirish/usb_serial.h @@ -0,0 +1,52 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 Perry Hung. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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 + * virtual com port implementation + */ + +#ifndef _USB_SERIAL_H_ +#define _USB_SERIAL_H_ + +#include "Print.h" + +class USBSerial : public Print { + public: + USBSerial(void); + + uint32 available(void); + + uint32 read(void *buf, uint32 len); + uint8 read(void); + + void write(uint8); + void write(const char *str); + void write(void *, uint32); +}; + +extern USBSerial SerialUSB; + +#endif + diff --git a/wirish/wirish.h b/wirish/wirish.h index f54517a..2541e5e 100644 --- a/wirish/wirish.h +++ b/wirish/wirish.h @@ -40,6 +40,12 @@ #include "ext_interrupts.h" #include "wirish_math.h" +#ifdef __cplusplus +#include "HardwareSPI.h" +#include "HardwareSerial.h" +#include "usb_serial.h" +#endif + #ifdef __cplusplus extern "C"{ #endif -- cgit v1.2.3