Driver for the AKM AK9752 IR sensor device.

Dependents:   AKDP-RevD7_014

Library for the AK9752 Ultra-Small IR Sensor with I2C Interface. Includes integrated temperature sensor (0 - 50C) and 16-bit ADC.

AK9752.h

Committer:
tkstreet
Date:
2016-11-01
Revision:
3:2035a4a54d3f
Parent:
0:51fa46d39a3e
Child:
6:254b7e5820e7

File content as of revision 3:2035a4a54d3f:

#ifndef __AK9752_H__
#define __AK9752_H__

#include "mbed.h"

/**
 * Device driver for AK9752.
 * @note AK9752 is an IR sensor with I2C interface.
 * Example:
 * @code
 * #include "mbed.h"
 * #include "ak9752_reg.h"
 * #include "ak9752.h"
 * 
 * int main()
 * {
 *      // Coming soon
 *      return 0;
 * }
 * @endcode
 * 
 */
class AK9752
{
public:
    /**
     * Enum for return status.
     */
    typedef enum {
        SUCCESS,                 /**< Success */
        ERROR,                   /**< Error */
        ERROR_I2C_WRITE,         /**< I2C write error */
        ERROR_I2C_READ,          /**< I2C read error */
        ERROR_ARG_OUT_OF_BOUNDS, /**< An argument is out of bounds */
    } Status;
    
    /**
     * Enum for slave address of AK9752.
     */
    typedef enum {
        SLAVE_ADDR_1 = 0x64,   /**< CAD1=0, CAD0=0 */
    } SlaveAddress;
    
    /**
     * Enum for operation mode of measurement.
     */
    typedef enum {
        MODE_STANDBY = 0x00,        /**< MODE_STANDBY:      0x00 */
        MODE_CONTINUOUS = 0x01,     /**< MODE_CONTINUOUS:   0x01 */
        MODE_SINGLE_SHOT = 0x02,    /**< MODE_SINGLE_SHOT:  0x02 */
    } OperationMode;

    /**
     * Enum for Cut-off frequency for tempearature sensor setting.
     */    
    typedef enum {
        FCTMP_NOFILTER = 0x00,      /**< No Filter */
        FCTMP_2P5HZ = 0x01,         /**< Fc = 2.5 Hz */
        FCTMP_0P9HZ = 0x02,         /**< Fc = 0.9 Hz */
        FCTMP_0P45HZ = 0x03,        /**< Fc = 0.45 Hz */
        FCTMP_0P22HZ = 0x04,        /**< Fc = 0.22 Hz */
    } FcTmp;
    
    /**
     * Enum for Cut-off frequency for IR sensor setting.
     */    
    typedef enum {
        FCIR_NOFILTER = 0x00,      /**< No Filter */
        FCIR_2P5HZ = 0x01,         /**< Fc = 2.5 Hz */
        FCIR_0P9HZ = 0x02,         /**< Fc = 0.9 Hz */
        FCIR_0P45HZ = 0x03,        /**< Fc = 0.45 Hz */
    } FcIr;
    
    /**
     * Structure for interrupt status.
     */
    typedef struct {
        bool irh;                   /**< IR Sensor crossed high threshold. */
        bool irl;                   /**< IR Sensor crossed low threshold. */
        bool tmph;                  /**< Tempearture Sensor crossed high threshold. */
        bool tmpl;                  /**< Temperature Sensor crossed low threshold. */
        bool dr;                    /**< Data Ready. */
    } InterruptStatus;

    /**
     * Structure to hold threshold levels.
     */    
    typedef struct {
        int16_t thirh;        /**< High Threshold level for IR sensor. */
        int16_t thirl;        /**< Low Threshold level of IR sensor. */
        int16_t thtmph;       /**< High Threshold level of temperature sensor. */
        int16_t thtmpl;       /**< Low Threshold level of temperature sensor. */
    } Threshold;
    
    /**
     * Structure to hold all sensor data: IR, temperature, and interrupt flags.
     */
    typedef struct {
        InterruptStatus intStatus;  /**< Interrupt status object */
        int16_t ir;             /**< IR Sensor Data (pA) */
        int16_t temperature;    /**< Temperature Data (deg C) */
        bool dor;               /**< Data Overrun: 1:data skip, 0:after ST2 read*/
    } SensorData;

    /**
     * Constructor.
     *
     * @param conn Instance of I2C
     * @param addr Slave address of the device
     */
    AK9752();

    /**
     * Initialize AK9752 connection.
     *
     * @param conn Instance of I2C
     * @param addr I2C slave address
     */
    void init(I2C *conn, SlaveAddress addr);

    /**
     * Checks AK9752 connection.
     *
     * @return SUCCESS if connection is confirmed, otherwise returns other value.
     */
    Status checkConnection();

    /**
     * Gets interrupt enable/disable status.
     *
     * @param intStatus Interrupt enable status
     *
     * @return SUCCESS if the interrupt status is obtained successfully, otherwise returns other value.
     */
    Status getInterruptEnable(InterruptStatus *intStatus);
       
    /**
     * Sets interrupt enable/disable status.
     *
     * @param intStatus interrupt status
     *
     * @return SUCCESS if the interrupt status is set successfully, otherwise returns other value.
     */
    Status setInterruptEnable(const InterruptStatus *intStatus);
    
    /**
     * Gets sensor operation mode: Standby, Continuous or Single-Shot.
     *
     * @param mode Pointer to the operation mode.
     * @param fc_tmp Pointer to the tempearture sensor filter setting.
     * @param fc_ir Pointer to the IR sensor filter setting.
     *
     * @return SUCCESS if operation mode is set successfully, otherwise returns other value.
     */
    Status getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir);
       
    /**
     * Sets sensor Operation mode: Standby, Continuous or Single-Shot.
     *
     * @param mode Operation mode to be set.
     * @param fc_tmp Filter cut-off frequency setting for temperature sensor.
     * @param fc_ir Filter cut-off frequency setting for IR sensor.
     *
     * @return SUCCESS if operation mode is set successfully, otherwise returns other value.
     */
    Status setOperationMode(OperationMode mode, FcTmp fc_tmp = FCTMP_NOFILTER, FcIr fc_ir = FCIR_NOFILTER);
       
    /**
     * Sets threshold of IR/temperature sensor.
     *
     * @param th Pointer to the threshold structure to be set.
     *
     * @return SUCCESS if threshold is set successfully, otherwise returns other value.
     */
    Status setThreshold(const Threshold *th);
    
    /**
     * Gets threshold of IR/temperature sensor.
     *
     * @param th Pointer to the threshold structure to store the read data.
     *
     * @return SUCCESS if threshold is read successfully, otherwise returns other value.
     */
    Status getThreshold(Threshold *th);
    
    /**
     * Resets the AK9752.
     *
     * @return SUCCESS if the device is reset successfully, otherwise returns other value.
     */
    Status reset();
    
    /**
     * Retrieves all sensor data: IR & Temperature data, and ST1, ST2, & INTCAUSE 
     * registers.
     *
     * @param data Pointer to the SensorData structure object to store the read data.
     *
     * @return SUCCESS if data is obtained successfully, otherwise returns other value.
     */
    Status getSensorData(SensorData *data);
    
    /**
     * Reads indicated AK9752 register(s).
     *
     * @param registerAddress Register address to be read.
     * @param buf Buffer to store the register data.
     * @param length Length in bytes to be read.
     *
     * @return SUCCESS if data is read successfully, otherwise returns other value.
     */
    Status read(char registerAddress, char *buf, int length);
    
    /**
     * Writes data into AK9752 register(s).
     *
     * @param registerAddress Register address to be written.
     * @param buf Data to be written.
     * @param length Length in bytes to be written.
     *
     * @return SUCCESS if data is written successfully, otherwise returns other value.
     */
    Status write(char registerAddress, const char *buf, int length);
    
private:
    /**
     * I2C connection. 
     */
    I2C *connection;
    
    /**
     * Slave address.
     */
    SlaveAddress slaveAddress;

//    const static uint8_t IR_DATA_LEN = 7;    /**<! Data length of IR sensor. From ST1 to ST2. */
    
};

#endif // __AK9752_H__