Nicholas Herriot / VodafoneK3770Lib
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostSerial.h Source File

USBHostSerial.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef USBHOSTSERIAL_H
00020 #define USBHOSTSERIAL_H
00021 
00022 #include "USBHost.h"
00023 #include "Stream.h"
00024 #include "MtxCircBuffer.h"
00025 
00026 enum IrqType {
00027     RxIrq,
00028     TxIrq
00029 };
00030 
00031 class USBHostSerial: public Stream {
00032 public:
00033     /*
00034     * Constructor
00035     */
00036     USBHostSerial();
00037 
00038     /*
00039     * Check if a serial port device is connected
00040     *
00041     * @returns true if a serial device is connected
00042     */
00043     bool connected();
00044 
00045     /*
00046     * _getc overriden to provide scanf capabilities
00047     */
00048     virtual int _getc();
00049 
00050     /*
00051     * _putc overriden to provide printf capabilities
00052     */
00053     virtual int _putc(int c);
00054     
00055     /**
00056     * Check the number of bytes available.
00057     *
00058     * @returns the number of bytes available
00059     */
00060     uint8_t available(); 
00061 
00062     /**
00063      *  Attach a member function to call when a packet is received.
00064      *
00065      *  @param tptr pointer to the object to call the member function on
00066      *  @param mptr pointer to the member function to be called
00067      */
00068     template<typename T>
00069     void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
00070         if ((mptr != NULL) && (tptr != NULL)) {
00071             if (irq == RxIrq) {
00072                 rx.attach(tptr, mptr);
00073             } else {
00074                 tx.attach(tptr, mptr);
00075             }
00076         }
00077     }
00078 
00079     /**
00080      * Attach a callback called when a packet is received
00081      *
00082      * @param fptr function pointer
00083      */
00084     void attach(void (*fn)(void), IrqType irq = RxIrq) {
00085         if (fn != NULL) {
00086             if (irq == RxIrq) {
00087                 rx.attach(fn);
00088             } else {
00089                 tx.attach(fn);
00090             }
00091         }
00092     }
00093     
00094 
00095 private:
00096     USBHost * host;
00097     USBDeviceConnected * dev;
00098     Endpoint * bulk_in;
00099     Endpoint * bulk_out;
00100 
00101     bool dev_connected;
00102 
00103     void init();
00104 
00105     MtxCircBuffer<uint8_t, 64> circ_buf;
00106 
00107     uint8_t buf[20];
00108 
00109     void rxHandler();
00110     void txHandler();
00111     FunctionPointer rx;
00112     FunctionPointer tx;
00113 
00114 };
00115 
00116 #endif