A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostSerial.h Source File

USBHostSerial.h

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef USBHOSTSERIAL_H
00018 #define USBHOSTSERIAL_H
00019 
00020 #include "USBHostConf.h"
00021 
00022 #if USBHOST_SERIAL
00023 
00024 #include "USBHost.h"
00025 #include "Stream.h"
00026 #include "MtxCircBuffer.h"
00027 
00028 /** 
00029  * A class to communicate a USB virtual serial port
00030  */
00031 class USBHostSerial : public IUSBEnumerator, public Stream {
00032 public:
00033     /**
00034     * Constructor
00035     */
00036     USBHostSerial();
00037 
00038     enum IrqType {
00039         RxIrq,
00040         TxIrq
00041     };
00042     
00043     enum Parity {
00044         None = 0,
00045         Odd,
00046         Even,
00047         Mark,
00048         Space
00049     };
00050 
00051     /**
00052     * Check if a virtual serial port is connected
00053     *
00054     * @returns true if a serial device is connected
00055     */
00056     bool connected();
00057     
00058     /**
00059      * Try to connect a serial device
00060      *
00061      * @return true if connection was successful
00062      */
00063     bool connect();
00064     
00065     /**
00066     * Check the number of bytes available.
00067     *
00068     * @returns the number of bytes available
00069     */
00070     uint8_t available(); 
00071 
00072     /**
00073      *  Attach a member function to call when a packet is received.
00074      *
00075      *  @param tptr pointer to the object to call the member function on
00076      *  @param mptr pointer to the member function to be called
00077      *  @param irq irq type
00078      */
00079     template<typename T>
00080     inline void attach(T* tptr, void (T::*mptr)(void), IrqType irq = RxIrq) {
00081         if ((mptr != NULL) && (tptr != NULL)) {
00082             if (irq == RxIrq) {
00083                 rx.attach(tptr, mptr);
00084             } else {
00085                 tx.attach(tptr, mptr);
00086             }
00087         }
00088     }
00089 
00090     /**
00091      * Attach a callback called when a packet is received
00092      *
00093      * @param ptr function pointer
00094      */
00095     inline void attach(void (*fn)(void), IrqType irq = RxIrq) {
00096         if (fn != NULL) {
00097             if (irq == RxIrq) {
00098                 rx.attach(fn);
00099             } else {
00100                 tx.attach(fn);
00101             }
00102         }
00103     }
00104     
00105     /** Set the baud rate of the serial port
00106      *
00107      *  @param baudrate The baudrate of the serial port (default = 9600).
00108      */
00109     void baud(int baudrate = 9600);
00110     
00111     /** Set the transmission format used by the Serial port
00112      *
00113      *  @param bits The number of bits in a word (default = 8)
00114      *  @param parity The parity used (USBHostSerial::None, USBHostSerial::Odd, USBHostSerial::Even, USBHostSerial::Mark, USBHostSerial::Space; default = USBHostSerial::None)
00115      *  @param stop The number of stop bits (1 or 2; default = 1)
00116      */
00117     void format(int bits = 8, Parity parity = USBHostSerial::None, int stop_bits = 1);
00118 
00119     //Added these methods to fix an overriding/inheritance issue I didn't want to deal with
00120     int putch(int c);
00121     int getch();
00122     int writeBuffer(uint8_t* buffer, int buffer_length);
00123     
00124 protected:
00125     //From IUSBEnumerator
00126     virtual void setVidPid(uint16_t vid, uint16_t pid);
00127     virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
00128     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00129 
00130     virtual int _getc();
00131     virtual int _putc(int c);
00132         
00133 private:
00134     USBHost * host;
00135     USBDeviceConnected * dev;
00136     USBEndpoint * bulk_in;
00137     USBEndpoint * bulk_out;
00138     uint32_t size_bulk_in;
00139     uint32_t size_bulk_out;
00140 
00141     bool dev_connected;
00142 
00143     void init();
00144 
00145     MtxCircBuffer<uint8_t, 64> circ_buf;
00146 
00147     uint8_t buf[64];
00148 
00149     typedef __packed struct {
00150         uint32_t baudrate;
00151         uint8_t stop_bits;
00152         uint8_t parity;
00153         uint8_t data_bits;
00154     } LINE_CODING;
00155     
00156     LINE_CODING line_coding;
00157 
00158     void rxHandler();
00159     void txHandler();
00160     FunctionPointer rx;
00161     FunctionPointer tx;
00162 
00163     int serial_intf;
00164     bool serial_device_found;
00165 
00166 };
00167 
00168 #endif
00169 
00170 #endif