ECE 4180 Lab 4. Wifi robot controlled via webpage with Huzzah ESP8266 chip as server.

Dependencies:   Motor mbed

Committer:
Zaydax
Date:
Tue Mar 15 22:10:56 2016 +0000
Revision:
0:908e99bd0369
Publishing to make available to import.

Who changed what in which revision?

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