Pinscape Controller version 1 fork. This is a fork to allow for ongoing bug fixes to the original controller version, from before the major changes for the expansion board project.
Dependencies: FastIO FastPWM SimpleDMA mbed
Fork of Pinscape_Controller by
USBDevice/USBSerial/USBSerial.h@68:edfecf67a931, 2016-02-15 (annotated)
- Committer:
- mjr
- Date:
- Mon Feb 15 23:03:55 2016 +0000
- Revision:
- 68:edfecf67a931
- Parent:
- 52:63f0a9b45f0c
Finalize USB library updates to fix compatibility problems introduced in USBHAL_KL25Z overhaul.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 52:63f0a9b45f0c | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mjr | 52:63f0a9b45f0c | 2 | * |
mjr | 52:63f0a9b45f0c | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mjr | 52:63f0a9b45f0c | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mjr | 52:63f0a9b45f0c | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mjr | 52:63f0a9b45f0c | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mjr | 52:63f0a9b45f0c | 7 | * Software is furnished to do so, subject to the following conditions: |
mjr | 52:63f0a9b45f0c | 8 | * |
mjr | 52:63f0a9b45f0c | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mjr | 52:63f0a9b45f0c | 10 | * substantial portions of the Software. |
mjr | 52:63f0a9b45f0c | 11 | * |
mjr | 52:63f0a9b45f0c | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mjr | 52:63f0a9b45f0c | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mjr | 52:63f0a9b45f0c | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mjr | 52:63f0a9b45f0c | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mjr | 52:63f0a9b45f0c | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mjr | 52:63f0a9b45f0c | 17 | */ |
mjr | 52:63f0a9b45f0c | 18 | |
mjr | 52:63f0a9b45f0c | 19 | #ifndef USBSERIAL_H |
mjr | 52:63f0a9b45f0c | 20 | #define USBSERIAL_H |
mjr | 52:63f0a9b45f0c | 21 | |
mjr | 52:63f0a9b45f0c | 22 | #include "USBCDC.h" |
mjr | 52:63f0a9b45f0c | 23 | #include "Stream.h" |
mjr | 52:63f0a9b45f0c | 24 | #include "CircBuffer.h" |
mjr | 52:63f0a9b45f0c | 25 | |
mjr | 52:63f0a9b45f0c | 26 | |
mjr | 52:63f0a9b45f0c | 27 | /** |
mjr | 52:63f0a9b45f0c | 28 | * USBSerial example |
mjr | 52:63f0a9b45f0c | 29 | * |
mjr | 52:63f0a9b45f0c | 30 | * @code |
mjr | 52:63f0a9b45f0c | 31 | * #include "mbed.h" |
mjr | 52:63f0a9b45f0c | 32 | * #include "USBSerial.h" |
mjr | 52:63f0a9b45f0c | 33 | * |
mjr | 52:63f0a9b45f0c | 34 | * //Virtual serial port over USB |
mjr | 52:63f0a9b45f0c | 35 | * USBSerial serial; |
mjr | 52:63f0a9b45f0c | 36 | * |
mjr | 52:63f0a9b45f0c | 37 | * int main(void) { |
mjr | 52:63f0a9b45f0c | 38 | * |
mjr | 52:63f0a9b45f0c | 39 | * while(1) |
mjr | 52:63f0a9b45f0c | 40 | * { |
mjr | 52:63f0a9b45f0c | 41 | * serial.printf("I am a virtual serial port\n"); |
mjr | 52:63f0a9b45f0c | 42 | * wait(1); |
mjr | 52:63f0a9b45f0c | 43 | * } |
mjr | 52:63f0a9b45f0c | 44 | * } |
mjr | 52:63f0a9b45f0c | 45 | * @endcode |
mjr | 52:63f0a9b45f0c | 46 | */ |
mjr | 52:63f0a9b45f0c | 47 | class USBSerial: public USBCDC, public Stream { |
mjr | 52:63f0a9b45f0c | 48 | public: |
mjr | 52:63f0a9b45f0c | 49 | |
mjr | 52:63f0a9b45f0c | 50 | /** |
mjr | 52:63f0a9b45f0c | 51 | * Constructor |
mjr | 52:63f0a9b45f0c | 52 | * |
mjr | 52:63f0a9b45f0c | 53 | * @param vendor_id Your vendor_id (default: 0x1f00) |
mjr | 52:63f0a9b45f0c | 54 | * @param product_id Your product_id (default: 0x2012) |
mjr | 52:63f0a9b45f0c | 55 | * @param product_release Your preoduct_release (default: 0x0001) |
mjr | 52:63f0a9b45f0c | 56 | * @param connect_blocking define if the connection must be blocked if USB not plugged in |
mjr | 52:63f0a9b45f0c | 57 | * |
mjr | 52:63f0a9b45f0c | 58 | */ |
mjr | 52:63f0a9b45f0c | 59 | USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001, bool connect_blocking = true): USBCDC(vendor_id, product_id, product_release, connect_blocking), buf(128){ |
mjr | 52:63f0a9b45f0c | 60 | settingsChangedCallback = 0; |
mjr | 52:63f0a9b45f0c | 61 | }; |
mjr | 52:63f0a9b45f0c | 62 | |
mjr | 52:63f0a9b45f0c | 63 | |
mjr | 52:63f0a9b45f0c | 64 | /** |
mjr | 52:63f0a9b45f0c | 65 | * Send a character. You can use puts, printf. |
mjr | 52:63f0a9b45f0c | 66 | * |
mjr | 52:63f0a9b45f0c | 67 | * @param c character to be sent |
mjr | 52:63f0a9b45f0c | 68 | * @returns true if there is no error, false otherwise |
mjr | 52:63f0a9b45f0c | 69 | */ |
mjr | 52:63f0a9b45f0c | 70 | virtual int _putc(int c); |
mjr | 52:63f0a9b45f0c | 71 | |
mjr | 52:63f0a9b45f0c | 72 | /** |
mjr | 52:63f0a9b45f0c | 73 | * Read a character: blocking |
mjr | 52:63f0a9b45f0c | 74 | * |
mjr | 52:63f0a9b45f0c | 75 | * @returns character read |
mjr | 52:63f0a9b45f0c | 76 | */ |
mjr | 52:63f0a9b45f0c | 77 | virtual int _getc(); |
mjr | 52:63f0a9b45f0c | 78 | |
mjr | 52:63f0a9b45f0c | 79 | /** |
mjr | 52:63f0a9b45f0c | 80 | * Check the number of bytes available. |
mjr | 52:63f0a9b45f0c | 81 | * |
mjr | 52:63f0a9b45f0c | 82 | * @returns the number of bytes available |
mjr | 52:63f0a9b45f0c | 83 | */ |
mjr | 52:63f0a9b45f0c | 84 | uint8_t available(); |
mjr | 52:63f0a9b45f0c | 85 | |
mjr | 52:63f0a9b45f0c | 86 | /** Determine if there is a character available to read |
mjr | 52:63f0a9b45f0c | 87 | * |
mjr | 52:63f0a9b45f0c | 88 | * @returns |
mjr | 52:63f0a9b45f0c | 89 | * 1 if there is a character available to read, |
mjr | 52:63f0a9b45f0c | 90 | * 0 otherwise |
mjr | 52:63f0a9b45f0c | 91 | */ |
mjr | 52:63f0a9b45f0c | 92 | int readable() { return available() ? 1 : 0; } |
mjr | 52:63f0a9b45f0c | 93 | |
mjr | 52:63f0a9b45f0c | 94 | /** Determine if there is space available to write a character |
mjr | 52:63f0a9b45f0c | 95 | * |
mjr | 52:63f0a9b45f0c | 96 | * @returns |
mjr | 52:63f0a9b45f0c | 97 | * 1 if there is space to write a character, |
mjr | 52:63f0a9b45f0c | 98 | * 0 otherwise |
mjr | 52:63f0a9b45f0c | 99 | */ |
mjr | 52:63f0a9b45f0c | 100 | int writeable() { return 1; } // always return 1, for write operation is blocking |
mjr | 52:63f0a9b45f0c | 101 | |
mjr | 52:63f0a9b45f0c | 102 | /** |
mjr | 52:63f0a9b45f0c | 103 | * Write a block of data. |
mjr | 52:63f0a9b45f0c | 104 | * |
mjr | 52:63f0a9b45f0c | 105 | * For more efficiency, a block of size 64 (maximum size of a bulk endpoint) has to be written. |
mjr | 52:63f0a9b45f0c | 106 | * |
mjr | 52:63f0a9b45f0c | 107 | * @param buf pointer on data which will be written |
mjr | 52:63f0a9b45f0c | 108 | * @param size size of the buffer. The maximum size of a block is limited by the size of the endpoint (64 bytes) |
mjr | 52:63f0a9b45f0c | 109 | * |
mjr | 52:63f0a9b45f0c | 110 | * @returns true if successfull |
mjr | 52:63f0a9b45f0c | 111 | */ |
mjr | 52:63f0a9b45f0c | 112 | bool writeBlock(uint8_t * buf, uint16_t size); |
mjr | 52:63f0a9b45f0c | 113 | |
mjr | 52:63f0a9b45f0c | 114 | /** |
mjr | 52:63f0a9b45f0c | 115 | * Attach a member function to call when a packet is received. |
mjr | 52:63f0a9b45f0c | 116 | * |
mjr | 52:63f0a9b45f0c | 117 | * @param tptr pointer to the object to call the member function on |
mjr | 52:63f0a9b45f0c | 118 | * @param mptr pointer to the member function to be called |
mjr | 52:63f0a9b45f0c | 119 | */ |
mjr | 52:63f0a9b45f0c | 120 | template<typename T> |
mjr | 52:63f0a9b45f0c | 121 | void attach(T* tptr, void (T::*mptr)(void)) { |
mjr | 52:63f0a9b45f0c | 122 | if((mptr != NULL) && (tptr != NULL)) { |
mjr | 52:63f0a9b45f0c | 123 | rx.attach(tptr, mptr); |
mjr | 52:63f0a9b45f0c | 124 | } |
mjr | 52:63f0a9b45f0c | 125 | } |
mjr | 52:63f0a9b45f0c | 126 | |
mjr | 52:63f0a9b45f0c | 127 | /** |
mjr | 52:63f0a9b45f0c | 128 | * Attach a callback called when a packet is received |
mjr | 52:63f0a9b45f0c | 129 | * |
mjr | 52:63f0a9b45f0c | 130 | * @param fptr function pointer |
mjr | 52:63f0a9b45f0c | 131 | */ |
mjr | 52:63f0a9b45f0c | 132 | void attach(void (*fptr)(void)) { |
mjr | 52:63f0a9b45f0c | 133 | if(fptr != NULL) { |
mjr | 52:63f0a9b45f0c | 134 | rx.attach(fptr); |
mjr | 52:63f0a9b45f0c | 135 | } |
mjr | 52:63f0a9b45f0c | 136 | } |
mjr | 52:63f0a9b45f0c | 137 | |
mjr | 52:63f0a9b45f0c | 138 | /** |
mjr | 52:63f0a9b45f0c | 139 | * Attach a callback to call when serial's settings are changed. |
mjr | 52:63f0a9b45f0c | 140 | * |
mjr | 52:63f0a9b45f0c | 141 | * @param fptr function pointer |
mjr | 52:63f0a9b45f0c | 142 | */ |
mjr | 52:63f0a9b45f0c | 143 | void attach(void (*fptr)(int baud, int bits, int parity, int stop)) { |
mjr | 52:63f0a9b45f0c | 144 | settingsChangedCallback = fptr; |
mjr | 52:63f0a9b45f0c | 145 | } |
mjr | 52:63f0a9b45f0c | 146 | |
mjr | 52:63f0a9b45f0c | 147 | protected: |
mjr | 52:63f0a9b45f0c | 148 | virtual bool EP2_OUT_callback(); |
mjr | 52:63f0a9b45f0c | 149 | virtual void lineCodingChanged(int baud, int bits, int parity, int stop){ |
mjr | 52:63f0a9b45f0c | 150 | if (settingsChangedCallback) { |
mjr | 52:63f0a9b45f0c | 151 | settingsChangedCallback(baud, bits, parity, stop); |
mjr | 52:63f0a9b45f0c | 152 | } |
mjr | 52:63f0a9b45f0c | 153 | } |
mjr | 52:63f0a9b45f0c | 154 | |
mjr | 52:63f0a9b45f0c | 155 | private: |
mjr | 52:63f0a9b45f0c | 156 | FunctionPointer rx; |
mjr | 52:63f0a9b45f0c | 157 | CircBuffer<uint8_t> buf; |
mjr | 52:63f0a9b45f0c | 158 | void (*settingsChangedCallback)(int baud, int bits, int parity, int stop); |
mjr | 52:63f0a9b45f0c | 159 | }; |
mjr | 52:63f0a9b45f0c | 160 | |
mjr | 52:63f0a9b45f0c | 161 | #endif |