123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053 |
-
- #include "DallasTemperature.h"
- #if ARDUINO >= 100
- #include "Arduino.h"
- #else
- extern "C" {
- #include "WConstants.h"
- }
- #endif
- #define STARTCONVO 0x44
- #define COPYSCRATCH 0x48
- #define READSCRATCH 0xBE
- #define WRITESCRATCH 0x4E
- #define RECALLSCRATCH 0xB8
- #define READPOWERSUPPLY 0xB4
- #define ALARMSEARCH 0xEC
- #define TEMP_LSB 0
- #define TEMP_MSB 1
- #define HIGH_ALARM_TEMP 2
- #define LOW_ALARM_TEMP 3
- #define CONFIGURATION 4
- #define INTERNAL_BYTE 5
- #define COUNT_REMAIN 6
- #define COUNT_PER_C 7
- #define SCRATCHPAD_CRC 8
- #define DSROM_FAMILY 0
- #define DSROM_CRC 7
- #define TEMP_9_BIT 0x1F
- #define TEMP_10_BIT 0x3F
- #define TEMP_11_BIT 0x5F
- #define TEMP_12_BIT 0x7F
- #define MAX_CONVERSION_TIMEOUT 750
- #define NO_ALARM_HANDLER ((AlarmHandler *)0)
- DallasTemperature::DallasTemperature() {
- #if REQUIRESALARMS
- setAlarmHandler(NO_ALARM_HANDLER);
- #endif
- useExternalPullup = false;
- }
- DallasTemperature::DallasTemperature(OneWire* _oneWire) : DallasTemperature() {
- setOneWire(_oneWire);
- }
- bool DallasTemperature::validFamily(const uint8_t* deviceAddress) {
- switch (deviceAddress[DSROM_FAMILY]) {
- case DS18S20MODEL:
- case DS18B20MODEL:
- case DS1822MODEL:
- case DS1825MODEL:
- case DS28EA00MODEL:
- return true;
- default:
- return false;
- }
- }
- DallasTemperature::DallasTemperature(OneWire* _oneWire, uint8_t _pullupPin) : DallasTemperature(_oneWire) {
- setPullupPin(_pullupPin);
- }
- void DallasTemperature::setPullupPin(uint8_t _pullupPin) {
- useExternalPullup = true;
- pullupPin = _pullupPin;
- pinMode(pullupPin, OUTPUT);
- deactivateExternalPullup();
- }
- void DallasTemperature::setOneWire(OneWire* _oneWire) {
- _wire = _oneWire;
- devices = 0;
- ds18Count = 0;
- parasite = false;
- bitResolution = 9;
- waitForConversion = true;
- checkForConversion = true;
- autoSaveScratchPad = true;
- }
- void DallasTemperature::begin(void) {
- DeviceAddress deviceAddress;
- _wire->reset_search();
- devices = 0;
- ds18Count = 0;
- while (_wire->search(deviceAddress)) {
- if (validAddress(deviceAddress)) {
- devices++;
- if (validFamily(deviceAddress)) {
- ds18Count++;
- if (!parasite && readPowerSupply(deviceAddress))
- parasite = true;
- parasite = true;
- uint8_t b = getResolution(deviceAddress);
- if (b > bitResolution) bitResolution = b;
- }
- }
- }
- }
- uint8_t DallasTemperature::getDeviceCount(void) {
- return devices;
- }
- uint8_t DallasTemperature::getDS18Count(void) {
- return ds18Count;
- }
- bool DallasTemperature::validAddress(const uint8_t* deviceAddress) {
- return (_wire->crc8(deviceAddress, 7) == deviceAddress[DSROM_CRC]);
- }
- bool DallasTemperature::getAddress(uint8_t* deviceAddress, uint8_t index) {
- uint8_t depth = 0;
- _wire->reset_search();
- while (depth <= index && _wire->search(deviceAddress)) {
- if (depth == index && validAddress(deviceAddress))
- return true;
- depth++;
- }
- return false;
- }
- bool DallasTemperature::isConnected(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- return isConnected(deviceAddress, scratchPad);
- }
- bool DallasTemperature::isConnected(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
- bool b = readScratchPad(deviceAddress, scratchPad);
- return b && !isAllZeros(scratchPad) && (_wire->crc8(scratchPad, 8) == scratchPad[SCRATCHPAD_CRC]);
- }
- bool DallasTemperature::readScratchPad(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
-
- int b = _wire->reset();
- if (b == 0)
- return false;
- _wire->select(deviceAddress);
- _wire->write(READSCRATCH);
-
-
-
-
-
-
-
-
-
-
-
-
-
- for (uint8_t i = 0; i < 9; i++) {
- scratchPad[i] = _wire->read();
- }
- b = _wire->reset();
- return (b == 1);
- }
- void DallasTemperature::writeScratchPad(const uint8_t* deviceAddress,
- const uint8_t* scratchPad) {
- _wire->reset();
- _wire->select(deviceAddress);
- _wire->write(WRITESCRATCH);
- _wire->write(scratchPad[HIGH_ALARM_TEMP]);
- _wire->write(scratchPad[LOW_ALARM_TEMP]);
-
- if (deviceAddress[DSROM_FAMILY] != DS18S20MODEL)
- _wire->write(scratchPad[CONFIGURATION]);
- if (autoSaveScratchPad)
- saveScratchPad(deviceAddress);
- else
- _wire->reset();
- }
- bool DallasTemperature::readPowerSupply(const uint8_t* deviceAddress)
- {
- bool parasiteMode = false;
- _wire->reset();
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
- _wire->write(READPOWERSUPPLY);
- if (_wire->read_bit() == 0)
- parasiteMode = true;
- _wire->reset();
- return parasiteMode;
- }
- void DallasTemperature::setResolution(uint8_t newResolution) {
- bitResolution = constrain(newResolution, 9, 12);
- DeviceAddress deviceAddress;
- for (uint8_t i = 0; i < devices; i++) {
- getAddress(deviceAddress, i);
- setResolution(deviceAddress, bitResolution, true);
- }
- }
- bool DallasTemperature::setResolution(const uint8_t* deviceAddress,
- uint8_t newResolution, bool skipGlobalBitResolutionCalculation) {
- bool success = false;
-
- if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL)
- {
- success = true;
- }
- else
- {
-
- newResolution = constrain(newResolution, 9, 12);
- uint8_t newValue = 0;
- ScratchPad scratchPad;
-
- if (isConnected(deviceAddress, scratchPad))
- {
- switch (newResolution) {
- case 12:
- newValue = TEMP_12_BIT;
- break;
- case 11:
- newValue = TEMP_11_BIT;
- break;
- case 10:
- newValue = TEMP_10_BIT;
- break;
- case 9:
- default:
- newValue = TEMP_9_BIT;
- break;
- }
-
- if (scratchPad[CONFIGURATION] != newValue)
- {
- scratchPad[CONFIGURATION] = newValue;
- writeScratchPad(deviceAddress, scratchPad);
- }
-
- success = true;
- }
- }
-
- if (skipGlobalBitResolutionCalculation == false)
- {
- bitResolution = newResolution;
- if (devices > 1)
- {
- for (uint8_t i = 0; i < devices; i++)
- {
- if (bitResolution == 12) break;
- DeviceAddress deviceAddr;
- getAddress(deviceAddr, i);
- uint8_t b = getResolution(deviceAddr);
- if (b > bitResolution) bitResolution = b;
- }
- }
- }
- return success;
- }
- uint8_t DallasTemperature::getResolution() {
- return bitResolution;
- }
- uint8_t DallasTemperature::getResolution(const uint8_t* deviceAddress) {
-
- if (deviceAddress[DSROM_FAMILY] == DS18S20MODEL)
- return 12;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- switch (scratchPad[CONFIGURATION]) {
- case TEMP_12_BIT:
- return 12;
- case TEMP_11_BIT:
- return 11;
- case TEMP_10_BIT:
- return 10;
- case TEMP_9_BIT:
- return 9;
- }
- }
- return 0;
- }
- void DallasTemperature::setWaitForConversion(bool flag) {
- waitForConversion = flag;
- }
- bool DallasTemperature::getWaitForConversion() {
- return waitForConversion;
- }
- void DallasTemperature::setCheckForConversion(bool flag) {
- checkForConversion = flag;
- }
- bool DallasTemperature::getCheckForConversion() {
- return checkForConversion;
- }
- bool DallasTemperature::isConversionComplete() {
- uint8_t b = _wire->read_bit();
- return (b == 1);
- }
- void DallasTemperature::requestTemperatures() {
- _wire->reset();
- _wire->skip();
- _wire->write(STARTCONVO, parasite);
-
- if (!waitForConversion)
- return;
- blockTillConversionComplete(bitResolution);
- }
- bool DallasTemperature::requestTemperaturesByAddress(
- const uint8_t* deviceAddress) {
- uint8_t bitResolution = getResolution(deviceAddress);
- if (bitResolution == 0) {
- return false;
- }
- ESP_LOGI(TAG,"BIT Resolution %i",bitResolution);
- ESP_LOGI(TAG,"Parasite %i",(parasite?1:0));
- _wire->reset();
- _wire->select(deviceAddress);
- _wire->write(STARTCONVO, parasite);
-
- if (!waitForConversion)
- return true;
- blockTillConversionComplete(bitResolution);
- return true;
- }
- void DallasTemperature::blockTillConversionComplete(uint8_t bitResolution) {
- if (checkForConversion && !parasite) {
- unsigned long start = millis();
- while (!isConversionComplete() && (millis() - start < MAX_CONVERSION_TIMEOUT ))
- yield();
- } else {
- unsigned long delms = millisToWaitForConversion(bitResolution);
- activateExternalPullup();
- delay(delms);
- deactivateExternalPullup();
- }
- }
- uint16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
- switch (bitResolution) {
- case 9:
- return 94;
- case 10:
- return 188;
- case 11:
- return 375;
- default:
- return 750;
- }
- }
- uint16_t DallasTemperature::millisToWaitForConversion() {
- return millisToWaitForConversion(bitResolution);
- }
- bool DallasTemperature::saveScratchPadByIndex(uint8_t deviceIndex) {
-
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) return false;
-
- return saveScratchPad(deviceAddress);
-
- }
- bool DallasTemperature::saveScratchPad(const uint8_t* deviceAddress) {
-
- if (_wire->reset() == 0)
- return false;
-
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
-
- _wire->write(COPYSCRATCH,parasite);
-
-
- if (!parasite) {
- delay(20);
- } else {
- activateExternalPullup();
- delay(20);
- deactivateExternalPullup();
- }
-
- return _wire->reset() == 1;
-
- }
- bool DallasTemperature::recallScratchPadByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) return false;
-
- return recallScratchPad(deviceAddress);
- }
- bool DallasTemperature::recallScratchPad(const uint8_t* deviceAddress) {
-
- if (_wire->reset() == 0)
- return false;
-
- if (deviceAddress == nullptr)
- _wire->skip();
- else
- _wire->select(deviceAddress);
-
- _wire->write(RECALLSCRATCH,parasite);
-
- unsigned long start = millis();
- while (_wire->read_bit() == 0) {
-
- if (millis() - start > 20) return false;
- yield();
- }
-
- return _wire->reset() == 1;
-
- }
- void DallasTemperature::setAutoSaveScratchPad(bool flag) {
- autoSaveScratchPad = flag;
- }
- bool DallasTemperature::getAutoSaveScratchPad() {
- return autoSaveScratchPad;
- }
- void DallasTemperature::activateExternalPullup() {
- if(useExternalPullup)
- digitalWrite(pullupPin, LOW);
- }
- void DallasTemperature::deactivateExternalPullup() {
- if(useExternalPullup)
- digitalWrite(pullupPin, HIGH);
- }
- bool DallasTemperature::requestTemperaturesByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- return requestTemperaturesByAddress(deviceAddress);
- }
- float DallasTemperature::getTempCByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) {
- return DEVICE_DISCONNECTED_C;
- }
- return getTempC((uint8_t*) deviceAddress);
- }
- float DallasTemperature::getTempFByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- if (!getAddress(deviceAddress, deviceIndex)) {
- return DEVICE_DISCONNECTED_F;
- }
- return getTempF((uint8_t*) deviceAddress);
- }
- int16_t DallasTemperature::calculateTemperature(const uint8_t* deviceAddress,
- uint8_t* scratchPad) {
- int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11)
- | (((int16_t) scratchPad[TEMP_LSB]) << 3);
-
- if ((deviceAddress[DSROM_FAMILY] == DS18S20MODEL) && (scratchPad[COUNT_PER_C] != 0)) {
- fpTemperature = ((fpTemperature & 0xfff0) << 3) - 32
- + (((scratchPad[COUNT_PER_C] - scratchPad[COUNT_REMAIN]) << 7)
- / scratchPad[COUNT_PER_C]);
- }
- return fpTemperature;
- }
- int16_t DallasTemperature::getTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return calculateTemperature(deviceAddress, scratchPad);
- return DEVICE_DISCONNECTED_RAW;
- }
- float DallasTemperature::getTempC(const uint8_t* deviceAddress) {
- return rawToCelsius(getTemp(deviceAddress));
- }
- float DallasTemperature::getTempF(const uint8_t* deviceAddress) {
- return rawToFahrenheit(getTemp(deviceAddress));
- }
- bool DallasTemperature::isParasitePowerMode(void) {
- return parasite;
- }
- void DallasTemperature::setUserData(const uint8_t* deviceAddress,
- int16_t data) {
-
- if (getUserData(deviceAddress) == data)
- return;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[HIGH_ALARM_TEMP] = data >> 8;
- scratchPad[LOW_ALARM_TEMP] = data & 255;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- int16_t DallasTemperature::getUserData(const uint8_t* deviceAddress) {
- int16_t data = 0;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- data = scratchPad[HIGH_ALARM_TEMP] << 8;
- data += scratchPad[LOW_ALARM_TEMP];
- }
- return data;
- }
- int16_t DallasTemperature::getUserDataByIndex(uint8_t deviceIndex) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- return getUserData((uint8_t*) deviceAddress);
- }
- void DallasTemperature::setUserDataByIndex(uint8_t deviceIndex, int16_t data) {
- DeviceAddress deviceAddress;
- getAddress(deviceAddress, deviceIndex);
- setUserData((uint8_t*) deviceAddress, data);
- }
- float DallasTemperature::toFahrenheit(float celsius) {
- return (celsius * 1.8f) + 32.0f;
- }
- float DallasTemperature::toCelsius(float fahrenheit) {
- return (fahrenheit - 32.0f) * 0.555555556f;
- }
- float DallasTemperature::rawToCelsius(int16_t raw) {
- if (raw <= DEVICE_DISCONNECTED_RAW)
- return DEVICE_DISCONNECTED_C;
-
- return (float) raw * 0.0078125f;
- }
- int16_t DallasTemperature::celsiusToRaw(float celsius) {
- return static_cast<uint16_t>( celsius * 128.f );
- }
- float DallasTemperature::rawToFahrenheit(int16_t raw) {
- if (raw <= DEVICE_DISCONNECTED_RAW)
- return DEVICE_DISCONNECTED_F;
-
-
- return ((float) raw * 0.0140625f) + 32.0f;
- }
- bool DallasTemperature::isAllZeros(const uint8_t * const scratchPad, const size_t length) {
- for (size_t i = 0; i < length; i++) {
- if (scratchPad[i] != 0) {
- return false;
- }
- }
- return true;
- }
- #if REQUIRESALARMS
- void DallasTemperature::setHighAlarmTemp(const uint8_t* deviceAddress,
- int8_t celsius) {
-
- if (getHighAlarmTemp(deviceAddress) == celsius)
- return;
-
- if (celsius > 125)
- celsius = 125;
- else if (celsius < -55)
- celsius = -55;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[HIGH_ALARM_TEMP] = (uint8_t) celsius;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- void DallasTemperature::setLowAlarmTemp(const uint8_t* deviceAddress,
- int8_t celsius) {
-
- if (getLowAlarmTemp(deviceAddress) == celsius)
- return;
-
- if (celsius > 125)
- celsius = 125;
- else if (celsius < -55)
- celsius = -55;
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- scratchPad[LOW_ALARM_TEMP] = (uint8_t) celsius;
- writeScratchPad(deviceAddress, scratchPad);
- }
- }
- int8_t DallasTemperature::getHighAlarmTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return (int8_t) scratchPad[HIGH_ALARM_TEMP];
- return DEVICE_DISCONNECTED_C;
- }
- int8_t DallasTemperature::getLowAlarmTemp(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad))
- return (int8_t) scratchPad[LOW_ALARM_TEMP];
- return DEVICE_DISCONNECTED_C;
- }
- void DallasTemperature::resetAlarmSearch() {
- alarmSearchJunction = -1;
- alarmSearchExhausted = 0;
- for (uint8_t i = 0; i < 7; i++) {
- alarmSearchAddress[i] = 0;
- }
- }
- bool DallasTemperature::alarmSearch(uint8_t* newAddr) {
- uint8_t i;
- int8_t lastJunction = -1;
- uint8_t done = 1;
- if (alarmSearchExhausted)
- return false;
- if (!_wire->reset())
- return false;
-
- _wire->write(0xEC, 0);
- for (i = 0; i < 64; i++) {
- uint8_t a = _wire->read_bit();
- uint8_t nota = _wire->read_bit();
- uint8_t ibyte = i / 8;
- uint8_t ibit = 1 << (i & 7);
-
-
- if (a && nota)
- return false;
- if (!a && !nota) {
- if (i == alarmSearchJunction) {
-
- a = 1;
- alarmSearchJunction = lastJunction;
- } else if (i < alarmSearchJunction) {
-
- if (alarmSearchAddress[ibyte] & ibit) {
- a = 1;
- } else {
-
- a = 0;
- done = 0;
- lastJunction = i;
- }
- } else {
-
- a = 0;
- alarmSearchJunction = i;
- done = 0;
- }
-
-
- }
- if (a)
- alarmSearchAddress[ibyte] |= ibit;
- else
- alarmSearchAddress[ibyte] &= ~ibit;
- _wire->write_bit(a);
- }
- if (done)
- alarmSearchExhausted = 1;
- for (i = 0; i < 8; i++)
- newAddr[i] = alarmSearchAddress[i];
- return true;
- }
- bool DallasTemperature::hasAlarm(const uint8_t* deviceAddress) {
- ScratchPad scratchPad;
- if (isConnected(deviceAddress, scratchPad)) {
- int8_t temp = calculateTemperature(deviceAddress, scratchPad) >> 7;
-
- if (temp <= (int8_t) scratchPad[LOW_ALARM_TEMP])
- return true;
-
- if (temp >= (int8_t) scratchPad[HIGH_ALARM_TEMP])
- return true;
- }
-
- return false;
- }
- bool DallasTemperature::hasAlarm(void) {
- DeviceAddress deviceAddress;
- resetAlarmSearch();
- return alarmSearch(deviceAddress);
- }
- void DallasTemperature::processAlarms(void) {
- if (!hasAlarmHandler())
- {
- return;
- }
- resetAlarmSearch();
- DeviceAddress alarmAddr;
- while (alarmSearch(alarmAddr)) {
- if (validAddress(alarmAddr)) {
- _AlarmHandler(alarmAddr);
- }
- }
- }
- void DallasTemperature::setAlarmHandler(const AlarmHandler *handler) {
- _AlarmHandler = handler;
- }
- bool DallasTemperature::hasAlarmHandler()
- {
- return _AlarmHandler != NO_ALARM_HANDLER;
- }
- #endif
- #if REQUIRESNEW
- void* DallasTemperature::operator new(unsigned int size) {
- void * p;
- p = malloc(size);
- memset((DallasTemperature*)p,0,size);
-
- return (DallasTemperature*) p;
- }
- void DallasTemperature::operator delete(void* p) {
- DallasTemperature* pNss = (DallasTemperature*) p;
- pNss->~DallasTemperature();
- free(p);
- }
- #endif
|