Skip to content

Commit bffa026

Browse files
committed
Adding result enum
See PR junkfix/esp32-ds18b20#28
1 parent a042e1a commit bffa026

3 files changed

Lines changed: 56 additions & 45 deletions

File tree

src/MycilaDS18.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
(((1ULL << (gpio_num)) & SOC_GPIO_VALID_OUTPUT_GPIO_MASK) != 0))
1212
#endif
1313

14-
static const char* err_desc[] = {"", "CRC ERROR", "BAD DATA", "TIMEOUT", "DRIVER NOT INITIALIZED"};
15-
1614
void Mycila::DS18::begin(const int8_t pin, uint8_t maxSearchCount) {
1715
if (_enabled)
1816
return;
@@ -71,14 +69,29 @@ bool Mycila::DS18::read() {
7169
return false;
7270

7371
float read;
74-
uint8_t err = _oneWire->getTemp(_deviceAddress, read);
72+
OneWire32::Result result = _oneWire->getTemp(_deviceAddress, read);
7573

7674
// request new reading
7775
_oneWire->request();
7876

7977
// process data when no error
80-
if (err) {
81-
ESP_LOGW(TAG, "%s 0x%llx @ pin %d: read error: %s", _name, _deviceAddress, _pin, err_desc[err]);
78+
if (result != OneWire32::Result::OK) {
79+
switch (result) {
80+
case OneWire32::Result::OK:
81+
break;
82+
case OneWire32::Result::CRC:
83+
ESP_LOGW(TAG, "%s 0x%llx @ pin %d: CRC error", _name, _deviceAddress, _pin);
84+
return false;
85+
case OneWire32::Result::BAD_DATA:
86+
ESP_LOGW(TAG, "%s 0x%llx @ pin %d: Bad data", _name, _deviceAddress, _pin);
87+
return false;
88+
case OneWire32::Result::TIMEOUT:
89+
ESP_LOGW(TAG, "%s 0x%llx @ pin %d: Timeout", _name, _deviceAddress, _pin);
90+
return false;
91+
case OneWire32::Result::DRIVER:
92+
ESP_LOGW(TAG, "%s 0x%llx @ pin %d: Driver not initialized", _name, _deviceAddress, _pin);
93+
return false;
94+
}
8295
return false;
8396
}
8497

src/esp32-ds18b20/OneWireESP32.cpp

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
#include <esp_idf_version.h>
88

9-
#define OWR_OK 0
10-
#define OWR_CRC 1
11-
#define OWR_BAD_DATA 2
12-
#define OWR_TIMEOUT 3
13-
#define OWR_DRIVER 4
14-
159
#define OW_RESET_PULSE 500
1610
#define OW_RESET_WAIT 200
1711
#define OW_RESET_PRESENCE_WAIT_MIN 15
@@ -251,44 +245,40 @@ void OneWire32::request() {
251245

252246
const uint8_t crc_table[] = {0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65, 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220, 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98, 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255, 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7, 219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154, 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36, 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185, 140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205, 17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80, 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238, 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115, 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139, 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22, 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168, 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
253247

254-
uint8_t OneWire32::getTemp(uint64_t& addr, float& temp) {
255-
uint8_t error = OWR_OK;
248+
OneWire32::Result OneWire32::getTemp(uint64_t& addr, float& temp) {
256249
if (!drv) {
257-
return OWR_DRIVER;
250+
return Result::DRIVER;
251+
}
252+
if (!reset()) {
253+
return Result::TIMEOUT;
254+
}
255+
write(0x55);
256+
uint8_t* a = (uint8_t*)&addr;
257+
for (uint8_t i = 0; i < 8; i++) {
258+
write(a[i]);
258259
}
259-
if (reset()) { // connected
260-
write(0x55);
261-
uint8_t* a = (uint8_t*)&addr;
262-
for (uint8_t i = 0; i < 8; i++) {
263-
write(a[i]);
260+
write(0xBE); // Read
261+
uint8_t data[9];
262+
uint16_t zero = 0;
263+
uint8_t crc = 0;
264+
for (uint8_t j = 0; j < 9; j++) {
265+
if (!read(data[j], 8)) {
266+
data[j] = 0;
264267
}
265-
write(0xBE); // Read
266-
uint8_t data[9];
267-
uint16_t zero = 0;
268-
uint8_t crc = 0;
269-
for (uint8_t j = 0; j < 9; j++) {
270-
if (!read(data[j], 8)) {
271-
data[j] = 0;
272-
}
273-
zero += data[j];
274-
if (j < 8) {
275-
crc = crc_table[crc ^ data[j]];
276-
}
268+
zero += data[j];
269+
if (j < 8) {
270+
crc = crc_table[crc ^ data[j]];
277271
}
278-
if (zero != 0x8f7 && zero) {
279-
if (data[8] == crc) { // CRC OK
280-
int16_t t = (data[1] << 8) | data[0];
281-
temp = ((float)t / 16.0);
282-
} else {
283-
error = OWR_CRC;
284-
}
285-
} else {
286-
error = OWR_BAD_DATA;
287-
}
288-
} else {
289-
error = OWR_TIMEOUT;
290272
}
291-
return error;
273+
if (zero == 0 || zero == 0x8f7) {
274+
return Result::BAD_DATA;
275+
}
276+
if (data[8] != crc) {
277+
return Result::CRC;
278+
}
279+
int16_t t = (data[1] << 8) | data[0];
280+
temp = ((float)t / 16.0);
281+
return Result::OK;
292282
}
293283

294284
uint8_t OneWire32::search(uint64_t* addresses, uint8_t total) {

src/esp32-ds18b20/OneWireESP32.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,19 @@ class OneWire32 {
3131
uint8_t drv = 0;
3232

3333
public:
34+
enum Result {
35+
OK = 0,
36+
CRC = 1,
37+
BAD_DATA = 2,
38+
TIMEOUT = 3,
39+
DRIVER = 4
40+
};
41+
3442
OneWire32(uint8_t pin);
3543
~OneWire32();
3644
bool reset();
3745
void request();
38-
uint8_t getTemp(uint64_t& addr, float& temp);
46+
Result getTemp(uint64_t& addr, float& temp);
3947
uint8_t search(uint64_t* addresses, uint8_t total);
4048
bool read(uint8_t& data, uint8_t len = 8);
4149
bool write(const uint8_t data, uint8_t len = 8);

0 commit comments

Comments
 (0)