Driver for the AKM AK9752 IR sensor device.
Library for the AK9752 Ultra-Small IR Sensor with I2C Interface. Includes integrated temperature sensor (0 - 50C) and 16-bit ADC.
AK9752.h@0:51fa46d39a3e, 2016-07-08 (annotated)
- Committer:
- masahikofukasawa
- Date:
- Fri Jul 08 22:27:55 2016 +0000
- Revision:
- 0:51fa46d39a3e
- Child:
- 2:5b437caef51f
- Child:
- 3:2035a4a54d3f
New for RevD7. Release to Japan.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
masahikofukasawa | 0:51fa46d39a3e | 1 | #ifndef __AK9752_H__ |
masahikofukasawa | 0:51fa46d39a3e | 2 | #define __AK9752_H__ |
masahikofukasawa | 0:51fa46d39a3e | 3 | |
masahikofukasawa | 0:51fa46d39a3e | 4 | #include "mbed.h" |
masahikofukasawa | 0:51fa46d39a3e | 5 | |
masahikofukasawa | 0:51fa46d39a3e | 6 | class AK9752 |
masahikofukasawa | 0:51fa46d39a3e | 7 | { |
masahikofukasawa | 0:51fa46d39a3e | 8 | public: |
masahikofukasawa | 0:51fa46d39a3e | 9 | /** |
masahikofukasawa | 0:51fa46d39a3e | 10 | * Enum for return status. |
masahikofukasawa | 0:51fa46d39a3e | 11 | */ |
masahikofukasawa | 0:51fa46d39a3e | 12 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 13 | SUCCESS, /**< Success */ |
masahikofukasawa | 0:51fa46d39a3e | 14 | ERROR, /**< Error */ |
masahikofukasawa | 0:51fa46d39a3e | 15 | ERROR_I2C_WRITE, /**< I2C write error */ |
masahikofukasawa | 0:51fa46d39a3e | 16 | ERROR_I2C_READ, /**< I2C read error */ |
masahikofukasawa | 0:51fa46d39a3e | 17 | ERROR_ARG_OUT_OF_BOUNDS, /**< An argument is out of bounds */ |
masahikofukasawa | 0:51fa46d39a3e | 18 | } Status; |
masahikofukasawa | 0:51fa46d39a3e | 19 | |
masahikofukasawa | 0:51fa46d39a3e | 20 | /** |
masahikofukasawa | 0:51fa46d39a3e | 21 | * Enum for slave address of AK9752. |
masahikofukasawa | 0:51fa46d39a3e | 22 | */ |
masahikofukasawa | 0:51fa46d39a3e | 23 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 24 | SLAVE_ADDR_1 = 0x64, /**< CAD1=0, CAD0=0 */ |
masahikofukasawa | 0:51fa46d39a3e | 25 | } SlaveAddress; |
masahikofukasawa | 0:51fa46d39a3e | 26 | |
masahikofukasawa | 0:51fa46d39a3e | 27 | /** |
masahikofukasawa | 0:51fa46d39a3e | 28 | * Enum for operation mode of measurement. |
masahikofukasawa | 0:51fa46d39a3e | 29 | */ |
masahikofukasawa | 0:51fa46d39a3e | 30 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 31 | MODE_STANDBY = 0x00, |
masahikofukasawa | 0:51fa46d39a3e | 32 | MODE_CONTINUOUS = 0x01, |
masahikofukasawa | 0:51fa46d39a3e | 33 | MODE_SINGLE_SHOT = 0x02, |
masahikofukasawa | 0:51fa46d39a3e | 34 | } OperationMode; |
masahikofukasawa | 0:51fa46d39a3e | 35 | |
masahikofukasawa | 0:51fa46d39a3e | 36 | /** |
masahikofukasawa | 0:51fa46d39a3e | 37 | * Enum for Cut-off frequency for tempearature sensor setting. |
masahikofukasawa | 0:51fa46d39a3e | 38 | */ |
masahikofukasawa | 0:51fa46d39a3e | 39 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 40 | FCTMP_NOFILTER = 0x00, /**< No Filter */ |
masahikofukasawa | 0:51fa46d39a3e | 41 | FCTMP_2P5HZ = 0x01, /**< Fc = 2.5 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 42 | FCTMP_0P9HZ = 0x02, /**< Fc = 0.9 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 43 | FCTMP_0P45HZ = 0x03, /**< Fc = 0.45 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 44 | FCTMP_0P22HZ = 0x04, /**< Fc = 0.22 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 45 | } FcTmp; |
masahikofukasawa | 0:51fa46d39a3e | 46 | |
masahikofukasawa | 0:51fa46d39a3e | 47 | /** |
masahikofukasawa | 0:51fa46d39a3e | 48 | * Enum for Cut-off frequency for IR sensor setting. |
masahikofukasawa | 0:51fa46d39a3e | 49 | */ |
masahikofukasawa | 0:51fa46d39a3e | 50 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 51 | FCIR_NOFILTER = 0x00, /**< No Filter */ |
masahikofukasawa | 0:51fa46d39a3e | 52 | FCIR_2P5HZ = 0x01, /**< Fc = 2.5 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 53 | FCIR_0P9HZ = 0x02, /**< Fc = 0.9 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 54 | FCIR_0P45HZ = 0x03, /**< Fc = 0.45 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 55 | } FcIr; |
masahikofukasawa | 0:51fa46d39a3e | 56 | |
masahikofukasawa | 0:51fa46d39a3e | 57 | /** |
masahikofukasawa | 0:51fa46d39a3e | 58 | * Structure for intterupt status. |
masahikofukasawa | 0:51fa46d39a3e | 59 | */ |
masahikofukasawa | 0:51fa46d39a3e | 60 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 61 | bool irh; |
masahikofukasawa | 0:51fa46d39a3e | 62 | bool irl; |
masahikofukasawa | 0:51fa46d39a3e | 63 | bool tmph; |
masahikofukasawa | 0:51fa46d39a3e | 64 | bool tmpl; |
masahikofukasawa | 0:51fa46d39a3e | 65 | bool dr; |
masahikofukasawa | 0:51fa46d39a3e | 66 | } InterruptStatus; |
masahikofukasawa | 0:51fa46d39a3e | 67 | |
masahikofukasawa | 0:51fa46d39a3e | 68 | /** |
masahikofukasawa | 0:51fa46d39a3e | 69 | * Structure for threshold. |
masahikofukasawa | 0:51fa46d39a3e | 70 | */ |
masahikofukasawa | 0:51fa46d39a3e | 71 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 72 | int16_t thirh; /**< High Threshold level of IR. */ |
masahikofukasawa | 0:51fa46d39a3e | 73 | int16_t thirl; /**< Low Threshold level of IR. */ |
masahikofukasawa | 0:51fa46d39a3e | 74 | int16_t thtmph; /**< High Threshold level of temperature sensor. */ |
masahikofukasawa | 0:51fa46d39a3e | 75 | int16_t thtmpl; /**< Low Threshold level of temperature sensor. */ |
masahikofukasawa | 0:51fa46d39a3e | 76 | } Threshold; |
masahikofukasawa | 0:51fa46d39a3e | 77 | |
masahikofukasawa | 0:51fa46d39a3e | 78 | /** |
masahikofukasawa | 0:51fa46d39a3e | 79 | * Structure for measurement data. |
masahikofukasawa | 0:51fa46d39a3e | 80 | */ |
masahikofukasawa | 0:51fa46d39a3e | 81 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 82 | InterruptStatus intStatus; |
masahikofukasawa | 0:51fa46d39a3e | 83 | int16_t ir; |
masahikofukasawa | 0:51fa46d39a3e | 84 | int16_t temperature; |
masahikofukasawa | 0:51fa46d39a3e | 85 | bool dor; |
masahikofukasawa | 0:51fa46d39a3e | 86 | } SensorData; |
masahikofukasawa | 0:51fa46d39a3e | 87 | |
masahikofukasawa | 0:51fa46d39a3e | 88 | /** |
masahikofukasawa | 0:51fa46d39a3e | 89 | * Constructor. |
masahikofukasawa | 0:51fa46d39a3e | 90 | * |
masahikofukasawa | 0:51fa46d39a3e | 91 | * @param conn instance of I2C |
masahikofukasawa | 0:51fa46d39a3e | 92 | * @param addr slave address of the device |
masahikofukasawa | 0:51fa46d39a3e | 93 | */ |
masahikofukasawa | 0:51fa46d39a3e | 94 | // AK9752(I2C *conn, SlaveAddress addr); |
masahikofukasawa | 0:51fa46d39a3e | 95 | AK9752(); |
masahikofukasawa | 0:51fa46d39a3e | 96 | |
masahikofukasawa | 0:51fa46d39a3e | 97 | void init(I2C *conn, SlaveAddress addr); |
masahikofukasawa | 0:51fa46d39a3e | 98 | |
masahikofukasawa | 0:51fa46d39a3e | 99 | /** |
masahikofukasawa | 0:51fa46d39a3e | 100 | * Checks AK9752 connection. |
masahikofukasawa | 0:51fa46d39a3e | 101 | * @return Returns SUCCESS if connection is confirmed, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 102 | */ |
masahikofukasawa | 0:51fa46d39a3e | 103 | Status checkConnection(); |
masahikofukasawa | 0:51fa46d39a3e | 104 | |
masahikofukasawa | 0:51fa46d39a3e | 105 | /** |
masahikofukasawa | 0:51fa46d39a3e | 106 | * Gets interrupt enable/disable status. |
masahikofukasawa | 0:51fa46d39a3e | 107 | * @param intStatus interrupt status |
masahikofukasawa | 0:51fa46d39a3e | 108 | * @return SUCCESS if the interrupt status is obtained successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 109 | */ |
masahikofukasawa | 0:51fa46d39a3e | 110 | Status getInterruptEnable(InterruptStatus *intStatus); |
masahikofukasawa | 0:51fa46d39a3e | 111 | |
masahikofukasawa | 0:51fa46d39a3e | 112 | /** |
masahikofukasawa | 0:51fa46d39a3e | 113 | * Sets interrupt enable/disable status. |
masahikofukasawa | 0:51fa46d39a3e | 114 | * @param intStatus interrupt status |
masahikofukasawa | 0:51fa46d39a3e | 115 | * @return SUCCESS if the interrupt status is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 116 | */ |
masahikofukasawa | 0:51fa46d39a3e | 117 | Status setInterruptEnable(const InterruptStatus *intStatus); |
masahikofukasawa | 0:51fa46d39a3e | 118 | |
masahikofukasawa | 0:51fa46d39a3e | 119 | /** |
masahikofukasawa | 0:51fa46d39a3e | 120 | * Gets sensor operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 121 | * @param mode Pointer to the operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 122 | * @param fc_tmp Pointer to the tempearture sensor filter setting. |
masahikofukasawa | 0:51fa46d39a3e | 123 | * @param fc_ir Pointer to the IR sensor filter setting. |
masahikofukasawa | 0:51fa46d39a3e | 124 | * @return SUCCESS if operation mode is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 125 | */ |
masahikofukasawa | 0:51fa46d39a3e | 126 | Status getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir); |
masahikofukasawa | 0:51fa46d39a3e | 127 | |
masahikofukasawa | 0:51fa46d39a3e | 128 | /** |
masahikofukasawa | 0:51fa46d39a3e | 129 | * Sets sensor operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 130 | * @param mode operation mode to be set. |
masahikofukasawa | 0:51fa46d39a3e | 131 | * @param fc_tmp filter cut-off frequency setting for temperature sensor |
masahikofukasawa | 0:51fa46d39a3e | 132 | * @param fc_ir filter cut-off frequency setting for ir sensor |
masahikofukasawa | 0:51fa46d39a3e | 133 | * @return SUCCESS if operation mode is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 134 | */ |
masahikofukasawa | 0:51fa46d39a3e | 135 | Status setOperationMode(OperationMode mode, FcTmp fc_tmp = FCTMP_NOFILTER, FcIr fc_ir = FCIR_NOFILTER); |
masahikofukasawa | 0:51fa46d39a3e | 136 | |
masahikofukasawa | 0:51fa46d39a3e | 137 | /** |
masahikofukasawa | 0:51fa46d39a3e | 138 | * Sets threshold. |
masahikofukasawa | 0:51fa46d39a3e | 139 | * @param th Pointer to the threshold structure to be set. |
masahikofukasawa | 0:51fa46d39a3e | 140 | * @return SUCCESS if threshold is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 141 | */ |
masahikofukasawa | 0:51fa46d39a3e | 142 | Status setThreshold(const Threshold *th); |
masahikofukasawa | 0:51fa46d39a3e | 143 | |
masahikofukasawa | 0:51fa46d39a3e | 144 | /** |
masahikofukasawa | 0:51fa46d39a3e | 145 | * Gets threshold. |
masahikofukasawa | 0:51fa46d39a3e | 146 | * @param th Pointer to the threshold structure to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 147 | * @return SUCCESS if threshold is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 148 | */ |
masahikofukasawa | 0:51fa46d39a3e | 149 | Status getThreshold(Threshold *th); |
masahikofukasawa | 0:51fa46d39a3e | 150 | |
masahikofukasawa | 0:51fa46d39a3e | 151 | /** |
masahikofukasawa | 0:51fa46d39a3e | 152 | * Resets the device. |
masahikofukasawa | 0:51fa46d39a3e | 153 | * @return SUCCESS if the device is reset successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 154 | */ |
masahikofukasawa | 0:51fa46d39a3e | 155 | Status reset(); |
masahikofukasawa | 0:51fa46d39a3e | 156 | |
masahikofukasawa | 0:51fa46d39a3e | 157 | /** |
masahikofukasawa | 0:51fa46d39a3e | 158 | * Gets sensor data. |
masahikofukasawa | 0:51fa46d39a3e | 159 | * @param data Pointer to the SensorData structure object to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 160 | * @return SUCCESS if data is obtained successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 161 | */ |
masahikofukasawa | 0:51fa46d39a3e | 162 | Status getSensorData(SensorData *data); |
masahikofukasawa | 0:51fa46d39a3e | 163 | |
masahikofukasawa | 0:51fa46d39a3e | 164 | /** |
masahikofukasawa | 0:51fa46d39a3e | 165 | * Reads register(s). |
masahikofukasawa | 0:51fa46d39a3e | 166 | * @param registerAddress Register address to be read. |
masahikofukasawa | 0:51fa46d39a3e | 167 | * @param buf Buffer to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 168 | * @param length Length in bytes to be read. |
masahikofukasawa | 0:51fa46d39a3e | 169 | * @return SUCCESS if data is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 170 | */ |
masahikofukasawa | 0:51fa46d39a3e | 171 | Status read(char registerAddress, char *buf, int length); |
masahikofukasawa | 0:51fa46d39a3e | 172 | |
masahikofukasawa | 0:51fa46d39a3e | 173 | /** |
masahikofukasawa | 0:51fa46d39a3e | 174 | * Writes data into register(s). |
masahikofukasawa | 0:51fa46d39a3e | 175 | * @param registerAddress Register address to be written. |
masahikofukasawa | 0:51fa46d39a3e | 176 | * @param buf Data to be written. |
masahikofukasawa | 0:51fa46d39a3e | 177 | * @param length Length in bytes to be written. |
masahikofukasawa | 0:51fa46d39a3e | 178 | * @return SUCCESS if data is written successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 179 | */ |
masahikofukasawa | 0:51fa46d39a3e | 180 | Status write(char registerAddress, const char *buf, int length); |
masahikofukasawa | 0:51fa46d39a3e | 181 | |
masahikofukasawa | 0:51fa46d39a3e | 182 | private: |
masahikofukasawa | 0:51fa46d39a3e | 183 | /** |
masahikofukasawa | 0:51fa46d39a3e | 184 | * Holds a pointer to an I2C object. |
masahikofukasawa | 0:51fa46d39a3e | 185 | */ |
masahikofukasawa | 0:51fa46d39a3e | 186 | I2C *connection; |
masahikofukasawa | 0:51fa46d39a3e | 187 | |
masahikofukasawa | 0:51fa46d39a3e | 188 | /** |
masahikofukasawa | 0:51fa46d39a3e | 189 | * Holds the slave address. |
masahikofukasawa | 0:51fa46d39a3e | 190 | */ |
masahikofukasawa | 0:51fa46d39a3e | 191 | SlaveAddress slaveAddress; |
masahikofukasawa | 0:51fa46d39a3e | 192 | |
masahikofukasawa | 0:51fa46d39a3e | 193 | // const static uint8_t IR_DATA_LEN = 7; /**<! Data length of IR sensor. From ST1 to ST2. */ |
masahikofukasawa | 0:51fa46d39a3e | 194 | |
masahikofukasawa | 0:51fa46d39a3e | 195 | /** |
masahikofukasawa | 0:51fa46d39a3e | 196 | * Gets sensor measurement data, from ST1 to ST2. |
masahikofukasawa | 0:51fa46d39a3e | 197 | * @param buf Buffer to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 198 | * @return SUCCESS if data is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 199 | */ |
masahikofukasawa | 0:51fa46d39a3e | 200 | Status getData(char *buf); |
masahikofukasawa | 0:51fa46d39a3e | 201 | }; |
masahikofukasawa | 0:51fa46d39a3e | 202 | |
masahikofukasawa | 0:51fa46d39a3e | 203 | #endif // __AK9752_H__ |