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