Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ChaNFSSD mbed ChaNFS
USBAudio.h
00001 /* Copyright (c) 2010-2011 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without 00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00007 * Software is furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #ifndef USBAudio_H 00020 #define USBAudio_H 00021 00022 /* These headers are included for child class. */ 00023 #include "USBEndpoints.h" 00024 #include "USBDescriptor.h" 00025 #include "USBDevice_Types.h" 00026 00027 #include "USBDevice.h" 00028 00029 00030 /** 00031 * USBAudio example 00032 * 00033 * #include "mbed.h" 00034 * #include "USBAudio.h" 00035 * 00036 * Serial pc(USBTX, USBRX); 00037 * 00038 * // frequency: 48 kHz 00039 * #define FREQ 48000 00040 * 00041 * // 1 channel: mono 00042 * #define NB_CHA 1 00043 * 00044 * // 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 00045 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1 00046 * 00047 * // USBAudio 00048 * USBAudio audio(FREQ, NB_CHA); 00049 * 00050 * int main() { 00051 * int16_t buf[AUDIO_LENGTH_PACKET/2]; 00052 * 00053 * while (1) { 00054 * // read an audio packet 00055 * audio.read((uint8_t *)buf); 00056 * 00057 * 00058 * // print packet received 00059 * pc.printf("recv: "); 00060 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) { 00061 * pc.printf("%d ", buf[i]); 00062 * } 00063 * pc.printf("\r\n"); 00064 * } 00065 * } 00066 * @endcode 00067 */ 00068 class USBAudio: public USBDevice { 00069 public: 00070 00071 /** 00072 * Constructor 00073 * 00074 * @param frequency frequency in Hz (default: 48000) 00075 * @param channel_nb channel number (1 or 2) (default: 1) 00076 * @param vendor_id Your vendor_id 00077 * @param product_id Your product_id 00078 * @param product_release Your preoduct_release 00079 */ 00080 USBAudio(uint32_t frequency = 48000, uint8_t channel_nb = 1, uint16_t vendor_id = 0x7bb8, uint16_t product_id = 0x1111, uint16_t product_release = 0x0100); 00081 00082 /** 00083 * Get current volume between 0.0 and 1.0 00084 * 00085 * @returns volume 00086 */ 00087 float getVolume(); 00088 00089 /** 00090 * Read an audio packet. warning: blocking 00091 * 00092 * @param buf pointer on a buffer which will be filled with an audio packet 00093 * 00094 * @returns true if successfull 00095 */ 00096 bool read(uint8_t * buf); 00097 00098 /** 00099 * Try to read an audio packet. warning: non blocking 00100 * 00101 * @param buf pointer on a buffer which will be filled if an audio packet is available 00102 * 00103 * @returns true if successfull 00104 */ 00105 bool readNB(uint8_t * buf); 00106 00107 00108 /** attach a handler to update the volume 00109 * 00110 * @param function Function to attach 00111 * 00112 */ 00113 void attach(void(*fptr)(void)) { 00114 updateVol.attach(fptr); 00115 } 00116 00117 /** Attach a nonstatic void/void member function to update the volume 00118 * 00119 * @param tptr Object pointer 00120 * @param mptr Member function pointer 00121 * 00122 */ 00123 template<typename T> 00124 void attach(T *tptr, void(T::*mptr)(void)) { 00125 updateVol.attach(tptr, mptr); 00126 } 00127 00128 00129 protected: 00130 00131 /* 00132 * Called by USBDevice layer. Set configuration of the device. 00133 * For instance, you can add all endpoints that you need on this function. 00134 * 00135 * @param configuration Number of the configuration 00136 * @returns true if class handles this request 00137 */ 00138 virtual bool USBCallback_setConfiguration(uint8_t configuration); 00139 00140 /* 00141 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context 00142 * This is used to handle extensions to standard requests 00143 * and class specific requests 00144 * 00145 * @returns true if class handles this request 00146 */ 00147 virtual bool USBCallback_request(); 00148 00149 /* 00150 * Get string product descriptor 00151 * 00152 * @returns pointer to the string product descriptor 00153 */ 00154 virtual uint8_t * stringIproductDesc(); 00155 00156 /* 00157 * Get string interface descriptor 00158 * 00159 * @returns pointer to the string interface descriptor 00160 */ 00161 virtual uint8_t * stringIinterfaceDesc(); 00162 00163 /* 00164 * Get configuration descriptor 00165 * 00166 * @returns pointer to the configuration descriptor 00167 */ 00168 virtual uint8_t * configurationDesc(); 00169 00170 /* 00171 * Called by USBDevice layer. Set interface/alternate of the device. 00172 * 00173 * @param interface Number of the interface to be configured 00174 * @param alternate Number of the alternate to be configured 00175 * @returns true if class handles this request 00176 */ 00177 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate); 00178 00179 /* 00180 * Called by USBDevice on Endpoint0 request completion 00181 * if the 'notify' flag has been set to true. Warning: Called in ISR context 00182 * 00183 * In this case it is used to indicate that a HID report has 00184 * been received from the host on endpoint 0 00185 * 00186 * @param buf buffer received on endpoint 0 00187 * @param length length of this buffer 00188 */ 00189 virtual void USBCallback_requestCompleted(uint8_t * buf, uint16_t length); 00190 00191 /* 00192 * Callback called on each Start of Frame event 00193 */ 00194 virtual void SOF(int frameNumber); 00195 00196 private: 00197 00198 // stream available ? 00199 volatile bool available; 00200 00201 // FREQ 00202 uint32_t FREQ; 00203 00204 // size of the maximum packet for the isochronous endpoint 00205 uint32_t PACKET_SIZE_ISO; 00206 00207 // mono, stereo,... 00208 uint8_t channel_nb; 00209 00210 // channel config: master, left, right 00211 uint8_t channel_config; 00212 00213 // mute state 00214 uint8_t mute; 00215 00216 // Volume Current Value 00217 uint16_t volCur; 00218 00219 // Volume Minimum Value 00220 uint16_t volMin; 00221 00222 // Volume Maximum Value 00223 uint16_t volMax; 00224 00225 // Volume Resolution 00226 uint16_t volRes; 00227 00228 // Buffer containing one audio packet 00229 uint8_t * buf_stream; 00230 00231 // callback to update volume 00232 FunctionPointer updateVol; 00233 00234 // boolean showing that the SOF handler has been called. Useful for readNB. 00235 volatile bool SOF_handler; 00236 00237 }; 00238 00239 #endif
Generated on Wed Jul 13 2022 11:42:56 by
1.7.2