+ 0.1.01 version of Cozir (archive purpose)

This commit is contained in:
rob tillaart 2014-11-16 16:00:08 +01:00
parent 16a464af0d
commit 12ce1e2710
3 changed files with 373 additions and 0 deletions

240
libraries/Cozir/cozir.cpp Normal file
View File

@ -0,0 +1,240 @@
//
// FILE: Cozir.cpp
// AUTHOR: DirtGambit & Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: library for COZIR range of sensors for Arduino
// URL:
//
// READ DATASHEET BEFORE USE OF THIS LIB !
//
// Released to the public domain
//
#include "Cozir.h"
#include "NewSoftSerial.h"
////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
COZIR::COZIR(NewSoftSerial& nss) : CZR_Serial(nss)
{
// overide default streaming (takes to much perf
SetOperatingMode(CZR_POLLING);
// delay for initialization
delay(1200);
}
////////////////////////////////////////////////////////////
//
// 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...
//
void COZIR::SetOperatingMode(uint8_t mode)
{
sprintf(buffer, "K %u", mode);
Command(buffer);
}
////////////////////////////////////////////////////////////
//
// POLLING MODE
//
// you need to set the polling mode explicitely before
// using these functions. SetOperatingMode(CZR_POLLING);
// this is the default behaviour of this Class but
// not of the sensor!!
//
float COZIR::Fahrenheit()
{
return (Celsius() * 1.8) + 32;
}
float COZIR::Celsius()
{
uint16_t rv = Request("T");
return 0.1 * rv;
}
float COZIR::Humidity()
{
return 0.1 * Request("H");
}
// TODO UNITS UNKNOWN
float COZIR::Light()
{
return 1.0 * Request("L");
}
uint16_t COZIR::CO2()
{
return Request("Z");
}
// CALLIBRATION - USE THESE WITH CARE
// use these only in pollingmode (on the Arduino)
// FineTuneZeroPoint()
// a reading of v1 will be reported as v2
// sort of mapping
// check datasheet for detailed description
uint16_t COZIR::FineTuneZeroPoint(uint16_t v1, uint16_t v2)
{
sprintf(buffer, "F %u %u", v1, v2);
return Request(buffer);
}
// mostly the default calibrator
uint16_t COZIR::CalibrateFreshAir()
{
return Request("G");
}
uint16_t COZIR::CalibrateNitrogen()
{
return Request("U");
}
uint16_t COZIR::CalibrateKnownGas(uint16_t value)
{
sprintf(buffer, "X %u", value);
return Request(buffer);
}
// NOT RECOMMENDED, see datasheet
uint16_t COZIR::CalibrateManual(uint16_t value)
{
return 0;
//sprintf(buffer, "u %u", value);
//return Request(buffer);
}
// NOT RECOMMENDED, see datasheet
uint16_t COZIR::SetSpanCalibrate(uint16_t value)
{
return 0;
//sprintf(buffer, "S %u", value);
//return Request(buffer);
}
// NOT RECOMMENDED, see datasheet
uint16_t COZIR::GetSpanCalibrate()
{
return Request("s");
}
// DIGIFILTER, use with care
// default value = 32,
// 1=fast (noisy) 255=slow (smoothed)
// 0 = special. details see datasheet
void COZIR::SetDigiFilter(uint8_t value)
{
sprintf(buffer, "A %u", value);
Command(buffer);
}
uint8_t COZIR::GetDigiFilter()
{
return Request("a");
}
////////////////////////////////////////////////////////////
//
// STREAMING MODE
//
// outputfields should be OR-ed
// e.g. SetOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2);
//
// you need to set the STREAMING mode explicitely
// SetOperatingMode(CZR_STREAMING);
//
// in STREAMING mode you must parse the output of serial yourself
//
void COZIR::SetOutputFields(uint16_t fields)
{
sprintf(buffer, "M %u", fields);
Command(buffer);
}
// For Arduino you must read the serial yourself as
// the internal buffer of this Class cannot handle
// large output - can be > 100 bytes!!
void COZIR::GetRecentFields()
{
Command("Q");
}
////////////////////////////////////////////////////////////
//
// EEPROM - USE WITH CARE
//
// SEE DATASHEET 7.2 EEPROM FOR DETAILS
//
// TODO
// - defines for addresses
// - do HILO values in one call
//
void COZIR::SetEEPROM(uint8_t address, uint8_t value)
{
sprintf(buffer, "P %u %u", address, value);
Command(buffer);
}
uint8_t COZIR::GetEEPROM(uint8_t address)
{
sprintf(buffer, "p %u", address);
return Request(buffer);
}
////////////////////////////////////////////////////////////
//
// COMMAND MODE
//
// read serial yourself
//
void COZIR::GetVersionSerial()
{
Command("Y");
}
void COZIR::GetConfiguration()
{
Command("*");
}
/////////////////////////////////////////////////////////
// PRIVATE
void COZIR::Command(char* s)
{
CZR_Serial.print(s);
CZR_Serial.print("\r\n");
}
uint16_t COZIR::Request(char* s)
{
Command(s);
// empty buffer
buffer[0] = '\0';
// read answer; there may be a 100ms delay!
// TODO: PROPER TIMEOUT CODE.
delay(100);
int idx = 0;
while(CZR_Serial.available())
{
buffer[idx++] = CZR_Serial.read();
}
buffer[idx] = '\0';
return atoi(&buffer[1]);
}

94
libraries/Cozir/cozir.h Normal file
View File

@ -0,0 +1,94 @@
//
// FILE: Cozir.h
// AUTHOR: DirtGambit & Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: library for COZIR range of sensors for Arduino
// URL:
//
// READ DATASHEET BEFORE USE OF THIS LIB !
//
// Released to the public domain
//
#ifndef Cozir_h
#define Cozir_h
#include "NewSoftSerial.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define COZIR_LIB_VERSION 0.1.01
// OUTPUTFIELDS
// See datasheet for details.
// These defines can be OR-ed for the SetOutputFields command
#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
// easy default setting for streaming
#define CZR_HTC (CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2)
// not in datasheet for debug only
#define CZR_ALL 0x3FFF
// OPERATING MODES
#define CZR_COMMAND 0x00
#define CZR_STREAMING 0x01
#define CZR_POLLING 0x02
class COZIR
{
public:
COZIR(NewSoftSerial&);
void SetOperatingMode(uint8_t mode);
float Celsius();
float Fahrenheit();
float Humidity();
float Light();
uint16_t CO2();
uint16_t FineTuneZeroPoint(uint16_t , uint16_t);
uint16_t CalibrateFreshAir();
uint16_t CalibrateNitrogen();
uint16_t CalibrateKnownGas(uint16_t );
uint16_t CalibrateManual(uint16_t );
uint16_t SetSpanCalibrate(uint16_t );
uint16_t GetSpanCalibrate();
void SetDigiFilter(uint8_t );
uint8_t GetDigiFilter();
void SetOutputFields(uint16_t );
void GetRecentFields();
void SetEEPROM(uint8_t , uint8_t );
uint8_t GetEEPROM(uint8_t );
void GetVersionSerial();
void GetConfiguration();
private:
NewSoftSerial& CZR_Serial;
char buffer[20];
void Command(char* );
uint16_t Request(char* );
};
#endif

View File

@ -0,0 +1,39 @@
#include <cozir.h>
#if defined(ARDUINO) && ARDUINO >= 100
#include <SoftwareSerial.h>
SoftwareSerial nss(3,2);
#else
#include <NewSoftSerial.h>
NewSoftSerial nss(3,2);
#endif
COZIR czr(nss);
void setup()
{
Serial.begin(9600);
Serial.println("Setup");
delay(1000);
}
void loop()
{
Serial.println("Loop");
float t = czr.Celsius();
float f = czr.Fahrenheit();
float h = czr.Humidity();
uint16_t c = czr.CO2();
Serial.print("Celcius = ");
Serial.println(t);
Serial.print("Fahrenheit = ");
Serial.println(f);
Serial.print("Humidity = ");
Serial.println(h);
Serial.print("CO2 = ");
Serial.println(c);
delay(3000);
}