diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h index 3c6b1a5..2eeb31d 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_serial.h @@ -1,17 +1,14 @@ - -/*------------------------------------------------*/ - void parseData() { // split the data into its parts - char * strtokIndx; // this is used by strtok() as an index + char *strtokIndx; // this is used by strtok() as an index - strtokIndx = strtok(inputBuffer,","); // get the first part - the string - strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn + strtokIndx = strtok(inputBuffer, " "); // get the first part - the string + strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn - strtokIndx = strtok(NULL, ","); // this continues where the previous call left off - serialInt = atoi(strtokIndx); // convert this part to an integer + strtokIndx = strtok(NULL, " "); // this continues where the previous call left off + serialInt = atoi(strtokIndx); // convert this part to an integer } /*------------------------------------------------*/ @@ -22,25 +19,20 @@ void identifyMarkers() { // char y = Wire.read(); if (x == endMarker) { - readInProgress = false; serialIncoming = true; inputBuffer[bytesRecvd] = 0; parseData(); - } - - else if(readInProgress) { + bytesRecvd = 0; + } else { inputBuffer[bytesRecvd] = x; - bytesRecvd ++; - if (bytesRecvd == buffSize) { + bytesRecvd++; + if (bytesRecvd == buffSize) + { bytesRecvd = buffSize - 1; } } - else if (x == startMarker) { - bytesRecvd = 0; - readInProgress = true; - } - #ifdef I2C + #ifdef I2C_INPUT if (y == endMarker) { readInProgress = false; serialIncoming = true; @@ -48,9 +40,9 @@ void identifyMarkers() { parseData(); } - if(readInProgress) { + if (readInProgress) { inputBuffer[bytesRecvd] = y; - bytesRecvd ++; + bytesRecvd++; if (bytesRecvd == buffSize) { bytesRecvd = buffSize - 1; } @@ -63,149 +55,203 @@ void identifyMarkers() { #endif } -/*------------------------------------------------*/ - -void updateTrigDuration() { - if (serialInt >= 0) { - TRG_DUR = serialInt; +void updateGainFactor() +{ + if (serialInt >= 0) + { + GAIN_FACTOR = serialInt; + adjustGain(); + EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); } } +} /*------------------------------------------------*/ -void updateGainFactor() { - if (serialInt >= 0) { - GAIN_FACTOR = serialInt; +void updateVFol() { + if (serialInt >= 0) + { + followerThrs = serialInt; + adjustFollow(); + EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); } } /*------------------------------------------------*/ void updateVComp() { - if (serialInt >= 0) { - compInt = serialInt; - //senseInt = compInt; // syncing these params til #24 is fixed + if (serialInt >= 0) + { + compThrs = serialInt; + adjustComp(); + EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); + } +} + +/*------------------------------------------------*/ + +void updateLoopDuration() +{ + if (serialInt >= 0) + { + LOOP_DUR = serialInt; + EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); } } /*------------------------------------------------*/ -void updateVAdj() { - if (serialInt >= 0) { - senseThrs = serialInt; - //compInt = senseInt; // syncing these params til #24 is fixed +void updateTrigDuration() { + if (serialInt >= 0) + { + TRG_DUR = serialInt; + EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); } } /*------------------------------------------------*/ void updateHysteresis() { - if (serialInt >= 0) { + if (serialInt >= 0) + { Hyst = serialInt; + EEPROM.put(HYST_ADDRESS, Hyst); } } /*------------------------------------------------*/ -void updateParams() { - if (strcmp(serialMessageIn, "TRG_D") == 0) { - updateTrigDuration(); +void updateDebug() { + if (serialInt > 0) { + Debug = 1; + } else if (serialInt == 0) { + Debug = 0; } - else if (strcmp(serialMessageIn, "GAIN_F") == 0) { +} + +/*------------------------------------------------*/ + +void serialPrintConfig() +{ + Serial.print("GAIN_F "); + Serial.print(GAIN_FACTOR); + switch (GAIN_FACTOR) + { + case 0: + Serial.println(" 3x"); + break; + case 1: + Serial.println(" 3.5x"); + break; + case 2: + Serial.println(" 4.33x"); + break; + case 3: + Serial.println(" 6x"); + break; + case 4: + Serial.println(" 11x"); + break; + default: + Serial.println(" INVALID"); + break; + } + + Serial.print("VFOL "); + Serial.println(followerThrs); + + Serial.print("VCOMP "); + Serial.println(compThrs); + + Serial.print("LOOP_D "); + Serial.println(LOOP_DUR); + + Serial.print("TRG_D "); + Serial.println(TRG_DUR); + + Serial.print("HYST "); + Serial.println(Hyst); +} + +void serialPrintState() +{ + Serial.print("{"); + + Serial.print("\"Vcc\":"); + Serial.print(Vin); + Serial.print(","); + + Serial.print("\"VComp\":"); + Serial.print(VComp); + Serial.print(","); + + Serial.print("\"VFol\":"); + Serial.print(VFol); + Serial.print(","); + + Serial.print("\"Err\":"); + Serial.print(ERR_STATE); + + Serial.println("}"); +} + +void updateParams() { + serialIncoming = false; + if (strcmp(serialMessageIn, "GAIN_F") == 0) { updateGainFactor(); } + else if (strcmp(serialMessageIn, "VFOL") == 0) { + updateVFol(); + } else if (strcmp(serialMessageIn, "VCOMP") == 0) { updateVComp(); } - else if (strcmp(serialMessageIn, "VADJ") == 0) { - updateVAdj(); + else if (strcmp(serialMessageIn, "LOOP_D") == 0) { + updateTrigDuration(); + } + else if (strcmp(serialMessageIn, "TRG_D") == 0) { + updateTrigDuration(); } else if (strcmp(serialMessageIn, "HYST") == 0) { updateHysteresis(); } - else if (strcmp(serialMessageIn, "HELP") == 0) { - Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]"); - Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]"); - Serial.println("To change ADC hysteresis value: HYST [integer]"); - Serial.println("To change sensor input pullup vRef low threshold: VADJ [float value]"); - Serial.println("To change comparator trigger high threshold: VCOMP [float value]"); - Serial.println(""); - Serial.println("These commands should be wrapped in this format:"); - Serial.println(""); - Serial.println(""); - Serial.println("Examples:"); - Serial.println(" <~ set gain factor to index 3 (6x)"); - Serial.println(" <~ set the vref floor to 2.35V"); - parseData(); + else if (strcmp(serialMessageIn, "DEBUG") == 0) { + updateDebug(); } -} + else if (strcmp(serialMessageIn, "CONFIG") == 0) { + serialPrintConfig(); + } + else if (strcmp(serialMessageIn, "RESET") == 0) { + resetConfig(); + serialPrintConfig(); + } + else if (strcmp(serialMessageIn, "STATE") == 0) { + serialPrintState(); + } + else if (strcmp(serialMessageIn, "HELP") == 0) { + // Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]"); + // Serial.println("To change voltage follower voltage (low threshold): VFOL [float value]"); + // Serial.println("To change comparator voltage (high threshold): VCOMP [float value]"); + // Serial.println("To change main loop period: LOOP_D [integer for milliseconds]"); + // Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]"); + // Serial.println("To change ADC hysteresis value: HYST [integer]"); + // Serial.println("To enable or disable debug output: DEBUG [0|1]"); + // Serial.println("To print current config: CONFIG"); + // Serial.println("To reset config to defaults: RESET"); + // Serial.println("To print current state: STATE"); + // Serial.println(""); + // Serial.println("Commands are entered in this format:"); + // Serial.println("CMD VAL"); + // Serial.println("Commands are confirmed with Enter key"); + // Serial.println(""); + // Serial.println("Examples:"); + // Serial.println("GAIN_F 3 <~ set gain factor to index 3 (6x)"); + // Serial.println("VFOL 2350 <~ set the vref floor to 2.35V"); + } + parseData(); +} -/*------------------------------------------------*/ void serialInput() { - // receive data from Serial and save it into inputBuffer - - if(Serial.available() > 0) { + if (Serial.available() > 0) { // the order of these IF clauses is significant identifyMarkers(); - } } - -/*------------------------------------------------*/ - -void serialReply() { - #ifndef VERBOSE - if (serialIncoming) { - serialIncoming = false; - #endif - #ifdef DEBUG - Serial.print("Vcc:"); - Serial.println(Vin); - Serial.print("Comp Sense:"); - Serial.print(VComp); - Serial.print(" "); - Serial.print("Comparator State:"); - Serial.print(ADJ_COMP); - Serial.print(" "); - Serial.println(compInt); - - Serial.print("Amp Sense:"); - Serial.print(VAdj); - Serial.print(" "); - Serial.print("Follower State:"); - Serial.print(ADJ_FOLLOW); - Serial.print(" "); - Serial.println(senseInt); - - Serial.print("Gain Factor:"); - Serial.print(GAIN_FACTOR); - switch (GAIN_FACTOR) { - case 0: - Serial.println(" 3x"); - break; - case 1: - Serial.println(" 3.5x"); - break; - case 2: - Serial.println(" 4.33x"); - break; - case 3: - Serial.println(" 6x"); - break; - case 4: - Serial.println(" 11x"); - break; - default: - Serial.println(" INVALID"); - break; - } - - #endif - - Serial.print("Delay:"); - Serial.println(TRG_DUR); - Serial.print("Error State:"); - Serial.println(ERR_STATE); - Serial.println("------------------"); - #ifndef VERBOSE - } - #endif -}