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