mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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