Refactored page access: added parameter to specify the page to write or read

This commit is contained in:
Ollo 2021-02-04 20:28:28 +01:00
parent 653d962cdf
commit 11cde021a2
2 changed files with 23 additions and 13 deletions

View File

@ -28,7 +28,9 @@
#define DS2438_COPY_SCRATCHPAD_COMMAND 0x48 #define DS2438_COPY_SCRATCHPAD_COMMAND 0x48
#define DS2438_READ_SCRATCHPAD_COMMAND 0xbe #define DS2438_READ_SCRATCHPAD_COMMAND 0xbe
#define DS2438_RECALL_MEMORY_COMMAND 0xb8 #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_CHA 0
#define DS2438_CHB 1 #define DS2438_CHB 1
@ -70,8 +72,8 @@ class DS2438 {
boolean _error; boolean _error;
boolean startConversion(int channel, boolean doTemperature); boolean startConversion(int channel, boolean doTemperature);
boolean selectChannel(int channel); boolean selectChannel(int channel);
void writePageZero(uint8_t *data); void writePage(int page, uint8_t *data);
boolean readPageZero(uint8_t *data); boolean readPage(int page, uint8_t *data);
}; };
#endif #endif

View File

@ -83,7 +83,7 @@ void DS2438::update() {
Serial.println("Error starting temp conversion ds2438 channel a"); Serial.println("Error starting temp conversion ds2438 channel a");
return; return;
} }
if (!readPageZero(data)){ if (!readPage(0, data)){
Serial.println("Error reading zero page ds2438 channel a"); Serial.println("Error reading zero page ds2438 channel a");
return; return;
} }
@ -101,7 +101,7 @@ void DS2438::update() {
Serial.println("Error starting temp conversion channel b ds2438"); Serial.println("Error starting temp conversion channel b ds2438");
return; return;
} }
if (!readPageZero(data)){ if (!readPage(0, data)){
Serial.println("Error reading zero page ds2438 channel b"); Serial.println("Error reading zero page ds2438 channel b");
return; return;
} }
@ -168,43 +168,51 @@ boolean DS2438::selectChannel(int channel) {
return false; return false;
} }
uint8_t data[9]; uint8_t data[9];
if (readPageZero(data)) { if (readPage(0, data)) {
if (channel == DS2438_CHB){ if (channel == DS2438_CHB){
data[0] = data[0] | 0x08; data[0] = data[0] | 0x08;
} }
else { else {
data[0] = data[0] & 0xf7; data[0] = data[0] & 0xf7;
} }
writePageZero(data); writePage(0, data);
return true; return true;
} }
Serial.println("Could not read page zero data"); Serial.println("Could not read page zero data");
return false; return false;
} }
void DS2438::writePageZero(uint8_t *data) { void DS2438::writePage(int page, uint8_t *data) {
_ow->reset(); _ow->reset();
_ow->select(_address); _ow->select(_address);
_ow->write(DS2438_WRITE_SCRATCHPAD_COMMAND, 0); _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++){ for (int i = 0; i < 8; i++){
_ow->write(data[i], 0); _ow->write(data[i], 0);
} }
_ow->reset(); _ow->reset();
_ow->select(_address); _ow->select(_address);
_ow->write(DS2438_COPY_SCRATCHPAD_COMMAND, 0); _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->reset();
_ow->select(_address); _ow->select(_address);
_ow->write(DS2438_RECALL_MEMORY_COMMAND, 0); _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->reset();
_ow->select(_address); _ow->select(_address);
_ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0); _ow->write(DS2438_READ_SCRATCHPAD_COMMAND, 0);
_ow->write(DS2438_PAGE_0, 0); _ow->write(page, 0);
for (int i = 0; i < 9; i++){ for (int i = 0; i < 9; i++){
data[i] = _ow->read(); data[i] = _ow->read();
} }