|
| 1 | +// |
| 2 | +// FILE: Cozir.cpp |
| 3 | +// AUTHOR: DirtGambit & Rob Tillaart |
| 4 | +// VERSION: 0.1.01 |
| 5 | +// PURPOSE: library for COZIR range of sensors for Arduino |
| 6 | +// URL: |
| 7 | +// |
| 8 | +// READ DATASHEET BEFORE USE OF THIS LIB ! |
| 9 | +// |
| 10 | +// Released to the public domain |
| 11 | +// |
| 12 | + |
| 13 | +#include "Cozir.h" |
| 14 | +#include "NewSoftSerial.h" |
| 15 | + |
| 16 | +//////////////////////////////////////////////////////////// |
| 17 | +// |
| 18 | +// CONSTRUCTOR |
| 19 | +// |
| 20 | +COZIR::COZIR(NewSoftSerial& nss) : CZR_Serial(nss) |
| 21 | +{ |
| 22 | + // overide default streaming (takes to much perf |
| 23 | + SetOperatingMode(CZR_POLLING); |
| 24 | + // delay for initialization |
| 25 | + delay(1200); |
| 26 | +} |
| 27 | + |
| 28 | +//////////////////////////////////////////////////////////// |
| 29 | +// |
| 30 | +// OPERATING MODE |
| 31 | +// |
| 32 | +// note: use CZR_COMMAND to minimize power consumption |
| 33 | +// CZR_POLLING and CZR_STREAMING use an equally amount |
| 34 | +// of power as both sample continuously... |
| 35 | +// |
| 36 | +void COZIR::SetOperatingMode(uint8_t mode) |
| 37 | +{ |
| 38 | + sprintf(buffer, "K %u", mode); |
| 39 | + Command(buffer); |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +//////////////////////////////////////////////////////////// |
| 45 | +// |
| 46 | +// POLLING MODE |
| 47 | +// |
| 48 | +// you need to set the polling mode explicitely before |
| 49 | +// using these functions. SetOperatingMode(CZR_POLLING); |
| 50 | +// this is the default behaviour of this Class but |
| 51 | +// not of the sensor!! |
| 52 | +// |
| 53 | +float COZIR::Fahrenheit() |
| 54 | +{ |
| 55 | + return (Celsius() * 1.8) + 32; |
| 56 | +} |
| 57 | + |
| 58 | +float COZIR::Celsius() |
| 59 | +{ |
| 60 | + uint16_t rv = Request("T"); |
| 61 | + return 0.1 * rv; |
| 62 | +} |
| 63 | + |
| 64 | +float COZIR::Humidity() |
| 65 | +{ |
| 66 | + return 0.1 * Request("H"); |
| 67 | +} |
| 68 | + |
| 69 | +// TODO UNITS UNKNOWN |
| 70 | +float COZIR::Light() |
| 71 | +{ |
| 72 | + return 1.0 * Request("L"); |
| 73 | +} |
| 74 | + |
| 75 | +uint16_t COZIR::CO2() |
| 76 | +{ |
| 77 | + return Request("Z"); |
| 78 | +} |
| 79 | + |
| 80 | +// CALLIBRATION - USE THESE WITH CARE |
| 81 | +// use these only in pollingmode (on the Arduino) |
| 82 | + |
| 83 | +// FineTuneZeroPoint() |
| 84 | +// a reading of v1 will be reported as v2 |
| 85 | +// sort of mapping |
| 86 | +// check datasheet for detailed description |
| 87 | +uint16_t COZIR::FineTuneZeroPoint(uint16_t v1, uint16_t v2) |
| 88 | +{ |
| 89 | + sprintf(buffer, "F %u %u", v1, v2); |
| 90 | + return Request(buffer); |
| 91 | +} |
| 92 | + |
| 93 | +// mostly the default calibrator |
| 94 | +uint16_t COZIR::CalibrateFreshAir() |
| 95 | +{ |
| 96 | + return Request("G"); |
| 97 | +} |
| 98 | + |
| 99 | +uint16_t COZIR::CalibrateNitrogen() |
| 100 | +{ |
| 101 | + return Request("U"); |
| 102 | +} |
| 103 | + |
| 104 | +uint16_t COZIR::CalibrateKnownGas(uint16_t value) |
| 105 | +{ |
| 106 | + sprintf(buffer, "X %u", value); |
| 107 | + return Request(buffer); |
| 108 | +} |
| 109 | + |
| 110 | +// NOT RECOMMENDED, see datasheet |
| 111 | +uint16_t COZIR::CalibrateManual(uint16_t value) |
| 112 | +{ |
| 113 | + return 0; |
| 114 | + //sprintf(buffer, "u %u", value); |
| 115 | + //return Request(buffer); |
| 116 | +} |
| 117 | + |
| 118 | +// NOT RECOMMENDED, see datasheet |
| 119 | +uint16_t COZIR::SetSpanCalibrate(uint16_t value) |
| 120 | +{ |
| 121 | + return 0; |
| 122 | + //sprintf(buffer, "S %u", value); |
| 123 | + //return Request(buffer); |
| 124 | +} |
| 125 | + |
| 126 | +// NOT RECOMMENDED, see datasheet |
| 127 | +uint16_t COZIR::GetSpanCalibrate() |
| 128 | +{ |
| 129 | + return Request("s"); |
| 130 | +} |
| 131 | + |
| 132 | +// DIGIFILTER, use with care |
| 133 | +// default value = 32, |
| 134 | +// 1=fast (noisy) 255=slow (smoothed) |
| 135 | +// 0 = special. details see datasheet |
| 136 | +void COZIR::SetDigiFilter(uint8_t value) |
| 137 | +{ |
| 138 | + sprintf(buffer, "A %u", value); |
| 139 | + Command(buffer); |
| 140 | +} |
| 141 | + |
| 142 | +uint8_t COZIR::GetDigiFilter() |
| 143 | +{ |
| 144 | + return Request("a"); |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +//////////////////////////////////////////////////////////// |
| 150 | +// |
| 151 | +// STREAMING MODE |
| 152 | +// |
| 153 | +// outputfields should be OR-ed |
| 154 | +// e.g. SetOutputFields(CZR_HUMIDITY | CZR_RAWTEMP | CZR_RAWCO2); |
| 155 | +// |
| 156 | +// you need to set the STREAMING mode explicitely |
| 157 | +// SetOperatingMode(CZR_STREAMING); |
| 158 | +// |
| 159 | +// in STREAMING mode you must parse the output of serial yourself |
| 160 | +// |
| 161 | +void COZIR::SetOutputFields(uint16_t fields) |
| 162 | +{ |
| 163 | + sprintf(buffer, "M %u", fields); |
| 164 | + Command(buffer); |
| 165 | +} |
| 166 | + |
| 167 | +// For Arduino you must read the serial yourself as |
| 168 | +// the internal buffer of this Class cannot handle |
| 169 | +// large output - can be > 100 bytes!! |
| 170 | +void COZIR::GetRecentFields() |
| 171 | +{ |
| 172 | + Command("Q"); |
| 173 | +} |
| 174 | + |
| 175 | + |
| 176 | +//////////////////////////////////////////////////////////// |
| 177 | +// |
| 178 | +// EEPROM - USE WITH CARE |
| 179 | +// |
| 180 | +// SEE DATASHEET 7.2 EEPROM FOR DETAILS |
| 181 | +// |
| 182 | +// TODO |
| 183 | +// - defines for addresses |
| 184 | +// - do HILO values in one call |
| 185 | +// |
| 186 | +void COZIR::SetEEPROM(uint8_t address, uint8_t value) |
| 187 | +{ |
| 188 | + sprintf(buffer, "P %u %u", address, value); |
| 189 | + Command(buffer); |
| 190 | +} |
| 191 | + |
| 192 | +uint8_t COZIR::GetEEPROM(uint8_t address) |
| 193 | +{ |
| 194 | + sprintf(buffer, "p %u", address); |
| 195 | + return Request(buffer); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | +//////////////////////////////////////////////////////////// |
| 201 | +// |
| 202 | +// COMMAND MODE |
| 203 | +// |
| 204 | +// read serial yourself |
| 205 | +// |
| 206 | +void COZIR::GetVersionSerial() |
| 207 | +{ |
| 208 | + Command("Y"); |
| 209 | +} |
| 210 | + |
| 211 | +void COZIR::GetConfiguration() |
| 212 | +{ |
| 213 | + Command("*"); |
| 214 | +} |
| 215 | + |
| 216 | +///////////////////////////////////////////////////////// |
| 217 | +// PRIVATE |
| 218 | + |
| 219 | +void COZIR::Command(char* s) |
| 220 | +{ |
| 221 | + CZR_Serial.print(s); |
| 222 | + CZR_Serial.print("\r\n"); |
| 223 | +} |
| 224 | + |
| 225 | +uint16_t COZIR::Request(char* s) |
| 226 | +{ |
| 227 | + Command(s); |
| 228 | + // empty buffer |
| 229 | + buffer[0] = '\0'; |
| 230 | + // read answer; there may be a 100ms delay! |
| 231 | + // TODO: PROPER TIMEOUT CODE. |
| 232 | + delay(100); |
| 233 | + int idx = 0; |
| 234 | + while(CZR_Serial.available()) |
| 235 | + { |
| 236 | +buffer[idx++] = CZR_Serial.read(); |
| 237 | + } |
| 238 | + buffer[idx] = '\0'; |
| 239 | + return atoi(&buffer[1]); |
| 240 | +} |
0 commit comments