Simple FM Sound Synthesis with touch screen and accelerometer control, based on STM32F746G-DISCO
Dependencies: ADXL345 AUDIO_DISCO_F746NG BSP_DISCO_F746NG LCD_DISCO_F746NG SDRAM_DISCO_F746NG TS_DISCO_F746NG mbed-dev
Fork of Workshop_5 by
main.cpp@2:a1330350c32e, 2016-08-23 (annotated)
- Committer:
- stefanofasciani
- Date:
- Tue Aug 23 06:34:53 2016 +0000
- Revision:
- 2:a1330350c32e
- Parent:
- 0:da04816fb411
- Child:
- 3:cd1cb0003a3d
First Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stefanofasciani | 2:a1330350c32e | 1 | //*********** |
stefanofasciani | 2:a1330350c32e | 2 | // WORKSHOP 4 |
stefanofasciani | 2:a1330350c32e | 3 | //*********** |
stefanofasciani | 2:a1330350c32e | 4 | |
stefanofasciani | 2:a1330350c32e | 5 | // Author: Stefano Fasciani - stefanofasciani@stefanofasciani.com |
stefanofasciani | 2:a1330350c32e | 6 | // Date 7/8/2016 |
stefanofasciani | 2:a1330350c32e | 7 | // FM Sinth for DISCO_F746NG and ADXL345 |
adustm | 0:da04816fb411 | 8 | |
stefanofasciani | 2:a1330350c32e | 9 | // Frequency Modulation Sound SYnthesis With Fixed Parameters Displayed on LCD |
stefanofasciani | 2:a1330350c32e | 10 | |
stefanofasciani | 2:a1330350c32e | 11 | |
stefanofasciani | 2:a1330350c32e | 12 | #include "sinth.h" |
stefanofasciani | 2:a1330350c32e | 13 | |
stefanofasciani | 2:a1330350c32e | 14 | |
stefanofasciani | 2:a1330350c32e | 15 | ADXL345 accelerometer(D11,D12,D13,D10); |
stefanofasciani | 2:a1330350c32e | 16 | TS_DISCO_F746NG ts; |
stefanofasciani | 2:a1330350c32e | 17 | TS_StateTypeDef TS; |
adustm | 0:da04816fb411 | 18 | AUDIO_DISCO_F746NG audio; |
adustm | 0:da04816fb411 | 19 | SDRAM_DISCO_F746NG sdram; |
stefanofasciani | 2:a1330350c32e | 20 | LCD_DISCO_F746NG lcd; |
adustm | 0:da04816fb411 | 21 | Serial pc(USBTX, USBRX); |
adustm | 0:da04816fb411 | 22 | |
stefanofasciani | 2:a1330350c32e | 23 | |
stefanofasciani | 2:a1330350c32e | 24 | //FM Synthesis Parameters |
stefanofasciani | 2:a1330350c32e | 25 | float amplitude = 0.5; //numbers between 0.0 and 1.0 |
stefanofasciani | 2:a1330350c32e | 26 | float pitch = 400; // numbers between 50 and 5000 |
stefanofasciani | 2:a1330350c32e | 27 | float modrate = 0; // numbers between -1000 and 1000 |
stefanofasciani | 2:a1330350c32e | 28 | float moddepth = 0; //numbers between -1000 and 1000 |
stefanofasciani | 2:a1330350c32e | 29 | float filter = 1.0f; //numbers between 0.0 (full filter) and 1.0 (do not filter) |
stefanofasciani | 2:a1330350c32e | 30 | |
stefanofasciani | 2:a1330350c32e | 31 | |
adustm | 0:da04816fb411 | 32 | |
stefanofasciani | 2:a1330350c32e | 33 | void get_parameters(){ |
stefanofasciani | 2:a1330350c32e | 34 | |
stefanofasciani | 2:a1330350c32e | 35 | char temp_string[20]; |
stefanofasciani | 2:a1330350c32e | 36 | |
stefanofasciani | 2:a1330350c32e | 37 | //read input data |
stefanofasciani | 2:a1330350c32e | 38 | ts.GetState(&TS); |
stefanofasciani | 2:a1330350c32e | 39 | accelerometer.getOutput(acc_readings); |
stefanofasciani | 2:a1330350c32e | 40 | |
stefanofasciani | 2:a1330350c32e | 41 | |
stefanofasciani | 2:a1330350c32e | 42 | if(TS.touchDetected){ |
stefanofasciani | 2:a1330350c32e | 43 | //map to synthesis parameters |
stefanofasciani | 2:a1330350c32e | 44 | amplitude = 0.5; //map(get_touch_vertical(),0.0,1.0); |
stefanofasciani | 2:a1330350c32e | 45 | } |
stefanofasciani | 2:a1330350c32e | 46 | else { |
stefanofasciani | 2:a1330350c32e | 47 | amplitude = 0; |
stefanofasciani | 2:a1330350c32e | 48 | } |
stefanofasciani | 2:a1330350c32e | 49 | |
stefanofasciani | 2:a1330350c32e | 50 | |
stefanofasciani | 2:a1330350c32e | 51 | pitch = get_note_pitch(); //map(get_touch_horizontal(),50.0,5000.0); |
stefanofasciani | 2:a1330350c32e | 52 | |
stefanofasciani | 2:a1330350c32e | 53 | modrate = map(get_acc_horizontal(), //input |
stefanofasciani | 2:a1330350c32e | 54 | 0, //modrate range minimum (not less than -1000) |
stefanofasciani | 2:a1330350c32e | 55 | 200.0); //modrate range maximum (not higher than 1000) |
stefanofasciani | 2:a1330350c32e | 56 | |
stefanofasciani | 2:a1330350c32e | 57 | moddepth = map(get_acc_vertical(), //input |
stefanofasciani | 2:a1330350c32e | 58 | -100.0, //moddepth range minimum (not less than -1000) |
stefanofasciani | 2:a1330350c32e | 59 | 100.0); //moddepth range maximum (not higher than 1000) |
stefanofasciani | 2:a1330350c32e | 60 | |
stefanofasciani | 2:a1330350c32e | 61 | |
stefanofasciani | 2:a1330350c32e | 62 | |
stefanofasciani | 2:a1330350c32e | 63 | filter = map(get_touch_vertical(), //input |
stefanofasciani | 2:a1330350c32e | 64 | 0.0, //filter range minimum (not less than 0.0) |
stefanofasciani | 2:a1330350c32e | 65 | 0.7); //filter range maximum (not higher than 1.0) |
stefanofasciani | 2:a1330350c32e | 66 | |
stefanofasciani | 2:a1330350c32e | 67 | |
stefanofasciani | 2:a1330350c32e | 68 | //update display |
stefanofasciani | 2:a1330350c32e | 69 | switch(update_round){ |
stefanofasciani | 2:a1330350c32e | 70 | case 0: |
stefanofasciani | 2:a1330350c32e | 71 | sprintf(temp_string,"%f",amplitude); lcd.DisplayStringAt(250, LINE(1),(uint8_t *)temp_string, LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 72 | case 1: |
stefanofasciani | 2:a1330350c32e | 73 | sprintf(temp_string,"%f",pitch); lcd.DisplayStringAt(250, LINE(3),(uint8_t *)temp_string, LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 74 | case 2: |
stefanofasciani | 2:a1330350c32e | 75 | sprintf(temp_string,"%f",modrate); lcd.DisplayStringAt(250, LINE(5),(uint8_t *)temp_string, LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 76 | case 3: |
stefanofasciani | 2:a1330350c32e | 77 | sprintf(temp_string,"%f",moddepth); lcd.DisplayStringAt(250, LINE(7),(uint8_t *)temp_string, LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 78 | case 4: |
stefanofasciani | 2:a1330350c32e | 79 | sprintf(temp_string,"%f",filter); lcd.DisplayStringAt(250, LINE(9),(uint8_t *)temp_string, LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 80 | } |
stefanofasciani | 2:a1330350c32e | 81 | update_round++; |
stefanofasciani | 2:a1330350c32e | 82 | if(update_round>4)update_round=0; |
stefanofasciani | 2:a1330350c32e | 83 | |
stefanofasciani | 2:a1330350c32e | 84 | return; |
stefanofasciani | 2:a1330350c32e | 85 | } |
stefanofasciani | 2:a1330350c32e | 86 | |
stefanofasciani | 2:a1330350c32e | 87 | |
stefanofasciani | 2:a1330350c32e | 88 | |
adustm | 0:da04816fb411 | 89 | int main() |
adustm | 0:da04816fb411 | 90 | { |
stefanofasciani | 2:a1330350c32e | 91 | |
stefanofasciani | 2:a1330350c32e | 92 | //initialize touch screen |
stefanofasciani | 2:a1330350c32e | 93 | ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
stefanofasciani | 2:a1330350c32e | 94 | wait(0.2); |
stefanofasciani | 2:a1330350c32e | 95 | |
stefanofasciani | 2:a1330350c32e | 96 | // lcd initialization to display synthesis parameter |
stefanofasciani | 2:a1330350c32e | 97 | lcd.Clear(LCD_COLOR_BLACK); |
stefanofasciani | 2:a1330350c32e | 98 | display_keyboard(); |
stefanofasciani | 2:a1330350c32e | 99 | lcd.SetBackColor(LCD_COLOR_BLACK); |
stefanofasciani | 2:a1330350c32e | 100 | lcd.SetTextColor(LCD_COLOR_LIGHTBLUE); |
stefanofasciani | 2:a1330350c32e | 101 | lcd.DisplayStringAt(10, LINE(1),(uint8_t *)"AMPLITUDE :", LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 102 | lcd.DisplayStringAt(10, LINE(3),(uint8_t *)"PITCH (Hz) :", LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 103 | lcd.DisplayStringAt(10, LINE(5),(uint8_t *)"MOD.RATE (Hz):", LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 104 | lcd.DisplayStringAt(10, LINE(7),(uint8_t *)"MOD.DEPTH :", LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 105 | lcd.DisplayStringAt(10, LINE(9),(uint8_t *)"FILTER :", LEFT_MODE); |
stefanofasciani | 2:a1330350c32e | 106 | |
stefanofasciani | 2:a1330350c32e | 107 | wait(0.2); |
stefanofasciani | 2:a1330350c32e | 108 | |
stefanofasciani | 2:a1330350c32e | 109 | |
stefanofasciani | 2:a1330350c32e | 110 | //initialize accelerometer |
stefanofasciani | 2:a1330350c32e | 111 | accelerometer.getDevId(); |
stefanofasciani | 2:a1330350c32e | 112 | accelerometer.setPowerControl(0x00); |
stefanofasciani | 2:a1330350c32e | 113 | accelerometer.setDataFormatControl(0x0B); |
stefanofasciani | 2:a1330350c32e | 114 | accelerometer.setDataRate(ADXL345_3200HZ); |
stefanofasciani | 2:a1330350c32e | 115 | accelerometer.setPowerControl(0x08); |
stefanofasciani | 2:a1330350c32e | 116 | wait(0.2); |
stefanofasciani | 2:a1330350c32e | 117 | |
stefanofasciani | 2:a1330350c32e | 118 | //sound synthesis initialization |
stefanofasciani | 2:a1330350c32e | 119 | init_sin_table(); |
adustm | 0:da04816fb411 | 120 | SetSysClock_PLL_HSE_200MHz(); |
adustm | 0:da04816fb411 | 121 | pc.baud(9600); |
stefanofasciani | 2:a1330350c32e | 122 | memset((void *)out_buffer, 0, AUDIO_BLOCK_SIZE*2*sizeof(uint16_t)); |
stefanofasciani | 2:a1330350c32e | 123 | audio_out_buffer_state = BUFFER_OFFSET_NONE; |
adustm | 0:da04816fb411 | 124 | audio.OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); |
stefanofasciani | 2:a1330350c32e | 125 | audio.OUT_Play((uint16_t*)out_buffer, AUDIO_BLOCK_SIZE * 2 * sizeof(int16_t)); |
adustm | 0:da04816fb411 | 126 | |
adustm | 0:da04816fb411 | 127 | |
adustm | 0:da04816fb411 | 128 | while (1) { |
stefanofasciani | 2:a1330350c32e | 129 | |
stefanofasciani | 2:a1330350c32e | 130 | while(audio_out_buffer_state != BUFFER_OFFSET_HALF); |
stefanofasciani | 2:a1330350c32e | 131 | audio_out_buffer_state = BUFFER_OFFSET_NONE; |
stefanofasciani | 2:a1330350c32e | 132 | get_parameters(); |
stefanofasciani | 2:a1330350c32e | 133 | sint_osc(); |
stefanofasciani | 2:a1330350c32e | 134 | copy_buffer(0); |
stefanofasciani | 2:a1330350c32e | 135 | |
stefanofasciani | 2:a1330350c32e | 136 | |
stefanofasciani | 2:a1330350c32e | 137 | while(audio_out_buffer_state != BUFFER_OFFSET_FULL); |
stefanofasciani | 2:a1330350c32e | 138 | audio_out_buffer_state = BUFFER_OFFSET_NONE; |
stefanofasciani | 2:a1330350c32e | 139 | get_parameters(); |
stefanofasciani | 2:a1330350c32e | 140 | sint_osc(); |
stefanofasciani | 2:a1330350c32e | 141 | copy_buffer(AUDIO_BLOCK_SIZE); |
stefanofasciani | 2:a1330350c32e | 142 | |
adustm | 0:da04816fb411 | 143 | } |
adustm | 0:da04816fb411 | 144 | } |
adustm | 0:da04816fb411 | 145 | |
stefanofasciani | 2:a1330350c32e | 146 | /* |
stefanofasciani | 2:a1330350c32e | 147 | CONNECTION DISCO_F746NG to ADXL345 |
stefanofasciani | 2:a1330350c32e | 148 | D10 - SS - CS |
stefanofasciani | 2:a1330350c32e | 149 | D11 - MOSI - SDA |
stefanofasciani | 2:a1330350c32e | 150 | D12 - MISO - SDO |
stefanofasciani | 2:a1330350c32e | 151 | D13 - SCK - SCL |
stefanofasciani | 2:a1330350c32e | 152 | 3V3 - VCC - VCC |
stefanofasciani | 2:a1330350c32e | 153 | GND - GND - GND |
stefanofasciani | 2:a1330350c32e | 154 | */ |
adustm | 0:da04816fb411 | 155 | |
stefanofasciani | 2:a1330350c32e | 156 | |
stefanofasciani | 2:a1330350c32e | 157 | |
adustm | 0:da04816fb411 | 158 | |
stefanofasciani | 2:a1330350c32e | 159 | |
stefanofasciani | 2:a1330350c32e | 160 |