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 USBAudio_H
lucaslwl 22:5c07298d3383 20 #define USBAudio_H
lucaslwl 22:5c07298d3383 21
lucaslwl 22:5c07298d3383 22 /* These headers are included for child class. */
lucaslwl 22:5c07298d3383 23 #include "USBEndpoints.h"
lucaslwl 22:5c07298d3383 24 #include "USBDescriptor.h"
lucaslwl 22:5c07298d3383 25 #include "USBDevice_Types.h"
lucaslwl 22:5c07298d3383 26
lucaslwl 22:5c07298d3383 27 #include "USBDevice.h"
lucaslwl 22:5c07298d3383 28
lucaslwl 22:5c07298d3383 29
lucaslwl 22:5c07298d3383 30 /**
lucaslwl 22:5c07298d3383 31 * USBAudio example
lucaslwl 22:5c07298d3383 32 *
lucaslwl 22:5c07298d3383 33 * @code
lucaslwl 22:5c07298d3383 34 * #include "mbed.h"
lucaslwl 22:5c07298d3383 35 * #include "USBAudio.h"
lucaslwl 22:5c07298d3383 36 *
lucaslwl 22:5c07298d3383 37 * Serial pc(USBTX, USBRX);
lucaslwl 22:5c07298d3383 38 *
lucaslwl 22:5c07298d3383 39 * // frequency: 48 kHz
lucaslwl 22:5c07298d3383 40 * #define FREQ 48000
lucaslwl 22:5c07298d3383 41 *
lucaslwl 22:5c07298d3383 42 * // 1 channel: mono
lucaslwl 22:5c07298d3383 43 * #define NB_CHA 1
lucaslwl 22:5c07298d3383 44 *
lucaslwl 22:5c07298d3383 45 * // length of an audio packet: each ms, we receive 48 * 16bits ->48 * 2 bytes. as there is one channel, the length will be 48 * 2 * 1
lucaslwl 22:5c07298d3383 46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
lucaslwl 22:5c07298d3383 47 *
lucaslwl 22:5c07298d3383 48 * // USBAudio
lucaslwl 22:5c07298d3383 49 * USBAudio audio(FREQ, NB_CHA);
lucaslwl 22:5c07298d3383 50 *
lucaslwl 22:5c07298d3383 51 * int main() {
lucaslwl 22:5c07298d3383 52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
lucaslwl 22:5c07298d3383 53 *
lucaslwl 22:5c07298d3383 54 * while (1) {
lucaslwl 22:5c07298d3383 55 * // read an audio packet
lucaslwl 22:5c07298d3383 56 * audio.read((uint8_t *)buf);
lucaslwl 22:5c07298d3383 57 *
lucaslwl 22:5c07298d3383 58 *
lucaslwl 22:5c07298d3383 59 * // print packet received
lucaslwl 22:5c07298d3383 60 * pc.printf("recv: ");
lucaslwl 22:5c07298d3383 61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
lucaslwl 22:5c07298d3383 62 * pc.printf("%d ", buf[i]);
lucaslwl 22:5c07298d3383 63 * }
lucaslwl 22:5c07298d3383 64 * pc.printf("\r\n");
lucaslwl 22:5c07298d3383 65 * }
lucaslwl 22:5c07298d3383 66 * }
lucaslwl 22:5c07298d3383 67 * @endcode
lucaslwl 22:5c07298d3383 68 */
lucaslwl 22:5c07298d3383 69 class USBAudio: public USBDevice {
lucaslwl 22:5c07298d3383 70 public:
lucaslwl 22:5c07298d3383 71
lucaslwl 22:5c07298d3383 72 /**
lucaslwl 22:5c07298d3383 73 * Constructor
lucaslwl 22:5c07298d3383 74 *
lucaslwl 22:5c07298d3383 75 * @param frequency_in frequency in Hz (default: 48000)
lucaslwl 22:5c07298d3383 76 * @param channel_nb_in channel number (1 or 2) (default: 1)
lucaslwl 22:5c07298d3383 77 * @param frequency_out frequency in Hz (default: 8000)
lucaslwl 22:5c07298d3383 78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
lucaslwl 22:5c07298d3383 79 * @param vendor_id Your vendor_id
lucaslwl 22:5c07298d3383 80 * @param product_id Your product_id
lucaslwl 22:5c07298d3383 81 * @param product_release Your preoduct_release
lucaslwl 22:5c07298d3383 82 */
lucaslwl 22:5c07298d3383 83 USBAudio(uint32_t frequency_in = 48000, uint8_t channel_nb_in = 1, uint32_t frequency_out = 8000, uint8_t channel_nb_out = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100);
lucaslwl 22:5c07298d3383 84
lucaslwl 22:5c07298d3383 85 /**
lucaslwl 22:5c07298d3383 86 * Get current volume between 0.0 and 1.0
lucaslwl 22:5c07298d3383 87 *
lucaslwl 22:5c07298d3383 88 * @returns volume
lucaslwl 22:5c07298d3383 89 */
lucaslwl 22:5c07298d3383 90 float getVolume();
lucaslwl 22:5c07298d3383 91
lucaslwl 22:5c07298d3383 92 /**
lucaslwl 22:5c07298d3383 93 * Read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Blocking
lucaslwl 22:5c07298d3383 94 *
lucaslwl 22:5c07298d3383 95 * @param buf pointer on a buffer which will be filled with an audio packet
lucaslwl 22:5c07298d3383 96 *
lucaslwl 22:5c07298d3383 97 * @returns true if successfull
lucaslwl 22:5c07298d3383 98 */
lucaslwl 22:5c07298d3383 99 bool read(uint8_t * buf);
lucaslwl 22:5c07298d3383 100
lucaslwl 22:5c07298d3383 101 /**
lucaslwl 22:5c07298d3383 102 * Try to read an audio packet. During a frame, only a single reading (you can't write and read an audio packet during the same frame)can be done using this method. Warning: Non Blocking
lucaslwl 22:5c07298d3383 103 *
lucaslwl 22:5c07298d3383 104 * @param buf pointer on a buffer which will be filled if an audio packet is available
lucaslwl 22:5c07298d3383 105 *
lucaslwl 22:5c07298d3383 106 * @returns true if successfull
lucaslwl 22:5c07298d3383 107 */
lucaslwl 22:5c07298d3383 108 bool readNB(uint8_t * buf);
lucaslwl 22:5c07298d3383 109
lucaslwl 22:5c07298d3383 110 /**
lucaslwl 22:5c07298d3383 111 * Write an audio packet. During a frame, only a single writing (you can't write and read an audio packet during the same frame)can be done using this method.
lucaslwl 22:5c07298d3383 112 *
lucaslwl 22:5c07298d3383 113 * @param buf pointer on the audio packet which will be sent
lucaslwl 22:5c07298d3383 114 * @returns true if successful
lucaslwl 22:5c07298d3383 115 */
lucaslwl 22:5c07298d3383 116 bool write(uint8_t * buf);
lucaslwl 22:5c07298d3383 117
lucaslwl 22:5c07298d3383 118 /**
lucaslwl 22:5c07298d3383 119 * Write and read an audio packet at the same time (on the same frame)
lucaslwl 22:5c07298d3383 120 *
lucaslwl 22:5c07298d3383 121 * @param buf_read pointer on a buffer which will be filled with an audio packet
lucaslwl 22:5c07298d3383 122 * @param buf_write pointer on the audio packet which will be sent
lucaslwl 22:5c07298d3383 123 * @returns true if successful
lucaslwl 22:5c07298d3383 124 */
lucaslwl 22:5c07298d3383 125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
lucaslwl 22:5c07298d3383 126
lucaslwl 22:5c07298d3383 127
lucaslwl 22:5c07298d3383 128 /** attach a handler to update the volume
lucaslwl 22:5c07298d3383 129 *
lucaslwl 22:5c07298d3383 130 * @param function Function to attach
lucaslwl 22:5c07298d3383 131 *
lucaslwl 22:5c07298d3383 132 */
lucaslwl 22:5c07298d3383 133 void attach(void(*fptr)(void)) {
lucaslwl 22:5c07298d3383 134 updateVol.attach(fptr);
lucaslwl 22:5c07298d3383 135 }
lucaslwl 22:5c07298d3383 136
lucaslwl 22:5c07298d3383 137 /** Attach a nonstatic void/void member function to update the volume
lucaslwl 22:5c07298d3383 138 *
lucaslwl 22:5c07298d3383 139 * @param tptr Object pointer
lucaslwl 22:5c07298d3383 140 * @param mptr Member function pointer
lucaslwl 22:5c07298d3383 141 *
lucaslwl 22:5c07298d3383 142 */
lucaslwl 22:5c07298d3383 143 template<typename T>
lucaslwl 22:5c07298d3383 144 void attach(T *tptr, void(T::*mptr)(void)) {
lucaslwl 22:5c07298d3383 145 updateVol.attach(tptr, mptr);
lucaslwl 22:5c07298d3383 146 }
lucaslwl 22:5c07298d3383 147
lucaslwl 22:5c07298d3383 148
lucaslwl 22:5c07298d3383 149 protected:
lucaslwl 22:5c07298d3383 150
lucaslwl 22:5c07298d3383 151 /*
lucaslwl 22:5c07298d3383 152 * Called by USBDevice layer. Set configuration of the device.
lucaslwl 22:5c07298d3383 153 * For instance, you can add all endpoints that you need on this function.
lucaslwl 22:5c07298d3383 154 *
lucaslwl 22:5c07298d3383 155 * @param configuration Number of the configuration
lucaslwl 22:5c07298d3383 156 * @returns true if class handles this request
lucaslwl 22:5c07298d3383 157 */
lucaslwl 22:5c07298d3383 158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
lucaslwl 22:5c07298d3383 159
lucaslwl 22:5c07298d3383 160 /*
lucaslwl 22:5c07298d3383 161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
lucaslwl 22:5c07298d3383 162 * This is used to handle extensions to standard requests
lucaslwl 22:5c07298d3383 163 * and class specific requests
lucaslwl 22:5c07298d3383 164 *
lucaslwl 22:5c07298d3383 165 * @returns true if class handles this request
lucaslwl 22:5c07298d3383 166 */
lucaslwl 22:5c07298d3383 167 virtual bool USBCallback_request();
lucaslwl 22:5c07298d3383 168
lucaslwl 22:5c07298d3383 169 /*
lucaslwl 22:5c07298d3383 170 * Get string product descriptor
lucaslwl 22:5c07298d3383 171 *
lucaslwl 22:5c07298d3383 172 * @returns pointer to the string product descriptor
lucaslwl 22:5c07298d3383 173 */
lucaslwl 22:5c07298d3383 174 virtual uint8_t * stringIproductDesc();
lucaslwl 22:5c07298d3383 175
lucaslwl 22:5c07298d3383 176 /*
lucaslwl 22:5c07298d3383 177 * Get string interface descriptor
lucaslwl 22:5c07298d3383 178 *
lucaslwl 22:5c07298d3383 179 * @returns pointer to the string interface descriptor
lucaslwl 22:5c07298d3383 180 */
lucaslwl 22:5c07298d3383 181 virtual uint8_t * stringIinterfaceDesc();
lucaslwl 22:5c07298d3383 182
lucaslwl 22:5c07298d3383 183 /*
lucaslwl 22:5c07298d3383 184 * Get configuration descriptor
lucaslwl 22:5c07298d3383 185 *
lucaslwl 22:5c07298d3383 186 * @returns pointer to the configuration descriptor
lucaslwl 22:5c07298d3383 187 */
lucaslwl 22:5c07298d3383 188 virtual uint8_t * configurationDesc();
lucaslwl 22:5c07298d3383 189
lucaslwl 22:5c07298d3383 190 /*
lucaslwl 22:5c07298d3383 191 * Called by USBDevice layer. Set interface/alternate of the device.
lucaslwl 22:5c07298d3383 192 *
lucaslwl 22:5c07298d3383 193 * @param interface Number of the interface to be configured
lucaslwl 22:5c07298d3383 194 * @param alternate Number of the alternate to be configured
lucaslwl 22:5c07298d3383 195 * @returns true if class handles this request
lucaslwl 22:5c07298d3383 196 */
lucaslwl 22:5c07298d3383 197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
lucaslwl 22:5c07298d3383 198
lucaslwl 22:5c07298d3383 199 /*
lucaslwl 22:5c07298d3383 200 * Called by USBDevice on Endpoint0 request completion
lucaslwl 22:5c07298d3383 201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
lucaslwl 22:5c07298d3383 202 *
lucaslwl 22:5c07298d3383 203 * In this case it is used to indicate that a HID report has
lucaslwl 22:5c07298d3383 204 * been received from the host on endpoint 0
lucaslwl 22:5c07298d3383 205 *
lucaslwl 22:5c07298d3383 206 * @param buf buffer received on endpoint 0
lucaslwl 22:5c07298d3383 207 * @param length length of this buffer
lucaslwl 22:5c07298d3383 208 */
lucaslwl 22:5c07298d3383 209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
lucaslwl 22:5c07298d3383 210
lucaslwl 22:5c07298d3383 211 /*
lucaslwl 22:5c07298d3383 212 * Callback called on each Start of Frame event
lucaslwl 22:5c07298d3383 213 */
lucaslwl 22:5c07298d3383 214 virtual void SOF(int frameNumber);
lucaslwl 22:5c07298d3383 215
lucaslwl 22:5c07298d3383 216 /*
lucaslwl 22:5c07298d3383 217 * Callback called when a packet is received
lucaslwl 22:5c07298d3383 218 */
lucaslwl 22:5c07298d3383 219 virtual bool EPISO_OUT_callback();
lucaslwl 22:5c07298d3383 220
lucaslwl 22:5c07298d3383 221 /*
lucaslwl 22:5c07298d3383 222 * Callback called when a packet has been sent
lucaslwl 22:5c07298d3383 223 */
lucaslwl 22:5c07298d3383 224 virtual bool EPISO_IN_callback();
lucaslwl 22:5c07298d3383 225
lucaslwl 22:5c07298d3383 226 private:
lucaslwl 22:5c07298d3383 227
lucaslwl 22:5c07298d3383 228 // stream available ?
lucaslwl 22:5c07298d3383 229 volatile bool available;
lucaslwl 22:5c07298d3383 230
lucaslwl 22:5c07298d3383 231 // interrupt OUT has been received
lucaslwl 22:5c07298d3383 232 volatile bool interruptOUT;
lucaslwl 22:5c07298d3383 233
lucaslwl 22:5c07298d3383 234 // interrupt IN has been received
lucaslwl 22:5c07298d3383 235 volatile bool interruptIN;
lucaslwl 22:5c07298d3383 236
lucaslwl 22:5c07298d3383 237 // audio packet has been written
lucaslwl 22:5c07298d3383 238 volatile bool writeIN;
lucaslwl 22:5c07298d3383 239
lucaslwl 22:5c07298d3383 240 // FREQ
lucaslwl 22:5c07298d3383 241 uint32_t FREQ_OUT;
lucaslwl 22:5c07298d3383 242 uint32_t FREQ_IN;
lucaslwl 22:5c07298d3383 243
lucaslwl 22:5c07298d3383 244 // size of the maximum packet for the isochronous endpoint
lucaslwl 22:5c07298d3383 245 uint32_t PACKET_SIZE_ISO_IN;
lucaslwl 22:5c07298d3383 246 uint32_t PACKET_SIZE_ISO_OUT;
lucaslwl 22:5c07298d3383 247
lucaslwl 22:5c07298d3383 248 // mono, stereo,...
lucaslwl 22:5c07298d3383 249 uint8_t channel_nb_in;
lucaslwl 22:5c07298d3383 250 uint8_t channel_nb_out;
lucaslwl 22:5c07298d3383 251
lucaslwl 22:5c07298d3383 252 // channel config: master, left, right
lucaslwl 22:5c07298d3383 253 uint8_t channel_config_in;
lucaslwl 22:5c07298d3383 254 uint8_t channel_config_out;
lucaslwl 22:5c07298d3383 255
lucaslwl 22:5c07298d3383 256 // mute state
lucaslwl 22:5c07298d3383 257 uint8_t mute;
lucaslwl 22:5c07298d3383 258
lucaslwl 22:5c07298d3383 259 // Volume Current Value
lucaslwl 22:5c07298d3383 260 uint16_t volCur;
lucaslwl 22:5c07298d3383 261
lucaslwl 22:5c07298d3383 262 // Volume Minimum Value
lucaslwl 22:5c07298d3383 263 uint16_t volMin;
lucaslwl 22:5c07298d3383 264
lucaslwl 22:5c07298d3383 265 // Volume Maximum Value
lucaslwl 22:5c07298d3383 266 uint16_t volMax;
lucaslwl 22:5c07298d3383 267
lucaslwl 22:5c07298d3383 268 // Volume Resolution
lucaslwl 22:5c07298d3383 269 uint16_t volRes;
lucaslwl 22:5c07298d3383 270
lucaslwl 22:5c07298d3383 271 // Buffer containing one audio packet (to be read)
lucaslwl 22:5c07298d3383 272 volatile uint8_t * buf_stream_in;
lucaslwl 22:5c07298d3383 273
lucaslwl 22:5c07298d3383 274 // Buffer containing one audio packet (to be written)
lucaslwl 22:5c07298d3383 275 volatile uint8_t * buf_stream_out;
lucaslwl 22:5c07298d3383 276
lucaslwl 22:5c07298d3383 277 // callback to update volume
lucaslwl 22:5c07298d3383 278 FunctionPointer updateVol;
lucaslwl 22:5c07298d3383 279
lucaslwl 22:5c07298d3383 280 // boolean showing that the SOF handler has been called. Useful for readNB.
lucaslwl 22:5c07298d3383 281 volatile bool SOF_handler;
lucaslwl 22:5c07298d3383 282
lucaslwl 22:5c07298d3383 283 volatile float volume;
lucaslwl 22:5c07298d3383 284
lucaslwl 22:5c07298d3383 285 };
lucaslwl 22:5c07298d3383 286
lucaslwl 22:5c07298d3383 287 #endif