Simple wrapper object for LTC2606 1CH 16Bit DAC. Instantiate DacLtc2606 object instance passing reference to mbed.I2C object instance. Call write() method on DacLtc2606 instance passing 16Bit integer (0 [Off] to 32767/-32768 [Half-On] to -1 [Full-On]) to set DAC output.

Files at this revision

API Documentation at this revision

Comitter:
rkimble
Date:
Fri Sep 14 17:23:05 2012 +0000
Parent:
1:5924632f3344
Commit message:
Improved numeric type usage/refined code.
; Added local storage and retrieval of DAC output value.

Changed in this revision

DacLtc2606.cpp Show annotated file Show diff for this revision Revisions of this file
DacLtc2606.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5924632f3344 -r d101ca4ae85e DacLtc2606.cpp
--- a/DacLtc2606.cpp	Thu Sep 13 23:10:35 2012 +0000
+++ b/DacLtc2606.cpp	Fri Sep 14 17:23:05 2012 +0000
@@ -21,9 +21,15 @@
 
 DacLtc2606::DacLtc2606(I2C i2c) : _i2c(i2c)
 {
+    _currentValue = 0;
 }
 
-bool DacLtc2606::write(int value)
+unsigned short DacLtc2606::getCurrentValue()
+{
+    return _currentValue;
+}
+
+bool DacLtc2606::write(unsigned short value)
 {
     int opR;
     char args[3];
@@ -31,24 +37,16 @@
     args[1] = (value >> 8) & 0xFF;
     args[2] = value & 0xFF;
     opR = _i2c.write(_slave, args, 3, false);
+    if(opR == 0) {_currentValue = value;}
     return (opR == 0) ? true : false;
 }
 
 bool DacLtc2606::write(double percent)
 {
-    if((percent >= 0.0) & (percent <= 1.0))
-    {
-        int value = 0;
-        if(percent < 0.5)
-        {
-            value = 32767.0 * (percent * 2);
-        }
-        else
-        {
-            value = -32768.0 * ((1.0 - percent) * 2);
-            if(value == 0) {value = -1;}
-        }
-        return write(value);
-    }
-    return false;
+    if(percent < 0.0) {percent = 0.0;}
+    if(percent > 1.0) {percent = 1.0;}
+    unsigned short value = 0;
+    value = 65535 * percent;
+    _currentValue = value;
+    return write(value);
 }
\ No newline at end of file
diff -r 5924632f3344 -r d101ca4ae85e DacLtc2606.h
--- a/DacLtc2606.h	Thu Sep 13 23:10:35 2012 +0000
+++ b/DacLtc2606.h	Fri Sep 14 17:23:05 2012 +0000
@@ -32,11 +32,11 @@
      */
     DacLtc2606(I2C i2c);
     
-    /** Set the DAC output to the specifed integer value
+    /** Set the DAC output to the specifed unsigned short integer value
      *
-     * @param value The 16 bit integer to write (0 to 32767 to -32768 to -1)
+     * @param value The 16 bit integer to write (0 to 65535)
      */
-    bool write(int value);
+    bool write(unsigned short value);
     
     /** Set the DAC output to the specified percentage of max
      *
@@ -44,10 +44,15 @@
      */
     bool write(double percent);
     
+    /** Retreives the unsigned short integer value currently assigned to the DAC output
+     */
+    unsigned short getCurrentValue();
+    
 private:
     static const char _slave = 0xE4;
     static const char _command = 0x30;
     I2C _i2c;
+    unsigned short _currentValue;
 };
 
 #endif
\ No newline at end of file