ryou sato / Mbed 2 deprecated LPC11U35_CTswitch_relay

Dependencies:   mbed LPC11U35_MCP41HV51-503EST

Committer:
ryousato
Date:
Mon Aug 17 01:01:49 2020 +0000
Revision:
0:df1e1f84ded8
1

Who changed what in which revision?

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