ReSpeaker DSP V02
Dependencies: mbed MbedJSONValue
main.cpp@8:8a3e6241c104, 2018-08-22 (annotated)
- Committer:
- Arkadi
- Date:
- Wed Aug 22 12:57:13 2018 +0000
- Revision:
- 8:8a3e6241c104
- Parent:
- 7:25c81cb23e42
- Child:
- 9:7e20c1e5a354
Added parameters to modify through gui
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Arkadi | 0:bfac05b2d50f | 1 | //////////////////////////////////////// |
Arkadi | 1:574b54755983 | 2 | // Tau_ReSpeaker_DSP V01 // |
Arkadi | 5:ec6f2323a263 | 3 | // Arkadiraf@gmail.com - 09/04/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 | 0:bfac05b2d50f | 32 | |
Arkadi | 0:bfac05b2d50f | 33 | Digital Input |
Arkadi | 0:bfac05b2d50f | 34 | PA_10 --> SW_Trigger |
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 | 1:574b54755983 | 67 | #define MSG_BUFFER_SIZE 512 |
Arkadi | 1:574b54755983 | 68 | #define HEADER_SIZE 5 |
Arkadi | 1:574b54755983 | 69 | #define FOOTER_SIZE 2 |
Arkadi | 0:bfac05b2d50f | 70 | |
Arkadi | 0:bfac05b2d50f | 71 | ///////////// |
Arkadi | 0:bfac05b2d50f | 72 | // Objects // |
Arkadi | 0:bfac05b2d50f | 73 | ///////////// |
Arkadi | 1:574b54755983 | 74 | // json |
Arkadi | 1:574b54755983 | 75 | MbedJSONValue guiCmd; |
Arkadi | 0:bfac05b2d50f | 76 | |
Arkadi | 0:bfac05b2d50f | 77 | // uart |
Arkadi | 1:574b54755983 | 78 | Serial pc(USBTX, USBRX); |
Arkadi | 0:bfac05b2d50f | 79 | |
Arkadi | 2:59c1a24aed7c | 80 | // uart switch_dsp |
Arkadi | 2:59c1a24aed7c | 81 | Serial switch_dsp(PC_10, PC_11); |
Arkadi | 2:59c1a24aed7c | 82 | |
Arkadi | 0:bfac05b2d50f | 83 | // digital |
Arkadi | 0:bfac05b2d50f | 84 | DigitalIn user_button(PC_13); |
Arkadi | 8:8a3e6241c104 | 85 | DigitalIn sw_trigger(PA_10);// D2 |
Arkadi | 8:8a3e6241c104 | 86 | DigitalOut led(PA_5); // D13 |
Arkadi | 8:8a3e6241c104 | 87 | DigitalOut mytoggle(PA_6); // D12 |
Arkadi | 0:bfac05b2d50f | 88 | |
Arkadi | 1:574b54755983 | 89 | // analog / define to init at mbed initialization. work directly with registers |
Arkadi | 0:bfac05b2d50f | 90 | AnalogOut dsp_output(PA_4); |
Arkadi | 0:bfac05b2d50f | 91 | AnalogIn dsp_input(PA_0); |
Arkadi | 0:bfac05b2d50f | 92 | |
Arkadi | 1:574b54755983 | 93 | |
Arkadi | 1:574b54755983 | 94 | // ADC/DAC declarations |
Arkadi | 1:574b54755983 | 95 | ADC_HandleTypeDef hadc1; |
Arkadi | 1:574b54755983 | 96 | DAC_HandleTypeDef hdac1; |
Arkadi | 1:574b54755983 | 97 | |
Arkadi | 0:bfac05b2d50f | 98 | /////////////// |
Arkadi | 0:bfac05b2d50f | 99 | // variables // |
Arkadi | 0:bfac05b2d50f | 100 | /////////////// |
Arkadi | 0:bfac05b2d50f | 101 | |
Arkadi | 1:574b54755983 | 102 | // json buffer |
Arkadi | 1:574b54755983 | 103 | char json[MSG_BUFFER_SIZE]; |
Arkadi | 1:574b54755983 | 104 | |
Arkadi | 1:574b54755983 | 105 | // packet variables |
Arkadi | 1:574b54755983 | 106 | struct packetMSG_struct { |
Arkadi | 1:574b54755983 | 107 | // recieve message variables |
Arkadi | 1:574b54755983 | 108 | uint8_t header[HEADER_SIZE]; |
Arkadi | 1:574b54755983 | 109 | uint8_t footer[FOOTER_SIZE]; |
Arkadi | 1:574b54755983 | 110 | uint8_t syncIndex; // sync index for header / footer |
Arkadi | 1:574b54755983 | 111 | 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 | 112 | // buffer |
Arkadi | 1:574b54755983 | 113 | uint16_t bufferIndex; // buffer index |
Arkadi | 1:574b54755983 | 114 | uint8_t buffer[MSG_BUFFER_SIZE]; |
Arkadi | 1:574b54755983 | 115 | } ; |
Arkadi | 1:574b54755983 | 116 | packetMSG_struct packetMSG; |
Arkadi | 1:574b54755983 | 117 | |
Arkadi | 1:574b54755983 | 118 | |
Arkadi | 1:574b54755983 | 119 | // Dac Register for direct method of setting DAC value`s |
Arkadi | 1:574b54755983 | 120 | __IO uint32_t Dac_Reg = 0; |
Arkadi | 1:574b54755983 | 121 | |
Arkadi | 0:bfac05b2d50f | 122 | // analog input |
Arkadi | 8:8a3e6241c104 | 123 | //uint16_t adc_in=0; |
Arkadi | 0:bfac05b2d50f | 124 | |
Arkadi | 1:574b54755983 | 125 | // toogle pin state |
Arkadi | 1:574b54755983 | 126 | bool toggelState=0; |
Arkadi | 1:574b54755983 | 127 | |
Arkadi | 1:574b54755983 | 128 | // create function pointer |
Arkadi | 1:574b54755983 | 129 | typedef void(*functionPtr)(void); |
Arkadi | 1:574b54755983 | 130 | functionPtr FilterFunction; |
Arkadi | 1:574b54755983 | 131 | |
Arkadi | 3:48258b86e182 | 132 | // alternative functuin selection (faster mcu process) |
Arkadi | 5:ec6f2323a263 | 133 | volatile uint8_t operationMode = 1; |
Arkadi | 4:59319802012b | 134 | // 0 - off |
Arkadi | 4:59319802012b | 135 | // 1 - passthrough - no filter |
Arkadi | 4:59319802012b | 136 | // 2 - highpass - High Pass filter |
Arkadi | 4:59319802012b | 137 | // 3 - hpf_trig - High Pass filter + Trigger mode |
Arkadi | 7:25c81cb23e42 | 138 | // 4 - gains_trig - highpass filter + trigger mode + Gains vector |
Arkadi | 3:48258b86e182 | 139 | |
Arkadi | 5:ec6f2323a263 | 140 | |
Arkadi | 5:ec6f2323a263 | 141 | // Trigger mode variables |
Arkadi | 8:8a3e6241c104 | 142 | float signalGain = 1.0; // Signal Gain |
Arkadi | 8:8a3e6241c104 | 143 | float trigTresh = 0.5; // threshold for trigger mode |
Arkadi | 8:8a3e6241c104 | 144 | uint32_t trigDelaySet = 5000; // counter for pulse pass |
Arkadi | 5:ec6f2323a263 | 145 | uint32_t trigDelay = trigDelaySet; // counter for pulse pass |
Arkadi | 8:8a3e6241c104 | 146 | uint32_t trigPause = 10; // pause after trigger in microseconds |
Arkadi | 5:ec6f2323a263 | 147 | |
Arkadi | 8:8a3e6241c104 | 148 | // Buffer mode variables |
Arkadi | 8:8a3e6241c104 | 149 | uint32_t bufferSizeSet = 5000; |
Arkadi | 8:8a3e6241c104 | 150 | uint32_t bufferSize = bufferSizeSet; |
Arkadi | 8:8a3e6241c104 | 151 | uint32_t preBufferSizeSet = 1000; |
Arkadi | 8:8a3e6241c104 | 152 | uint32_t bufferCountDown = bufferSizeSet - preBufferSizeSet; |
Arkadi | 8:8a3e6241c104 | 153 | float bufferADC[5000] = {0}; |
Arkadi | 0:bfac05b2d50f | 154 | /////////////// |
Arkadi | 0:bfac05b2d50f | 155 | // Functions // |
Arkadi | 0:bfac05b2d50f | 156 | /////////////// |
Arkadi | 0:bfac05b2d50f | 157 | |
Arkadi | 1:574b54755983 | 158 | // init functions header //hadc1 needs to be defined preiod #incude |
Arkadi | 1:574b54755983 | 159 | #include "adc_init.h" // initialize adc/dac directly for continious sample |
Arkadi | 1:574b54755983 | 160 | #include "filters.h" |
Arkadi | 1:574b54755983 | 161 | |
Arkadi | 1:574b54755983 | 162 | // nop operation |
Arkadi | 1:574b54755983 | 163 | inline void NOP() |
Arkadi | 1:574b54755983 | 164 | { |
Arkadi | 1:574b54755983 | 165 | __ASM volatile ("nop"); // one tick operation, Use to adjust frequency by slowing down the proccess |
Arkadi | 1:574b54755983 | 166 | } |
Arkadi | 1:574b54755983 | 167 | |
Arkadi | 1:574b54755983 | 168 | // Serial Event function |
Arkadi | 1:574b54755983 | 169 | void rxCallback(void); |
Arkadi | 1:574b54755983 | 170 | |
Arkadi | 1:574b54755983 | 171 | // initialize packet struct |
Arkadi | 1:574b54755983 | 172 | void initPacket(void); |
Arkadi | 1:574b54755983 | 173 | |
Arkadi | 1:574b54755983 | 174 | // Packet Parser |
Arkadi | 1:574b54755983 | 175 | void parsePacket(void); |
Arkadi | 1:574b54755983 | 176 | |
Arkadi | 1:574b54755983 | 177 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 178 | void dspPacket(void); |
Arkadi | 1:574b54755983 | 179 | |
Arkadi | 8:8a3e6241c104 | 180 | // DSP Param Packet Handler |
Arkadi | 8:8a3e6241c104 | 181 | void dspParamPacket(void); |
Arkadi | 8:8a3e6241c104 | 182 | |
Arkadi | 1:574b54755983 | 183 | // initialize DSP |
Arkadi | 1:574b54755983 | 184 | void initDSP(void); |
Arkadi | 0:bfac05b2d50f | 185 | |
Arkadi | 0:bfac05b2d50f | 186 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 187 | // Main Code Setup : // |
Arkadi | 0:bfac05b2d50f | 188 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 189 | int main() |
Arkadi | 0:bfac05b2d50f | 190 | { |
Arkadi | 1:574b54755983 | 191 | // init packet: |
Arkadi | 1:574b54755983 | 192 | initPacket(); |
Arkadi | 1:574b54755983 | 193 | |
Arkadi | 1:574b54755983 | 194 | // initialize DSP |
Arkadi | 1:574b54755983 | 195 | initDSP(); |
Arkadi | 1:574b54755983 | 196 | |
Arkadi | 0:bfac05b2d50f | 197 | pc.baud(57600); |
Arkadi | 2:59c1a24aed7c | 198 | switch_dsp.baud(57600); |
Arkadi | 2:59c1a24aed7c | 199 | |
Arkadi | 2:59c1a24aed7c | 200 | |
Arkadi | 2:59c1a24aed7c | 201 | // pc is used for debbuging, in application mode the commands are from the switch_dsp. |
Arkadi | 1:574b54755983 | 202 | // attach serial event interrupt |
Arkadi | 1:574b54755983 | 203 | pc.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 204 | |
Arkadi | 2:59c1a24aed7c | 205 | // attach serial event interrupt |
Arkadi | 2:59c1a24aed7c | 206 | switch_dsp.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 207 | |
Arkadi | 0:bfac05b2d50f | 208 | #ifdef DEBUG_MOD1 |
Arkadi | 0:bfac05b2d50f | 209 | pc.printf("ReSpeaker Test \r\n"); |
Arkadi | 0:bfac05b2d50f | 210 | #endif |
Arkadi | 0:bfac05b2d50f | 211 | |
Arkadi | 0:bfac05b2d50f | 212 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 213 | // Main Code Loop : // |
Arkadi | 0:bfac05b2d50f | 214 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 215 | while(1) { |
Arkadi | 1:574b54755983 | 216 | // run selected filter |
Arkadi | 3:48258b86e182 | 217 | if (operationMode == 0) { |
Arkadi | 4:59319802012b | 218 | offMode(); |
Arkadi | 3:48258b86e182 | 219 | } else if (operationMode == 1) { |
Arkadi | 4:59319802012b | 220 | passthrough(); |
Arkadi | 4:59319802012b | 221 | } else if (operationMode == 2) { |
Arkadi | 4:59319802012b | 222 | highpass(); |
Arkadi | 4:59319802012b | 223 | } else if (operationMode == 3) { |
Arkadi | 4:59319802012b | 224 | highpassTrig(); |
Arkadi | 7:25c81cb23e42 | 225 | } else if (operationMode == 4) { |
Arkadi | 7:25c81cb23e42 | 226 | GainsTrig(); |
Arkadi | 3:48258b86e182 | 227 | } |
Arkadi | 4:59319802012b | 228 | //highpass_filter(); |
Arkadi | 3:48258b86e182 | 229 | // more elegant but much slower option: |
Arkadi | 3:48258b86e182 | 230 | //FilterFunction(); |
Arkadi | 1:574b54755983 | 231 | #ifdef DEBUG_MOD11 |
Arkadi | 1:574b54755983 | 232 | // toggle pin, Loop frequency indicator |
Arkadi | 1:574b54755983 | 233 | toggelState=!toggelState; |
Arkadi | 1:574b54755983 | 234 | mytoggle.write(toggelState); |
Arkadi | 1:574b54755983 | 235 | #endif |
Arkadi | 3:48258b86e182 | 236 | } // end loop |
Arkadi | 3:48258b86e182 | 237 | } // end main |
Arkadi | 0:bfac05b2d50f | 238 | |
Arkadi | 0:bfac05b2d50f | 239 | /////////////// |
Arkadi | 0:bfac05b2d50f | 240 | // Functions // |
Arkadi | 0:bfac05b2d50f | 241 | /////////////// |
Arkadi | 0:bfac05b2d50f | 242 | |
Arkadi | 1:574b54755983 | 243 | // initialize DSP |
Arkadi | 1:574b54755983 | 244 | void initDSP(void) |
Arkadi | 1:574b54755983 | 245 | { |
Arkadi | 1:574b54755983 | 246 | // init dac |
Arkadi | 1:574b54755983 | 247 | ADC1_Init(); |
Arkadi | 1:574b54755983 | 248 | DAC1_Init(); |
Arkadi | 1:574b54755983 | 249 | |
Arkadi | 1:574b54755983 | 250 | HAL_ADC_Start(&hadc1); |
Arkadi | 1:574b54755983 | 251 | HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); |
Arkadi | 1:574b54755983 | 252 | |
Arkadi | 1:574b54755983 | 253 | // define Dac Register for direct method of setting DAC value`s |
Arkadi | 1:574b54755983 | 254 | Dac_Reg = (uint32_t) (hdac1.Instance); |
Arkadi | 1:574b54755983 | 255 | Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R); |
Arkadi | 1:574b54755983 | 256 | |
Arkadi | 1:574b54755983 | 257 | // intialize filter function pointer |
Arkadi | 5:ec6f2323a263 | 258 | FilterFunction = passthrough;//highpass_filter; |
Arkadi | 6:e8b4ca41c691 | 259 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 260 | } |
Arkadi | 1:574b54755983 | 261 | |
Arkadi | 1:574b54755983 | 262 | // Serial Event function |
Arkadi | 1:574b54755983 | 263 | void rxCallback(void) |
Arkadi | 0:bfac05b2d50f | 264 | { |
Arkadi | 2:59c1a24aed7c | 265 | while ((pc.readable()) || (switch_dsp.readable())) { |
Arkadi | 1:574b54755983 | 266 | // read icoming |
Arkadi | 2:59c1a24aed7c | 267 | uint8_t in_byte=0; |
Arkadi | 1:574b54755983 | 268 | //led = !led; |
Arkadi | 2:59c1a24aed7c | 269 | if (pc.readable()) { |
Arkadi | 2:59c1a24aed7c | 270 | in_byte = pc.getc(); |
Arkadi | 2:59c1a24aed7c | 271 | } else if (switch_dsp.readable()) { |
Arkadi | 2:59c1a24aed7c | 272 | in_byte = switch_dsp.getc(); |
Arkadi | 2:59c1a24aed7c | 273 | } |
Arkadi | 1:574b54755983 | 274 | #ifdef DEBUG_MOD1 |
Arkadi | 1:574b54755983 | 275 | pc.putc(in_byte); |
Arkadi | 1:574b54755983 | 276 | #endif |
Arkadi | 1:574b54755983 | 277 | // detect start message , end message |
Arkadi | 1:574b54755983 | 278 | switch (packetMSG.syncFlag) { |
Arkadi | 1:574b54755983 | 279 | // waiting for header |
Arkadi | 1:574b54755983 | 280 | case 0: { |
Arkadi | 1:574b54755983 | 281 | if (packetMSG.header[packetMSG.syncIndex] == in_byte) { |
Arkadi | 1:574b54755983 | 282 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 283 | if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC |
Arkadi | 1:574b54755983 | 284 | packetMSG.syncFlag = 1; // start collecting data, wait for footer |
Arkadi | 1:574b54755983 | 285 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 286 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 287 | } |
Arkadi | 1:574b54755983 | 288 | } else { // reinit sync |
Arkadi | 1:574b54755983 | 289 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 290 | } |
Arkadi | 1:574b54755983 | 291 | //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 292 | break; |
Arkadi | 1:574b54755983 | 293 | } |
Arkadi | 1:574b54755983 | 294 | // waiting for footer |
Arkadi | 1:574b54755983 | 295 | case 1: { |
Arkadi | 1:574b54755983 | 296 | // add byte to buffer |
Arkadi | 1:574b54755983 | 297 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 298 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 299 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 300 | // reset buffer |
Arkadi | 1:574b54755983 | 301 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 302 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 303 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 304 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 305 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 306 | packetMSG.syncFlag=2; // move to verify footer |
Arkadi | 1:574b54755983 | 307 | } |
Arkadi | 1:574b54755983 | 308 | //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 309 | break; |
Arkadi | 1:574b54755983 | 310 | } |
Arkadi | 1:574b54755983 | 311 | // verify footer |
Arkadi | 1:574b54755983 | 312 | case 2: { |
Arkadi | 1:574b54755983 | 313 | // add byte to buffer |
Arkadi | 1:574b54755983 | 314 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 315 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 316 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 317 | // reset buffer |
Arkadi | 1:574b54755983 | 318 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 319 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 320 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 321 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 322 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 323 | if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC |
Arkadi | 1:574b54755983 | 324 | packetMSG.syncFlag = 3; |
Arkadi | 1:574b54755983 | 325 | // copy packet to json buffer |
Arkadi | 1:574b54755983 | 326 | memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex); |
Arkadi | 1:574b54755983 | 327 | json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string |
Arkadi | 1:574b54755983 | 328 | // copy packet to json buffer with sprintf |
Arkadi | 1:574b54755983 | 329 | //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 330 | // send msg to parse. |
Arkadi | 1:574b54755983 | 331 | parsePacket(); |
Arkadi | 1:574b54755983 | 332 | // reset buffer |
Arkadi | 1:574b54755983 | 333 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 334 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 335 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 336 | } |
Arkadi | 1:574b54755983 | 337 | } else { // footer broke restart wait for footer |
Arkadi | 1:574b54755983 | 338 | packetMSG.syncFlag=1; |
Arkadi | 1:574b54755983 | 339 | // verify that it didnt broke on first footer char |
Arkadi | 1:574b54755983 | 340 | if (packetMSG.footer[0] == in_byte) { |
Arkadi | 1:574b54755983 | 341 | packetMSG.syncIndex=1; |
Arkadi | 1:574b54755983 | 342 | } else { |
Arkadi | 1:574b54755983 | 343 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 344 | } |
Arkadi | 1:574b54755983 | 345 | } |
Arkadi | 1:574b54755983 | 346 | break; |
Arkadi | 1:574b54755983 | 347 | } |
Arkadi | 1:574b54755983 | 348 | default: { |
Arkadi | 1:574b54755983 | 349 | pc.printf("Sonmething went wrong \r\n"); |
Arkadi | 1:574b54755983 | 350 | break; |
Arkadi | 1:574b54755983 | 351 | } |
Arkadi | 1:574b54755983 | 352 | } // end switch |
Arkadi | 1:574b54755983 | 353 | }// end uart readable |
Arkadi | 1:574b54755983 | 354 | } // end rxCallback |
Arkadi | 1:574b54755983 | 355 | |
Arkadi | 1:574b54755983 | 356 | // initialize packet struct |
Arkadi | 1:574b54755983 | 357 | void initPacket(void) |
Arkadi | 1:574b54755983 | 358 | { |
Arkadi | 1:574b54755983 | 359 | // init variables to default: |
Arkadi | 1:574b54755983 | 360 | packetMSG.header[0] = 'j'; |
Arkadi | 1:574b54755983 | 361 | packetMSG.header[1] = 's'; |
Arkadi | 1:574b54755983 | 362 | packetMSG.header[2] = 'o'; |
Arkadi | 1:574b54755983 | 363 | packetMSG.header[3] = 'n'; |
Arkadi | 1:574b54755983 | 364 | packetMSG.header[4] = ':'; |
Arkadi | 1:574b54755983 | 365 | |
Arkadi | 1:574b54755983 | 366 | packetMSG.footer[0]= 13; // /r |
Arkadi | 1:574b54755983 | 367 | packetMSG.footer[1]= 10; // /n |
Arkadi | 1:574b54755983 | 368 | |
Arkadi | 1:574b54755983 | 369 | packetMSG.syncIndex=0; // sync index for header / footer |
Arkadi | 1:574b54755983 | 370 | 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 | 371 | packetMSG.bufferIndex=0; // buffer index |
Arkadi | 1:574b54755983 | 372 | }// end init Packet struct |
Arkadi | 1:574b54755983 | 373 | |
Arkadi | 1:574b54755983 | 374 | |
Arkadi | 1:574b54755983 | 375 | |
Arkadi | 1:574b54755983 | 376 | // Packet Parser |
Arkadi | 1:574b54755983 | 377 | void parsePacket(void) |
Arkadi | 1:574b54755983 | 378 | { |
Arkadi | 1:574b54755983 | 379 | string targetName; |
Arkadi | 1:574b54755983 | 380 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 381 | // write buffer to screen |
Arkadi | 1:574b54755983 | 382 | //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 383 | pc.printf("%s", json); |
Arkadi | 1:574b54755983 | 384 | #endif |
Arkadi | 1:574b54755983 | 385 | |
Arkadi | 1:574b54755983 | 386 | // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]} |
Arkadi | 1:574b54755983 | 387 | parse(guiCmd, json); |
Arkadi | 1:574b54755983 | 388 | |
Arkadi | 1:574b54755983 | 389 | // get target: |
Arkadi | 1:574b54755983 | 390 | targetName = guiCmd["name"].get<string>(); // switch / dsp |
Arkadi | 1:574b54755983 | 391 | |
Arkadi | 1:574b54755983 | 392 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 393 | // send parsed values |
Arkadi | 1:574b54755983 | 394 | pc.printf("targetName: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 395 | #endif |
Arkadi | 1:574b54755983 | 396 | |
Arkadi | 1:574b54755983 | 397 | // select handler based on target mcu |
Arkadi | 1:574b54755983 | 398 | if (targetName == "dsp") { |
Arkadi | 1:574b54755983 | 399 | dspPacket(); |
Arkadi | 8:8a3e6241c104 | 400 | } else if (targetName == "dspParam") { |
Arkadi | 8:8a3e6241c104 | 401 | dspParamPacket(); |
Arkadi | 1:574b54755983 | 402 | } else { |
Arkadi | 1:574b54755983 | 403 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 404 | // unrecognised target |
Arkadi | 1:574b54755983 | 405 | pc.printf("unrecognised target: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 406 | #endif |
Arkadi | 0:bfac05b2d50f | 407 | } |
Arkadi | 1:574b54755983 | 408 | // led blink |
Arkadi | 1:574b54755983 | 409 | led = !led; |
Arkadi | 1:574b54755983 | 410 | }// end parse |
Arkadi | 1:574b54755983 | 411 | |
Arkadi | 1:574b54755983 | 412 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 413 | void dspPacket(void) |
Arkadi | 1:574b54755983 | 414 | { |
Arkadi | 1:574b54755983 | 415 | string modeType; |
Arkadi | 4:59319802012b | 416 | // get operation mode |
Arkadi | 1:574b54755983 | 417 | modeType = guiCmd["mode"].get<string>(); |
Arkadi | 1:574b54755983 | 418 | |
Arkadi | 1:574b54755983 | 419 | #ifdef DEBUG_MOD10 |
Arkadi | 1:574b54755983 | 420 | // send parsed values |
Arkadi | 4:59319802012b | 421 | pc.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 8:8a3e6241c104 | 422 | //switch_dsp.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 1:574b54755983 | 423 | #endif |
Arkadi | 1:574b54755983 | 424 | // selected operation mode |
Arkadi | 4:59319802012b | 425 | if ( modeType == "off" ) { |
Arkadi | 4:59319802012b | 426 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 427 | operationMode = 0 ; |
Arkadi | 4:59319802012b | 428 | } else if( modeType == "passthrough" ) { |
Arkadi | 4:59319802012b | 429 | FilterFunction = passthrough; |
Arkadi | 3:48258b86e182 | 430 | operationMode = 1 ; |
Arkadi | 8:8a3e6241c104 | 431 | } else if( modeType == "highpass" ) { |
Arkadi | 4:59319802012b | 432 | FilterFunction = highpass; |
Arkadi | 4:59319802012b | 433 | operationMode = 2 ; |
Arkadi | 8:8a3e6241c104 | 434 | } else if( modeType == "hpf_trig" ) { |
Arkadi | 4:59319802012b | 435 | FilterFunction = highpassTrig; |
Arkadi | 4:59319802012b | 436 | operationMode = 3 ; |
Arkadi | 8:8a3e6241c104 | 437 | } else if( modeType == "gain_trig" ) { |
Arkadi | 7:25c81cb23e42 | 438 | FilterFunction = GainsTrig; |
Arkadi | 7:25c81cb23e42 | 439 | operationMode = 4 ; |
Arkadi | 1:574b54755983 | 440 | } else { |
Arkadi | 4:59319802012b | 441 | switch_dsp.printf("undefined mode %s \r\n", modeType.c_str()); |
Arkadi | 4:59319802012b | 442 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 443 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 444 | } |
Arkadi | 1:574b54755983 | 445 | |
Arkadi | 8:8a3e6241c104 | 446 | }// end dspPacket |
Arkadi | 8:8a3e6241c104 | 447 | // DSP Param Packet Handler |
Arkadi | 8:8a3e6241c104 | 448 | void dspParamPacket(void) |
Arkadi | 8:8a3e6241c104 | 449 | { |
Arkadi | 8:8a3e6241c104 | 450 | // get values. |
Arkadi | 8:8a3e6241c104 | 451 | signalGain = ((float)guiCmd["gain"].get<int>())/1000; // issue in parsing doubles when the number is round |
Arkadi | 8:8a3e6241c104 | 452 | trigTresh = ((float)guiCmd["trigTresh"].get<int>()) / 100.0f; |
Arkadi | 8:8a3e6241c104 | 453 | trigDelaySet = (uint32_t)guiCmd["trigPass"].get<int>(); |
Arkadi | 8:8a3e6241c104 | 454 | trigPause = (uint32_t)guiCmd["trigPause"].get<int>(); |
Arkadi | 8:8a3e6241c104 | 455 | |
Arkadi | 8:8a3e6241c104 | 456 | trigDelay = trigDelaySet; |
Arkadi | 8:8a3e6241c104 | 457 | #ifdef DEBUG_MOD10 |
Arkadi | 8:8a3e6241c104 | 458 | // send parsed values |
Arkadi | 8:8a3e6241c104 | 459 | pc.printf("SignalGain: %f , trigTresh: %f , trigDelaySet %d , trigPause: %d\r\n", signalGain , trigTresh , trigDelaySet , trigPause); |
Arkadi | 8:8a3e6241c104 | 460 | #endif |
Arkadi | 8:8a3e6241c104 | 461 | |
Arkadi | 8:8a3e6241c104 | 462 | } // end dspParamPacket |