Signal Generator

Dependencies:   IniManager RA8875 Watchdog mbed-rtos mbed

Fork of speaker_demo_Analog by jim hamblen

Committer:
WiredHome
Date:
Sat May 20 19:52:23 2017 +0000
Revision:
6:1f48212fbaf9
Parent:
1:dd07e1deec6c
Signal Generator - a work in process as the need arises.

Who changed what in which revision?

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