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

249 lines
7.2 KiB
C
Raw Normal View History

2020-11-27 05:10:47 -05:00
#pragma once
//
// FILE: Cozir.h
2022-03-04 09:07:12 -05:00
// VERSION: 0.3.5
// 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-02-03 11:20:20 -05:00
// http://forum.arduino.cc/index.php?topic=91467.0
//
// READ DATASHEET BEFORE USE OF THIS LIB !
//
2021-08-15 13:38:45 -04:00
#include "Arduino.h"
2021-08-15 13:38:45 -04:00
2022-03-04 09:07:12 -05:00
#define COZIR_LIB_VERSION (F("0.3.5"))
2021-08-15 13:38:45 -04:00
2022-02-25 06:16:35 -05:00
// OUTPUT FIELDS
// See datasheet for details.
// These defines can be OR-ed for the SetOutputFields command
2022-02-25 06:16:35 -05:00
//
#define CZR_UNKNOWN_2 0x8000 // returns P 00128 ?
#define CZR_UNKNOWN_1 0x4000 // returns E 00016 ?
2021-08-15 13:38:45 -04:00
#define CZR_LIGHT 0x2000
#define CZR_HUMIDITY 0x1000
#define CZR_FILTLED 0x0800
#define CZR_RAWLED 0x0400
#define CZR_MAXLED 0x0200
#define CZR_ZEROPOINT 0x0100
#define CZR_RAWTEMP 0x0080
#define CZR_FILTTEMP 0x0040
#define CZR_FILTLEDSIGNAL 0x0020
#define CZR_RAWLEDSIGNAL 0x0010
#define CZR_SENSTEMP 0x0008
#define CZR_FILTCO2 0x0004
#define CZR_RAWCO2 0x0002
#define CZR_NONE 0x0001
2022-02-21 13:40:59 -05:00
// factory default
#define CZR_DEFAULT (CZR_FILTCO2 | CZR_RAWCO2)
// easy default setting for streaming
2021-08-15 13:38:45 -04:00
#define CZR_HTC (CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2)
// not in datasheet for debug only
2021-08-15 13:38:45 -04:00
#define CZR_ALL 0x3FFE
// OPERATING MODES
2021-08-15 13:38:45 -04:00
#define CZR_COMMAND 0x00
#define CZR_STREAMING 0x01
#define CZR_POLLING 0x02
2020-11-27 05:10:47 -05:00
class COZIR
{
public:
2021-08-15 13:38:45 -04:00
COZIR(Stream * str);
void init(); // sets operatingMode to CZR_POLLING
bool isInitialized();
// warning: CZR_STREAMING is experimental, minimal tested.
2022-02-21 13:40:59 -05:00
bool setOperatingMode(uint8_t mode);
2021-08-15 13:38:45 -04:00
uint8_t getOperatingMode() { return _operatingMode; };
2022-02-21 13:40:59 -05:00
// POLLING MODE
2021-08-15 13:38:45 -04:00
float celsius();
float fahrenheit() { return (celsius() * 1.8) + 32; };
float kelvin() { return celsius() + 273.15; };
float humidity();
float light();
uint32_t CO2();
uint16_t getPPMFactor(); // P14 . command return 1, 10 or 100
2022-02-21 13:40:59 -05:00
// CALIBRATION
// read datasheet before use
2021-08-15 13:38:45 -04:00
uint16_t fineTuneZeroPoint(uint16_t v1, uint16_t v2);
uint16_t calibrateFreshAir();
uint16_t calibrateNitrogen();
uint16_t calibrateKnownGas(uint16_t value);
2022-02-21 13:40:59 -05:00
// WARNING: following 3 functions are NOT RECOMMENDED,
// read datasheet before use
// uint16_t calibrateManual(uint16_t value);
// uint16_t setSpanCalibrate(uint16_t value);
// uint16_t getSpanCalibrate();
2021-08-15 13:38:45 -04:00
2022-02-21 13:40:59 -05:00
// DIGIFILTER, use with care, read datasheet before use
// 32 = good default value,
// 1 = fast (noisy and responsive to fast changes)
// 255 = slow (smoothed to the max)
2021-08-15 13:38:45 -04:00
// 0 = special. details see datasheet
void setDigiFilter(uint8_t value);
uint8_t getDigiFilter();
2022-02-21 13:40:59 -05:00
// STREAMING MODE
2021-08-15 13:38:45 -04:00
void setOutputFields(uint16_t fields);
uint16_t getOutputFields() { return _outputFields; };
bool inOutputFields(uint16_t field);
void clearOutputFields() { setOutputFields(CZR_NONE); };
2022-02-21 13:40:59 -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.
// Answers can be over 100 bytes long!
2021-08-15 13:38:45 -04:00
void getRecentFields();
2022-02-21 13:40:59 -05:00
// EEPROM
// not all sensors support these.
2021-08-15 13:38:45 -04:00
void setAutoCalibrationPreload(uint16_t value);
uint16_t getAutoCalibrationPreload();
void setAutoCalibrationInterval(uint16_t value);
uint16_t getAutoCalibrationInterval();
void setAutoCalibrationOn();
void setAutoCalibrationOff();
bool getAutoCalibration();
void setAutoCalibrationBackgroundConcentration(uint16_t value);
uint16_t getAutoCalibrationBackgroundConcentration();
void setAmbientConcentration(uint16_t value);
uint16_t getAmbientConcentration();
void setBufferClearTime(uint16_t value);
uint16_t getBufferClearTime();
// TODO test EEPROM function first.
// void setEEPROMFactoryReset();
2022-02-21 13:40:59 -05:00
// META INFORMATION
// library does not parse the output (yet)
2021-08-15 13:38:45 -04:00
void getVersionSerial();
void getConfiguration();
///////////////////////////////////////////////
//
// SEMI PRIVATE FOR UNIT TESTING THEM
//
void _setEEPROM(uint8_t address, uint8_t value);
uint8_t _getEEPROM(uint8_t address);
void _setEEPROM2(uint8_t address, uint16_t value);
uint16_t _getEEPROM2(uint8_t address);
private:
2021-08-15 13:38:45 -04:00
Stream * _ser;
char _buffer[20];
2020-11-27 05:10:47 -05:00
2021-08-15 13:38:45 -04:00
uint32_t _initTimeStamp = 0;
uint16_t _ppmFactor = 1;
uint8_t _operatingMode = CZR_STREAMING;
uint16_t _outputFields = CZR_NONE;
void _command(const char* str);
uint32_t _request(const char* str);
};
2021-08-15 13:38:45 -04:00
2022-02-25 06:16:35 -05:00
////////////////////////////////////////////////////////////////////////////////
//
// C0ZIRParser
//
// used to parse the stream from a COZIR CO2 sensor.
// Note: one can comment fields / code not used to minimize footprint.
//
class C0ZIRParser
{
public:
C0ZIRParser();
2022-03-04 09:07:12 -05:00
// init resets all internal values
2022-02-25 06:16:35 -05:00
void init();
2022-03-04 09:07:12 -05:00
// resetParser only resets current FIELD (last values are kept).
void resetParser() { _field = 0; };
2022-02-25 06:16:35 -05:00
// returns field char if a field is completed, 0 otherwise.
uint8_t nextChar(char c);
// FETCH LAST READ VALUES
float celsius();
float fahrenheit() { return (celsius() * 1.8) + 32; };
float kelvin() { return celsius() + 273.15; };
float humidity() { return 0.1 * _humidity; };
uint16_t light() { return _light; };
uint16_t ledFilt() { return _LED_FILT; };
uint16_t ledRaw() { return _LED_RAW; };
uint16_t ledMax() { return _LED_MAX; };
uint16_t ledSignalFilt() { return _LED_signal_FILT; };
uint16_t ledSignalRaw() { return _LED_signal_RAW; };
uint16_t zeroPoint() { return _zeroPoint; };
uint16_t tempFilt() { return _temperature_FILT; };
uint16_t tempRaw() { return _temperature_RAW; };
uint16_t tempSensor() { return _temperature_Sensor; };
uint16_t CO2() { return _CO2_FILT; };
uint16_t CO2Raw() { return _CO2_RAW; };
uint16_t samples() { return _samples; };
uint16_t getPPMFactor() { return _PPM; }
private:
// FIELD ID character
uint16_t _light; // L
uint16_t _humidity; // H
uint16_t _LED_FILT; // D
uint16_t _LED_RAW; // d
uint16_t _LED_MAX; // l // el not one
uint16_t _zeroPoint; // h
uint16_t _temperature_RAW; // V
uint16_t _temperature_FILT; // T
uint16_t _LED_signal_FILT; // o // oo not zero
uint16_t _LED_signal_RAW; // O // oo not zero
uint16_t _temperature_Sensor; // v
uint16_t _CO2_FILT; // Z
uint16_t _CO2_RAW; // z
// not output fields sec but useful.
uint16_t _samples; // a
uint16_t _PPM; // . // point
// parsing helpers
2022-03-04 09:07:12 -05:00
uint32_t _value; // to build up the numeric value
uint8_t _field; // last read FIELD
2022-02-25 06:16:35 -05:00
2022-03-04 09:07:12 -05:00
// returns FIELD char if a FIELD is completed, 0 otherwise.
2022-02-25 06:16:35 -05:00
uint8_t store();
};
2014-12-07 05:39:59 -05:00
// -- END OF FILE --
2022-02-21 13:40:59 -05:00