wang tang / watersenor_and_temp_code

Dependencies:   OneWire

Dependents:   IDW01M1-MQTT3 DS1820_IDW01M1 DS1820_IDW01M1_finish Dissolved_oxygen_sensor_online ... more

Fork of DS1820 by Zoltan Hudak

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Fri Mar 27 12:34:16 2015 +0000
Parent:
7:4403a206e78a
Child:
10:51fd66ded1e4
Child:
11:9be3e73c01f1
Child:
14:af21738cf2da
Commit message:
Function isPresent() added.

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
--- a/DS1820.cpp	Thu Mar 26 20:57:59 2015 +0000
+++ b/DS1820.cpp	Fri Mar 27 12:34:16 2015 +0000
@@ -46,8 +46,8 @@
  */
 DS1820::DS1820(PinName pin) :
     oneWire(pin) {
-    present = 0;
-    type_s = 0;
+    present = false;
+    model_s = false;
 }
 
 /**
@@ -60,15 +60,15 @@
 DS1820::DS1820(char model, PinName pin) :
     oneWire(pin) {
     if((model == 'S') or (model == 's')) {
-        present = 1;
-        type_s = 1;
+        present = true;
+        model_s = true;
     }
     else if((model == 'B') or (model == 'b')) {
-        present = 1;
-        type_s = 0;
+        present = true;
+        model_s = false;
     }
     else
-        present = 0;
+        present = false;
 }
 
 /**
@@ -99,33 +99,33 @@
 #endif
 
     if(OneWire::crc8(addr, 7) == addr[7]) {
-        present = 1;
+        present = true;
 
         // the first ROM byte indicates which chip
         switch(addr[0]) {
         case 0x10:
-            type_s = 1;
+            model_s = true;
 #if DEBUG
             serial.printf("DS18S20 or old DS1820\r\n");
 #endif            
             break;
 
         case 0x28:
-            type_s = 0;
+            model_s = false;
 #if DEBUG
             serial.printf("DS18B20\r\n");
 #endif            
             break;
 
         case 0x22:
-            type_s = 0;
+            model_s = false;
 #if DEBUG
             serial.printf("DS1822\r\n");
 #endif            
             break;
 
         default:
-            present = 0;
+            present = false;
 #if DEBUG
             serial.printf("Device doesn't belong to the DS1820 family\r\n");
 #endif            
@@ -142,6 +142,19 @@
 }
 
 /**
+ * @brief   Informs about presence of a DS1820 sensor.
+ * @note    begin() shall be called before using this function
+ *          if a generic DS1820 instance was created by the user. 
+ *          No need to call begin() for a specific DS1820 instance.
+ * @param
+ * @retval  true:   when a DS1820 sensor is present
+ *          false:  otherwise
+ */
+bool DS1820::isPresent(void) {
+    return present;
+}
+
+/**
  * @brief   Sets temperature-to-digital conversion resolution.
  * @note    The configuration register allows the user to set the resolution
  *          of the temperature-to-digital conversion to 9, 10, 11, or 12 bits.
@@ -156,7 +169,7 @@
         res = 12;
     if(res < 9)
         res = 9;      
-    if(type_s)
+    if(model_s)
         res = 9;
        
     oneWire.reset();
@@ -173,7 +186,6 @@
         oneWire.write(data[i]);
 }
 
-
 /**
  * @brief   Starts temperature conversion
  * @note    The time to complete the converion depends on the selected resolution:
@@ -213,7 +225,7 @@
         serial.printf("raw = %#x\r\n", *p_word);
 #endif            
 
-        if(type_s) {
+        if(model_s) {
             *p_word = *p_word << 3;         // 9-bit resolution
             if(data[7] == 0x10) {
 
@@ -264,3 +276,4 @@
         return (float(word) / 256.0f);
 }
 
+
--- a/DS1820.h	Thu Mar 26 20:57:59 2015 +0000
+++ b/DS1820.h	Fri Mar 27 12:34:16 2015 +0000
@@ -6,16 +6,17 @@
 class   DS1820
 {
     OneWire oneWire;
-    uint8_t type_s;
+    bool    present;    
+    bool    model_s;
     uint8_t data[12];
     uint8_t addr[8];
     float   toFloat(uint16_t word);
 public:
-    uint8_t present;
 
     DS1820(PinName pin);
     DS1820(char model, PinName pin);
     bool   begin(void);
+    bool   isPresent();
     void   setResolution(uint8_t res);
     void   startConversion(void);
     float  read(void);