J M / MCP9808

Files at this revision

API Documentation at this revision

Comitter:
Jmfox24
Date:
Fri Jan 20 19:48:45 2017 +0000
Parent:
1:7e98ef2bd303
Commit message:
add sensor detection function

Changed in this revision

MCP9808.cpp Show annotated file Show diff for this revision Revisions of this file
MCP9808.hpp Show annotated file Show diff for this revision Revisions of this file
--- a/MCP9808.cpp	Mon Jan 09 17:57:58 2017 +0000
+++ b/MCP9808.cpp	Fri Jan 20 19:48:45 2017 +0000
@@ -7,6 +7,29 @@
 
 MCP9808:: ~MCP9808() {}
 
+bool MCP9808::is_detected()
+{
+    uint16_t v = 0;
+
+    /* check MFG ID */
+    if (reg_read(MCP9808_REG_MFG_ID, &v)) {
+        return false;
+    }
+    if (v != MCP9808_MFG_ID) {
+        return false;
+    }
+
+    /* check DEV ID */
+    if (reg_read(MCP9808_REG_DEV_ID, &v)) {
+        return false;
+    }
+    if (v != MCP9808_DEV_ID) {
+        return false;
+    }
+
+    return true;
+}
+
 int MCP9808::sleep()
 {
     return set_cfg_flag(MCP9808_CFG_FLAG_SHDN_ENABLED, true);
--- a/MCP9808.hpp	Mon Jan 09 17:57:58 2017 +0000
+++ b/MCP9808.hpp	Fri Jan 20 19:48:45 2017 +0000
@@ -56,6 +56,14 @@
         MCP9808_CFG_FLAG_SHDN_ENABLED =        (1 << 8),
     };
 
+    /** Manufacturer ID from MCP9808_REG_MFG_ID register
+     */
+    static const uint16_t MCP9808_MFG_ID = 0x0054;
+
+    /** Device ID from MCP9808_REG_DEV_ID register
+     */
+    static const uint16_t MCP9808_DEV_ID = 0x0400;
+
     /** Create an MCP9808 using the specified pins for I2C and specified I2C
      *  address
      *
@@ -67,6 +75,12 @@
 
     virtual ~MCP9808();
 
+    /** Detects the sensor's presence
+     *
+     * @returns true if sensor is detected
+     */
+    bool is_detected();
+
     /** Puts the sensor into (low-power) shutdown mode
      *
      *  @returns 0 on success, negative on error