Compare commits
No commits in common. "451cefdae0df174fce66b4d84de4e4c9b1d84b6a" and "cc123773b5e4747e362e0579a0cf4f44cf6d02e7" have entirely different histories.
451cefdae0
...
cc123773b5
|
@ -1,152 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2019 stickbreaker@github
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "XGZP.h"
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
#define I2C_ERROR_OK 0
|
|
||||||
|
|
||||||
XGZP::XGZP(void):
|
|
||||||
_devID(0)
|
|
||||||
{}
|
|
||||||
|
|
||||||
bool XGZP::begin(uint8_t devID) {
|
|
||||||
_devID=0;
|
|
||||||
Wire.begin();
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5); // Sys Config Register
|
|
||||||
Wire.endTransmission();
|
|
||||||
uint8_t count = Wire.requestFrom(devID, (uint8_t) 1);
|
|
||||||
if (count == 1) {
|
|
||||||
uint8_t reg = Wire.read();
|
|
||||||
Serial.printf(" Read System Configuration register = 0x%02X\n", reg);
|
|
||||||
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5); // sys configuration register
|
|
||||||
// DAC ON, Single Conversion, Vout=Fixed by Vext*1.5, Vext=3.6V, Calibrated Output, Diag=on
|
|
||||||
Wire.write(0xFD); //
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if(err != 0){
|
|
||||||
Serial.printf("Writing to register 0xA5 value 0xFD failed = %d\n", err);
|
|
||||||
return false; // fail
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ready = false;
|
|
||||||
uint32_t tick = millis(); // timout
|
|
||||||
while (!ready && (millis() - tick < 1000)) {
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
ready = I2C_ERROR_OK == Wire.endTransmission();
|
|
||||||
}
|
|
||||||
if (ready) {
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5);
|
|
||||||
Wire.write(0x0A);
|
|
||||||
err = Wire.endTransmission();
|
|
||||||
if(err != 0){
|
|
||||||
Serial.printf("Writing to register 0xA5 value 0x0A failed = %d\n", err);
|
|
||||||
return false; // fail
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("Timeout, Sensor did not respond after first config set.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("Reading from 0xA5 failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
_devID = devID;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool XGZP::read(float * reading) {
|
|
||||||
if ((! _devID ) || ( *reading == NULL )) {
|
|
||||||
Serial.println("Not initialized or no return value location, returning");
|
|
||||||
if (*reading != NULL) *reading = NAN;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
float XGZPC_Value = 0;
|
|
||||||
bool ready = false;
|
|
||||||
uint32_t tick = millis();
|
|
||||||
while (!ready && (millis() - tick < 1000)) { // Wait upto 1sec for conversion to complete
|
|
||||||
Wire.beginTransmission(_devID);
|
|
||||||
Wire.write(0x02); // status register
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err == I2C_ERROR_OK) {
|
|
||||||
uint8_t count = Wire.requestFrom(_devID, (uint8_t) 1); // get status byte
|
|
||||||
if (count==1) { // got data
|
|
||||||
uint8_t status = Wire.read();
|
|
||||||
ready = (status & 0x01) == 0x01; // data ready!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ready) {
|
|
||||||
Wire.beginTransmission(_devID);
|
|
||||||
Wire.write(0x06); // data register
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err == I2C_ERROR_OK) {
|
|
||||||
uint8_t count = Wire.requestFrom(_devID, (uint8_t) 3); // get data
|
|
||||||
if (count == 3) { // got data
|
|
||||||
XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read();
|
|
||||||
XGZPC_Value = XGZPC_Value / 8.0;
|
|
||||||
Serial.print("Pa Value: ");
|
|
||||||
Serial.println(XGZPC_Value);
|
|
||||||
|
|
||||||
XGZPC_Value = XGZPC_Value / 6895.0;
|
|
||||||
|
|
||||||
*reading = XGZPC_Value;
|
|
||||||
Wire.beginTransmission(_devID); // start next sample?
|
|
||||||
Wire.write(0x30);
|
|
||||||
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err != 0) {
|
|
||||||
Serial.printf(" next Sample start failed? i2cError = %d\n", err);
|
|
||||||
return true; // data valid, next sample will be a problem.
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Serial.println("Read data failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("setData address failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Wire.beginTransmission(_devID); // start next sample?
|
|
||||||
Wire.write(0x30);
|
|
||||||
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err != 0) {
|
|
||||||
Serial.printf(" Next Sample start failed? i2cError = %d\n", err);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2019 stickbreaker@github
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to
|
|
||||||
deal in the Software without restriction, including without limitation the
|
|
||||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
||||||
sell copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
||||||
IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef XGZP_h
|
|
||||||
#define XGZP_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
class XGZP {
|
|
||||||
|
|
||||||
public:
|
|
||||||
XGZP();
|
|
||||||
|
|
||||||
bool begin( uint8_t devID = 0x6D );
|
|
||||||
bool read( float * reading);
|
|
||||||
private:
|
|
||||||
uint8_t _devID;
|
|
||||||
|
|
||||||
};
|
|
||||||
#endif
|
|
|
@ -2,36 +2,23 @@
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
#include "XGZP.h"
|
|
||||||
|
|
||||||
bool simulating = false;
|
bool simulating = false;
|
||||||
double simulatedPressure = 0.0;
|
float simulatedPressure = 0.0;
|
||||||
double inflatePSIPerSecond = 0.0;
|
float inflatePSIPerSecond = 0.0;
|
||||||
double deflatePSIPerSecond = 0.0;
|
float deflatePSIPerSecond = 0.0;
|
||||||
double pressureChangeRate = 0.0;
|
float pressureChangeRate = 0.0;
|
||||||
double deflateOffsetMultiplier = 0.0;
|
float deflateOffsetMultiplier = 0.0;
|
||||||
double inflateOffsetMultiplier = 0.0;
|
float inflateOffsetMultiplier = 0.0;
|
||||||
double offsetMultiplier = 0.0;
|
float offsetMultiplier = 0.0;
|
||||||
float value = 1;
|
|
||||||
|
|
||||||
XGZP sensor;
|
|
||||||
|
|
||||||
void initHardware() {
|
|
||||||
Serial.println("Initializing hardware...");
|
|
||||||
|
|
||||||
if(!sensor.begin()){
|
|
||||||
Serial.printf("Error initializing Sensor\n Locking up.");
|
|
||||||
while(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void initSimulation() {
|
void initSimulation() {
|
||||||
simulating = true;
|
simulating = true;
|
||||||
simulatedPressure = (double) random(10, 30);
|
simulatedPressure = (float) random(10, 30);
|
||||||
inflatePSIPerSecond = 0.0303;
|
inflatePSIPerSecond = 0.0303;
|
||||||
deflatePSIPerSecond = 0.0958;
|
deflatePSIPerSecond = 0.0958;
|
||||||
deflateOffsetMultiplier = random(3, 10) / 100.0;
|
deflateOffsetMultiplier = random(3, 10) / 100.0;
|
||||||
inflateOffsetMultiplier = random(103, 160) / 100.0;
|
inflateOffsetMultiplier = 1.0 - deflateOffsetMultiplier;
|
||||||
offsetMultiplier = 1.0;
|
offsetMultiplier = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,33 +26,33 @@ void tickSimulation() {
|
||||||
static unsigned long lastTick = millis();
|
static unsigned long lastTick = millis();
|
||||||
|
|
||||||
if (millis() > lastTick + 100) {
|
if (millis() > lastTick + 100) {
|
||||||
double pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
|
float pressureDelta = (millis() - lastTick) / 1000.0 * pressureChangeRate;
|
||||||
|
|
||||||
simulatedPressure += pressureDelta;
|
simulatedPressure += pressureDelta;
|
||||||
lastTick = millis();
|
lastTick = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void measurePressure(double &pressureValue) {
|
void measurePressure(float &pressureValue) {
|
||||||
if (simulating) {
|
if (simulating) {
|
||||||
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
|
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
|
||||||
adjusted *= offsetMultiplier;
|
adjusted *= offsetMultiplier;
|
||||||
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
|
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
|
||||||
} else {
|
} else {
|
||||||
sensor.read(&value);
|
int sensorValue = analogRead(PRESSURE_SENSOR_PIN);
|
||||||
double adjusted = 1.368 * value - 6.4;
|
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
|
||||||
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
|
pressureValue = 0.99 * pressureValue + 0.01 * adjusted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void resamplePressure(double &pressureValue) {
|
void resamplePressure(float &pressureValue) {
|
||||||
if (simulating) {
|
if (simulating) {
|
||||||
double adjusted = simulatedPressure + (random(-100, 100) / 100.0);
|
float adjusted = simulatedPressure + (random(-100, 100) / 100.0);
|
||||||
adjusted *= offsetMultiplier;
|
adjusted *= offsetMultiplier;
|
||||||
pressureValue = adjusted;
|
pressureValue = adjusted;
|
||||||
} else {
|
} else {
|
||||||
sensor.read(&value);
|
int sensorValue = analogRead(PRESSURE_SENSOR_PIN);
|
||||||
double adjusted = 1.368 * value - 6.4;
|
float adjusted = 0.098 * sensorValue - 16.56 + 3.58;
|
||||||
pressureValue = adjusted;
|
pressureValue = adjusted;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,18 +7,17 @@
|
||||||
#define SOLENOID_INFLATE 1
|
#define SOLENOID_INFLATE 1
|
||||||
#define SOLENOID_DEFLATE 2
|
#define SOLENOID_DEFLATE 2
|
||||||
|
|
||||||
#define RELAY1_PIN 7
|
#define RELAY1_PIN 11
|
||||||
#define RELAY2_PIN 11
|
#define RELAY2_PIN 7
|
||||||
|
|
||||||
#define PRESSURE_SENSOR_PIN A0
|
#define PRESSURE_SENSOR_PIN A0
|
||||||
|
|
||||||
extern double simulatedPressure;
|
extern float simulatedPressure;
|
||||||
|
|
||||||
void initSimulation();
|
void initSimulation();
|
||||||
void initHardware();
|
|
||||||
void tickSimulation();
|
void tickSimulation();
|
||||||
void measurePressure(double &pressureValue);
|
void measurePressure(float &pressureValue);
|
||||||
void resamplePressure(double &pressureValue);
|
void resamplePressure(float &pressureValue);
|
||||||
void setSoleniod(int solenoidState);
|
void setSoleniod(int solenoidState);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
#include "hardware.h"
|
#include "hardware.h"
|
||||||
|
|
||||||
//#define SIMULATE 1
|
#define SIMULATE 1
|
||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
#define TIMEOUT_TIME 7000
|
#define TIMEOUT_TIME 7000
|
||||||
|
@ -72,14 +72,13 @@ enum buttonStates enterButton = OPEN;
|
||||||
enum buttonStates downButton = OPEN;
|
enum buttonStates downButton = OPEN;
|
||||||
|
|
||||||
enum screenStates screenState = BOOT_UP;
|
enum screenStates screenState = BOOT_UP;
|
||||||
double pressureValue = 0.0;
|
float pressureValue = 0.0;
|
||||||
int pressureSetPoint = 0;
|
int pressureSetPoint = 0;
|
||||||
double initialPressure = 0.0;
|
//float initialPressure = 0.0;
|
||||||
//double runningPressure = 0.0;
|
//float runningPressure = 0.0;
|
||||||
double sampledPressure = 0.0;
|
float sampledPressure = 0.0;
|
||||||
double runningRateInv = 0.0;
|
|
||||||
|
|
||||||
int debounceValue(double value) {
|
int debounceValue(float value) {
|
||||||
static int prevValue = (int) value;
|
static int prevValue = (int) value;
|
||||||
|
|
||||||
if (abs(prevValue - value) > 0.4) {
|
if (abs(prevValue - value) > 0.4) {
|
||||||
|
@ -110,8 +109,6 @@ void setup()
|
||||||
initSimulation();
|
initSimulation();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
initHardware();
|
|
||||||
|
|
||||||
resamplePressure(pressureValue);
|
resamplePressure(pressureValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,8 +135,6 @@ void logData() {
|
||||||
#endif
|
#endif
|
||||||
Serial.print(", sampled: ");
|
Serial.print(", sampled: ");
|
||||||
Serial.print(sampledPressure);
|
Serial.print(sampledPressure);
|
||||||
Serial.print(", rate (inv): ");
|
|
||||||
Serial.print(runningRateInv);
|
|
||||||
Serial.print(", setpoint: ");
|
Serial.print(", setpoint: ");
|
||||||
Serial.print(pressureSetPoint);
|
Serial.print(pressureSetPoint);
|
||||||
Serial.print(", state: ");
|
Serial.print(", state: ");
|
||||||
|
@ -156,7 +151,6 @@ void runUI() {
|
||||||
static unsigned long timer = millis();
|
static unsigned long timer = millis();
|
||||||
|
|
||||||
static unsigned long startTime = millis();
|
static unsigned long startTime = millis();
|
||||||
static unsigned long stopTime = millis();
|
|
||||||
static bool isInflating = false;
|
static bool isInflating = false;
|
||||||
static bool isDeflating = false;
|
static bool isDeflating = false;
|
||||||
|
|
||||||
|
@ -255,7 +249,7 @@ void runUI() {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case INIT_RUN:
|
case INIT_RUN:
|
||||||
initialPressure = pressureValue;
|
//initialPressure = pressureValue;
|
||||||
startTime = millis();
|
startTime = millis();
|
||||||
timer = millis();
|
timer = millis();
|
||||||
screenState = BEGIN_RUN;
|
screenState = BEGIN_RUN;
|
||||||
|
@ -290,7 +284,6 @@ void runUI() {
|
||||||
setSoleniod(SOLENOID_STOP);
|
setSoleniod(SOLENOID_STOP);
|
||||||
screenState = MEASURING;
|
screenState = MEASURING;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
stopTime = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -299,22 +292,6 @@ void runUI() {
|
||||||
if (millis() >= timer + 3000) {
|
if (millis() >= timer + 3000) {
|
||||||
sampledPressure = pressureValue;
|
sampledPressure = pressureValue;
|
||||||
|
|
||||||
runningRateInv = (stopTime - startTime) / (sampledPressure - initialPressure);
|
|
||||||
|
|
||||||
Serial.print("stopTime: ");
|
|
||||||
Serial.print(stopTime);
|
|
||||||
Serial.print(", startTime: ");
|
|
||||||
Serial.print(startTime);
|
|
||||||
Serial.print(", sampledPressure: ");
|
|
||||||
Serial.print(sampledPressure);
|
|
||||||
Serial.print(", initialPressure: ");
|
|
||||||
Serial.print(initialPressure);
|
|
||||||
Serial.print(", rate: ");
|
|
||||||
Serial.print(runningRateInv);
|
|
||||||
Serial.println("");
|
|
||||||
|
|
||||||
initialPressure = sampledPressure;
|
|
||||||
|
|
||||||
if (isInflating && (int) sampledPressure >= pressureSetPoint) {
|
if (isInflating && (int) sampledPressure >= pressureSetPoint) {
|
||||||
screenState = SAY_DONE;
|
screenState = SAY_DONE;
|
||||||
nextState = PRESSURE;
|
nextState = PRESSURE;
|
||||||
|
@ -326,7 +303,6 @@ void runUI() {
|
||||||
}
|
}
|
||||||
|
|
||||||
timer = millis();
|
timer = millis();
|
||||||
startTime = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (millis() < timer + 500) {
|
if (millis() < timer + 500) {
|
||||||
|
@ -347,17 +323,14 @@ void runUI() {
|
||||||
|
|
||||||
case RUNNING:
|
case RUNNING:
|
||||||
if (enterButton == PRESSED) {
|
if (enterButton == PRESSED) {
|
||||||
setSoleniod(SOLENOID_STOP);
|
|
||||||
screenState = SAY_CANCEL;
|
screenState = SAY_CANCEL;
|
||||||
nextState = PRESSURE;
|
nextState = PRESSURE;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (upButton == PRESSED) {
|
} else if (upButton == PRESSED) {
|
||||||
setSoleniod(SOLENOID_STOP);
|
|
||||||
screenState = SAY_CANCEL;
|
screenState = SAY_CANCEL;
|
||||||
nextState = PRESSURE;
|
nextState = PRESSURE;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
} else if (downButton == PRESSED) {
|
} else if (downButton == PRESSED) {
|
||||||
setSoleniod(SOLENOID_STOP);
|
|
||||||
screenState = SAY_CANCEL;
|
screenState = SAY_CANCEL;
|
||||||
nextState = PRESSURE;
|
nextState = PRESSURE;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
|
@ -366,11 +339,9 @@ void runUI() {
|
||||||
if (isInflating && millis() >= timer + 20000) {
|
if (isInflating && millis() >= timer + 20000) {
|
||||||
screenState = MEASURING;
|
screenState = MEASURING;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
stopTime = millis();
|
|
||||||
} else if (isDeflating && millis() >= timer + 10000) {
|
} else if (isDeflating && millis() >= timer + 10000) {
|
||||||
screenState = MEASURING;
|
screenState = MEASURING;
|
||||||
timer = millis();
|
timer = millis();
|
||||||
stopTime = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isInflating) {
|
if (isInflating) {
|
||||||
|
|
|
@ -1,152 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2019 stickbreaker@github
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "XGZP.h"
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
#define I2C_ERROR_OK 0
|
|
||||||
|
|
||||||
XGZP::XGZP(void):
|
|
||||||
_devID(0)
|
|
||||||
{}
|
|
||||||
|
|
||||||
bool XGZP::begin(uint8_t devID) {
|
|
||||||
_devID=0;
|
|
||||||
Wire.begin();
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5); // Sys Config Register
|
|
||||||
Wire.endTransmission();
|
|
||||||
uint8_t count = Wire.requestFrom(devID, (uint8_t) 1);
|
|
||||||
if (count == 1) {
|
|
||||||
uint8_t reg = Wire.read();
|
|
||||||
Serial.printf(" Read System Configuration register = 0x%02X\n", reg);
|
|
||||||
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5); // sys configuration register
|
|
||||||
// DAC ON, Single Conversion, Vout=Fixed by Vext*1.5, Vext=3.6V, Calibrated Output, Diag=on
|
|
||||||
Wire.write(0xFD); //
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if(err != 0){
|
|
||||||
Serial.printf("Writing to register 0xA5 value 0xFD failed = %d\n", err);
|
|
||||||
return false; // fail
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ready = false;
|
|
||||||
uint32_t tick = millis(); // timout
|
|
||||||
while (!ready && (millis() - tick < 1000)) {
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
ready = I2C_ERROR_OK == Wire.endTransmission();
|
|
||||||
}
|
|
||||||
if (ready) {
|
|
||||||
Wire.beginTransmission(devID);
|
|
||||||
Wire.write(0xA5);
|
|
||||||
Wire.write(0x0A);
|
|
||||||
err = Wire.endTransmission();
|
|
||||||
if(err != 0){
|
|
||||||
Serial.printf("Writing to register 0xA5 value 0x0A failed = %d\n", err);
|
|
||||||
return false; // fail
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("Timeout, Sensor did not respond after first config set.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("Reading from 0xA5 failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
_devID = devID;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool XGZP::read(float * reading) {
|
|
||||||
if ((! _devID ) || ( *reading == NULL )) {
|
|
||||||
Serial.println("Not initialized or no return value location, returning");
|
|
||||||
if (*reading != NULL) *reading = NAN;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
float XGZPC_Value = 0;
|
|
||||||
bool ready = false;
|
|
||||||
uint32_t tick = millis();
|
|
||||||
while (!ready && (millis() - tick < 1000)) { // Wait upto 1sec for conversion to complete
|
|
||||||
Wire.beginTransmission(_devID);
|
|
||||||
Wire.write(0x02); // status register
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err == I2C_ERROR_OK) {
|
|
||||||
uint8_t count = Wire.requestFrom(_devID, (uint8_t) 1); // get status byte
|
|
||||||
if (count==1) { // got data
|
|
||||||
uint8_t status = Wire.read();
|
|
||||||
ready = (status & 0x01) == 0x01; // data ready!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ready) {
|
|
||||||
Wire.beginTransmission(_devID);
|
|
||||||
Wire.write(0x06); // data register
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err == I2C_ERROR_OK) {
|
|
||||||
uint8_t count = Wire.requestFrom(_devID, (uint8_t) 3); // get data
|
|
||||||
if (count == 3) { // got data
|
|
||||||
XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read();
|
|
||||||
XGZPC_Value = XGZPC_Value / 8.0;
|
|
||||||
Serial.print("Pa Value: ");
|
|
||||||
Serial.println(XGZPC_Value);
|
|
||||||
|
|
||||||
XGZPC_Value = XGZPC_Value / 6895.0;
|
|
||||||
|
|
||||||
*reading = XGZPC_Value;
|
|
||||||
Wire.beginTransmission(_devID); // start next sample?
|
|
||||||
Wire.write(0x30);
|
|
||||||
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err != 0) {
|
|
||||||
Serial.printf(" next Sample start failed? i2cError = %d\n", err);
|
|
||||||
return true; // data valid, next sample will be a problem.
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Serial.println("Read data failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.println("setData address failed");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Wire.beginTransmission(_devID); // start next sample?
|
|
||||||
Wire.write(0x30);
|
|
||||||
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate
|
|
||||||
uint8_t err = Wire.endTransmission();
|
|
||||||
if (err != 0) {
|
|
||||||
Serial.printf(" Next Sample start failed? i2cError = %d\n", err);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
/*
|
|
||||||
Copyright 2019 stickbreaker@github
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to
|
|
||||||
deal in the Software without restriction, including without limitation the
|
|
||||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
||||||
sell copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
||||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
||||||
IN THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef XGZP_h
|
|
||||||
#define XGZP_h
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
|
|
||||||
class XGZP {
|
|
||||||
|
|
||||||
public:
|
|
||||||
XGZP();
|
|
||||||
|
|
||||||
bool begin( uint8_t devID = 0x6D );
|
|
||||||
bool read( float * reading);
|
|
||||||
private:
|
|
||||||
uint8_t _devID;
|
|
||||||
|
|
||||||
};
|
|
||||||
#endif
|
|
|
@ -1,28 +0,0 @@
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
#include "XGZP.h"
|
|
||||||
|
|
||||||
XGZP sensor;
|
|
||||||
|
|
||||||
float value = 1;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
|
|
||||||
if(!sensor.begin()){
|
|
||||||
Serial.printf("Error initializing Sensor\n Locking up.");
|
|
||||||
while(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
sensor.read(&value);
|
|
||||||
|
|
||||||
float corrected = 1.368 * value - 6.4;
|
|
||||||
|
|
||||||
Serial.print(corrected);
|
|
||||||
Serial.println(" PSI");
|
|
||||||
|
|
||||||
delay(250);
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user