Device driver for AD converters MCP3426, MCP3427, and MCP3428.

Revision:
1:bc877c37027c
Parent:
0:78c512aa4d18
Child:
2:96d9bfe25b03
--- a/mcp342x.h	Fri Apr 29 16:38:37 2016 +0000
+++ b/mcp342x.h	Fri Apr 29 23:53:18 2016 +0000
@@ -83,7 +83,15 @@
         PGA_4X,                  /**< Gain 4x. */
         PGA_8X,                  /**< Gain 8x. */
     } PgaSetting;
-        
+    
+    /**
+     * AD result data.
+     */
+    typedef struct {
+        DataStatus st;
+        int16_t value;
+    } Data;
+    
     /**
      * Constructor.
      *
@@ -145,16 +153,10 @@
     PgaSetting getPgaSetting();
 
     /**
-     * Checks availability of new data.
-     * @return Returns true if new data is available. Returns false if no new data is available.
-     */
-    bool isNewDataAvailable();    
-
-    /**
      * Gets the AD value.
      * @return AD value.
      */
-    int16_t getValue();
+    Status getData(Data *pt);
     
     /**
      * Trigger AD conversion.
@@ -170,18 +172,24 @@
     
 private:
     typedef struct {
-        DataStatus      dataStatus;
-        ConversionMode  conversionMode;
-        SampleSetting   sampleSetting;
-        AdcChannel      adcChannel;
-        PGASetting      pgaSetting;
+        MeasurementTrigger   measurementTrigger;
+        DataStatus           dataStatus;
+        ConversionMode       conversionMode;
+        SampleSetting        sampleSetting;
+        AdcChannel           adcChannel;
+        PgaSetting           pgaSetting;
     } Config;
 
 
     I2C *connection;        /**< Pointer to an I2C object. */
     uint8_t slaveAddress;   /**< Slave address. */
-    uint8_t dataBuffer[3];  /**< Data buffer including configuration register. */
-    uint8_t Config config;  /**< Stores the latest configuration. */
+    Config currentConfig;  /**< Stores the latest configuration. */
+
+    /**
+     * Initialize this device.
+     * @return SUCCESS when succeeded. Other value will be returned when error.
+     */
+    Status init();
 
     /**
      * Reads the data registers including the configuration register.
@@ -189,7 +197,7 @@
      * @param currentConfig Pointer to the structure which stores the current configuration.
      * @return SUCCESS when succeeded. Other value will be returned when error.
      */
-    Status readData(int16_t *val, Config *currentConfig);
+    Status readData(int16_t *val, Config *config);
     
     /**
      * Sets the configuration register.
@@ -197,6 +205,13 @@
      * @return SUCCESS when succeeded. Other value will be returned when error.
      */
     Status setConfig(const Config *config);
+    
+    /**
+     * Decodes a configuration register value and put them into the specified Config structure.
+     * @param config Pointer to a Config structure to store the result.
+     * @param regVal Register value of the configuration register.
+     */
+    void decodeConfigurationRegister(Config *config, uint8_t regVal);
 };
 
 #endif