Library for the DS1721, 2-Wire Digital Thermometer and Thermostat from Dallas Semiconductor (Maxim Integrated)

Revision:
2:22dbeccb82be
Parent:
1:4fd830a97574
Child:
3:3c182751e655
--- a/DS1721.cpp	Thu May 02 17:12:06 2013 +0000
+++ b/DS1721.cpp	Tue Jul 30 20:23:07 2013 +0000
@@ -1,93 +1,99 @@
-/***********************************************************************************
- * @file        ds1721.cpp
- * @brief       Source file, enabling communication to/from the Dallas Semiconductor 
- *              (Maxim Integrated) DS1721 2-Wire Digital Theremometer and Thermostat.
- * @version     1.00
- * @date        04/29/2013
+/*
+ * @file DS1721.cpp
+ * @author Cameron Haegle
+ *
+ * @section LICENSE
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * @section DESCRIPTION
+ *
+ * Library for the DS1721, 2-Wire Digital Thermometer and Thermostat, from Maxim Semiconductor
+ *  (www.maiximintegrated.com). 
+ *
+ * @section LIMITATIONS
  * 
- * @author      Cameron Haegle
- * @company     Digi International
- * @copyright   
- * 
- * @limitations 
+ * This library was not written with for use with the mbed-RTOS, as the I2C read/write calls are 
+ *  not thread safe.
  *
- * 
- **********************************************************************************/
+ * ChangeLog
+ * 07/30/2013 
+ *   - 
+ */
 
 
-#include "ds1721.h"
+
+#include "DS1721.h"
+
 
-/**
- *
- **/
-DS1721::DS1721(PinName sda, PinName scl, int addr) : m_i2c(sda, scl) , m_addr(addr)
+DS1721::DS1721(I2C &i2c, int addr) : m_i2c(i2c) , m_addr(addr)
 {    
-     _resolution = RES_9_BIT;
-     _polarity   = POLARITY_ACTIVE_HIGH;
-     _mode   = CONV_FOREVER;
+     _resolution    = RES_9_BIT;
+     _polarity      = POLARITY_ACTIVE_HIGH;
+     _mode          = CONV_FOREVER;
      _num_read = 1;
      
-     setConfig(_resolution | _polarity | _one_shot);
+     //setConfig(_resolution | _polarity | _mode);
 }
 
-/**
- *
- **/
-DS1721::DS1721(PinName sda, PinName scl, int addr, int resolution, int polarity, int mode) 
-        : m_i2c(sda, scl) , m_addr(addr) , _resolution(resolution) , _polarity(polarity) , _one_shot(one_shot)
+DS1721::DS1721(I2C &i2c, int resolution, int polarity, int mode, int addr) 
+        : m_i2c(i2c) , _resolution(resolution) , _polarity(polarity) , _mode(mode) , m_addr(addr)
 {    
     _num_read = 1;     
-    setConfig(_resolution | _polarity | _mode);
+    //setConfig(_resolution | _polarity | _mode);
 }
 
-/**
- * deconstructor
- *
- **/
 DS1721::~DS1721(void) 
 { 
 }
 
-
-/**
- *
- **/
 int DS1721::setConfig(int config)
 {
     char cmd[2];
     cmd[0] = CMD_ACCESS_CFG;
     cmd[1] = (config & 0xFF);
-    
-    if(!m_i2c.write(m_addr, cmd, 2) != 0)
+        
+    if(writeData(cmd, 2) != 0)
     {        
-        return 0;
+        return 1;
     }    
-    return 1;
+    return 0;
 }
 
-/**
- *
- **/
 int DS1721::getConfig(int* config)
 {
     char cmd[2];
     
     cmd[0] = CMD_ACCESS_CFG;      // 0xAC
-    if(m_i2c.write(m_addr, cmd, 1) != 0)
+  
+    if(writeData(cmd, 1) != 0)
     {       
-        return 0;
+        return 1;
     }
-    //Delay(RW_DELAY);    
-    if((m_i2c.read(m_addr, cmd, 1)) != 0)
+
+    if(readData(cmd, 1) != 0)
     {      
-        return 0;
+        return 1;
     }      
     *config = cmd[0];
-    return 1;
+    return 0;
 }
  
 
-int DS1721::getTemp(void)
+float DS1721::getTemp(void)
 {
     char cmd[3];
    
@@ -97,162 +103,156 @@
     // this eliminate  an erroneous initial read.
     do
     {        
-        if((m_i2c.write(m_addr, cmd, 1)) != 0)
+        if(writeData(cmd, 1) != 0)
         {       
-            return 0;
+            return 1;
         }    
-        //Delay(RW_DELAY);
+        
         // read 1 byte, ignoring fractional portion of temperature
-        if((m_i2c.read(m_addr, cmd, 2)) != 0)
+        if(readData(cmd, 2) != 0)
         {
-            return 0;
+            return 1;
         }
         _num_read--;
     }while(_num_read);
     
     _num_read = 1;
     
-    return cmd[0];
+    return ((float)(cmd[1] + (cmd[0] << 8))/256);
 }
 
-
-/**
- *
- *
- **/
 int DS1721::setPolarity(int polarity)
 {
     _polarity = polarity;
-    return setConfig(_resolution | _polarity | _one_shot);
+    return setConfig(_resolution | _polarity | _mode);
 }
 
-/**
- *
- *
- **/
 int DS1721::getPolarity(void)
 {
-    int config;    
+    int config = 0;    
     getConfig(&config);
     return ((config & (1<<1)) ? 1: 0);
 }
 
-
-/**
- *
- *
- **/
 int DS1721::startConversion(void)
 {
    char cmd[1];
     
     cmd[0] = CMD_START_CONVT; // 0x51
     
-    if((m_i2c.write(m_addr, cmd, 1)) != 1)
+    if(writeData(cmd, 1) != 0)
     {      
         return 0;
     }  
     return 1;
 }
 
-/**
- *
- *
- **/
 int DS1721::stopConversion(void)
 {
     char cmd[1];
     
     cmd[0] = CMD_STOP_CONVT; // 0x51
     
-    if((m_i2c.write(m_addr, cmd, 1)) != 0)
+    if(writeData(cmd, 1) != 0)
     {       
         return 0;
     }  
     return 1;
 }
 
-/**
- *
- **/
-int DS1721::getLowSetpoint(void)
+float DS1721::getLowSp(void)
 {
     char cmd[4];
         
     cmd[0] = CMD_ACCESS_TL;
-    if((m_i2c.write(m_addr, cmd, 1)) != 0)
+    
+    if(writeData(cmd, 1) != 0)
     {     
         //return FALSE;
     }
-    //Delay(RW_DELAY);
     
     // read back TL msb & lsb bytes
-    if((m_i2c.read(m_addr, cmd, 2)) != 0)
+    if(readData(cmd, 2) != 0)
     {       
         return 0;
     }    
-    _tl = (cmd[0] & 0xFF);  
-    return _tl;
+       
+    _tl = ((cmd[0] << 8) + cmd[1]);  
+    
+    return ((float)_tl/256);
 }
 
-/**
- *
- **/
-int DS1721::setLowSetpoint(int newSp)
+int DS1721::setLowSp(float newSp)
 {
     char cmd[3];
     
-    _tl = newSp;
+    _tl = (newSp*256);
     
     cmd[0] = CMD_ACCESS_TL;
-    cmd[1] = _tl;         // temp MSB
-    cmd[2] = 0x80;        // temp LSB (+/-0.5)
-
-    if((m_i2c.write(m_addr, cmd, 3)) != 0)
+    cmd[1] = (char)(_tl >> 8);         // temp MSB
+    cmd[2] = (char)(_tl & 0x00FF);     // temp LSB (+/-0.5)
+    
+    if(writeData(cmd, 3) != 0)
     {       
         return 0;
     }
     return 1;
 }
 
-/**
- *
- **/
-int DS1721::getHighSetpoint(void)
+float DS1721::getHighSp(void)
 {
     char cmd[3];
         
     cmd[0] = CMD_ACCESS_TH;
-    if((m_i2c.write(m_addr, cmd, 1)) != 0)
+
+    if(writeData(cmd, 1) != 0)
     {
         //return 0;
     }
-    //Delay(RW_DELAY);
     // read back TL msb & lsb bytes
-    if((m_i2c.read(m_addr, cmd, 2)) != 0)
+    if(readData(cmd, 2) != 0)
     {
         //return 0;
     }    
-    _th = (cmd[0] & 0xFF);
-    return _th; 
+    
+    _th = ((cmd[0] << 8) + cmd[1]);  
+    
+    return ((float)_th/256);
 }
 
-/**
- *
- **/
-int DS1721::setHighSetpoint(int newSp)
+int DS1721::setHighSp(float newSp)
 {
     char cmd[3];
     
-    _th = newSp;
+    _th = (newSp * 256);
 
     cmd[0] = CMD_ACCESS_TH;
-    cmd[1] = _th;           // temp MSB
-    cmd[2] = 0x00;          // temp LSB
+    cmd[1] = (char)(_th >> 8);         // setpoint MSB
+    cmd[2] = (char)(_th & 0x00FF);     // setpoint LSB (+/-0.5)
 
-    if((m_i2c.write(m_addr, cmd, 3)) != 0)
+    if(writeData(cmd, 3) != 0)
     {      
         return 0;
     }
     return 1;
 }
+
+float DS1721::temp_CtoF(float tempC)
+{
+    return ((tempC * 1.8) + 32);
+}
+
+float DS1721::temp_FtoC(float tempF)
+{
+    return ((tempF - 32)* 0.55);
+}
+
+int DS1721::writeData(char* data, int length)
+{
+    return (m_i2c.write(m_addr, data, length));
+}
+
+int DS1721::readData(char* data, int length)
+{
+    return (m_i2c.read(m_addr, data, length));
+}
\ No newline at end of file