Yet another DS1820 lib, but this one is make in sweat of struggling with other ones. It's a fork really of <insert original as mbed doesn't track idk why> with *added capability* of not blocking.

Dependents:   Nucleo_praktyki

Revision:
1:1020ba9ab5d2
Parent:
0:3968e3a2e047
--- a/DS1820.cpp	Wed Aug 30 21:15:06 2017 +0000
+++ b/DS1820.cpp	Sat Sep 02 20:13:38 2017 +0000
@@ -32,7 +32,7 @@
 
 void DS1820::_auto_convert_temperature()
 {
-    convert_temperature(DS1820::this_device);
+    convert_temperature(false, DS1820::this_device);
 }
 
 void DS1820::enable_auto_convert(float interval)
@@ -282,10 +282,10 @@
     return xCRC;
 }
 
-void DS1820::convert_temperature(devices device)
+void DS1820::convert_temperature(bool blocking, devices device)
 {
     // Convert temperature into scratchpad RAM for all devices at once
-    int delay_time = 750; // Default delay time
+    int delay_time = 750; // Default delay time (for 12 bits)
     char resolution;
     if (device==all_devices)
         skip_ROM();          // Skip ROM command, will convert for ALL devices
@@ -304,12 +304,13 @@
     onewire_byte_out( 0x44);  // perform temperature conversion
     if (_parasite_power)
         _parasitepin = 1;       // Parasite power strong pullup
-    wait_ms(delay_time);
-//    Timeout auto_get_new_temperature;
-//    float timeout_time = ((float)delay_time)/1000.0;
-//    auto_get_new_temperature.attach(this, &DS1820::_temperature, timeout_time); // update _last_temperture automagically
-    _temperature();
-
+    if (blocking)  { // blocking
+        wait_ms(delay_time);
+        _temperature_raw();
+    } else { // non-blocking
+        float timeout_time = ((float)delay_time)/1000.0;
+        auto_get_new_temperature.attach(callback(this, &DS1820::_temperature_raw), timeout_time); // update _last_temperture automagically
+    }
     if (_parasite_power)
         _parasitepin = 0;
 }
@@ -399,9 +400,8 @@
     }
     return answer;
 }
-void DS1820::_temperature()
+void DS1820::_temperature_raw()
 {
-    char scale = 'c';
     float answer, remaining_count, count_per_degree;
     int reading;
     read_RAM();
@@ -417,13 +417,6 @@
         count_per_degree = RAM[7];
         answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree;
     }
-
-    if (scale=='C' or scale=='c')
-        answer = answer / 2.0;
-    else
-        // Convert to deg F
-        answer = answer * 9.0 / 10.0 + 32.0;
-//    return answer;
     _last_temperature = answer;
 }
 
@@ -435,10 +428,15 @@
 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
 // deg C or F scales.
     if (_last_temperature == NULL) {
-        convert_temperature();
-        wait_ms(800);
+        convert_temperature(true);
     }
-    return _last_temperature;
+    float answer = _last_temperature;
+    if (scale=='C' or scale=='c')
+        answer = answer / 2.0;
+    else
+        // Convert to deg F
+        answer = answer * 9.0 / 10.0 + 32.0;
+    return answer;
 }
 
 bool DS1820::read_power_supply(devices device)