Measure Distance

Dependencies:   mbed

Committer:
tadhgjones
Date:
Wed Dec 16 10:38:23 2020 +0000
Revision:
8:f3ab61374cd0
EE3023

Who changed what in which revision?

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