Point Labs / Mbed OS Threaded_LoRa_Modem

Dependencies:   RadioHeadLite

Committer:
rlanders73
Date:
Mon Nov 26 21:01:04 2018 +0000
Revision:
1:75d533f15c95
merged in _write and _read functions

Who changed what in which revision?

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