ReSpeaker DSP V02
Dependencies: mbed MbedJSONValue
main.cpp@6:e8b4ca41c691, 2018-08-06 (annotated)
- Committer:
- Arkadi
- Date:
- Mon Aug 06 09:34:45 2018 +0000
- Revision:
- 6:e8b4ca41c691
- Parent:
- 5:ec6f2323a263
- Child:
- 7:25c81cb23e42
Minor changes Trigger mode bug fix
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 | 0:bfac05b2d50f | 85 | DigitalIn sw_trigger(PA_10); |
Arkadi | 0:bfac05b2d50f | 86 | DigitalOut led(PA_5); |
Arkadi | 1:574b54755983 | 87 | DigitalOut mytoggle(PA_6); |
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 | 0:bfac05b2d50f | 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 | 3:48258b86e182 | 138 | |
Arkadi | 5:ec6f2323a263 | 139 | |
Arkadi | 5:ec6f2323a263 | 140 | // Trigger mode variables |
Arkadi | 5:ec6f2323a263 | 141 | float trigTresh = 0.75; // threshold for trigger mode |
Arkadi | 5:ec6f2323a263 | 142 | uint32_t trigDelaySet = 3000; // counter for pulse pass |
Arkadi | 5:ec6f2323a263 | 143 | uint32_t trigDelay = trigDelaySet; // counter for pulse pass |
Arkadi | 5:ec6f2323a263 | 144 | uint32_t trigPause = 5000; // pause after trigger in microseconds |
Arkadi | 5:ec6f2323a263 | 145 | |
Arkadi | 0:bfac05b2d50f | 146 | /////////////// |
Arkadi | 0:bfac05b2d50f | 147 | // Functions // |
Arkadi | 0:bfac05b2d50f | 148 | /////////////// |
Arkadi | 0:bfac05b2d50f | 149 | |
Arkadi | 1:574b54755983 | 150 | // init functions header //hadc1 needs to be defined preiod #incude |
Arkadi | 1:574b54755983 | 151 | #include "adc_init.h" // initialize adc/dac directly for continious sample |
Arkadi | 1:574b54755983 | 152 | #include "filters.h" |
Arkadi | 1:574b54755983 | 153 | |
Arkadi | 1:574b54755983 | 154 | // nop operation |
Arkadi | 1:574b54755983 | 155 | inline void NOP() |
Arkadi | 1:574b54755983 | 156 | { |
Arkadi | 1:574b54755983 | 157 | __ASM volatile ("nop"); // one tick operation, Use to adjust frequency by slowing down the proccess |
Arkadi | 1:574b54755983 | 158 | } |
Arkadi | 1:574b54755983 | 159 | |
Arkadi | 1:574b54755983 | 160 | // Serial Event function |
Arkadi | 1:574b54755983 | 161 | void rxCallback(void); |
Arkadi | 1:574b54755983 | 162 | |
Arkadi | 1:574b54755983 | 163 | // initialize packet struct |
Arkadi | 1:574b54755983 | 164 | void initPacket(void); |
Arkadi | 1:574b54755983 | 165 | |
Arkadi | 1:574b54755983 | 166 | // Packet Parser |
Arkadi | 1:574b54755983 | 167 | void parsePacket(void); |
Arkadi | 1:574b54755983 | 168 | |
Arkadi | 1:574b54755983 | 169 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 170 | void dspPacket(void); |
Arkadi | 1:574b54755983 | 171 | |
Arkadi | 1:574b54755983 | 172 | // initialize DSP |
Arkadi | 1:574b54755983 | 173 | void initDSP(void); |
Arkadi | 0:bfac05b2d50f | 174 | |
Arkadi | 0:bfac05b2d50f | 175 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 176 | // Main Code Setup : // |
Arkadi | 0:bfac05b2d50f | 177 | //////////////////////// |
Arkadi | 0:bfac05b2d50f | 178 | int main() |
Arkadi | 0:bfac05b2d50f | 179 | { |
Arkadi | 1:574b54755983 | 180 | // init packet: |
Arkadi | 1:574b54755983 | 181 | initPacket(); |
Arkadi | 1:574b54755983 | 182 | |
Arkadi | 1:574b54755983 | 183 | // initialize DSP |
Arkadi | 1:574b54755983 | 184 | initDSP(); |
Arkadi | 1:574b54755983 | 185 | |
Arkadi | 0:bfac05b2d50f | 186 | pc.baud(57600); |
Arkadi | 2:59c1a24aed7c | 187 | switch_dsp.baud(57600); |
Arkadi | 2:59c1a24aed7c | 188 | |
Arkadi | 2:59c1a24aed7c | 189 | |
Arkadi | 2:59c1a24aed7c | 190 | // pc is used for debbuging, in application mode the commands are from the switch_dsp. |
Arkadi | 1:574b54755983 | 191 | // attach serial event interrupt |
Arkadi | 1:574b54755983 | 192 | pc.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 193 | |
Arkadi | 2:59c1a24aed7c | 194 | // attach serial event interrupt |
Arkadi | 2:59c1a24aed7c | 195 | switch_dsp.attach(&rxCallback, Serial::RxIrq); |
Arkadi | 2:59c1a24aed7c | 196 | |
Arkadi | 0:bfac05b2d50f | 197 | #ifdef DEBUG_MOD1 |
Arkadi | 0:bfac05b2d50f | 198 | pc.printf("ReSpeaker Test \r\n"); |
Arkadi | 0:bfac05b2d50f | 199 | #endif |
Arkadi | 0:bfac05b2d50f | 200 | |
Arkadi | 0:bfac05b2d50f | 201 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 202 | // Main Code Loop : // |
Arkadi | 0:bfac05b2d50f | 203 | /////////////////////// |
Arkadi | 0:bfac05b2d50f | 204 | while(1) { |
Arkadi | 1:574b54755983 | 205 | // run selected filter |
Arkadi | 3:48258b86e182 | 206 | if (operationMode == 0) { |
Arkadi | 4:59319802012b | 207 | offMode(); |
Arkadi | 3:48258b86e182 | 208 | } else if (operationMode == 1) { |
Arkadi | 4:59319802012b | 209 | passthrough(); |
Arkadi | 4:59319802012b | 210 | } else if (operationMode == 2) { |
Arkadi | 4:59319802012b | 211 | highpass(); |
Arkadi | 4:59319802012b | 212 | } else if (operationMode == 3) { |
Arkadi | 4:59319802012b | 213 | highpassTrig(); |
Arkadi | 3:48258b86e182 | 214 | } |
Arkadi | 4:59319802012b | 215 | //highpass_filter(); |
Arkadi | 3:48258b86e182 | 216 | // more elegant but much slower option: |
Arkadi | 3:48258b86e182 | 217 | //FilterFunction(); |
Arkadi | 1:574b54755983 | 218 | #ifdef DEBUG_MOD11 |
Arkadi | 1:574b54755983 | 219 | // toggle pin, Loop frequency indicator |
Arkadi | 1:574b54755983 | 220 | toggelState=!toggelState; |
Arkadi | 1:574b54755983 | 221 | mytoggle.write(toggelState); |
Arkadi | 1:574b54755983 | 222 | #endif |
Arkadi | 3:48258b86e182 | 223 | } // end loop |
Arkadi | 3:48258b86e182 | 224 | } // end main |
Arkadi | 0:bfac05b2d50f | 225 | |
Arkadi | 0:bfac05b2d50f | 226 | /////////////// |
Arkadi | 0:bfac05b2d50f | 227 | // Functions // |
Arkadi | 0:bfac05b2d50f | 228 | /////////////// |
Arkadi | 0:bfac05b2d50f | 229 | |
Arkadi | 1:574b54755983 | 230 | // initialize DSP |
Arkadi | 1:574b54755983 | 231 | void initDSP(void) |
Arkadi | 1:574b54755983 | 232 | { |
Arkadi | 1:574b54755983 | 233 | // init dac |
Arkadi | 1:574b54755983 | 234 | ADC1_Init(); |
Arkadi | 1:574b54755983 | 235 | DAC1_Init(); |
Arkadi | 1:574b54755983 | 236 | |
Arkadi | 1:574b54755983 | 237 | HAL_ADC_Start(&hadc1); |
Arkadi | 1:574b54755983 | 238 | HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); |
Arkadi | 1:574b54755983 | 239 | |
Arkadi | 1:574b54755983 | 240 | // define Dac Register for direct method of setting DAC value`s |
Arkadi | 1:574b54755983 | 241 | Dac_Reg = (uint32_t) (hdac1.Instance); |
Arkadi | 1:574b54755983 | 242 | Dac_Reg += __HAL_DHR12R1_ALIGNEMENT(DAC_ALIGN_12B_R); |
Arkadi | 1:574b54755983 | 243 | |
Arkadi | 1:574b54755983 | 244 | // intialize filter function pointer |
Arkadi | 5:ec6f2323a263 | 245 | FilterFunction = passthrough;//highpass_filter; |
Arkadi | 6:e8b4ca41c691 | 246 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 247 | } |
Arkadi | 1:574b54755983 | 248 | |
Arkadi | 1:574b54755983 | 249 | // Serial Event function |
Arkadi | 1:574b54755983 | 250 | void rxCallback(void) |
Arkadi | 0:bfac05b2d50f | 251 | { |
Arkadi | 2:59c1a24aed7c | 252 | while ((pc.readable()) || (switch_dsp.readable())) { |
Arkadi | 1:574b54755983 | 253 | // read icoming |
Arkadi | 2:59c1a24aed7c | 254 | uint8_t in_byte=0; |
Arkadi | 1:574b54755983 | 255 | //led = !led; |
Arkadi | 2:59c1a24aed7c | 256 | if (pc.readable()) { |
Arkadi | 2:59c1a24aed7c | 257 | in_byte = pc.getc(); |
Arkadi | 2:59c1a24aed7c | 258 | } else if (switch_dsp.readable()) { |
Arkadi | 2:59c1a24aed7c | 259 | in_byte = switch_dsp.getc(); |
Arkadi | 2:59c1a24aed7c | 260 | } |
Arkadi | 1:574b54755983 | 261 | #ifdef DEBUG_MOD1 |
Arkadi | 1:574b54755983 | 262 | pc.putc(in_byte); |
Arkadi | 1:574b54755983 | 263 | #endif |
Arkadi | 1:574b54755983 | 264 | // detect start message , end message |
Arkadi | 1:574b54755983 | 265 | switch (packetMSG.syncFlag) { |
Arkadi | 1:574b54755983 | 266 | // waiting for header |
Arkadi | 1:574b54755983 | 267 | case 0: { |
Arkadi | 1:574b54755983 | 268 | if (packetMSG.header[packetMSG.syncIndex] == in_byte) { |
Arkadi | 1:574b54755983 | 269 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 270 | if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC |
Arkadi | 1:574b54755983 | 271 | packetMSG.syncFlag = 1; // start collecting data, wait for footer |
Arkadi | 1:574b54755983 | 272 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 273 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 274 | } |
Arkadi | 1:574b54755983 | 275 | } else { // reinit sync |
Arkadi | 1:574b54755983 | 276 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 277 | } |
Arkadi | 1:574b54755983 | 278 | //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 279 | break; |
Arkadi | 1:574b54755983 | 280 | } |
Arkadi | 1:574b54755983 | 281 | // waiting for footer |
Arkadi | 1:574b54755983 | 282 | case 1: { |
Arkadi | 1:574b54755983 | 283 | // add byte to buffer |
Arkadi | 1:574b54755983 | 284 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 285 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 286 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 287 | // reset buffer |
Arkadi | 1:574b54755983 | 288 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 289 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 290 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 291 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 292 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 293 | packetMSG.syncFlag=2; // move to verify footer |
Arkadi | 1:574b54755983 | 294 | } |
Arkadi | 1:574b54755983 | 295 | //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex); |
Arkadi | 1:574b54755983 | 296 | break; |
Arkadi | 1:574b54755983 | 297 | } |
Arkadi | 1:574b54755983 | 298 | // verify footer |
Arkadi | 1:574b54755983 | 299 | case 2: { |
Arkadi | 1:574b54755983 | 300 | // add byte to buffer |
Arkadi | 1:574b54755983 | 301 | packetMSG.buffer[packetMSG.bufferIndex] = in_byte; |
Arkadi | 1:574b54755983 | 302 | packetMSG.bufferIndex++; |
Arkadi | 1:574b54755983 | 303 | if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow |
Arkadi | 1:574b54755983 | 304 | // reset buffer |
Arkadi | 1:574b54755983 | 305 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 306 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 307 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 308 | } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved |
Arkadi | 1:574b54755983 | 309 | packetMSG.syncIndex++; |
Arkadi | 1:574b54755983 | 310 | if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC |
Arkadi | 1:574b54755983 | 311 | packetMSG.syncFlag = 3; |
Arkadi | 1:574b54755983 | 312 | // copy packet to json buffer |
Arkadi | 1:574b54755983 | 313 | memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex); |
Arkadi | 1:574b54755983 | 314 | json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string |
Arkadi | 1:574b54755983 | 315 | // copy packet to json buffer with sprintf |
Arkadi | 1:574b54755983 | 316 | //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 317 | // send msg to parse. |
Arkadi | 1:574b54755983 | 318 | parsePacket(); |
Arkadi | 1:574b54755983 | 319 | // reset buffer |
Arkadi | 1:574b54755983 | 320 | packetMSG.bufferIndex = 0; |
Arkadi | 1:574b54755983 | 321 | packetMSG.syncIndex = 0; |
Arkadi | 1:574b54755983 | 322 | packetMSG.syncFlag = 0; |
Arkadi | 1:574b54755983 | 323 | } |
Arkadi | 1:574b54755983 | 324 | } else { // footer broke restart wait for footer |
Arkadi | 1:574b54755983 | 325 | packetMSG.syncFlag=1; |
Arkadi | 1:574b54755983 | 326 | // verify that it didnt broke on first footer char |
Arkadi | 1:574b54755983 | 327 | if (packetMSG.footer[0] == in_byte) { |
Arkadi | 1:574b54755983 | 328 | packetMSG.syncIndex=1; |
Arkadi | 1:574b54755983 | 329 | } else { |
Arkadi | 1:574b54755983 | 330 | packetMSG.syncIndex=0; |
Arkadi | 1:574b54755983 | 331 | } |
Arkadi | 1:574b54755983 | 332 | } |
Arkadi | 1:574b54755983 | 333 | break; |
Arkadi | 1:574b54755983 | 334 | } |
Arkadi | 1:574b54755983 | 335 | default: { |
Arkadi | 1:574b54755983 | 336 | pc.printf("Sonmething went wrong \r\n"); |
Arkadi | 1:574b54755983 | 337 | break; |
Arkadi | 1:574b54755983 | 338 | } |
Arkadi | 1:574b54755983 | 339 | } // end switch |
Arkadi | 1:574b54755983 | 340 | }// end uart readable |
Arkadi | 1:574b54755983 | 341 | } // end rxCallback |
Arkadi | 1:574b54755983 | 342 | |
Arkadi | 1:574b54755983 | 343 | // initialize packet struct |
Arkadi | 1:574b54755983 | 344 | void initPacket(void) |
Arkadi | 1:574b54755983 | 345 | { |
Arkadi | 1:574b54755983 | 346 | // init variables to default: |
Arkadi | 1:574b54755983 | 347 | packetMSG.header[0] = 'j'; |
Arkadi | 1:574b54755983 | 348 | packetMSG.header[1] = 's'; |
Arkadi | 1:574b54755983 | 349 | packetMSG.header[2] = 'o'; |
Arkadi | 1:574b54755983 | 350 | packetMSG.header[3] = 'n'; |
Arkadi | 1:574b54755983 | 351 | packetMSG.header[4] = ':'; |
Arkadi | 1:574b54755983 | 352 | |
Arkadi | 1:574b54755983 | 353 | packetMSG.footer[0]= 13; // /r |
Arkadi | 1:574b54755983 | 354 | packetMSG.footer[1]= 10; // /n |
Arkadi | 1:574b54755983 | 355 | |
Arkadi | 1:574b54755983 | 356 | packetMSG.syncIndex=0; // sync index for header / footer |
Arkadi | 1:574b54755983 | 357 | 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 | 358 | packetMSG.bufferIndex=0; // buffer index |
Arkadi | 1:574b54755983 | 359 | }// end init Packet struct |
Arkadi | 1:574b54755983 | 360 | |
Arkadi | 1:574b54755983 | 361 | |
Arkadi | 1:574b54755983 | 362 | |
Arkadi | 1:574b54755983 | 363 | // Packet Parser |
Arkadi | 1:574b54755983 | 364 | void parsePacket(void) |
Arkadi | 1:574b54755983 | 365 | { |
Arkadi | 1:574b54755983 | 366 | string targetName; |
Arkadi | 1:574b54755983 | 367 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 368 | // write buffer to screen |
Arkadi | 1:574b54755983 | 369 | //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer ); |
Arkadi | 1:574b54755983 | 370 | pc.printf("%s", json); |
Arkadi | 1:574b54755983 | 371 | #endif |
Arkadi | 1:574b54755983 | 372 | |
Arkadi | 1:574b54755983 | 373 | // GUI message format Switch: {"name":"switch","mic":0, "spk": [0,1,0,0,0]} |
Arkadi | 1:574b54755983 | 374 | parse(guiCmd, json); |
Arkadi | 1:574b54755983 | 375 | |
Arkadi | 1:574b54755983 | 376 | // get target: |
Arkadi | 1:574b54755983 | 377 | targetName = guiCmd["name"].get<string>(); // switch / dsp |
Arkadi | 1:574b54755983 | 378 | |
Arkadi | 1:574b54755983 | 379 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 380 | // send parsed values |
Arkadi | 1:574b54755983 | 381 | pc.printf("targetName: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 382 | #endif |
Arkadi | 1:574b54755983 | 383 | |
Arkadi | 1:574b54755983 | 384 | // select handler based on target mcu |
Arkadi | 1:574b54755983 | 385 | if (targetName == "dsp") { |
Arkadi | 1:574b54755983 | 386 | dspPacket(); |
Arkadi | 1:574b54755983 | 387 | } else { |
Arkadi | 1:574b54755983 | 388 | #ifdef DEBUG_MOD2 |
Arkadi | 1:574b54755983 | 389 | // unrecognised target |
Arkadi | 1:574b54755983 | 390 | pc.printf("unrecognised target: %s \r\n", targetName.c_str()); |
Arkadi | 1:574b54755983 | 391 | #endif |
Arkadi | 0:bfac05b2d50f | 392 | } |
Arkadi | 1:574b54755983 | 393 | // led blink |
Arkadi | 1:574b54755983 | 394 | led = !led; |
Arkadi | 1:574b54755983 | 395 | }// end parse |
Arkadi | 1:574b54755983 | 396 | |
Arkadi | 1:574b54755983 | 397 | // DSP Packet Handler |
Arkadi | 1:574b54755983 | 398 | void dspPacket(void) |
Arkadi | 1:574b54755983 | 399 | { |
Arkadi | 1:574b54755983 | 400 | string modeType; |
Arkadi | 4:59319802012b | 401 | // get operation mode |
Arkadi | 1:574b54755983 | 402 | modeType = guiCmd["mode"].get<string>(); |
Arkadi | 1:574b54755983 | 403 | |
Arkadi | 1:574b54755983 | 404 | #ifdef DEBUG_MOD10 |
Arkadi | 1:574b54755983 | 405 | // send parsed values |
Arkadi | 4:59319802012b | 406 | pc.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 4:59319802012b | 407 | switch_dsp.printf("mode: %s\r\n", modeType.c_str()); |
Arkadi | 1:574b54755983 | 408 | #endif |
Arkadi | 1:574b54755983 | 409 | // selected operation mode |
Arkadi | 4:59319802012b | 410 | if ( modeType == "off" ) { |
Arkadi | 4:59319802012b | 411 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 412 | operationMode = 0 ; |
Arkadi | 4:59319802012b | 413 | } else if( modeType == "passthrough" ) { |
Arkadi | 4:59319802012b | 414 | FilterFunction = passthrough; |
Arkadi | 3:48258b86e182 | 415 | operationMode = 1 ; |
Arkadi | 4:59319802012b | 416 | }else if( modeType == "highpass" ) { |
Arkadi | 4:59319802012b | 417 | FilterFunction = highpass; |
Arkadi | 4:59319802012b | 418 | operationMode = 2 ; |
Arkadi | 4:59319802012b | 419 | }else if( modeType == "hpf_trig" ) { |
Arkadi | 4:59319802012b | 420 | FilterFunction = highpassTrig; |
Arkadi | 4:59319802012b | 421 | operationMode = 3 ; |
Arkadi | 1:574b54755983 | 422 | } else { |
Arkadi | 4:59319802012b | 423 | switch_dsp.printf("undefined mode %s \r\n", modeType.c_str()); |
Arkadi | 4:59319802012b | 424 | FilterFunction = offMode; |
Arkadi | 3:48258b86e182 | 425 | operationMode = 0 ; |
Arkadi | 1:574b54755983 | 426 | } |
Arkadi | 1:574b54755983 | 427 | |
Arkadi | 1:574b54755983 | 428 | }// end dspPacket |