Skip to content

Commit

Permalink
Dallas Plugin enhanced : no more "delay()" (#488)
Browse files Browse the repository at this point in the history
* Enhanced Dallas pooling to avoid use of delay(800)

DS_readTemp has been split in 2: DS_convertTemp and DS_readonlyTemp.
DS_convertTemp is first called after DS_setResolution.
Then DS_convertTemp is called after temp has been read.
DS_readTemp has been kept for compatibility.
The only downside is that you read the temperature of previous query (value set by Timer/TDT option).

* typo

* Moved to Environment + code cleaning

- Moved Plugin from Temperature to Environment
- Modified Plugin_004_DS_crc8 to check CRC8, not return it
- Renamed Plugin_004_DS_convertTemp to Plugin_004_DS_startConvertion and removed useless vars in it
- Removed unneed braces and empty lines
- Added comments
  • Loading branch information
BadWolf42 authored and psy0rz committed Oct 15, 2017
1 parent 5c4ae38 commit d7d913d
Showing 1 changed file with 57 additions and 55 deletions.
112 changes: 57 additions & 55 deletions src/_P004_Dallas.ino
Expand Up @@ -2,6 +2,8 @@
// #################################### Plugin 004: TempSensor Dallas DS18B20 ###########################
// #######################################################################################################

// Maxime Integrated (ex Dallas) DS18B20 datasheet : https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

#define PLUGIN_004
#define PLUGIN_ID_004 4
#define PLUGIN_NAME_004 "Environment - DS18b20"
Expand Down Expand Up @@ -52,9 +54,8 @@ boolean Plugin_004(byte function, struct EventStruct * event, String& string)
Plugin_004_DallasPin = Settings.TaskDevicePin1[event->TaskIndex];

// get currently saved address
for (byte i = 0; i < 8; i++) {
for (byte i = 0; i < 8; i++)
savedAddress[i] = ExtraTaskSettings.TaskDevicePluginConfigLong[i];
}

// find all suitable devices
addRowLabel(string, F("Device Address"));
Expand Down Expand Up @@ -104,6 +105,7 @@ boolean Plugin_004(byte function, struct EventStruct * event, String& string)
ExtraTaskSettings.TaskDevicePluginConfigLong[x] = addr[x];

Plugin_004_DS_setResolution(addr, getFormItemInt(F("plugin_004_res")));
Plugin_004_DS_startConvertion(addr);

success = true;
break;
Expand Down Expand Up @@ -133,6 +135,7 @@ boolean Plugin_004(byte function, struct EventStruct * event, String& string)
Plugin_004_DallasPin = Settings.TaskDevicePin1[event->TaskIndex];
float value = 0;
String log = F("DS : Temperature: ");

if (Plugin_004_DS_readTemp(addr, &value))
{
UserVar[event->BaseVarIndex] = value;
Expand All @@ -144,6 +147,7 @@ boolean Plugin_004(byte function, struct EventStruct * event, String& string)
UserVar[event->BaseVarIndex] = NAN;
log += F("Error!");
}
Plugin_004_DS_startConvertion(addr);

log += (" (");
for (byte x = 0; x < 8; x++)
Expand All @@ -167,47 +171,56 @@ boolean Plugin_004(byte function, struct EventStruct * event, String& string)
\*********************************************************************************************/
byte Plugin_004_DS_scan(byte getDeviceROM, uint8_t* ROM)
{
byte tmpaddr[8];
byte devCount = 0;
Plugin_004_DS_reset();

Plugin_004_DS_reset_search();
while (Plugin_004_DS_search(tmpaddr))
{
if (getDeviceROM == devCount)
for (byte i = 0; i < 8; i++)
ROM[i] = tmpaddr[i];
devCount++;
}
return devCount;
byte tmpaddr[8];
byte devCount = 0;
Plugin_004_DS_reset();

Plugin_004_DS_reset_search();
while (Plugin_004_DS_search(tmpaddr))
{
if (getDeviceROM == devCount)
for (byte i = 0; i < 8; i++)
ROM[i] = tmpaddr[i];
devCount++;
}
return devCount;
}

/*********************************************************************************************\
* Dallas Read temperature
* Dallas Start Temperature Conversion, expected duration:
* 9 bits resolution -> 93.75 ms
* 10 bits resolution -> 187.5 ms
* 11 bits resolution -> 375 ms
* 12 bits resolution -> 750 ms
\*********************************************************************************************/
boolean Plugin_004_DS_readTemp(uint8_t ROM[8], float * value)
void Plugin_004_DS_startConvertion(uint8_t ROM[8])
{
int16_t DSTemp;
byte ScratchPad[12];

Plugin_004_DS_reset();
Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
Plugin_004_DS_write(ROM[i]);
Plugin_004_DS_write(0x44);
Plugin_004_DS_write(0x44); // Take temperature mesurement
}

delay(800);
/*********************************************************************************************\
* Dallas Read temperature from scratchpad
\*********************************************************************************************/
boolean Plugin_004_DS_readTemp(uint8_t ROM[8], float * value)
{
int16_t DSTemp;
byte ScratchPad[12];

Plugin_004_DS_reset();
Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
Plugin_004_DS_write(ROM[i]);

Plugin_004_DS_write(0xBE); // Read scratchpad

for (byte i = 0; i < 9; i++) // copy 8 bytes
for (byte i = 0; i < 9; i++) // read 9 bytes
ScratchPad[i] = Plugin_004_DS_read();

if (Plugin_004_DS_crc8(ScratchPad, 8) != ScratchPad[8])
if (!Plugin_004_DS_crc8(ScratchPad))
{
*value = 0;
return false;
Expand All @@ -222,15 +235,11 @@ boolean Plugin_004_DS_readTemp(uint8_t ROM[8], float * value)
{
DSTemp = (ScratchPad[1] << 11) | ScratchPad[0] << 3;
DSTemp = ((DSTemp & 0xfff0) << 3) - 16 +
(
((ScratchPad[7] - ScratchPad[6]) << 7) /
ScratchPad[7]
);
(((ScratchPad[7] - ScratchPad[6]) << 7) / ScratchPad[7]);
*value = float(DSTemp) * 0.0078125;
}

return true;
} // Plugin_004_DS_readTemp
}

/*********************************************************************************************\
* Dallas Get Resolution
Expand All @@ -245,19 +254,15 @@ int Plugin_004_DS_getResolution(uint8_t ROM[8])
Plugin_004_DS_reset();
Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
{
Plugin_004_DS_write(ROM[i]);
}

Plugin_004_DS_write(0xBE); // Read scratchpad

for (byte i = 0; i < 9; i++) // copy 8 bytes
for (byte i = 0; i < 9; i++) // read 9 bytes
ScratchPad[i] = Plugin_004_DS_read();

if (Plugin_004_DS_crc8(ScratchPad, 8) != ScratchPad[8])
{
if (!Plugin_004_DS_crc8(ScratchPad))
return 0;
}
else
{
switch (ScratchPad[4])
Expand All @@ -272,6 +277,7 @@ int Plugin_004_DS_getResolution(uint8_t ROM[8])
return 10;

case 0x1F: // 9 bit
default:
return 9;
}
}
Expand All @@ -291,44 +297,38 @@ boolean Plugin_004_DS_setResolution(uint8_t ROM[8], byte res)
Plugin_004_DS_reset();
Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
{
Plugin_004_DS_write(ROM[i]);
}

Plugin_004_DS_write(0xBE); // Read scratchpad

for (byte i = 0; i < 9; i++) // copy 8 bytes
for (byte i = 0; i < 9; i++) // read 9 bytes
ScratchPad[i] = Plugin_004_DS_read();

if (Plugin_004_DS_crc8(ScratchPad, 8) != ScratchPad[8])
{
return 0;
}
if (!Plugin_004_DS_crc8(ScratchPad))
return false;
else
{
switch (res)
{
case 12:
ScratchPad[4] = 0x7F; // 12 bit
ScratchPad[4] = 0x7F; // 12 bits
break;
case 11:
ScratchPad[4] = 0x5F; // 11 bit
ScratchPad[4] = 0x5F; // 11 bits
break;
case 10:
ScratchPad[4] = 0x3F; // 10 bit
ScratchPad[4] = 0x3F; // 10 bits
break;
case 9:
default:
ScratchPad[4] = 0x1F; // 9 bit
ScratchPad[4] = 0x1F; // 9 bits
break;
}

Plugin_004_DS_reset();
Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
{
Plugin_004_DS_write(ROM[i]);
}

Plugin_004_DS_write(0x4E); // Write to EEPROM
Plugin_004_DS_write(ScratchPad[2]); // high alarm temp
Expand All @@ -337,9 +337,7 @@ boolean Plugin_004_DS_setResolution(uint8_t ROM[8], byte res)

Plugin_004_DS_write(0x55); // Choose ROM
for (byte i = 0; i < 8; i++)
{
Plugin_004_DS_write(ROM[i]);
}

// save the newly written values to eeprom
Plugin_004_DS_write(0x48);
Expand Down Expand Up @@ -594,13 +592,17 @@ void Plugin_004_DS_write_bit(uint8_t v)
}
}

uint8_t Plugin_004_DS_crc8(uint8_t * addr, uint8_t len)
/*********************************************************************************************\
* Dallas Calculate CRC8 and compare it of addr[0-7] and compares it to addr[8]
\*********************************************************************************************/
boolean Plugin_004_DS_crc8(uint8_t * addr)
{
uint8_t crc = 0;
uint8_t crc = 0;
uint8_t len = 8;

while (len--)
{
uint8_t inbyte = *addr++;
uint8_t inbyte = *addr++; // from 0 to 7
for (uint8_t i = 8; i; i--)
{
uint8_t mix = (crc ^ inbyte) & 0x01;
Expand All @@ -609,5 +611,5 @@ uint8_t Plugin_004_DS_crc8(uint8_t * addr, uint8_t len)
inbyte >>= 1;
}
}
return crc;
return crc == *addr; // addr 8
}

0 comments on commit d7d913d

Please sign in to comment.