USBAudio example using a microphone

Dependencies:   USBDevice mbed

Committer:
samux
Date:
Fri Dec 16 12:31:41 2011 +0000
Revision:
0:539ec61e1fbb
works with m0 and m3 (sinus) but the code is different to have the same result...

Who changed what in which revision?

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