From 55c6f867c20f665d069595652a5c63bec4fcfa90 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Fri, 15 Mar 2019 19:30:05 -0700 Subject: [PATCH] Added rudimentary i2c listener, fixed loop Rearranged a couple of functions from the serial input function to be back in the loop where they belong. Added a rudimentary i2c listener that should allow for parameter updates, but will only reply with "OK" for now Progress on: - https://github.com/pyr0ball/pyr0piezo/issues/2 - https://github.com/pyr0ball/pyr0piezo/issues/22 --- .../Pyr0_Piezo_Sensor_v2.x.x.ino | 49 ++++++++++++++++--- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino index 4e28af5..d43e453 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/Pyr0_Piezo_Sensor_v2.x.x.ino @@ -55,6 +55,8 @@ The gain STATE is representative of these values: 3 = 6x 4 = 11x */ + +//#include // Set variables for working parameters int GAIN_FACTOR = 2; // Gain adjustment factor. 0=2x, 1=2.5x, 2=3.33x, 3=5x, 4=10x @@ -254,14 +256,38 @@ void serialInput() { char x = Serial.read(); // the order of these IF clauses is significant - - if (x == endMarker) { + identifyMarkers(); + + } +} + +/*------------------------------------------------*/ + +void i2cInput() { + + // receive data from Serial and save it into inputBuffer + + while(Wire.available()) { + + char x = Wire.read(); + + identifyMarkers(); + updateParams(); + i2cReply(); + } +} + +/*------------------------------------------------*/ + +void identifyMarkers(){ + + if (x == endMarker) { readInProgress = false; serialIncoming = true; inputBuffer[bytesRecvd] = 0; parseData(); } - + if(readInProgress) { inputBuffer[bytesRecvd] = x; bytesRecvd ++; @@ -274,7 +300,6 @@ void serialInput() { bytesRecvd = 0; readInProgress = true; } - } } /*------------------------------------------------*/ @@ -362,7 +387,6 @@ void updateVAdjL() { } /*------------------------------------------------*/ - // Checks state of the interrupt trigger, prints status, then sets output pin low void serialReply() { if (serialIncoming) { serialIncoming = false; @@ -385,9 +409,13 @@ void serialReply() { Serial.print("Error State:"); Serial.println(ERR_STATE); Serial.println("------------------"); - delay(TRG_DUR); - digitalWrite(TRG_OUT, HIGH); - sensorHReading = 0; + } +} +/*------------------------------------------------*/ + +void i2cReply() { + if (serialIncoming) { + Wire.write("OK"); } } /*------------------------------------------------*/ @@ -435,4 +463,9 @@ void loop() { // Reply with status serialReply(); + + // Sets trigger output state to false after completing loop + delay(TRG_DUR); + digitalWrite(TRG_OUT, HIGH); + sensorHReading = 0; }