![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Ben: change on lcd and sd card draft
Dependencies: mbed mbed-rtos 4DGL-uLCD-SE SDFileSystem ATParser
Speaker.h@6:57e646faac4d, 2020-04-26 (annotated)
- Committer:
- bleverett3
- Date:
- Sun Apr 26 01:42:51 2020 +0000
- Revision:
- 6:57e646faac4d
- Parent:
- 2:f65d4206b47b
Ben, did the stupid BITCH ASS MOTHAFUCKING SD CARD!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chenchen2020 | 2:f65d4206b47b | 1 | class Speaker |
chenchen2020 | 2:f65d4206b47b | 2 | { |
chenchen2020 | 2:f65d4206b47b | 3 | public: |
chenchen2020 | 2:f65d4206b47b | 4 | Speaker(PinName pin) : _pin(pin) { |
chenchen2020 | 2:f65d4206b47b | 5 | // _pin(pin) means pass pin to the Speaker Constructor |
chenchen2020 | 2:f65d4206b47b | 6 | // precompute 32 sample points on one sine wave cycle |
chenchen2020 | 2:f65d4206b47b | 7 | // used for continuous sine wave output later |
chenchen2020 | 2:f65d4206b47b | 8 | for(int k=0; k<32; k++) { |
chenchen2020 | 2:f65d4206b47b | 9 | Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0)); |
chenchen2020 | 2:f65d4206b47b | 10 | // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg |
chenchen2020 | 2:f65d4206b47b | 11 | } |
chenchen2020 | 2:f65d4206b47b | 12 | |
chenchen2020 | 2:f65d4206b47b | 13 | } |
chenchen2020 | 2:f65d4206b47b | 14 | // class method to play a note based on AnalogOut class |
chenchen2020 | 2:f65d4206b47b | 15 | void PlayNote(float frequency, float duration, float volume) { |
chenchen2020 | 2:f65d4206b47b | 16 | // scale samples using current volume level arg |
chenchen2020 | 2:f65d4206b47b | 17 | for(int k=0; k<32; k++) { |
chenchen2020 | 2:f65d4206b47b | 18 | Analog_scaled_data[k] = Analog_out_data[k] * volume; |
chenchen2020 | 2:f65d4206b47b | 19 | } |
chenchen2020 | 2:f65d4206b47b | 20 | // reset to start of sample array |
chenchen2020 | 2:f65d4206b47b | 21 | i=0; |
chenchen2020 | 2:f65d4206b47b | 22 | // turn on timer interrupts to start sine wave output |
chenchen2020 | 2:f65d4206b47b | 23 | Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0)); |
chenchen2020 | 2:f65d4206b47b | 24 | // play note for specified time |
chenchen2020 | 2:f65d4206b47b | 25 | wait(duration); |
chenchen2020 | 2:f65d4206b47b | 26 | // turns off timer interrupts |
chenchen2020 | 2:f65d4206b47b | 27 | Sample_Period.detach(); |
chenchen2020 | 2:f65d4206b47b | 28 | // sets output to mid range - analog zero |
chenchen2020 | 2:f65d4206b47b | 29 | this->_pin.write_u16(32768); |
chenchen2020 | 2:f65d4206b47b | 30 | |
chenchen2020 | 2:f65d4206b47b | 31 | } |
chenchen2020 | 2:f65d4206b47b | 32 | private: |
chenchen2020 | 2:f65d4206b47b | 33 | // sets up specified pin for analog using AnalogOut class |
chenchen2020 | 2:f65d4206b47b | 34 | AnalogOut _pin; |
chenchen2020 | 2:f65d4206b47b | 35 | // set up a timer to be used for sample rate interrupts |
chenchen2020 | 2:f65d4206b47b | 36 | Ticker Sample_Period; |
chenchen2020 | 2:f65d4206b47b | 37 | |
chenchen2020 | 2:f65d4206b47b | 38 | //variables used by interrupt routine and PlayNote |
chenchen2020 | 2:f65d4206b47b | 39 | volatile int i; |
chenchen2020 | 2:f65d4206b47b | 40 | short unsigned Analog_out_data[32]; |
chenchen2020 | 2:f65d4206b47b | 41 | short unsigned Analog_scaled_data[32]; |
chenchen2020 | 2:f65d4206b47b | 42 | |
chenchen2020 | 2:f65d4206b47b | 43 | // Interrupt routine |
chenchen2020 | 2:f65d4206b47b | 44 | // used to output next analog sample whenever a timer interrupt occurs |
chenchen2020 | 2:f65d4206b47b | 45 | void Sample_timer_interrupt(void) { |
chenchen2020 | 2:f65d4206b47b | 46 | // send next analog sample out to D to A |
chenchen2020 | 2:f65d4206b47b | 47 | this->_pin.write_u16(Analog_scaled_data[i]); |
chenchen2020 | 2:f65d4206b47b | 48 | // increment pointer and wrap around back to 0 at 32 |
chenchen2020 | 2:f65d4206b47b | 49 | i = (i+1) & 0x01F; |
chenchen2020 | 2:f65d4206b47b | 50 | } |
chenchen2020 | 2:f65d4206b47b | 51 | }; |
chenchen2020 | 2:f65d4206b47b | 52 |