RTOS

Fork of wave_player by Steve Ravet

Committer:
conantina
Date:
Wed Feb 24 16:43:33 2016 +0000
Revision:
2:fe95c7574400
ECE4180 Lab3

Who changed what in which revision?

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