Simplified access to a Microchip Digital Potentiometer (MCP41xxx/MCP42xxx) devices

Dependents:   MCP41xxxApp MCP320xApp MCP41xxxApp

Revision:
1:cf3cee91eb87
Parent:
0:03314ad622d6
Child:
2:7c27fb9785be
diff -r 03314ad622d6 -r cf3cee91eb87 MCP4xxxx_SPI.cpp
--- a/MCP4xxxx_SPI.cpp	Fri Jan 25 16:10:58 2013 +0000
+++ b/MCP4xxxx_SPI.cpp	Sun Jan 27 17:04:05 2013 +0000
@@ -1,5 +1,5 @@
 /* mbed simplified access to Microchip 24LCxx Serial EEPROM devices (SPI)
- * Copyright (c) 2010-2012 ygarcia, MIT License
+ * Copyright (c) 2013 ygarcia, MIT 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, 
@@ -84,6 +84,12 @@
     unsigned short CMCP4xxxx_SPI::Write(const Commands p_command, const unsigned char p_value) {
         DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x - 0x%02x", (unsigned char)p_command, p_value)
         
+        // Sanity check
+        if ((p_command != WriteToPot1) && (p_command != WriteToPot2) && (p_command != WriteToBoth)) {
+            // Wrong parameters
+            return (unsigned short) -1;
+        }
+        
         unsigned short command = 0;
         switch (p_command) {
             case WriteToPot1: 
@@ -92,9 +98,28 @@
             case WriteToPot2:
                 command = (0x12 << 8 | p_value);
                 break;
-            case WriteToPotBoth:
+            default:
                 command = (0x13 << 8 | p_value);
-                break;
+        } // End of 'switch' statement
+        
+        DEBUG("CMCP4xxxx_SPI: Send command: 0x%04x", command)
+        unsigned short result = _spiInstance->write(command);
+        
+        DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result)
+        return result;
+    }
+
+    unsigned short CMCP4xxxx_SPI::Write(const Commands p_command) {
+        DEBUG_ENTER("CMCP4xxxx_SPI::Write: 0x%02x", (unsigned char)p_command)
+        
+        // Sanity check
+        if ((p_command != ShutdownPot1) && (p_command != ShutdownPot2) && (p_command != ShutdownBoth)) {
+            // Wrong parameters
+            return (unsigned short) -1;
+        }
+        
+       unsigned short command = 0;
+        switch (p_command) {
             case ShutdownPot1:
                 command = (0x21 << 8);
                 break;
@@ -111,7 +136,6 @@
         DEBUG_LEAVE("CMCP4xxxx_SPI::Write: %d", result)
         return result;
     }
-
     unsigned short CMCP4xxxx_SPI::Write() {
         return _spiInstance->write(0);
     }