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
This commit is contained in:
pyr0ball 2019-03-15 19:30:05 -07:00
parent 1a06027bc1
commit 55c6f867c2

View file

@ -56,6 +56,8 @@ The gain STATE is representative of these values:
4 = 11x
*/
//#include <Wire.h>
// Set variables for working parameters
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=2x, 1=2.5x, 2=3.33x, 3=5x, 4=10x
int InitCount = 6; // Number of times to blink the LED on start
@ -254,6 +256,30 @@ void serialInput() {
char x = Serial.read();
// the order of these IF clauses is significant
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;
@ -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;
}