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