ReSpeaker DSP V02

Dependencies:   mbed MbedJSONValue

Committer:
Arkadi
Date:
Mon Aug 27 11:22:46 2018 +0000
Revision:
11:07981b0d28d3
Parent:
10:273127efdc6e
Child:
12:9d30df1529be
Change SOSMat from mat to vector

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Arkadi 0:bfac05b2d50f 1 ////////////////////////////////////////
Arkadi 1:574b54755983 2 // Tau_ReSpeaker_DSP V01 //
Arkadi 9:7e20c1e5a354 3 // Arkadiraf@gmail.com - 26/08/2018 //
Arkadi 0:bfac05b2d50f 4 ////////////////////////////////////////
Arkadi 0:bfac05b2d50f 5 /*
Arkadi 5:ec6f2323a263 6 Json Format: json:{"name":"dsp","mode":"off"}\r\n
Arkadi 2:59c1a24aed7c 7 */
Arkadi 1:574b54755983 8
Arkadi 3:48258b86e182 9
Arkadi 0:bfac05b2d50f 10
Arkadi 0:bfac05b2d50f 11 /*
Arkadi 0:bfac05b2d50f 12 Board : Nucleo STM32F446RE
Arkadi 0:bfac05b2d50f 13 Power Source : USB
Arkadi 0:bfac05b2d50f 14 */
Arkadi 0:bfac05b2d50f 15
Arkadi 0:bfac05b2d50f 16 /*
Arkadi 0:bfac05b2d50f 17 Pinout:
Arkadi 0:bfac05b2d50f 18 PC - Serial 2
Arkadi 0:bfac05b2d50f 19 PA_2 (Tx) --> STLINK
Arkadi 0:bfac05b2d50f 20 PA_3 (Rx) --> STLINK
Arkadi 0:bfac05b2d50f 21
Arkadi 0:bfac05b2d50f 22 Switch - Serial 3
Arkadi 0:bfac05b2d50f 23 PC_10 (Tx) --> SW_Rx
Arkadi 0:bfac05b2d50f 24 PC_11 (Rx) --> SW_Tx
Arkadi 0:bfac05b2d50f 25
Arkadi 0:bfac05b2d50f 26 I2C_Bus
Arkadi 0:bfac05b2d50f 27 PB_8 --> SCL
Arkadi 0:bfac05b2d50f 28 PB_9 --> SDA
Arkadi 0:bfac05b2d50f 29
Arkadi 0:bfac05b2d50f 30 Digital output
Arkadi 0:bfac05b2d50f 31 PA_5 --> led (DigitalOut)
Arkadi 9:7e20c1e5a354 32 PA_10 --> DSP_Trigger
Arkadi 11:07981b0d28d3 33
Arkadi 0:bfac05b2d50f 34 Digital Input
Arkadi 0:bfac05b2d50f 35 PC_13 --> BTN (Blue)
Arkadi 0:bfac05b2d50f 36
Arkadi 1:574b54755983 37 PA_6 --> Toggle Pin (Loop Freq D12)
Arkadi 1:574b54755983 38
Arkadi 0:bfac05b2d50f 39 Analog Input
Arkadi 1:574b54755983 40 PA_0 --> SIG_IN_DSP (A0)
Arkadi 0:bfac05b2d50f 41
Arkadi 0:bfac05b2d50f 42 Analog Output
Arkadi 1:574b54755983 43 PA_4 --> SIG_OUT_DSP (A2)
Arkadi 0:bfac05b2d50f 44
Arkadi 0:bfac05b2d50f 45 */
Arkadi 0:bfac05b2d50f 46
Arkadi 0:bfac05b2d50f 47 ///////////////
Arkadi 0:bfac05b2d50f 48 // Libraries //
Arkadi 0:bfac05b2d50f 49 ///////////////
Arkadi 0:bfac05b2d50f 50 #include "mbed.h"
Arkadi 1:574b54755983 51 #include "MbedJSONValue.h"
Arkadi 0:bfac05b2d50f 52
Arkadi 0:bfac05b2d50f 53 ///////////////
Arkadi 0:bfac05b2d50f 54 // #defines //
Arkadi 0:bfac05b2d50f 55 ///////////////
Arkadi 1:574b54755983 56 // UART debug modes:
Arkadi 1:574b54755983 57 //#define DEBUG_MOD1 // json packet recognise
Arkadi 1:574b54755983 58 #define DEBUG_MOD2 // json parse
Arkadi 1:574b54755983 59 #define DEBUG_MOD3 // dsp handler
Arkadi 5:ec6f2323a263 60 #define DEBUG_MOD10 // responsivity msges to gui
Arkadi 6:e8b4ca41c691 61 #define DEBUG_MOD11 // loop frequency indicator
Arkadi 0:bfac05b2d50f 62 // Sine generator
Arkadi 1:574b54755983 63 #define PI_DOUBLE (3.141592653589793238462)
Arkadi 1:574b54755983 64 #define PI_FLOAT (3.14159f)
Arkadi 1:574b54755983 65
Arkadi 1:574b54755983 66 // json commad
Arkadi 10:273127efdc6e 67 #define MSG_BUFFER_SIZE 1024
Arkadi 1:574b54755983 68 #define HEADER_SIZE 5
Arkadi 1:574b54755983 69 #define FOOTER_SIZE 2
Arkadi 0:bfac05b2d50f 70
Arkadi 9:7e20c1e5a354 71 // Frequency counter
Arkadi 9:7e20c1e5a354 72 #define TICKS2TOGGLE 1000000
Arkadi 0:bfac05b2d50f 73 /////////////
Arkadi 0:bfac05b2d50f 74 // Objects //
Arkadi 0:bfac05b2d50f 75 /////////////
Arkadi 1:574b54755983 76 // json
Arkadi 1:574b54755983 77 MbedJSONValue guiCmd;
Arkadi 0:bfac05b2d50f 78
Arkadi 0:bfac05b2d50f 79 // uart
Arkadi 1:574b54755983 80 Serial pc(USBTX, USBRX);
Arkadi 0:bfac05b2d50f 81
Arkadi 2:59c1a24aed7c 82 // uart switch_dsp
Arkadi 2:59c1a24aed7c 83 Serial switch_dsp(PC_10, PC_11);
Arkadi 2:59c1a24aed7c 84
Arkadi 0:bfac05b2d50f 85 // digital
Arkadi 0:bfac05b2d50f 86 DigitalIn user_button(PC_13);
Arkadi 9:7e20c1e5a354 87 DigitalOut dsp_trigger(PA_10);// D2
Arkadi 8:8a3e6241c104 88 DigitalOut led(PA_5); // D13
Arkadi 8:8a3e6241c104 89 DigitalOut mytoggle(PA_6); // D12
Arkadi 0:bfac05b2d50f 90
Arkadi 1:574b54755983 91 // analog / define to init at mbed initialization. work directly with registers
Arkadi 0:bfac05b2d50f 92 AnalogOut dsp_output(PA_4);
Arkadi 0:bfac05b2d50f 93 AnalogIn dsp_input(PA_0);
Arkadi 0:bfac05b2d50f 94
Arkadi 1:574b54755983 95
Arkadi 1:574b54755983 96 // ADC/DAC declarations
Arkadi 1:574b54755983 97 ADC_HandleTypeDef hadc1;
Arkadi 1:574b54755983 98 DAC_HandleTypeDef hdac1;
Arkadi 1:574b54755983 99
Arkadi 0:bfac05b2d50f 100 ///////////////
Arkadi 0:bfac05b2d50f 101 // variables //
Arkadi 0:bfac05b2d50f 102 ///////////////
Arkadi 0:bfac05b2d50f 103
Arkadi 9:7e20c1e5a354 104 // Frequency counter
Arkadi 9:7e20c1e5a354 105 uint32_t toggleCounter = 0;
Arkadi 9:7e20c1e5a354 106 // toogle pin state
Arkadi 9:7e20c1e5a354 107 bool toggleCounterState = 0;
Arkadi 9:7e20c1e5a354 108
Arkadi 1:574b54755983 109 // json buffer
Arkadi 1:574b54755983 110 char json[MSG_BUFFER_SIZE];
Arkadi 1:574b54755983 111
Arkadi 1:574b54755983 112 // packet variables
Arkadi 1:574b54755983 113 struct packetMSG_struct {
Arkadi 1:574b54755983 114 // recieve message variables
Arkadi 1:574b54755983 115 uint8_t header[HEADER_SIZE];
Arkadi 1:574b54755983 116 uint8_t footer[FOOTER_SIZE];
Arkadi 1:574b54755983 117 uint8_t syncIndex; // sync index for header / footer
Arkadi 1:574b54755983 118 uint8_t syncFlag; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer
Arkadi 1:574b54755983 119 // buffer
Arkadi 1:574b54755983 120 uint16_t bufferIndex; // buffer index
Arkadi 1:574b54755983 121 uint8_t buffer[MSG_BUFFER_SIZE];
Arkadi 1:574b54755983 122 } ;
Arkadi 1:574b54755983 123 packetMSG_struct packetMSG;
Arkadi 1:574b54755983 124
Arkadi 1:574b54755983 125
Arkadi 1:574b54755983 126 // Dac Register for direct method of setting DAC value`s
Arkadi 1:574b54755983 127 __IO uint32_t Dac_Reg = 0;
Arkadi 1:574b54755983 128
Arkadi 0:bfac05b2d50f 129 // analog input
Arkadi 8:8a3e6241c104 130 //uint16_t adc_in=0;
Arkadi 0:bfac05b2d50f 131
Arkadi 1:574b54755983 132 // toogle pin state
Arkadi 1:574b54755983 133 bool toggelState=0;
Arkadi 1:574b54755983 134
Arkadi 1:574b54755983 135 // create function pointer
Arkadi 1:574b54755983 136 typedef void(*functionPtr)(void);
Arkadi 1:574b54755983 137 functionPtr FilterFunction;
Arkadi 1:574b54755983 138
Arkadi 3:48258b86e182 139 // alternative functuin selection (faster mcu process)
Arkadi 5:ec6f2323a263 140 volatile uint8_t operationMode = 1;
Arkadi 4:59319802012b 141 // 0 - off
Arkadi 4:59319802012b 142 // 1 - passthrough - no filter
Arkadi 4:59319802012b 143 // 2 - highpass - High Pass filter
Arkadi 4:59319802012b 144 // 3 - hpf_trig - High Pass filter + Trigger mode
Arkadi 7:25c81cb23e42 145 // 4 - gains_trig - highpass filter + trigger mode + Gains vector
Arkadi 3:48258b86e182 146
Arkadi 5:ec6f2323a263 147
Arkadi 5:ec6f2323a263 148 // Trigger mode variables
Arkadi 8:8a3e6241c104 149 float signalGain = 1.0; // Signal Gain
Arkadi 8:8a3e6241c104 150 float trigTresh = 0.5; // threshold for trigger mode
Arkadi 8:8a3e6241c104 151 uint32_t trigDelaySet = 5000; // counter for pulse pass
Arkadi 5:ec6f2323a263 152 uint32_t trigDelay = trigDelaySet; // counter for pulse pass
Arkadi 8:8a3e6241c104 153 uint32_t trigPause = 10; // pause after trigger in microseconds
Arkadi 5:ec6f2323a263 154
Arkadi 10:273127efdc6e 155 // Filter variables
Arkadi 10:273127efdc6e 156 //SOS Matrix limited to order 10
Arkadi 10:273127efdc6e 157 int FilterSections=1;
Arkadi 11:07981b0d28d3 158 float SOSMat[5][6]= { // Predefined SOS Matrix (second order 5Khz filter
Arkadi 11:07981b0d28d3 159 1, -2, 1, 1, -1.961, 0.9624,
Arkadi 11:07981b0d28d3 160 0, 0, 0, 0, 0, 0,
Arkadi 11:07981b0d28d3 161 0, 0, 0, 0, 0, 0,
Arkadi 11:07981b0d28d3 162 0, 0, 0, 0, 0, 0,
Arkadi 11:07981b0d28d3 163 0, 0, 0, 0, 0, 0
Arkadi 11:07981b0d28d3 164 };
Arkadi 10:273127efdc6e 165 float Gscale = 0.9810f;
Arkadi 10:273127efdc6e 166 float filterCurrInput[6] = {0};
Arkadi 10:273127efdc6e 167 float filterLastInput[6] = {0};
Arkadi 10:273127efdc6e 168 float filterLLastInput[6] = {0};
Arkadi 10:273127efdc6e 169
Arkadi 8:8a3e6241c104 170 // Buffer mode variables
Arkadi 8:8a3e6241c104 171 uint32_t bufferSizeSet = 5000;
Arkadi 8:8a3e6241c104 172 uint32_t bufferSize = bufferSizeSet;
Arkadi 8:8a3e6241c104 173 uint32_t preBufferSizeSet = 1000;
Arkadi 8:8a3e6241c104 174 uint32_t bufferCountDown = bufferSizeSet - preBufferSizeSet;
Arkadi 8:8a3e6241c104 175 float bufferADC[5000] = {0};
Arkadi 9:7e20c1e5a354 176
Arkadi 9:7e20c1e5a354 177
Arkadi 0:bfac05b2d50f 178 ///////////////
Arkadi 0:bfac05b2d50f 179 // Functions //
Arkadi 0:bfac05b2d50f 180 ///////////////
Arkadi 0:bfac05b2d50f 181
Arkadi 1:574b54755983 182 // init functions header //hadc1 needs to be defined preiod #incude
Arkadi 1:574b54755983 183 #include "adc_init.h" // initialize adc/dac directly for continious sample
Arkadi 1:574b54755983 184 #include "filters.h"
Arkadi 1:574b54755983 185
Arkadi 1:574b54755983 186 // nop operation
Arkadi 1:574b54755983 187 inline void NOP()
Arkadi 1:574b54755983 188 {
Arkadi 1:574b54755983 189 __ASM volatile ("nop"); // one tick operation, Use to adjust frequency by slowing down the proccess
Arkadi 1:574b54755983 190 }
Arkadi 1:574b54755983 191
Arkadi 1:574b54755983 192 // Serial Event function
Arkadi 1:574b54755983 193 void rxCallback(void);
Arkadi 1:574b54755983 194
Arkadi 1:574b54755983 195 // initialize packet struct
Arkadi 1:574b54755983 196 void initPacket(void);
Arkadi 1:574b54755983 197
Arkadi 1:574b54755983 198 // Packet Parser
Arkadi 1:574b54755983 199 void parsePacket(void);
Arkadi 1:574b54755983 200
Arkadi 1:574b54755983 201 // DSP Packet Handler
Arkadi 1:574b54755983 202 void dspPacket(void);
Arkadi 1:574b54755983 203
Arkadi 8:8a3e6241c104 204 // DSP Param Packet Handler
Arkadi 8:8a3e6241c104 205 void dspParamPacket(void);
Arkadi 8:8a3e6241c104 206
Arkadi 10:273127efdc6e 207 // DSP Filter Packet Handler
Arkadi 10:273127efdc6e 208 void dspFilterPacket(void);
Arkadi 10:273127efdc6e 209
Arkadi 1:574b54755983 210 // initialize DSP
Arkadi 1:574b54755983 211 void initDSP(void);
Arkadi 0:bfac05b2d50f 212
Arkadi 0:bfac05b2d50f 213 ////////////////////////
Arkadi 0:bfac05b2d50f 214 // Main Code Setup : //
Arkadi 0:bfac05b2d50f 215 ////////////////////////
Arkadi 0:bfac05b2d50f 216 int main()
Arkadi 0:bfac05b2d50f 217 {
Arkadi 1:574b54755983 218 // init packet:
Arkadi 1:574b54755983 219 initPacket();
Arkadi 1:574b54755983 220
Arkadi 1:574b54755983 221 // initialize DSP
Arkadi 1:574b54755983 222 initDSP();
Arkadi 1:574b54755983 223
Arkadi 0:bfac05b2d50f 224 pc.baud(57600);
Arkadi 2:59c1a24aed7c 225 switch_dsp.baud(57600);
Arkadi 2:59c1a24aed7c 226
Arkadi 2:59c1a24aed7c 227
Arkadi 2:59c1a24aed7c 228 // pc is used for debbuging, in application mode the commands are from the switch_dsp.
Arkadi 1:574b54755983 229 // attach serial event interrupt
Arkadi 1:574b54755983 230 pc.attach(&rxCallback, Serial::RxIrq);
Arkadi 2:59c1a24aed7c 231
Arkadi 2:59c1a24aed7c 232 // attach serial event interrupt
Arkadi 2:59c1a24aed7c 233 switch_dsp.attach(&rxCallback, Serial::RxIrq);
Arkadi 2:59c1a24aed7c 234
Arkadi 0:bfac05b2d50f 235 #ifdef DEBUG_MOD1
Arkadi 0:bfac05b2d50f 236 pc.printf("ReSpeaker Test \r\n");
Arkadi 0:bfac05b2d50f 237 #endif
Arkadi 0:bfac05b2d50f 238
Arkadi 0:bfac05b2d50f 239 ///////////////////////
Arkadi 0:bfac05b2d50f 240 // Main Code Loop : //
Arkadi 0:bfac05b2d50f 241 ///////////////////////
Arkadi 0:bfac05b2d50f 242 while(1) {
Arkadi 1:574b54755983 243 // run selected filter
Arkadi 3:48258b86e182 244 if (operationMode == 0) {
Arkadi 4:59319802012b 245 offMode();
Arkadi 3:48258b86e182 246 } else if (operationMode == 1) {
Arkadi 4:59319802012b 247 passthrough();
Arkadi 4:59319802012b 248 } else if (operationMode == 2) {
Arkadi 4:59319802012b 249 highpass();
Arkadi 4:59319802012b 250 } else if (operationMode == 3) {
Arkadi 4:59319802012b 251 highpassTrig();
Arkadi 7:25c81cb23e42 252 } else if (operationMode == 4) {
Arkadi 7:25c81cb23e42 253 GainsTrig();
Arkadi 3:48258b86e182 254 }
Arkadi 4:59319802012b 255 //highpass_filter();
Arkadi 3:48258b86e182 256 // more elegant but much slower option:
Arkadi 3:48258b86e182 257 //FilterFunction();
Arkadi 1:574b54755983 258 #ifdef DEBUG_MOD11
Arkadi 1:574b54755983 259 // toggle pin, Loop frequency indicator
Arkadi 1:574b54755983 260 toggelState=!toggelState;
Arkadi 1:574b54755983 261 mytoggle.write(toggelState);
Arkadi 1:574b54755983 262 #endif
Arkadi 9:7e20c1e5a354 263 // Frequency counter
Arkadi 9:7e20c1e5a354 264 toggleCounter++;
Arkadi 11:07981b0d28d3 265 if (toggleCounter == TICKS2TOGGLE) {
Arkadi 9:7e20c1e5a354 266 toggleCounter=0;
Arkadi 9:7e20c1e5a354 267 toggleCounterState = !toggleCounterState;
Arkadi 9:7e20c1e5a354 268 dsp_trigger.write(toggleCounterState);
Arkadi 9:7e20c1e5a354 269 }
Arkadi 3:48258b86e182 270 } // end loop
Arkadi 3:48258b86e182 271 } // end main
Arkadi 0:bfac05b2d50f 272
Arkadi 0:bfac05b2d50f 273 ///////////////
Arkadi 0:bfac05b2d50f 274 // Functions //
Arkadi 0:bfac05b2d50f 275 ///////////////
Arkadi 0:bfac05b2d50f 276
Arkadi 1:574b54755983 277 // initialize DSP
Arkadi 1:574b54755983 278 void initDSP(void)
Arkadi 1:574b54755983 279 {
Arkadi 1:574b54755983 280 // init dac
Arkadi 1:574b54755983 281 ADC1_Init();
Arkadi 1:574b54755983 282 DAC1_Init();
Arkadi 1:574b54755983 283
Arkadi 1:574b54755983 284 HAL_ADC_Start(&hadc1);
Arkadi 1:574b54755983 285 HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
Arkadi 1:574b54755983 286
Arkadi 1:574b54755983 287 // define Dac Register for direct method of setting DAC value`s
Arkadi 1:574b54755983 288 Dac_Reg = (uint32_t) (hdac1.Instance);
Arkadi 1:574b54755983 289 Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R);
Arkadi 1:574b54755983 290
Arkadi 1:574b54755983 291 // intialize filter function pointer
Arkadi 5:ec6f2323a263 292 FilterFunction = passthrough;//highpass_filter;
Arkadi 6:e8b4ca41c691 293 operationMode = 0 ;
Arkadi 1:574b54755983 294 }
Arkadi 1:574b54755983 295
Arkadi 1:574b54755983 296 // Serial Event function
Arkadi 1:574b54755983 297 void rxCallback(void)
Arkadi 0:bfac05b2d50f 298 {
Arkadi 2:59c1a24aed7c 299 while ((pc.readable()) || (switch_dsp.readable())) {
Arkadi 1:574b54755983 300 // read icoming
Arkadi 2:59c1a24aed7c 301 uint8_t in_byte=0;
Arkadi 1:574b54755983 302 //led = !led;
Arkadi 2:59c1a24aed7c 303 if (pc.readable()) {
Arkadi 2:59c1a24aed7c 304 in_byte = pc.getc();
Arkadi 2:59c1a24aed7c 305 } else if (switch_dsp.readable()) {
Arkadi 2:59c1a24aed7c 306 in_byte = switch_dsp.getc();
Arkadi 2:59c1a24aed7c 307 }
Arkadi 1:574b54755983 308 #ifdef DEBUG_MOD1
Arkadi 1:574b54755983 309 pc.putc(in_byte);
Arkadi 1:574b54755983 310 #endif
Arkadi 1:574b54755983 311 // detect start message , end message
Arkadi 1:574b54755983 312 switch (packetMSG.syncFlag) {
Arkadi 1:574b54755983 313 // waiting for header
Arkadi 1:574b54755983 314 case 0: {
Arkadi 1:574b54755983 315 if (packetMSG.header[packetMSG.syncIndex] == in_byte) {
Arkadi 1:574b54755983 316 packetMSG.syncIndex++;
Arkadi 1:574b54755983 317 if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC
Arkadi 1:574b54755983 318 packetMSG.syncFlag = 1; // start collecting data, wait for footer
Arkadi 1:574b54755983 319 packetMSG.bufferIndex = 0;
Arkadi 1:574b54755983 320 packetMSG.syncIndex=0;
Arkadi 1:574b54755983 321 }
Arkadi 1:574b54755983 322 } else { // reinit sync
Arkadi 1:574b54755983 323 packetMSG.syncIndex=0;
Arkadi 1:574b54755983 324 }
Arkadi 1:574b54755983 325 //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex);
Arkadi 1:574b54755983 326 break;
Arkadi 1:574b54755983 327 }
Arkadi 1:574b54755983 328 // waiting for footer
Arkadi 1:574b54755983 329 case 1: {
Arkadi 1:574b54755983 330 // add byte to buffer
Arkadi 1:574b54755983 331 packetMSG.buffer[packetMSG.bufferIndex] = in_byte;
Arkadi 1:574b54755983 332 packetMSG.bufferIndex++;
Arkadi 1:574b54755983 333 if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow
Arkadi 1:574b54755983 334 // reset buffer
Arkadi 1:574b54755983 335 packetMSG.bufferIndex = 0;
Arkadi 1:574b54755983 336 packetMSG.syncIndex = 0;
Arkadi 1:574b54755983 337 packetMSG.syncFlag = 0;
Arkadi 1:574b54755983 338 } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved
Arkadi 1:574b54755983 339 packetMSG.syncIndex++;
Arkadi 1:574b54755983 340 packetMSG.syncFlag=2; // move to verify footer
Arkadi 1:574b54755983 341 }
Arkadi 1:574b54755983 342 //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex);
Arkadi 1:574b54755983 343 break;
Arkadi 1:574b54755983 344 }
Arkadi 1:574b54755983 345 // verify footer
Arkadi 1:574b54755983 346 case 2: {
Arkadi 1:574b54755983 347 // add byte to buffer
Arkadi 1:574b54755983 348 packetMSG.buffer[packetMSG.bufferIndex] = in_byte;
Arkadi 1:574b54755983 349 packetMSG.bufferIndex++;
Arkadi 1:574b54755983 350 if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow
Arkadi 1:574b54755983 351 // reset buffer
Arkadi 1:574b54755983 352 packetMSG.bufferIndex = 0;
Arkadi 1:574b54755983 353 packetMSG.syncIndex = 0;
Arkadi 1:574b54755983 354 packetMSG.syncFlag = 0;
Arkadi 1:574b54755983 355 } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved
Arkadi 1:574b54755983 356 packetMSG.syncIndex++;
Arkadi 1:574b54755983 357 if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC
Arkadi 1:574b54755983 358 packetMSG.syncFlag = 3;
Arkadi 1:574b54755983 359 // copy packet to json buffer
Arkadi 1:574b54755983 360 memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex);
Arkadi 1:574b54755983 361 json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string
Arkadi 1:574b54755983 362 // copy packet to json buffer with sprintf
Arkadi 1:574b54755983 363 //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer );
Arkadi 1:574b54755983 364 // send msg to parse.
Arkadi 1:574b54755983 365 parsePacket();
Arkadi 1:574b54755983 366 // reset buffer
Arkadi 1:574b54755983 367 packetMSG.bufferIndex = 0;
Arkadi 1:574b54755983 368 packetMSG.syncIndex = 0;
Arkadi 1:574b54755983 369 packetMSG.syncFlag = 0;
Arkadi 1:574b54755983 370 }
Arkadi 1:574b54755983 371 } else { // footer broke restart wait for footer
Arkadi 1:574b54755983 372 packetMSG.syncFlag=1;
Arkadi 1:574b54755983 373 // verify that it didnt broke on first footer char
Arkadi 1:574b54755983 374 if (packetMSG.footer[0] == in_byte) {
Arkadi 1:574b54755983 375 packetMSG.syncIndex=1;
Arkadi 1:574b54755983 376 } else {
Arkadi 1:574b54755983 377 packetMSG.syncIndex=0;
Arkadi 1:574b54755983 378 }
Arkadi 1:574b54755983 379 }
Arkadi 1:574b54755983 380 break;
Arkadi 1:574b54755983 381 }
Arkadi 1:574b54755983 382 default: {
Arkadi 1:574b54755983 383 pc.printf("Sonmething went wrong \r\n");
Arkadi 1:574b54755983 384 break;
Arkadi 1:574b54755983 385 }
Arkadi 1:574b54755983 386 } // end switch
Arkadi 1:574b54755983 387 }// end uart readable
Arkadi 1:574b54755983 388 } // end rxCallback
Arkadi 1:574b54755983 389
Arkadi 1:574b54755983 390 // initialize packet struct
Arkadi 1:574b54755983 391 void initPacket(void)
Arkadi 1:574b54755983 392 {
Arkadi 1:574b54755983 393 // init variables to default:
Arkadi 1:574b54755983 394 packetMSG.header[0] = 'j';
Arkadi 1:574b54755983 395 packetMSG.header[1] = 's';
Arkadi 1:574b54755983 396 packetMSG.header[2] = 'o';
Arkadi 1:574b54755983 397 packetMSG.header[3] = 'n';
Arkadi 1:574b54755983 398 packetMSG.header[4] = ':';
Arkadi 1:574b54755983 399
Arkadi 1:574b54755983 400 packetMSG.footer[0]= 13; // /r
Arkadi 1:574b54755983 401 packetMSG.footer[1]= 10; // /n
Arkadi 1:574b54755983 402
Arkadi 1:574b54755983 403 packetMSG.syncIndex=0; // sync index for header / footer
Arkadi 1:574b54755983 404 packetMSG.syncFlag=0; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer
Arkadi 1:574b54755983 405 packetMSG.bufferIndex=0; // buffer index
Arkadi 1:574b54755983 406 }// end init Packet struct
Arkadi 1:574b54755983 407
Arkadi 1:574b54755983 408
Arkadi 1:574b54755983 409
Arkadi 1:574b54755983 410 // Packet Parser
Arkadi 1:574b54755983 411 void parsePacket(void)
Arkadi 1:574b54755983 412 {
Arkadi 1:574b54755983 413 string targetName;
Arkadi 1:574b54755983 414 #ifdef DEBUG_MOD2
Arkadi 1:574b54755983 415 // write buffer to screen
Arkadi 1:574b54755983 416 //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer );
Arkadi 1:574b54755983 417 pc.printf("%s", json);
Arkadi 1:574b54755983 418 #endif
Arkadi 1:574b54755983 419
Arkadi 1:574b54755983 420 // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]}
Arkadi 1:574b54755983 421 parse(guiCmd, json);
Arkadi 1:574b54755983 422
Arkadi 1:574b54755983 423 // get target:
Arkadi 1:574b54755983 424 targetName = guiCmd["name"].get<string>(); // switch / dsp
Arkadi 1:574b54755983 425
Arkadi 1:574b54755983 426 #ifdef DEBUG_MOD2
Arkadi 1:574b54755983 427 // send parsed values
Arkadi 1:574b54755983 428 pc.printf("targetName: %s \r\n", targetName.c_str());
Arkadi 1:574b54755983 429 #endif
Arkadi 1:574b54755983 430
Arkadi 1:574b54755983 431 // select handler based on target mcu
Arkadi 1:574b54755983 432 if (targetName == "dsp") {
Arkadi 1:574b54755983 433 dspPacket();
Arkadi 8:8a3e6241c104 434 } else if (targetName == "dspParam") {
Arkadi 8:8a3e6241c104 435 dspParamPacket();
Arkadi 10:273127efdc6e 436 } else if (targetName == "dspFilter") {
Arkadi 10:273127efdc6e 437 dspFilterPacket();
Arkadi 1:574b54755983 438 } else {
Arkadi 1:574b54755983 439 #ifdef DEBUG_MOD2
Arkadi 1:574b54755983 440 // unrecognised target
Arkadi 1:574b54755983 441 pc.printf("unrecognised target: %s \r\n", targetName.c_str());
Arkadi 1:574b54755983 442 #endif
Arkadi 0:bfac05b2d50f 443 }
Arkadi 1:574b54755983 444 // led blink
Arkadi 1:574b54755983 445 led = !led;
Arkadi 1:574b54755983 446 }// end parse
Arkadi 1:574b54755983 447
Arkadi 1:574b54755983 448 // DSP Packet Handler
Arkadi 1:574b54755983 449 void dspPacket(void)
Arkadi 1:574b54755983 450 {
Arkadi 1:574b54755983 451 string modeType;
Arkadi 4:59319802012b 452 // get operation mode
Arkadi 1:574b54755983 453 modeType = guiCmd["mode"].get<string>();
Arkadi 1:574b54755983 454
Arkadi 1:574b54755983 455 #ifdef DEBUG_MOD10
Arkadi 1:574b54755983 456 // send parsed values
Arkadi 4:59319802012b 457 pc.printf("mode: %s\r\n", modeType.c_str());
Arkadi 8:8a3e6241c104 458 //switch_dsp.printf("mode: %s\r\n", modeType.c_str());
Arkadi 1:574b54755983 459 #endif
Arkadi 1:574b54755983 460 // selected operation mode
Arkadi 4:59319802012b 461 if ( modeType == "off" ) {
Arkadi 4:59319802012b 462 FilterFunction = offMode;
Arkadi 3:48258b86e182 463 operationMode = 0 ;
Arkadi 4:59319802012b 464 } else if( modeType == "passthrough" ) {
Arkadi 4:59319802012b 465 FilterFunction = passthrough;
Arkadi 3:48258b86e182 466 operationMode = 1 ;
Arkadi 8:8a3e6241c104 467 } else if( modeType == "highpass" ) {
Arkadi 4:59319802012b 468 FilterFunction = highpass;
Arkadi 4:59319802012b 469 operationMode = 2 ;
Arkadi 8:8a3e6241c104 470 } else if( modeType == "hpf_trig" ) {
Arkadi 4:59319802012b 471 FilterFunction = highpassTrig;
Arkadi 4:59319802012b 472 operationMode = 3 ;
Arkadi 8:8a3e6241c104 473 } else if( modeType == "gain_trig" ) {
Arkadi 7:25c81cb23e42 474 FilterFunction = GainsTrig;
Arkadi 7:25c81cb23e42 475 operationMode = 4 ;
Arkadi 1:574b54755983 476 } else {
Arkadi 4:59319802012b 477 switch_dsp.printf("undefined mode %s \r\n", modeType.c_str());
Arkadi 4:59319802012b 478 FilterFunction = offMode;
Arkadi 3:48258b86e182 479 operationMode = 0 ;
Arkadi 1:574b54755983 480 }
Arkadi 10:273127efdc6e 481 // succesfull parse
Arkadi 10:273127efdc6e 482 switch_dsp.printf("{\"Ack\":\"dsp\",\"mode\":\"%s\"}\r\n",modeType.c_str());
Arkadi 8:8a3e6241c104 483 }// end dspPacket
Arkadi 8:8a3e6241c104 484 // DSP Param Packet Handler
Arkadi 8:8a3e6241c104 485 void dspParamPacket(void)
Arkadi 11:07981b0d28d3 486 {
Arkadi 8:8a3e6241c104 487 // get values.
Arkadi 8:8a3e6241c104 488 signalGain = ((float)guiCmd["gain"].get<int>())/1000; // issue in parsing doubles when the number is round
Arkadi 8:8a3e6241c104 489 trigTresh = ((float)guiCmd["trigTresh"].get<int>()) / 100.0f;
Arkadi 8:8a3e6241c104 490 trigDelaySet = (uint32_t)guiCmd["trigPass"].get<int>();
Arkadi 8:8a3e6241c104 491 trigPause = (uint32_t)guiCmd["trigPause"].get<int>();
Arkadi 8:8a3e6241c104 492
Arkadi 8:8a3e6241c104 493 trigDelay = trigDelaySet;
Arkadi 8:8a3e6241c104 494 #ifdef DEBUG_MOD10
Arkadi 8:8a3e6241c104 495 // send parsed values
Arkadi 10:273127efdc6e 496 pc.printf("SignalGain: %.3f , trigTresh: %.3f , trigDelaySet %d , trigPause: %d\r\n", signalGain , trigTresh , trigDelaySet , trigPause);
Arkadi 8:8a3e6241c104 497 #endif
Arkadi 10:273127efdc6e 498 // succesfull parse
Arkadi 10:273127efdc6e 499 switch_dsp.printf("{\"Ack\":\"dspParam\",\"SignalGain\":%.3f,\"trigTresh\":%.3f,\"trigDelaySet\":%d,\"trigPause\":%d}\r\n", signalGain , trigTresh , trigDelaySet , trigPause);
Arkadi 10:273127efdc6e 500 } // end dspParamPacket
Arkadi 8:8a3e6241c104 501
Arkadi 10:273127efdc6e 502 // DSP Filter Packet Handler
Arkadi 10:273127efdc6e 503 void dspFilterPacket(void)
Arkadi 11:07981b0d28d3 504 {
Arkadi 10:273127efdc6e 505 // get values.
Arkadi 10:273127efdc6e 506 FilterSections = (guiCmd["Sections"].get<int>());
Arkadi 10:273127efdc6e 507 Gscale = ((float)(guiCmd["Gscale"].get<int>()))/10000.0f; // issue in parsing doubles
Arkadi 11:07981b0d28d3 508
Arkadi 10:273127efdc6e 509 // get SOSMat
Arkadi 11:07981b0d28d3 510 for (int jj=0 ; jj < FilterSections ; jj++) {
Arkadi 11:07981b0d28d3 511 for (int ii=0 ; ii < 6 ; ii++) {
Arkadi 11:07981b0d28d3 512 SOSMat[jj][ii] = ((float)guiCmd["SOSMat"][ii+jj*6].get<int>())/10000.0f;
Arkadi 10:273127efdc6e 513 }
Arkadi 10:273127efdc6e 514 }
Arkadi 10:273127efdc6e 515 #ifdef DEBUG_MOD10
Arkadi 10:273127efdc6e 516 // send parsed values
Arkadi 10:273127efdc6e 517 pc.printf("FilterSections: %d , Gscale: %.3f\r\n", FilterSections , Gscale);
Arkadi 10:273127efdc6e 518 pc.printf("SOSMat \r\n");
Arkadi 11:07981b0d28d3 519 for (int jj=0 ; jj < FilterSections ; jj++) {
Arkadi 10:273127efdc6e 520 pc.printf("%.3f , %.3f , %.3f , %.3f , %.3f , %.3f \r\n",SOSMat[jj][0] ,SOSMat[jj][1],SOSMat[jj][2],SOSMat[jj][3] ,SOSMat[jj][4] ,SOSMat[jj][5]);
Arkadi 10:273127efdc6e 521 }
Arkadi 10:273127efdc6e 522 #endif
Arkadi 10:273127efdc6e 523 // succesfull parse
Arkadi 10:273127efdc6e 524 switch_dsp.printf("{\"Ack\":\"dspFilter\",\"FilterSections\":%d,\"Gscale\":%.3f}\r\n", FilterSections , Gscale);
Arkadi 10:273127efdc6e 525 } // end dspFilterPacket