2022-12-15 21:56:07 +00:00
|
|
|
/*
|
|
|
|
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;
|
2022-12-19 00:53:00 +00:00
|
|
|
//Serial.print("Pa Value: ");
|
|
|
|
//Serial.println(XGZPC_Value);
|
2022-12-15 21:56:07 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|