Fork of AK9752. Removed getData() and put functionality in getSensorData(). Added comments and documentation to library.
Fork of AK9752 by
AK9752.h@4:2f4c8e641ce9, 2016-11-02 (annotated)
- Committer:
- tkstreet
- Date:
- Wed Nov 02 00:14:52 2016 +0000
- Revision:
- 4:2f4c8e641ce9
- Parent:
- 3:5b437caef51f
AK9752 working version. ID2 checking needs debugging.
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 | 3:5b437caef51f | 6 | /** |
masahikofukasawa | 3:5b437caef51f | 7 | * This is a device driver of AK9752. |
masahikofukasawa | 3:5b437caef51f | 8 | * |
masahikofukasawa | 3:5b437caef51f | 9 | * @note AK9752 IR sensor device manufactured by AKM. |
masahikofukasawa | 3:5b437caef51f | 10 | * Example: |
masahikofukasawa | 3:5b437caef51f | 11 | * @code |
masahikofukasawa | 3:5b437caef51f | 12 | * #include "mbed.h" |
masahikofukasawa | 3:5b437caef51f | 13 | * #include "ak9750.h" |
masahikofukasawa | 3:5b437caef51f | 14 | * |
masahikofukasawa | 3:5b437caef51f | 15 | * #define I2C_SPEED_100KHZ 100000 |
masahikofukasawa | 3:5b437caef51f | 16 | * #define I2C_SPEED_400KHZ 400000 |
masahikofukasawa | 3:5b437caef51f | 17 | * |
masahikofukasawa | 3:5b437caef51f | 18 | * int main() { |
masahikofukasawa | 3:5b437caef51f | 19 | * // Creates an instance of I2C |
masahikofukasawa | 3:5b437caef51f | 20 | * I2C connection(I2C_SDA0, I2C_SCL0); |
masahikofukasawa | 3:5b437caef51f | 21 | * connection.frequency(I2C_SPEED_100KHZ); |
masahikofukasawa | 3:5b437caef51f | 22 | * |
masahikofukasawa | 3:5b437caef51f | 23 | * // TBD |
masahikofukasawa | 3:5b437caef51f | 24 | * |
masahikofukasawa | 3:5b437caef51f | 25 | * while(true) { |
masahikofukasawa | 3:5b437caef51f | 26 | * // TBD |
masahikofukasawa | 3:5b437caef51f | 27 | * } |
masahikofukasawa | 3:5b437caef51f | 28 | * } |
masahikofukasawa | 3:5b437caef51f | 29 | * } |
masahikofukasawa | 3:5b437caef51f | 30 | * @endcode |
masahikofukasawa | 3:5b437caef51f | 31 | */ |
masahikofukasawa | 0:51fa46d39a3e | 32 | class AK9752 |
masahikofukasawa | 0:51fa46d39a3e | 33 | { |
masahikofukasawa | 0:51fa46d39a3e | 34 | public: |
masahikofukasawa | 0:51fa46d39a3e | 35 | /** |
masahikofukasawa | 0:51fa46d39a3e | 36 | * Enum for return status. |
masahikofukasawa | 0:51fa46d39a3e | 37 | */ |
masahikofukasawa | 0:51fa46d39a3e | 38 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 39 | SUCCESS, /**< Success */ |
masahikofukasawa | 0:51fa46d39a3e | 40 | ERROR, /**< Error */ |
masahikofukasawa | 0:51fa46d39a3e | 41 | ERROR_I2C_WRITE, /**< I2C write error */ |
masahikofukasawa | 0:51fa46d39a3e | 42 | ERROR_I2C_READ, /**< I2C read error */ |
masahikofukasawa | 0:51fa46d39a3e | 43 | ERROR_ARG_OUT_OF_BOUNDS, /**< An argument is out of bounds */ |
masahikofukasawa | 0:51fa46d39a3e | 44 | } Status; |
masahikofukasawa | 0:51fa46d39a3e | 45 | |
masahikofukasawa | 0:51fa46d39a3e | 46 | /** |
masahikofukasawa | 0:51fa46d39a3e | 47 | * Enum for slave address of AK9752. |
masahikofukasawa | 0:51fa46d39a3e | 48 | */ |
masahikofukasawa | 0:51fa46d39a3e | 49 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 50 | SLAVE_ADDR_1 = 0x64, /**< CAD1=0, CAD0=0 */ |
masahikofukasawa | 0:51fa46d39a3e | 51 | } SlaveAddress; |
masahikofukasawa | 0:51fa46d39a3e | 52 | |
masahikofukasawa | 0:51fa46d39a3e | 53 | /** |
masahikofukasawa | 0:51fa46d39a3e | 54 | * Enum for operation mode of measurement. |
masahikofukasawa | 0:51fa46d39a3e | 55 | */ |
masahikofukasawa | 0:51fa46d39a3e | 56 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 57 | MODE_STANDBY = 0x00, |
masahikofukasawa | 0:51fa46d39a3e | 58 | MODE_CONTINUOUS = 0x01, |
masahikofukasawa | 0:51fa46d39a3e | 59 | MODE_SINGLE_SHOT = 0x02, |
masahikofukasawa | 0:51fa46d39a3e | 60 | } OperationMode; |
masahikofukasawa | 0:51fa46d39a3e | 61 | |
masahikofukasawa | 0:51fa46d39a3e | 62 | /** |
masahikofukasawa | 0:51fa46d39a3e | 63 | * Enum for Cut-off frequency for tempearature sensor setting. |
masahikofukasawa | 0:51fa46d39a3e | 64 | */ |
masahikofukasawa | 0:51fa46d39a3e | 65 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 66 | FCTMP_NOFILTER = 0x00, /**< No Filter */ |
masahikofukasawa | 0:51fa46d39a3e | 67 | FCTMP_2P5HZ = 0x01, /**< Fc = 2.5 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 68 | FCTMP_0P9HZ = 0x02, /**< Fc = 0.9 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 69 | FCTMP_0P45HZ = 0x03, /**< Fc = 0.45 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 70 | FCTMP_0P22HZ = 0x04, /**< Fc = 0.22 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 71 | } FcTmp; |
masahikofukasawa | 0:51fa46d39a3e | 72 | |
masahikofukasawa | 0:51fa46d39a3e | 73 | /** |
masahikofukasawa | 0:51fa46d39a3e | 74 | * Enum for Cut-off frequency for IR sensor setting. |
masahikofukasawa | 0:51fa46d39a3e | 75 | */ |
masahikofukasawa | 0:51fa46d39a3e | 76 | typedef enum { |
masahikofukasawa | 0:51fa46d39a3e | 77 | FCIR_NOFILTER = 0x00, /**< No Filter */ |
masahikofukasawa | 0:51fa46d39a3e | 78 | FCIR_2P5HZ = 0x01, /**< Fc = 2.5 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 79 | FCIR_0P9HZ = 0x02, /**< Fc = 0.9 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 80 | FCIR_0P45HZ = 0x03, /**< Fc = 0.45 Hz */ |
masahikofukasawa | 0:51fa46d39a3e | 81 | } FcIr; |
masahikofukasawa | 0:51fa46d39a3e | 82 | |
masahikofukasawa | 0:51fa46d39a3e | 83 | /** |
masahikofukasawa | 0:51fa46d39a3e | 84 | * Structure for intterupt status. |
masahikofukasawa | 0:51fa46d39a3e | 85 | */ |
masahikofukasawa | 0:51fa46d39a3e | 86 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 87 | bool irh; |
masahikofukasawa | 0:51fa46d39a3e | 88 | bool irl; |
masahikofukasawa | 0:51fa46d39a3e | 89 | bool tmph; |
masahikofukasawa | 0:51fa46d39a3e | 90 | bool tmpl; |
masahikofukasawa | 0:51fa46d39a3e | 91 | bool dr; |
masahikofukasawa | 0:51fa46d39a3e | 92 | } InterruptStatus; |
masahikofukasawa | 0:51fa46d39a3e | 93 | |
masahikofukasawa | 0:51fa46d39a3e | 94 | /** |
masahikofukasawa | 0:51fa46d39a3e | 95 | * Structure for threshold. |
masahikofukasawa | 0:51fa46d39a3e | 96 | */ |
masahikofukasawa | 0:51fa46d39a3e | 97 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 98 | int16_t thirh; /**< High Threshold level of IR. */ |
masahikofukasawa | 0:51fa46d39a3e | 99 | int16_t thirl; /**< Low Threshold level of IR. */ |
masahikofukasawa | 0:51fa46d39a3e | 100 | int16_t thtmph; /**< High Threshold level of temperature sensor. */ |
masahikofukasawa | 0:51fa46d39a3e | 101 | int16_t thtmpl; /**< Low Threshold level of temperature sensor. */ |
masahikofukasawa | 0:51fa46d39a3e | 102 | } Threshold; |
masahikofukasawa | 0:51fa46d39a3e | 103 | |
masahikofukasawa | 0:51fa46d39a3e | 104 | /** |
masahikofukasawa | 0:51fa46d39a3e | 105 | * Structure for measurement data. |
masahikofukasawa | 0:51fa46d39a3e | 106 | */ |
masahikofukasawa | 0:51fa46d39a3e | 107 | typedef struct { |
masahikofukasawa | 0:51fa46d39a3e | 108 | InterruptStatus intStatus; |
masahikofukasawa | 0:51fa46d39a3e | 109 | int16_t ir; |
masahikofukasawa | 0:51fa46d39a3e | 110 | int16_t temperature; |
masahikofukasawa | 0:51fa46d39a3e | 111 | bool dor; |
masahikofukasawa | 0:51fa46d39a3e | 112 | } SensorData; |
masahikofukasawa | 0:51fa46d39a3e | 113 | |
masahikofukasawa | 0:51fa46d39a3e | 114 | /** |
masahikofukasawa | 0:51fa46d39a3e | 115 | * Constructor. |
masahikofukasawa | 0:51fa46d39a3e | 116 | * |
masahikofukasawa | 0:51fa46d39a3e | 117 | * @param conn instance of I2C |
masahikofukasawa | 0:51fa46d39a3e | 118 | * @param addr slave address of the device |
masahikofukasawa | 0:51fa46d39a3e | 119 | */ |
masahikofukasawa | 0:51fa46d39a3e | 120 | // AK9752(I2C *conn, SlaveAddress addr); |
masahikofukasawa | 0:51fa46d39a3e | 121 | AK9752(); |
masahikofukasawa | 0:51fa46d39a3e | 122 | |
masahikofukasawa | 0:51fa46d39a3e | 123 | void init(I2C *conn, SlaveAddress addr); |
masahikofukasawa | 0:51fa46d39a3e | 124 | |
masahikofukasawa | 0:51fa46d39a3e | 125 | /** |
masahikofukasawa | 0:51fa46d39a3e | 126 | * Checks AK9752 connection. |
masahikofukasawa | 0:51fa46d39a3e | 127 | * @return Returns SUCCESS if connection is confirmed, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 128 | */ |
masahikofukasawa | 0:51fa46d39a3e | 129 | Status checkConnection(); |
masahikofukasawa | 0:51fa46d39a3e | 130 | |
masahikofukasawa | 0:51fa46d39a3e | 131 | /** |
masahikofukasawa | 0:51fa46d39a3e | 132 | * Gets interrupt enable/disable status. |
masahikofukasawa | 0:51fa46d39a3e | 133 | * @param intStatus interrupt status |
masahikofukasawa | 0:51fa46d39a3e | 134 | * @return SUCCESS if the interrupt status is obtained successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 135 | */ |
masahikofukasawa | 0:51fa46d39a3e | 136 | Status getInterruptEnable(InterruptStatus *intStatus); |
masahikofukasawa | 0:51fa46d39a3e | 137 | |
masahikofukasawa | 0:51fa46d39a3e | 138 | /** |
masahikofukasawa | 0:51fa46d39a3e | 139 | * Sets interrupt enable/disable status. |
masahikofukasawa | 0:51fa46d39a3e | 140 | * @param intStatus interrupt status |
masahikofukasawa | 0:51fa46d39a3e | 141 | * @return SUCCESS if the interrupt status is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 142 | */ |
masahikofukasawa | 0:51fa46d39a3e | 143 | Status setInterruptEnable(const InterruptStatus *intStatus); |
masahikofukasawa | 0:51fa46d39a3e | 144 | |
masahikofukasawa | 0:51fa46d39a3e | 145 | /** |
masahikofukasawa | 0:51fa46d39a3e | 146 | * Gets sensor operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 147 | * @param mode Pointer to the operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 148 | * @param fc_tmp Pointer to the tempearture sensor filter setting. |
masahikofukasawa | 0:51fa46d39a3e | 149 | * @param fc_ir Pointer to the IR sensor filter setting. |
masahikofukasawa | 0:51fa46d39a3e | 150 | * @return SUCCESS if operation mode is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 151 | */ |
masahikofukasawa | 0:51fa46d39a3e | 152 | Status getOperationMode(OperationMode *mode, FcTmp *fc_tmp, FcIr *fc_ir); |
masahikofukasawa | 0:51fa46d39a3e | 153 | |
masahikofukasawa | 0:51fa46d39a3e | 154 | /** |
masahikofukasawa | 0:51fa46d39a3e | 155 | * Sets sensor operation mode. |
masahikofukasawa | 0:51fa46d39a3e | 156 | * @param mode operation mode to be set. |
masahikofukasawa | 0:51fa46d39a3e | 157 | * @param fc_tmp filter cut-off frequency setting for temperature sensor |
masahikofukasawa | 0:51fa46d39a3e | 158 | * @param fc_ir filter cut-off frequency setting for ir sensor |
masahikofukasawa | 0:51fa46d39a3e | 159 | * @return SUCCESS if operation mode is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 160 | */ |
masahikofukasawa | 0:51fa46d39a3e | 161 | Status setOperationMode(OperationMode mode, FcTmp fc_tmp = FCTMP_NOFILTER, FcIr fc_ir = FCIR_NOFILTER); |
masahikofukasawa | 0:51fa46d39a3e | 162 | |
masahikofukasawa | 0:51fa46d39a3e | 163 | /** |
masahikofukasawa | 0:51fa46d39a3e | 164 | * Sets threshold. |
masahikofukasawa | 0:51fa46d39a3e | 165 | * @param th Pointer to the threshold structure to be set. |
masahikofukasawa | 0:51fa46d39a3e | 166 | * @return SUCCESS if threshold is set successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 167 | */ |
masahikofukasawa | 0:51fa46d39a3e | 168 | Status setThreshold(const Threshold *th); |
masahikofukasawa | 0:51fa46d39a3e | 169 | |
masahikofukasawa | 0:51fa46d39a3e | 170 | /** |
masahikofukasawa | 0:51fa46d39a3e | 171 | * Gets threshold. |
masahikofukasawa | 0:51fa46d39a3e | 172 | * @param th Pointer to the threshold structure to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 173 | * @return SUCCESS if threshold is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 174 | */ |
masahikofukasawa | 0:51fa46d39a3e | 175 | Status getThreshold(Threshold *th); |
masahikofukasawa | 0:51fa46d39a3e | 176 | |
masahikofukasawa | 0:51fa46d39a3e | 177 | /** |
masahikofukasawa | 0:51fa46d39a3e | 178 | * Resets the device. |
masahikofukasawa | 0:51fa46d39a3e | 179 | * @return SUCCESS if the device is reset successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 180 | */ |
masahikofukasawa | 0:51fa46d39a3e | 181 | Status reset(); |
masahikofukasawa | 0:51fa46d39a3e | 182 | |
masahikofukasawa | 0:51fa46d39a3e | 183 | /** |
masahikofukasawa | 0:51fa46d39a3e | 184 | * Gets sensor data. |
masahikofukasawa | 0:51fa46d39a3e | 185 | * @param data Pointer to the SensorData structure object to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 186 | * @return SUCCESS if data is obtained successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 187 | */ |
masahikofukasawa | 0:51fa46d39a3e | 188 | Status getSensorData(SensorData *data); |
masahikofukasawa | 0:51fa46d39a3e | 189 | |
masahikofukasawa | 0:51fa46d39a3e | 190 | /** |
masahikofukasawa | 0:51fa46d39a3e | 191 | * Reads register(s). |
masahikofukasawa | 0:51fa46d39a3e | 192 | * @param registerAddress Register address to be read. |
masahikofukasawa | 0:51fa46d39a3e | 193 | * @param buf Buffer to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 194 | * @param length Length in bytes to be read. |
masahikofukasawa | 0:51fa46d39a3e | 195 | * @return SUCCESS if data is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 196 | */ |
masahikofukasawa | 0:51fa46d39a3e | 197 | Status read(char registerAddress, char *buf, int length); |
masahikofukasawa | 0:51fa46d39a3e | 198 | |
masahikofukasawa | 0:51fa46d39a3e | 199 | /** |
masahikofukasawa | 0:51fa46d39a3e | 200 | * Writes data into register(s). |
masahikofukasawa | 0:51fa46d39a3e | 201 | * @param registerAddress Register address to be written. |
masahikofukasawa | 0:51fa46d39a3e | 202 | * @param buf Data to be written. |
masahikofukasawa | 0:51fa46d39a3e | 203 | * @param length Length in bytes to be written. |
masahikofukasawa | 0:51fa46d39a3e | 204 | * @return SUCCESS if data is written successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 205 | */ |
masahikofukasawa | 0:51fa46d39a3e | 206 | Status write(char registerAddress, const char *buf, int length); |
masahikofukasawa | 0:51fa46d39a3e | 207 | |
masahikofukasawa | 0:51fa46d39a3e | 208 | private: |
masahikofukasawa | 0:51fa46d39a3e | 209 | /** |
masahikofukasawa | 0:51fa46d39a3e | 210 | * Holds a pointer to an I2C object. |
masahikofukasawa | 0:51fa46d39a3e | 211 | */ |
masahikofukasawa | 0:51fa46d39a3e | 212 | I2C *connection; |
masahikofukasawa | 0:51fa46d39a3e | 213 | |
masahikofukasawa | 0:51fa46d39a3e | 214 | /** |
masahikofukasawa | 0:51fa46d39a3e | 215 | * Holds the slave address. |
masahikofukasawa | 0:51fa46d39a3e | 216 | */ |
masahikofukasawa | 0:51fa46d39a3e | 217 | SlaveAddress slaveAddress; |
masahikofukasawa | 0:51fa46d39a3e | 218 | |
masahikofukasawa | 0:51fa46d39a3e | 219 | // const static uint8_t IR_DATA_LEN = 7; /**<! Data length of IR sensor. From ST1 to ST2. */ |
masahikofukasawa | 0:51fa46d39a3e | 220 | |
masahikofukasawa | 0:51fa46d39a3e | 221 | /** |
masahikofukasawa | 0:51fa46d39a3e | 222 | * Gets sensor measurement data, from ST1 to ST2. |
masahikofukasawa | 0:51fa46d39a3e | 223 | * @param buf Buffer to store the read data. |
masahikofukasawa | 0:51fa46d39a3e | 224 | * @return SUCCESS if data is read successfully, otherwise returns other value. |
masahikofukasawa | 0:51fa46d39a3e | 225 | */ |
masahikofukasawa | 0:51fa46d39a3e | 226 | Status getData(char *buf); |
masahikofukasawa | 0:51fa46d39a3e | 227 | }; |
masahikofukasawa | 0:51fa46d39a3e | 228 | |
masahikofukasawa | 0:51fa46d39a3e | 229 | #endif // __AK9752_H__ |