The project is not done yet
Dependencies: USBHost USBHostXpad mbed-rtos mbed
Fork of Totaleprogramma by
Speaker.h@1:da390b3b1330, 2015-03-02 (annotated)
- Committer:
- juliandekker
- Date:
- Mon Mar 02 10:58:30 2015 +0000
- Revision:
- 1:da390b3b1330
- Parent:
- 0:345f76c72b9a
xbox controller robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
347467 | 0:345f76c72b9a | 1 | class Speaker |
347467 | 0:345f76c72b9a | 2 | { |
347467 | 0:345f76c72b9a | 3 | public: |
347467 | 0:345f76c72b9a | 4 | Speaker(PinName pin) : _pin(pin) { |
347467 | 0:345f76c72b9a | 5 | // _pin(pin) means pass pin to the Speaker Constructor |
347467 | 0:345f76c72b9a | 6 | // precompute 64 sample points on one sine wave cycle |
347467 | 0:345f76c72b9a | 7 | // used for continuous sine wave output later |
347467 | 0:345f76c72b9a | 8 | for(int k=0; k<64; k++) { |
347467 | 0:345f76c72b9a | 9 | Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/64.0*6.28318530717959)))/2.0)); |
347467 | 0:345f76c72b9a | 10 | // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg |
347467 | 0:345f76c72b9a | 11 | } |
347467 | 0:345f76c72b9a | 12 | |
347467 | 0:345f76c72b9a | 13 | } |
347467 | 0:345f76c72b9a | 14 | // class method to play a note based on AnalogOut class |
347467 | 0:345f76c72b9a | 15 | void PlayNote(float frequency, float duration, float volume) { |
347467 | 0:345f76c72b9a | 16 | // scale samples using current volume level arg |
347467 | 0:345f76c72b9a | 17 | for(int k=0; k<64; k++) { |
347467 | 0:345f76c72b9a | 18 | Analog_scaled_data[k] = Analog_out_data[k] * volume; |
347467 | 0:345f76c72b9a | 19 | } |
347467 | 0:345f76c72b9a | 20 | // reset to start of sample array |
347467 | 0:345f76c72b9a | 21 | i=0; |
347467 | 0:345f76c72b9a | 22 | // turn on timer interrupts to start sine wave output |
347467 | 0:345f76c72b9a | 23 | Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*64.0)); |
347467 | 0:345f76c72b9a | 24 | // play note for specified time |
347467 | 0:345f76c72b9a | 25 | wait(duration); |
347467 | 0:345f76c72b9a | 26 | // turns off timer interrupts |
347467 | 0:345f76c72b9a | 27 | Sample_Period.detach(); |
347467 | 0:345f76c72b9a | 28 | // sets output to mid range - analog zero |
347467 | 0:345f76c72b9a | 29 | this->_pin.write_u16(32768); |
347467 | 0:345f76c72b9a | 30 | |
347467 | 0:345f76c72b9a | 31 | } |
347467 | 0:345f76c72b9a | 32 | private: |
347467 | 0:345f76c72b9a | 33 | // sets up specified pin for analog using AnalogOut class |
347467 | 0:345f76c72b9a | 34 | AnalogOut _pin; |
347467 | 0:345f76c72b9a | 35 | // set up a timer to be used for sample rate interrupts |
347467 | 0:345f76c72b9a | 36 | Ticker Sample_Period; |
347467 | 0:345f76c72b9a | 37 | |
347467 | 0:345f76c72b9a | 38 | //variables used by interrupt routine and PlayNote |
347467 | 0:345f76c72b9a | 39 | volatile int i; |
347467 | 0:345f76c72b9a | 40 | short unsigned Analog_out_data[64]; |
347467 | 0:345f76c72b9a | 41 | short unsigned Analog_scaled_data[64]; |
347467 | 0:345f76c72b9a | 42 | |
347467 | 0:345f76c72b9a | 43 | // Interrupt routine |
347467 | 0:345f76c72b9a | 44 | // used to output next analog sample whenever a timer interrupt occurs |
347467 | 0:345f76c72b9a | 45 | void Sample_timer_interrupt(void) { |
347467 | 0:345f76c72b9a | 46 | // send next analog sample out to D to A |
347467 | 0:345f76c72b9a | 47 | this->_pin.write_u16(Analog_scaled_data[i]); |
347467 | 0:345f76c72b9a | 48 | // increment pointer and wrap around back to 0 at 64 |
347467 | 0:345f76c72b9a | 49 | i = (i+1) & 0x03F; |
347467 | 0:345f76c72b9a | 50 | } |
347467 | 0:345f76c72b9a | 51 | }; |
347467 | 0:345f76c72b9a | 52 |