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