GY-63_MS5611/libraries/Cozir/cozir.cpp

603 lines
12 KiB
C++
Raw Normal View History

//
// FILE: Cozir.cpp
// AUTHOR: DirtGambit & Rob Tillaart
2023-10-19 06:07:06 -04:00
// VERSION: 0.3.7
// PURPOSE: library for COZIR range of sensors for Arduino
2022-03-04 09:07:12 -05:00
// Polling Mode + stream parser
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/Cozir
2021-01-29 06:31:58 -05:00
// http://forum.arduino.cc/index.php?topic=91467.0
//
// READ DATASHEET BEFORE USE OF THIS LIB !
//
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
#include "cozir.h"
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
#define CZR_INIT_DELAY 1200
#define CZR_REQUEST_TIMEOUT 200
// EEPROM ADRESSES
2022-02-21 13:40:59 -05:00
// P 11-12 manual WHICH
//
2021-08-15 13:38:45 -04:00
// Name Address Default value/ notes
2022-10-30 15:10:57 -04:00
#define CZR_AHHI 0x00 // reserved
#define CZR_ANLO 0x01 // reserved
#define CZR_ANSOURCE 0x02 // reserved
#define CZR_ACINITHI 0x03 // 87
#define CZR_ACINITLO 0x04 // 192
#define CZR_ACHI 0x05 // 94
#define CZR_ACLO 0x06 // 128
#define CZR_ACONOFF 0x07 // 0
#define CZR_ACPPMHI 0x08 // 1
#define CZR_ACPPMLO 0x09 // 194
#define CZR_AMBHI 0x0A // 1
#define CZR_AMBLO 0x0B // 194
#define CZR_BCHI 0x0C // 0
#define CZR_BCLO 0x0D // 8
2021-08-15 13:38:45 -04:00
2020-11-27 05:10:47 -05:00
COZIR::COZIR(Stream * str)
{
2021-08-15 13:38:45 -04:00
_ser = str;
_buffer[0] = '\0';
2020-11-27 05:10:47 -05:00
}
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
void COZIR::init()
{
2021-08-15 13:38:45 -04:00
// override default streaming (takes too much performance)
setOperatingMode(CZR_POLLING);
_initTimeStamp = millis();
// delay for initialization is kept until next major release.
// timestamp + isInitialized() is prepared.
// users can comment next line.
delay(CZR_INIT_DELAY);
}
bool COZIR::isInitialized()
{
return (millis() - _initTimeStamp) > CZR_INIT_DELAY;
}
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////////////
//
// OPERATING MODE
//
// note: use CZR_COMMAND to minimize power consumption
// CZR_POLLING and CZR_STREAMING use an equally amount
// of power as both sample continuously...
//
2022-02-21 13:40:59 -05:00
bool COZIR::setOperatingMode(uint8_t mode)
{
2022-02-21 13:40:59 -05:00
if (mode > CZR_POLLING) return false;
2021-08-15 13:38:45 -04:00
_operatingMode = mode;
sprintf(_buffer, "K %u", mode);
_command(_buffer);
2022-02-21 13:40:59 -05:00
return true;
}
2021-01-29 06:31:58 -05:00
////////////////////////////////////////////////////////////
//
// POLLING MODE
//
2021-08-15 13:38:45 -04:00
// you need to set the polling mode explicitly before
// using these functions. SetOperatingMode(CZR_POLLING);
// this is the default behaviour of this Class but
// not of the sensor!!
//
2021-08-15 13:38:45 -04:00
float COZIR::celsius()
{
2021-08-15 13:38:45 -04:00
uint16_t rv = _request("T");
2022-02-21 13:40:59 -05:00
return 0.1 * (rv - 1000.0);
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
float COZIR::humidity()
{
2021-08-15 13:38:45 -04:00
return 0.1 * _request("H");
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
// UNITS UNKNOWN lux??
float COZIR::light()
{
2021-08-15 13:38:45 -04:00
return 1.0 * _request("L");
}
2021-01-29 06:31:58 -05:00
uint32_t COZIR::CO2()
{
2021-08-15 13:38:45 -04:00
return _request("Z");
}
2021-08-15 13:38:45 -04:00
2021-01-29 06:31:58 -05:00
uint16_t COZIR::getPPMFactor()
{
2021-08-15 13:38:45 -04:00
_ppmFactor = _request(".");
2021-01-29 06:31:58 -05:00
return _ppmFactor;
}
// CALLIBRATION - USE THESE WITH CARE
2021-08-15 13:38:45 -04:00
// use these only in polling mode (on the Arduino)
// FineTuneZeroPoint()
// a reading of v1 will be reported as v2
2022-02-21 13:40:59 -05:00
// sort of mapping / offset
// check datasheet for detailed description
2021-08-15 13:38:45 -04:00
uint16_t COZIR::fineTuneZeroPoint(uint16_t v1, uint16_t v2)
{
2021-08-15 13:38:45 -04:00
sprintf(_buffer, "F %u %u", v1, v2);
return _request(_buffer);
}
2021-01-29 06:31:58 -05:00
// mostly the default calibrator
2021-08-15 13:38:45 -04:00
uint16_t COZIR::calibrateFreshAir()
{
2021-08-15 13:38:45 -04:00
return _request("G");
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint16_t COZIR::calibrateNitrogen()
{
2021-08-15 13:38:45 -04:00
return _request("U");
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint16_t COZIR::calibrateKnownGas(uint16_t value)
{
2021-08-15 13:38:45 -04:00
sprintf(_buffer, "X %u", value);
return _request(_buffer);
}
2020-11-27 05:10:47 -05:00
2021-08-15 13:38:45 -04:00
//uint16_t COZIR::calibrateManual(uint16_t value)
2020-11-27 05:10:47 -05:00
//{
2021-08-15 13:38:45 -04:00
//sprintf(_buffer, "u %u", value);
//return _request(_buffer);
2020-11-27 05:10:47 -05:00
//}
2021-08-15 13:38:45 -04:00
//uint16_t COZIR::setSpanCalibrate(uint16_t value)
2020-11-27 05:10:47 -05:00
//{
2021-08-15 13:38:45 -04:00
//sprintf(_buffer, "S %u", value);
//return _request(_buffer);
2020-11-27 05:10:47 -05:00
//}
2021-08-15 13:38:45 -04:00
//uint16_t COZIR::getSpanCalibrate()
2020-11-27 05:10:47 -05:00
//{
2021-08-15 13:38:45 -04:00
// return _request("s");
2020-11-27 05:10:47 -05:00
//}
2021-08-15 13:38:45 -04:00
void COZIR::setDigiFilter(uint8_t value)
{
2021-08-15 13:38:45 -04:00
sprintf(_buffer, "A %u", value);
_command(_buffer);
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint8_t COZIR::getDigiFilter()
{
2021-08-15 13:38:45 -04:00
return _request("a");
}
2020-11-27 05:10:47 -05:00
////////////////////////////////////////////////////////////
//
// STREAMING MODE
//
2021-08-15 13:38:45 -04:00
// output fields should be OR-ed
// e.g. SetOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2);
//
2021-08-15 13:38:45 -04:00
// you need to set the STREAMING mode explicitly
// SetOperatingMode(CZR_STREAMING);
//
2022-02-21 13:40:59 -05:00
// in STREAMING mode you must parse the output of serial yourself.
// stream looks like [space field space value]* \n
//
// - find separator ('\n')
// - read until next separator ('\n') in a buffer,
// - parse buffer [field space value]
//
2021-08-15 13:38:45 -04:00
void COZIR::setOutputFields(uint16_t fields)
{
_outputFields = fields;
sprintf(_buffer, "M %u", fields);
_command(_buffer);
}
bool COZIR::inOutputFields(uint16_t field)
{
2021-08-15 13:38:45 -04:00
return (_outputFields & field) == field;
}
2021-01-29 06:31:58 -05:00
// WARNING:
// After a call to GetRecentFields() you must read the serial port yourself as
// the internal buffer of this Class cannot handle the possible large output.
// It can be over 100 bytes long lines!
2021-08-15 13:38:45 -04:00
void COZIR::getRecentFields()
{
2021-08-15 13:38:45 -04:00
_command("Q");
}
////////////////////////////////////////////////////////////
//
2021-08-15 13:38:45 -04:00
// EEPROM CALLS - USE WITH CARE
//
2021-08-15 13:38:45 -04:00
void COZIR::setAutoCalibrationPreload(uint16_t value)
{
_setEEPROM2(CZR_ACINITHI, value);
}
uint16_t COZIR::getAutoCalibrationPreload()
{
return _getEEPROM2(CZR_ACINITHI);
}
void COZIR::setAutoCalibrationInterval(uint16_t value)
{
_setEEPROM2(CZR_ACHI, value);
}
uint16_t COZIR::getAutoCalibrationInterval()
{
return _getEEPROM2(CZR_ACHI);
}
void COZIR::setAutoCalibrationOn()
{
_setEEPROM(CZR_ACONOFF, 1);
}
void COZIR::setAutoCalibrationOff()
{
_setEEPROM(CZR_ACONOFF, 0);
}
bool COZIR::getAutoCalibration()
{
return _getEEPROM(CZR_ACONOFF);
}
void COZIR::setAutoCalibrationBackgroundConcentration(uint16_t value)
{
2021-08-15 13:38:45 -04:00
_setEEPROM2(CZR_ACPPMHI, value);
}
2021-08-15 13:38:45 -04:00
uint16_t COZIR::getAutoCalibrationBackgroundConcentration()
{
return _getEEPROM2(CZR_ACPPMHI);
}
void COZIR::setAmbientConcentration(uint16_t value)
{
_setEEPROM2(CZR_AMBHI, value);
}
uint16_t COZIR::getAmbientConcentration()
{
return _getEEPROM2(CZR_AMBHI);
}
void COZIR::setBufferClearTime(uint16_t value)
{
_setEEPROM2(CZR_BCHI, value);
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint16_t COZIR::getBufferClearTime()
{
2021-08-15 13:38:45 -04:00
return _getEEPROM2(CZR_BCHI);
}
2021-08-15 13:38:45 -04:00
/*
// TODO first verify if single functions work.
void COZIR::setEEPROMFactoryReset()
{
setAutoCalibrationPreload(0x57C0);
setAutoCalibrationInterval(0x8E80);
setAutoCalibrationOff();
setAutoCalibrationBackgroundConcentration(0x01C2);
setAmbientConcentration(0x01C2);
setBufferClearTime(0x0008);
}
*/
////////////////////////////////////////////////////////////
//
// COMMAND MODE
//
2021-01-29 06:31:58 -05:00
// read serial yourself -
//
// TODO Page 5: Mode 0 Command Mode
2021-08-15 13:38:45 -04:00
// This is primarily intended for use when extracting larger chunks
2021-01-29 06:31:58 -05:00
// of information from the sensor (for example using the Y and * commands).
// In this mode, the sensor is stopped waiting for commands.
//
2021-08-15 13:38:45 -04:00
void COZIR::getVersionSerial()
{
2021-08-15 13:38:45 -04:00
// override modes to prevent interference in output
setOperatingMode(CZR_COMMAND);
_command("Y");
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
void COZIR::getConfiguration()
{
2021-08-15 13:38:45 -04:00
// override modes to prevent interference in output
setOperatingMode(CZR_COMMAND);
_command("*");
}
2020-11-27 05:10:47 -05:00
/////////////////////////////////////////////////////////
2020-11-27 05:10:47 -05:00
//
// PRIVATE
2020-11-27 05:10:47 -05:00
//
2021-08-15 13:38:45 -04:00
void COZIR::_command(const char* str)
{
2021-08-15 13:38:45 -04:00
_ser->print(str);
_ser->print("\r\n");
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint32_t COZIR::_request(const char* str)
{
2021-08-15 13:38:45 -04:00
_command(str);
// read the answer from serial.
// TODO: PROPER TIMEOUT CODE.
// - might be a big delay
2022-02-21 13:40:59 -05:00
// - what is longest answer possible? CZR_REQUEST_TIMEOUT?
2021-08-15 13:38:45 -04:00
uint8_t idx = 0;
uint32_t start = millis();
2022-02-21 13:40:59 -05:00
while (millis() - start < CZR_REQUEST_TIMEOUT)
2021-08-15 13:38:45 -04:00
{
if (_ser->available())
{
2021-08-15 13:38:45 -04:00
char c = _ser->read();
2022-03-04 09:07:12 -05:00
if (c == '\n') break;
2021-08-15 13:38:45 -04:00
_buffer[idx++] = c;
_buffer[idx] = '\0';
}
2021-08-15 13:38:45 -04:00
}
2022-02-21 13:40:59 -05:00
// Serial.print("buffer: ");
// Serial.println(_buffer);
2022-03-04 09:07:12 -05:00
uint32_t rv = 0;
// default for PPM is different.
if (str[0] == '.') rv = 1;
// do we got the requested field?
if (strchr(_buffer, str[0]) && (idx > 2))
{
rv = atol(&_buffer[2]);
}
return rv;
2021-08-15 13:38:45 -04:00
}
void COZIR::_setEEPROM(uint8_t address, uint8_t value)
{
if (address > CZR_BCLO) return;
sprintf(_buffer, "P %u %u", address, value);
_command(_buffer);
}
uint8_t COZIR::_getEEPROM(uint8_t address)
{
sprintf(_buffer, "p %u", address);
return _request(_buffer);
}
2021-08-15 13:38:45 -04:00
void COZIR::_setEEPROM2(uint8_t address, uint16_t value)
{
if (address > CZR_BCLO) return;
sprintf(_buffer, "P %u %u", address, value >> 8);
_command(_buffer);
sprintf(_buffer, "P %u %u", address + 1, value & 0xFF);
_command(_buffer);
}
2021-01-29 06:31:58 -05:00
2021-08-15 13:38:45 -04:00
uint16_t COZIR::_getEEPROM2(uint8_t address)
{
sprintf(_buffer, "p %u", address);
uint16_t val = _request(_buffer) << 8;
sprintf(_buffer, "p %u", address + 1);
return val + _request(_buffer);
}
2022-02-25 06:16:35 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// C0ZIRParser
//
C0ZIRParser::C0ZIRParser()
{
init();
}
void C0ZIRParser::init()
{
_light = 0;
_humidity = 0;
_LED_FILT = 0;
_LED_RAW = 0;
_LED_MAX = 0;
_zeroPoint = 0;
_temperature_RAW = 0;
_temperature_FILT = 0;
_LED_signal_FILT = 0;
_LED_signal_RAW = 0;
_temperature_Sensor = 0;
_CO2_FILT = 0;
_CO2_RAW = 0;
_samples = 0;
_PPM = 1; // Note default one
_value = 0;
_field = 0;
}
uint8_t C0ZIRParser::nextChar(char c)
{
2022-03-04 09:07:12 -05:00
static bool skipLine = false;
2022-02-25 06:16:35 -05:00
uint8_t rv = 0;
2022-03-04 09:07:12 -05:00
// SKIP * and Y until next return.
// as output of these two commands not handled by this parser
if ((c == '*') || (c == 'Y') || (c == '@')) skipLine = true;
if (c == '\n') skipLine = false;
if (skipLine) return 0;
// TODO investigate
// if the last char is more than 2..5 ms ago (9600 baud ~ 1 char/ms)
// it probably needs to sync with the stream again.
// but it depends on how calling process behaves.
// - need for uint32_t _lastChar time stamp?
2022-02-25 06:16:35 -05:00
switch(c)
{
case '0' ... '9':
_value *= 10;
_value += (c - '0');
break;
2022-03-04 09:07:12 -05:00
// major responses to catch
case 'z':
case 'Z':
2022-02-25 06:16:35 -05:00
case 'L':
case 'T':
case 'H':
2022-03-04 09:07:12 -05:00
// all other known responses, starting a new field
case 'X':
case '.':
case '@': // skipped
case 'Y': // skipped
case '*': // skipped
case 'Q':
case 'F':
case 'G':
case 'M':
case 'K': // mode
2022-02-25 06:16:35 -05:00
case 'A':
2022-03-04 09:07:12 -05:00
case 'a':
2022-02-25 06:16:35 -05:00
case 'P':
2022-03-04 09:07:12 -05:00
case 'p':
case 'S':
case 's':
case 'U':
case 'u':
// new line triggers store() to have results available faster.
// saves ~500 millis() for the last FIELD
case '\n':
2022-02-25 06:16:35 -05:00
rv = store();
_field = c;
_value = 0;
break;
2022-03-04 09:07:12 -05:00
// drop fields of Y, and * command.
// reset parsing on separators of Y and * commands
case ':':
case ',':
_field = 0;
_value = 0;
break;
case ' ': // known separator
case '\r': // known return
break;
default: // catch all unknown characters, including glitches.
2022-02-25 06:16:35 -05:00
break;
}
return rv;
}
float C0ZIRParser::celsius()
{
return 0.1 * (_temperature_FILT - 1000.0);
}
//////////////////////////////////
//
// PRIVATE
//
uint8_t C0ZIRParser::store()
{
switch(_field)
{
// LIGHT related
case 'L':
_light = _value;
return _field;
case 'D':
_LED_FILT = _value;
return _field;
case 'd':
_LED_RAW = _value;
return _field;
case 'l':
_LED_MAX = _value;
return _field;
case 'o':
_LED_signal_FILT = _value;
return _field;
case 'O':
_LED_signal_RAW = _value;
return _field;
// TEMPERATURE & HUMIDITY
case 'V':
_temperature_RAW = _value;
return _field;
case 'v':
_temperature_Sensor = _value;
return _field;
case 'T':
_temperature_FILT = _value;
return _field;
case 'H':
_humidity = _value;
return _field;
// CO2 related
case 'z':
_CO2_RAW = _value;
return _field;
case 'Z':
_CO2_FILT = _value;
return _field;
// OTHER
case 'h':
_zeroPoint = _value;
return _field;
case 'a':
_samples = _value;
return _field;
case '.':
_PPM = _value;
return _field;
}
return 0;
}
2021-01-29 06:31:58 -05:00
// -- END OF FILE --
2022-02-25 06:16:35 -05:00