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

Files at this revision

API Documentation at this revision

Comitter:
amateusz
Date:
Sat Sep 02 20:13:38 2017 +0000
Parent:
0:3968e3a2e047
Commit message:
Non blocking convert. Updates internal variable, which can be read by temperature() method.

Changed in this revision

DS1820.cpp Show annotated file Show diff for this revision Revisions of this file
DS1820.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3968e3a2e047 -r 1020ba9ab5d2 DS1820.cpp
--- 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)
diff -r 3968e3a2e047 -r 1020ba9ab5d2 DS1820.h
--- a/DS1820.h	Wed Aug 30 21:15:06 2017 +0000
+++ b/DS1820.h	Sat Sep 02 20:13:38 2017 +0000
@@ -168,7 +168,7 @@
       * @param allows the fnction to apply to a specific device or
       * to all devices on the 1-Wire bus.
       */
-    void convert_temperature(devices device=this_device);
+    void convert_temperature(bool blocking = false, devices device=this_device);
 
     /** This function will return the probe temperature. This function
       * uses the count remainding values to interpolate the temperature
@@ -247,8 +247,9 @@
 
     void enable_auto_convert(float interval);
 private:
+    Timeout auto_get_new_temperature;
+    Ticker _autoConvert;
     bool _parasite_power;
-    Ticker _autoConvert;
     bool _autoConvertEnabled;
     char CRC_byte (char xCRC, char byte );
     bool onewire_reset();
@@ -259,10 +260,10 @@
     void onewire_byte_out(char data);
     bool onewire_bit_in();
     char onewire_byte_in();
-    void _temperature();
+    void _temperature_raw();
     void _auto_convert_temperature();
     float _last_temperature;
-
+    
 protected:
     DigitalInOut _datapin;
     DigitalOut _parasitepin;