]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBDevice/USBAudio/USBAudio.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBDevice / USBAudio / USBAudio.h
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #ifndef USBAudio_H
20 #define USBAudio_H
21
22 /* These headers are included for child class. */
23 #include "USBEndpoints.h"
24 #include "USBDescriptor.h"
25 #include "USBDevice_Types.h"
26
27 #include "USBDevice.h"
28
29
30 /**
31 * USBAudio example
32 *
33 * @code
34 * #include "mbed.h"
35 * #include "USBAudio.h"
36 *
37 * Serial pc(USBTX, USBRX);
38 *
39 * // frequency: 48 kHz
40 * #define FREQ 48000
41 *
42 * // 1 channel: mono
43 * #define NB_CHA 1
44 *
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
46 * #define AUDIO_LENGTH_PACKET 48 * 2 * 1
47 *
48 * // USBAudio
49 * USBAudio audio(FREQ, NB_CHA);
50 *
51 * int main() {
52 * int16_t buf[AUDIO_LENGTH_PACKET/2];
53 *
54 * while (1) {
55 * // read an audio packet
56 * audio.read((uint8_t *)buf);
57 *
58 *
59 * // print packet received
60 * pc.printf("recv: ");
61 * for(int i = 0; i < AUDIO_LENGTH_PACKET/2; i++) {
62 * pc.printf("%d ", buf[i]);
63 * }
64 * pc.printf("\r\n");
65 * }
66 * }
67 * @endcode
68 */
69 class USBAudio: public USBDevice {
70 public:
71
72 /**
73 * Constructor
74 *
75 * @param frequency_in frequency in Hz (default: 48000)
76 * @param channel_nb_in channel number (1 or 2) (default: 1)
77 * @param frequency_out frequency in Hz (default: 8000)
78 * @param channel_nb_out_in channel number (1 or 2) (default: 1)
79 * @param vendor_id Your vendor_id
80 * @param product_id Your product_id
81 * @param product_release Your preoduct_release
82 */
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);
84
85 /**
86 * Get current volume between 0.0 and 1.0
87 *
88 * @returns volume
89 */
90 float getVolume();
91
92 /**
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
94 *
95 * @param buf pointer on a buffer which will be filled with an audio packet
96 *
97 * @returns true if successfull
98 */
99 bool read(uint8_t * buf);
100
101 /**
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
103 *
104 * @param buf pointer on a buffer which will be filled if an audio packet is available
105 *
106 * @returns true if successfull
107 */
108 bool readNB(uint8_t * buf);
109
110 /**
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.
112 *
113 * @param buf pointer on the audio packet which will be sent
114 * @returns true if successful
115 */
116 bool write(uint8_t * buf);
117
118 /**
119 * Write and read an audio packet at the same time (on the same frame)
120 *
121 * @param buf_read pointer on a buffer which will be filled with an audio packet
122 * @param buf_write pointer on the audio packet which will be sent
123 * @returns true if successful
124 */
125 bool readWrite(uint8_t * buf_read, uint8_t * buf_write);
126
127
128 /** attach a handler to update the volume
129 *
130 * @param function Function to attach
131 *
132 */
133 void attach(void(*fptr)(void)) {
134 updateVol.attach(fptr);
135 }
136
137 /** Attach a nonstatic void/void member function to update the volume
138 *
139 * @param tptr Object pointer
140 * @param mptr Member function pointer
141 *
142 */
143 template<typename T>
144 void attach(T *tptr, void(T::*mptr)(void)) {
145 updateVol.attach(tptr, mptr);
146 }
147
148
149 protected:
150
151 /*
152 * Called by USBDevice layer. Set configuration of the device.
153 * For instance, you can add all endpoints that you need on this function.
154 *
155 * @param configuration Number of the configuration
156 * @returns true if class handles this request
157 */
158 virtual bool USBCallback_setConfiguration(uint8_t configuration);
159
160 /*
161 * Called by USBDevice on Endpoint0 request. Warning: Called in ISR context
162 * This is used to handle extensions to standard requests
163 * and class specific requests
164 *
165 * @returns true if class handles this request
166 */
167 virtual bool USBCallback_request();
168
169 /*
170 * Get string product descriptor
171 *
172 * @returns pointer to the string product descriptor
173 */
174 virtual uint8_t * stringIproductDesc();
175
176 /*
177 * Get string interface descriptor
178 *
179 * @returns pointer to the string interface descriptor
180 */
181 virtual uint8_t * stringIinterfaceDesc();
182
183 /*
184 * Get configuration descriptor
185 *
186 * @returns pointer to the configuration descriptor
187 */
188 virtual uint8_t * configurationDesc();
189
190 /*
191 * Called by USBDevice layer. Set interface/alternate of the device.
192 *
193 * @param interface Number of the interface to be configured
194 * @param alternate Number of the alternate to be configured
195 * @returns true if class handles this request
196 */
197 virtual bool USBCallback_setInterface(uint16_t interface, uint8_t alternate);
198
199 /*
200 * Called by USBDevice on Endpoint0 request completion
201 * if the 'notify' flag has been set to true. Warning: Called in ISR context
202 *
203 * In this case it is used to indicate that a HID report has
204 * been received from the host on endpoint 0
205 *
206 * @param buf buffer received on endpoint 0
207 * @param length length of this buffer
208 */
209 virtual void USBCallback_requestCompleted(uint8_t * buf, uint32_t length);
210
211 /*
212 * Callback called on each Start of Frame event
213 */
214 virtual void SOF(int frameNumber);
215
216 /*
217 * Callback called when a packet is received
218 */
219 virtual bool EPISO_OUT_callback();
220
221 /*
222 * Callback called when a packet has been sent
223 */
224 virtual bool EPISO_IN_callback();
225
226 private:
227
228 // stream available ?
229 volatile bool available;
230
231 // interrupt OUT has been received
232 volatile bool interruptOUT;
233
234 // interrupt IN has been received
235 volatile bool interruptIN;
236
237 // audio packet has been written
238 volatile bool writeIN;
239
240 // FREQ
241 uint32_t FREQ_OUT;
242 uint32_t FREQ_IN;
243
244 // size of the maximum packet for the isochronous endpoint
245 uint32_t PACKET_SIZE_ISO_IN;
246 uint32_t PACKET_SIZE_ISO_OUT;
247
248 // mono, stereo,...
249 uint8_t channel_nb_in;
250 uint8_t channel_nb_out;
251
252 // channel config: master, left, right
253 uint8_t channel_config_in;
254 uint8_t channel_config_out;
255
256 // mute state
257 uint8_t mute;
258
259 // Volume Current Value
260 uint16_t volCur;
261
262 // Volume Minimum Value
263 uint16_t volMin;
264
265 // Volume Maximum Value
266 uint16_t volMax;
267
268 // Volume Resolution
269 uint16_t volRes;
270
271 // Buffer containing one audio packet (to be read)
272 volatile uint8_t * buf_stream_in;
273
274 // Buffer containing one audio packet (to be written)
275 volatile uint8_t * buf_stream_out;
276
277 // callback to update volume
278 FunctionPointer updateVol;
279
280 // boolean showing that the SOF handler has been called. Useful for readNB.
281 volatile bool SOF_handler;
282
283 volatile float volume;
284
285 };
286
287 #endif
Imprint / Impressum