![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Kalibriersoftware Stromwerte
USBSerial/USBSerial.h@0:5e35c180ed4a, 2015-11-23 (annotated)
- Committer:
- Racer01014
- Date:
- Mon Nov 23 16:09:54 2015 +0000
- Revision:
- 0:5e35c180ed4a
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Racer01014 | 0:5e35c180ed4a | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
Racer01014 | 0:5e35c180ed4a | 2 | * |
Racer01014 | 0:5e35c180ed4a | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Racer01014 | 0:5e35c180ed4a | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Racer01014 | 0:5e35c180ed4a | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Racer01014 | 0:5e35c180ed4a | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Racer01014 | 0:5e35c180ed4a | 7 | * Software is furnished to do so, subject to the following conditions: |
Racer01014 | 0:5e35c180ed4a | 8 | * |
Racer01014 | 0:5e35c180ed4a | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Racer01014 | 0:5e35c180ed4a | 10 | * substantial portions of the Software. |
Racer01014 | 0:5e35c180ed4a | 11 | * |
Racer01014 | 0:5e35c180ed4a | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Racer01014 | 0:5e35c180ed4a | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Racer01014 | 0:5e35c180ed4a | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Racer01014 | 0:5e35c180ed4a | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Racer01014 | 0:5e35c180ed4a | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Racer01014 | 0:5e35c180ed4a | 17 | */ |
Racer01014 | 0:5e35c180ed4a | 18 | |
Racer01014 | 0:5e35c180ed4a | 19 | #ifndef USBSERIAL_H |
Racer01014 | 0:5e35c180ed4a | 20 | #define USBSERIAL_H |
Racer01014 | 0:5e35c180ed4a | 21 | |
Racer01014 | 0:5e35c180ed4a | 22 | #include "USBCDC.h" |
Racer01014 | 0:5e35c180ed4a | 23 | #include "Stream.h" |
Racer01014 | 0:5e35c180ed4a | 24 | #include "CircBuffer.h" |
Racer01014 | 0:5e35c180ed4a | 25 | |
Racer01014 | 0:5e35c180ed4a | 26 | |
Racer01014 | 0:5e35c180ed4a | 27 | /** |
Racer01014 | 0:5e35c180ed4a | 28 | * USBSerial example |
Racer01014 | 0:5e35c180ed4a | 29 | * |
Racer01014 | 0:5e35c180ed4a | 30 | * @code |
Racer01014 | 0:5e35c180ed4a | 31 | * #include "mbed.h" |
Racer01014 | 0:5e35c180ed4a | 32 | * #include "USBSerial.h" |
Racer01014 | 0:5e35c180ed4a | 33 | * |
Racer01014 | 0:5e35c180ed4a | 34 | * //Virtual serial port over USB |
Racer01014 | 0:5e35c180ed4a | 35 | * USBSerial serial; |
Racer01014 | 0:5e35c180ed4a | 36 | * |
Racer01014 | 0:5e35c180ed4a | 37 | * int main(void) { |
Racer01014 | 0:5e35c180ed4a | 38 | * |
Racer01014 | 0:5e35c180ed4a | 39 | * while(1) |
Racer01014 | 0:5e35c180ed4a | 40 | * { |
Racer01014 | 0:5e35c180ed4a | 41 | * serial.printf("I am a virtual serial port\n"); |
Racer01014 | 0:5e35c180ed4a | 42 | * wait(1); |
Racer01014 | 0:5e35c180ed4a | 43 | * } |
Racer01014 | 0:5e35c180ed4a | 44 | * } |
Racer01014 | 0:5e35c180ed4a | 45 | * @endcode |
Racer01014 | 0:5e35c180ed4a | 46 | */ |
Racer01014 | 0:5e35c180ed4a | 47 | class USBSerial: public USBCDC, public Stream { |
Racer01014 | 0:5e35c180ed4a | 48 | public: |
Racer01014 | 0:5e35c180ed4a | 49 | |
Racer01014 | 0:5e35c180ed4a | 50 | /** |
Racer01014 | 0:5e35c180ed4a | 51 | * Constructor |
Racer01014 | 0:5e35c180ed4a | 52 | * |
Racer01014 | 0:5e35c180ed4a | 53 | * @param vendor_id Your vendor_id (default: 0x1f00) |
Racer01014 | 0:5e35c180ed4a | 54 | * @param product_id Your product_id (default: 0x2012) |
Racer01014 | 0:5e35c180ed4a | 55 | * @param product_release Your preoduct_release (default: 0x0001) |
Racer01014 | 0:5e35c180ed4a | 56 | * |
Racer01014 | 0:5e35c180ed4a | 57 | */ |
Racer01014 | 0:5e35c180ed4a | 58 | USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ }; |
Racer01014 | 0:5e35c180ed4a | 59 | |
Racer01014 | 0:5e35c180ed4a | 60 | |
Racer01014 | 0:5e35c180ed4a | 61 | /** |
Racer01014 | 0:5e35c180ed4a | 62 | * Send a character. You can use puts, printf. |
Racer01014 | 0:5e35c180ed4a | 63 | * |
Racer01014 | 0:5e35c180ed4a | 64 | * @param c character to be sent |
Racer01014 | 0:5e35c180ed4a | 65 | * @returns true if there is no error, false otherwise |
Racer01014 | 0:5e35c180ed4a | 66 | */ |
Racer01014 | 0:5e35c180ed4a | 67 | virtual int _putc(int c); |
Racer01014 | 0:5e35c180ed4a | 68 | |
Racer01014 | 0:5e35c180ed4a | 69 | /** |
Racer01014 | 0:5e35c180ed4a | 70 | * Read a character: blocking |
Racer01014 | 0:5e35c180ed4a | 71 | * |
Racer01014 | 0:5e35c180ed4a | 72 | * @returns character read |
Racer01014 | 0:5e35c180ed4a | 73 | */ |
Racer01014 | 0:5e35c180ed4a | 74 | virtual int _getc(); |
Racer01014 | 0:5e35c180ed4a | 75 | |
Racer01014 | 0:5e35c180ed4a | 76 | /** |
Racer01014 | 0:5e35c180ed4a | 77 | * Check the number of bytes available. |
Racer01014 | 0:5e35c180ed4a | 78 | * |
Racer01014 | 0:5e35c180ed4a | 79 | * @returns the number of bytes available |
Racer01014 | 0:5e35c180ed4a | 80 | */ |
Racer01014 | 0:5e35c180ed4a | 81 | uint8_t available(); |
Racer01014 | 0:5e35c180ed4a | 82 | |
Racer01014 | 0:5e35c180ed4a | 83 | /** |
Racer01014 | 0:5e35c180ed4a | 84 | * Write a block of data. |
Racer01014 | 0:5e35c180ed4a | 85 | * |
Racer01014 | 0:5e35c180ed4a | 86 | * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. |
Racer01014 | 0:5e35c180ed4a | 87 | * |
Racer01014 | 0:5e35c180ed4a | 88 | * @param buf pointer on data which will be written |
Racer01014 | 0:5e35c180ed4a | 89 | * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) |
Racer01014 | 0:5e35c180ed4a | 90 | * |
Racer01014 | 0:5e35c180ed4a | 91 | * @returns true if successfull |
Racer01014 | 0:5e35c180ed4a | 92 | */ |
Racer01014 | 0:5e35c180ed4a | 93 | bool writeBlock(uint8_t * buf, uint16_t size); |
Racer01014 | 0:5e35c180ed4a | 94 | |
Racer01014 | 0:5e35c180ed4a | 95 | /** |
Racer01014 | 0:5e35c180ed4a | 96 | * Attach a member function to call when a packet is received. |
Racer01014 | 0:5e35c180ed4a | 97 | * |
Racer01014 | 0:5e35c180ed4a | 98 | * @param tptr pointer to the object to call the member function on |
Racer01014 | 0:5e35c180ed4a | 99 | * @param mptr pointer to the member function to be called |
Racer01014 | 0:5e35c180ed4a | 100 | */ |
Racer01014 | 0:5e35c180ed4a | 101 | template<typename T> |
Racer01014 | 0:5e35c180ed4a | 102 | void attach(T* tptr, void (T::*mptr)(void)) { |
Racer01014 | 0:5e35c180ed4a | 103 | if((mptr != NULL) && (tptr != NULL)) { |
Racer01014 | 0:5e35c180ed4a | 104 | rx.attach(tptr, mptr); |
Racer01014 | 0:5e35c180ed4a | 105 | } |
Racer01014 | 0:5e35c180ed4a | 106 | } |
Racer01014 | 0:5e35c180ed4a | 107 | |
Racer01014 | 0:5e35c180ed4a | 108 | /** |
Racer01014 | 0:5e35c180ed4a | 109 | * Attach a callback called when a packet is received |
Racer01014 | 0:5e35c180ed4a | 110 | * |
Racer01014 | 0:5e35c180ed4a | 111 | * @param fptr function pointer |
Racer01014 | 0:5e35c180ed4a | 112 | */ |
Racer01014 | 0:5e35c180ed4a | 113 | void attach(void (*fn)(void)) { |
Racer01014 | 0:5e35c180ed4a | 114 | if(fn != NULL) { |
Racer01014 | 0:5e35c180ed4a | 115 | rx.attach(fn); |
Racer01014 | 0:5e35c180ed4a | 116 | } |
Racer01014 | 0:5e35c180ed4a | 117 | } |
Racer01014 | 0:5e35c180ed4a | 118 | |
Racer01014 | 0:5e35c180ed4a | 119 | |
Racer01014 | 0:5e35c180ed4a | 120 | protected: |
Racer01014 | 0:5e35c180ed4a | 121 | virtual bool EP2_OUT_callback(); |
Racer01014 | 0:5e35c180ed4a | 122 | |
Racer01014 | 0:5e35c180ed4a | 123 | private: |
Racer01014 | 0:5e35c180ed4a | 124 | FunctionPointer rx; |
Racer01014 | 0:5e35c180ed4a | 125 | CircBuffer<uint8_t> buf; |
Racer01014 | 0:5e35c180ed4a | 126 | }; |
Racer01014 | 0:5e35c180ed4a | 127 | |
Racer01014 | 0:5e35c180ed4a | 128 | #endif |