diff --git a/esp32test/Esp32DeepSleepTest/include/DS2438.h b/esp32test/Esp32DeepSleepTest/include/DS2438.h index 062822f..9529d93 100644 --- a/esp32test/Esp32DeepSleepTest/include/DS2438.h +++ b/esp32test/Esp32DeepSleepTest/include/DS2438.h @@ -28,7 +28,9 @@ #define DS2438_COPY_SCRATCHPAD_COMMAND 0x48 #define DS2438_READ_SCRATCHPAD_COMMAND 0xbe #define DS2438_RECALL_MEMORY_COMMAND 0xb8 -#define DS2438_PAGE_0 0x00 + +#define PAGE_MIN 0 +#define PAGE_MAX 7 #define DS2438_CHA 0 #define DS2438_CHB 1 @@ -70,8 +72,8 @@ class DS2438 { boolean _error; boolean startConversion(int channel, boolean doTemperature); boolean selectChannel(int channel); - void writePageZero(uint8_t *data); - boolean readPageZero(uint8_t *data); + void writePage(int page, uint8_t *data); + boolean readPage(int page, uint8_t *data); }; #endif \ No newline at end of file diff --git a/esp32test/Esp32DeepSleepTest/src/DS2438.cpp b/esp32test/Esp32DeepSleepTest/src/DS2438.cpp index a2abf59..1fafb8e 100644 --- a/esp32test/Esp32DeepSleepTest/src/DS2438.cpp +++ b/esp32test/Esp32DeepSleepTest/src/DS2438.cpp @@ -83,7 +83,7 @@ void DS2438::update() { Serial.println("Error starting temp conversion ds2438 channel a"); return; } - if (!readPageZero(data)){ + if (!readPage(0, data)){ Serial.println("Error reading zero page ds2438 channel a"); return; } @@ -101,7 +101,7 @@ void DS2438::update() { Serial.println("Error starting temp conversion channel b ds2438"); return; } - if (!readPageZero(data)){ + if (!readPage(0, data)){ Serial.println("Error reading zero page ds2438 channel b"); return; } @@ -168,43 +168,51 @@ boolean DS2438::selectChannel(int channel) { return false; } uint8_t data[9]; - if (readPageZero(data)) { + if (readPage(0, data)) { if (channel == DS2438_CHB){ data[0] = data[0] | 0x08; } else { data[0] = data[0] & 0xf7; } - writePageZero(data); + writePage(0, data); return true; } Serial.println("Could not read page zero data"); return false; } -void DS2438::writePageZero(uint8_t *data) { +void DS2438::writePage(int page, uint8_t *data) { _ow->reset(); _ow->select(_address); _ow->write(DS2438_WRITE_SCRATCHPAD_COMMAND, 0); - _ow->write(DS2438_PAGE_0, 0); + if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { + _ow->write(page, 0); + } else { + return; + } for (int i = 0; i < 8; i++){ _ow->write(data[i], 0); } _ow->reset(); _ow->select(_address); _ow->write(DS2438_COPY_SCRATCHPAD_COMMAND, 0); - _ow->write(DS2438_PAGE_0, 0); + _ow->write(page, 0); } -boolean DS2438::readPageZero(uint8_t *data) { +boolean DS2438::readPage(int page, uint8_t *data) { _ow->reset(); _ow->select(_address); _ow->write(DS2438_RECALL_MEMORY_COMMAND, 0); - _ow->write(DS2438_PAGE_0, 0); + if ((page >= PAGE_MIN) && (page <= PAGE_MAX)) { + _ow->write(page, 0); + } else { + return false; + } _ow->reset(); _ow->select(_address); _ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0); - _ow->write(DS2438_PAGE_0, 0); + _ow->write(page, 0); for (int i = 0; i < 9; i++){ data[i] = _ow->read(); }