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