2020-10-18 13:10:12 +01:00
|
|
|
/*
|
2020-10-23 11:47:24 +01:00
|
|
|
xsns_78_ezo.ino - EZO modules base class
|
2020-10-18 13:10:12 +01:00
|
|
|
|
|
|
|
Copyright (C) 2020 Christopher Tremblay
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#ifdef USE_I2C
|
2020-10-25 13:46:13 +00:00
|
|
|
|
2020-10-29 09:34:44 +00:00
|
|
|
#if defined(USE_EZOPH) || defined(USE_EZOORP) || defined(USE_EZORTD) || defined(USE_EZOHUM) || defined(USE_EZOEC) || defined(USE_EZOCO2) || defined(USE_EZOO2) || defined(USE_EZOPRS)
|
2020-10-25 13:46:13 +00:00
|
|
|
#define USE_EZO
|
|
|
|
#endif
|
2020-10-25 08:21:01 +00:00
|
|
|
#if defined(USE_EZO)
|
2020-10-18 13:10:12 +01:00
|
|
|
|
|
|
|
#define D_EZO_DELAY 300 // Minimum delay for any instruction
|
|
|
|
#define D_EZO_MAX_BUF 40 // Maximum response
|
|
|
|
|
2020-10-23 11:47:24 +01:00
|
|
|
const char D_EZO_NAME[] PROGMEM = "EZO";
|
|
|
|
|
2020-10-18 13:10:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
// This baseclass provides common functionality for all EZO devices
|
|
|
|
struct EZOStruct {
|
2020-10-23 11:47:24 +01:00
|
|
|
EZOStruct(uint32_t addr) : valid(0), addr(addr), lastRead(-2000) {}
|
|
|
|
|
2020-10-18 13:10:12 +01:00
|
|
|
void MeasureRequest(void)
|
|
|
|
{
|
|
|
|
const uint8_t EZOMeasureCmd[2] = {'R', 0};
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
valid--;
|
|
|
|
}
|
|
|
|
|
|
|
|
Wire.beginTransmission(addr);
|
|
|
|
Wire.write(EZOMeasureCmd, sizeof(EZOMeasureCmd));
|
|
|
|
Wire.endTransmission();
|
|
|
|
|
|
|
|
lastRead = millis();
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:47:24 +01:00
|
|
|
void HandleCommand(char *cmd, uint32_t len) const
|
|
|
|
{
|
|
|
|
// Transmit our command verbatim
|
|
|
|
Wire.beginTransmission(addr);
|
|
|
|
Wire.write(cmd, len);
|
|
|
|
if (Wire.endTransmission() != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to read the results
|
|
|
|
char data[D_EZO_MAX_BUF];
|
|
|
|
for (uint32_t code = 254; code == 254; code = Wire.read()) {
|
|
|
|
delay(D_EZO_DELAY);
|
|
|
|
Wire.requestFrom(addr, sizeof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; Wire.available() > 0; i++) {
|
|
|
|
data[i] = Wire.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
ResponseCmndChar((char *)data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isValid(void)
|
|
|
|
{
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ProcessMeasurement(void);
|
|
|
|
virtual void Show(bool json, const char *name);
|
|
|
|
|
2020-10-26 07:09:44 +00:00
|
|
|
static const char id[] PROGMEM;
|
|
|
|
|
2020-10-23 11:47:24 +01:00
|
|
|
protected:
|
2020-10-18 13:10:12 +01:00
|
|
|
void ProcessMeasurement(char *const data, const uint32_t len, const uint32_t latency)
|
|
|
|
{
|
|
|
|
// Wait for the data to arrive first
|
|
|
|
const int32_t dur = lastRead + latency - millis();
|
|
|
|
|
|
|
|
// Wait for the last readout to complete (only when commands are issued)
|
|
|
|
if (dur > 0) {
|
|
|
|
delay(dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
Wire.requestFrom(addr, len);
|
|
|
|
const char code = Wire.read();
|
|
|
|
|
|
|
|
if (code == 1) {
|
|
|
|
for (uint32_t i = 0; (Wire.available() > 0) && (i < len); i++) {
|
|
|
|
data[i] = Wire.read();
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = SENSOR_MAX_MISS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:47:24 +01:00
|
|
|
uint8_t valid;
|
2020-10-25 13:46:13 +00:00
|
|
|
uint8_t addr;
|
2020-10-23 11:47:24 +01:00
|
|
|
uint32_t lastRead;
|
2020-10-18 13:10:12 +01:00
|
|
|
};
|
|
|
|
|
2020-10-26 07:09:44 +00:00
|
|
|
const char EZOStruct::id[] PROGMEM = "";
|
2020-10-18 13:10:12 +01:00
|
|
|
|
2020-10-25 08:21:01 +00:00
|
|
|
#endif // USE_EZO
|
2020-10-18 13:10:12 +01:00
|
|
|
#endif // USE_I2C
|