Lucas Lim / Mbed 2 deprecated HSP_Temperature_Barometer_CS3237

Dependencies:   mbed

Committer:
lucaslwl
Date:
Mon Aug 26 08:11:41 2019 +0000
Revision:
22:5c07298d3383
add library folder

Who changed what in which revision?

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