Library for the MAX44000 Ambient Light Sensor / Proximity Detector

Dependents:   LED_Demo LED_Demo2 LED_Demo

Fork of BMP180 by Kevin Gillepsie

MAX44000 Device Driver

Committer:
switches
Date:
Wed Sep 21 12:00:34 2016 +0000
Revision:
4:a9f09252653a
Parent:
2:91f97c274e89
Set TRIM bit in init function to use factory calibration data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kgills 0:b2219e6e444b 1 /*******************************************************************************
switches 1:1a770989adcb 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
kgills 0:b2219e6e444b 3 *
kgills 0:b2219e6e444b 4 * Permission is hereby granted, free of charge, to any person obtaining a
kgills 0:b2219e6e444b 5 * copy of this software and associated documentation files (the "Software"),
kgills 0:b2219e6e444b 6 * to deal in the Software without restriction, including without limitation
kgills 0:b2219e6e444b 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
kgills 0:b2219e6e444b 8 * and/or sell copies of the Software, and to permit persons to whom the
kgills 0:b2219e6e444b 9 * Software is furnished to do so, subject to the following conditions:
kgills 0:b2219e6e444b 10 *
kgills 0:b2219e6e444b 11 * The above copyright notice and this permission notice shall be included
kgills 0:b2219e6e444b 12 * in all copies or substantial portions of the Software.
kgills 0:b2219e6e444b 13 *
kgills 0:b2219e6e444b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
kgills 0:b2219e6e444b 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kgills 0:b2219e6e444b 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
kgills 0:b2219e6e444b 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
kgills 0:b2219e6e444b 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
kgills 0:b2219e6e444b 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
kgills 0:b2219e6e444b 20 * OTHER DEALINGS IN THE SOFTWARE.
kgills 0:b2219e6e444b 21 *
kgills 0:b2219e6e444b 22 * Except as contained in this notice, the name of Maxim Integrated
kgills 0:b2219e6e444b 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
kgills 0:b2219e6e444b 24 * Products, Inc. Branding Policy.
kgills 0:b2219e6e444b 25 *
kgills 0:b2219e6e444b 26 * The mere transfer of this software does not imply any licenses
kgills 0:b2219e6e444b 27 * of trade secrets, proprietary technology, copyrights, patents,
kgills 0:b2219e6e444b 28 * trademarks, maskwork rights, or any other form of intellectual
kgills 0:b2219e6e444b 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
kgills 0:b2219e6e444b 30 * ownership rights.
kgills 0:b2219e6e444b 31 *******************************************************************************
kgills 0:b2219e6e444b 32 */
kgills 0:b2219e6e444b 33
switches 1:1a770989adcb 34 #ifndef _MAX44000_H_
switches 1:1a770989adcb 35 #define _MAX44000_H_
kgills 0:b2219e6e444b 36
kgills 0:b2219e6e444b 37 #include "mbed.h"
kgills 0:b2219e6e444b 38
kgills 0:b2219e6e444b 39 /**
switches 2:91f97c274e89 40 * MAX44000 Ambient Light and Infrared Proximity Sensor
kgills 0:b2219e6e444b 41 *
kgills 0:b2219e6e444b 42 * @code
kgills 0:b2219e6e444b 43 * #include <stdio.h>
kgills 0:b2219e6e444b 44 * #include "mbed.h"
switches 1:1a770989adcb 45 * #include "MAX44000.h"
kgills 0:b2219e6e444b 46 *
kgills 0:b2219e6e444b 47 * I2C i2c(I2C_SDA, I2C_SCL);
switches 1:1a770989adcb 48 * MAX44000 max44000(&i2c);
kgills 0:b2219e6e444b 49 *
kgills 0:b2219e6e444b 50 * int main(void) {
kgills 0:b2219e6e444b 51 *
kgills 0:b2219e6e444b 52 * while(1) {
switches 1:1a770989adcb 53 * if (max44000.init(MAX44000::MODE_ALS_PROX, MAX44000::ALSTIM_1X, MAX44000::ALSPGA_128X, MAX44000::DRV_110) != 0) {
switches 1:1a770989adcb 54 * printf("Error communicating with MAX44000\n");
kgills 0:b2219e6e444b 55 * } else {
switches 1:1a770989adcb 56 * printf("Initialized MAX44000\n");
kgills 0:b2219e6e444b 57 * break;
kgills 0:b2219e6e444b 58 * }
kgills 0:b2219e6e444b 59 * wait(1);
kgills 0:b2219e6e444b 60 * }
kgills 0:b2219e6e444b 61 *
kgills 0:b2219e6e444b 62 * while(1) {
switches 1:1a770989adcb 63 * int alsValue = max44000.readALS();
switches 1:1a770989adcb 64 * if(alsValue < 0) {
switches 1:1a770989adcb 65 * printf("Error reading ALS value\n");
kgills 0:b2219e6e444b 66 * continue;
kgills 0:b2219e6e444b 67 * }
kgills 0:b2219e6e444b 68 *
switches 1:1a770989adcb 69 * int proxValue = max44000.readReg(MAX44000::REG_PRX_DATA);
switches 1:1a770989adcb 70 * if(proxValue < 0) {
switches 1:1a770989adcb 71 * printf("Error reading proximity value\n");
kgills 0:b2219e6e444b 72 * continue;
kgills 0:b2219e6e444b 73 * }
kgills 0:b2219e6e444b 74 *
switches 1:1a770989adcb 75 * printf("ALS = 0x%04X Proximity = 0x%02X\n", alsValue, proxValue);
kgills 0:b2219e6e444b 76 * wait(1);
kgills 0:b2219e6e444b 77 * }
kgills 0:b2219e6e444b 78 * }
kgills 0:b2219e6e444b 79 * @endcode
kgills 0:b2219e6e444b 80 */
switches 1:1a770989adcb 81 class MAX44000
kgills 0:b2219e6e444b 82 {
kgills 0:b2219e6e444b 83
kgills 0:b2219e6e444b 84 public:
kgills 0:b2219e6e444b 85
kgills 0:b2219e6e444b 86 /**
switches 1:1a770989adcb 87 * @brief Register Addresses
switches 1:1a770989adcb 88 * @details Enumerated MAX44000 register addresses
switches 1:1a770989adcb 89 */
switches 1:1a770989adcb 90 typedef enum {
switches 2:91f97c274e89 91 REG_STATUS, ///< Interrupt Status
switches 2:91f97c274e89 92 REG_MAIN_CONFIG, ///< Main Configuration
switches 2:91f97c274e89 93 REG_RX_CONFIG, ///< Receive Configuration
switches 2:91f97c274e89 94 REG_TX_CONFIG, ///< Transmit Configuration
switches 2:91f97c274e89 95 REG_ALS_DATA_HIGH, ///< ADC High Byte (ALS)
switches 2:91f97c274e89 96 REG_ALS_DATA_LOW, ///< ADC Low Byte (ALS)
switches 2:91f97c274e89 97 REG_ALS_UPTHR_HIGH, ///< ALS Upper Threshold (High Byte)
switches 2:91f97c274e89 98 REG_ALS_UPTHR_LOW, ///< ALS Upper Threshold (Low Byte)
switches 2:91f97c274e89 99 REG_ALS_LOTHR_HIGH, ///< ALS Lower Threshold (High Byte)
switches 2:91f97c274e89 100 REG_ALS_LOTHR_LOW, ///< ALS Lower Threshold (Low Byte)
switches 2:91f97c274e89 101 REG_PST, ///< Threshold Persist Timer
switches 2:91f97c274e89 102 REG_PRX_IND, ///< PROX Threshold Indicator
switches 2:91f97c274e89 103 REG_PRX_THR, ///< PROX Threshold
switches 2:91f97c274e89 104 REG_TRIM_GAIN_GREEN, ///< Digital Gain Trim of Green Channel
switches 2:91f97c274e89 105 REG_TRIM_GAIN_IR = 0x0F, ///< Digital Gain Trim of Infrared Channel
switches 2:91f97c274e89 106 REG_PRX_DATA = 0x16 ///< ADC Byte (PROX)
switches 1:1a770989adcb 107 } registers_t;
switches 1:1a770989adcb 108
switches 1:1a770989adcb 109
switches 1:1a770989adcb 110 /**
switches 1:1a770989adcb 111 * @brief Operating Modes
switches 1:1a770989adcb 112 * @details Enumerated MAX44000 operating modes
switches 1:1a770989adcb 113 */
switches 1:1a770989adcb 114 typedef enum {
switches 2:91f97c274e89 115 MODE_SHUTDOWN, ///< Analog circuits are shut down, but the digital register retains values
switches 2:91f97c274e89 116 MODE_ALS_GMIR, ///< Standard ALS mode, stores difference between green and IR channels
switches 2:91f97c274e89 117 MODE_ALS_G, ///< ALS green channel only
switches 2:91f97c274e89 118 MODE_ALS_IR, ///< Infrared channel only
switches 2:91f97c274e89 119 MODE_ALS_PROX, ///< ALS and PROX are interleaved continuously
switches 2:91f97c274e89 120 MODE_PROX, ///< PROX only
switches 2:91f97c274e89 121 MODE_RSVD_110, ///< Reserved, do not use
switches 2:91f97c274e89 122 MODE_RSVD_111 ///< Reserved, do not use
switches 1:1a770989adcb 123 } modes_t;
switches 1:1a770989adcb 124
switches 1:1a770989adcb 125
switches 1:1a770989adcb 126 /**
switches 1:1a770989adcb 127 * @brief ALS ADC Conversion Time
switches 1:1a770989adcb 128 * @details MAX44000 Ambient Light Sensor ADC Conversion Time
kgills 0:b2219e6e444b 129 */
kgills 0:b2219e6e444b 130 typedef enum {
switches 2:91f97c274e89 131 ALSTIM_1X, ///< 100ms, 16,384 counts, 14 bit resolution
switches 2:91f97c274e89 132 ALSTIM_4X, ///< 25ms, 4096 counts, 12 bit resolution
switches 2:91f97c274e89 133 ALSTIM_16X, ///< 6.25ms, 1024 counts, 10 bit resolution
switches 2:91f97c274e89 134 ALSTIM_64X ///< 1.5625ms, 256 counts, 8 bit resolution
switches 1:1a770989adcb 135 } alstim_t;
switches 1:1a770989adcb 136
switches 1:1a770989adcb 137
switches 1:1a770989adcb 138 /**
switches 1:1a770989adcb 139 * alspga_t - MAX44000 Ambient Light Measurement Gain
switches 1:1a770989adcb 140 */
switches 1:1a770989adcb 141 typedef enum {
switches 2:91f97c274e89 142 ALSPGA_1X, ///< 0.03125 lux/lsb
switches 2:91f97c274e89 143 ALSPGA_4X, ///< 0.125 lux/lsb
switches 2:91f97c274e89 144 ALSPGA_16X, ///< 0.5 lux/lsb
switches 2:91f97c274e89 145 ALSPGA_128X ///< 4 lux/lsb
switches 1:1a770989adcb 146 } alspga_t;
switches 1:1a770989adcb 147
kgills 0:b2219e6e444b 148
kgills 0:b2219e6e444b 149 /**
switches 1:1a770989adcb 150 * @brief LED Driver Current
switches 1:1a770989adcb 151 * @details MAX44000 LED Driver Current Settings
switches 1:1a770989adcb 152 */
switches 1:1a770989adcb 153 typedef enum {
switches 2:91f97c274e89 154 DRV_0, ///< LED driver disabled
switches 2:91f97c274e89 155 DRV_10, ///< 10mA
switches 2:91f97c274e89 156 DRV_20, ///< 20mA
switches 2:91f97c274e89 157 DRV_30, ///< 30mA
switches 2:91f97c274e89 158 DRV_40, ///< 40mA
switches 2:91f97c274e89 159 DRV_50, ///< 50mA
switches 2:91f97c274e89 160 DRV_60, ///< 60mA
switches 2:91f97c274e89 161 DRV_70, ///< 70mA
switches 2:91f97c274e89 162 DUP_40, ///< 40mA
switches 2:91f97c274e89 163 DUP_50, ///< 50mA
switches 2:91f97c274e89 164 DUP_60, ///< 60mA
switches 2:91f97c274e89 165 DUP_70, ///< 70mA
switches 2:91f97c274e89 166 DRV_80, ///< 80mA
switches 2:91f97c274e89 167 DRV_90, ///< 90mA
switches 2:91f97c274e89 168 DRV_100, ///< 100mA
switches 2:91f97c274e89 169 DRV_110 ///< 110mA
switches 1:1a770989adcb 170 } drive_t;
switches 1:1a770989adcb 171
switches 1:1a770989adcb 172
switches 1:1a770989adcb 173 /**
switches 1:1a770989adcb 174 * @brief Persist Times
switches 1:1a770989adcb 175 * @details MAX44000 ALS/PROX Threshold Persist Timer Settings
switches 1:1a770989adcb 176 */
switches 1:1a770989adcb 177 typedef enum {
switches 2:91f97c274e89 178 PST_1, ///< 1 trigger before interrupt
switches 2:91f97c274e89 179 PST_2, ///< 2 consecutive triggers before interrupt
switches 2:91f97c274e89 180 PST_4, ///< 4 consecutive triggers before interrupt
switches 2:91f97c274e89 181 PST_16 ///< 16 consecutive triggers before interrupt
switches 1:1a770989adcb 182 } persist_t;
switches 1:1a770989adcb 183
switches 1:1a770989adcb 184
switches 1:1a770989adcb 185 /**
switches 1:1a770989adcb 186 * MAX44000 constructor.
kgills 0:b2219e6e444b 187 *
kgills 0:b2219e6e444b 188 * @param sda mbed pin to use for SDA line of I2C interface.
kgills 0:b2219e6e444b 189 * @param scl mbed pin to use for SCL line of I2C interface.
kgills 0:b2219e6e444b 190 */
switches 1:1a770989adcb 191 MAX44000(PinName sda, PinName scl);
kgills 0:b2219e6e444b 192
kgills 0:b2219e6e444b 193 /**
switches 1:1a770989adcb 194 * MAX44000 constructor.
kgills 0:b2219e6e444b 195 *
kgills 0:b2219e6e444b 196 * @param i2c I2C object to use.
kgills 0:b2219e6e444b 197 */
switches 1:1a770989adcb 198 MAX44000(I2C *i2c);
kgills 0:b2219e6e444b 199
kgills 0:b2219e6e444b 200 /**
switches 1:1a770989adcb 201 * MAX44000 destructor.
kgills 0:b2219e6e444b 202 */
switches 1:1a770989adcb 203 ~MAX44000();
kgills 0:b2219e6e444b 204
kgills 0:b2219e6e444b 205 /**
switches 1:1a770989adcb 206 * @brief Initialize MAX44000.
switches 1:1a770989adcb 207 * @details Gets the device ID and saves the calibration values.
switches 1:1a770989adcb 208 * @param mode Operating Mode
switches 1:1a770989adcb 209 * @param alstim Ambient Light ADC Conversion Time
switches 1:1a770989adcb 210 * @param alspga Ambient Light Measurement Gain
switches 1:1a770989adcb 211 * @param drive LED Driver Current
kgills 0:b2219e6e444b 212 * @returns 0 if no errors, -1 if error.
kgills 0:b2219e6e444b 213 */
switches 2:91f97c274e89 214 int init(MAX44000::modes_t mode, MAX44000::alstim_t alstim, MAX44000::alspga_t alspga, MAX44000::drive_t drive);
kgills 0:b2219e6e444b 215
kgills 0:b2219e6e444b 216 /**
switches 1:1a770989adcb 217 * @brief Write Register
switches 1:1a770989adcb 218 * @details Writes data to MAX44000 register
switches 1:1a770989adcb 219 *
switches 1:1a770989adcb 220 * @param reg_addr Register to write
switches 1:1a770989adcb 221 * @param reg_data Data to write
kgills 0:b2219e6e444b 222 * @returns 0 if no errors, -1 if error.
kgills 0:b2219e6e444b 223 */
switches 1:1a770989adcb 224 int writeReg(MAX44000::registers_t reg_addr, char reg_data);
kgills 0:b2219e6e444b 225
kgills 0:b2219e6e444b 226 /**
switches 1:1a770989adcb 227 * @brief Read Register
switches 1:1a770989adcb 228 * @details Reads data from MAX44000 register
kgills 0:b2219e6e444b 229 *
switches 1:1a770989adcb 230 * @param reg_addr Register to read
switches 1:1a770989adcb 231 * @returns data if no errors, -1 if error.
switches 1:1a770989adcb 232 */
switches 1:1a770989adcb 233 int readReg(MAX44000::registers_t reg_addr);
switches 1:1a770989adcb 234
switches 1:1a770989adcb 235 /**
switches 1:1a770989adcb 236 * @brief Read ALS Data
switches 1:1a770989adcb 237 * @details Reads both ALS data registers and returns combined value
kgills 0:b2219e6e444b 238 *
switches 1:1a770989adcb 239 * @returns data if no errors, -1 if error.
kgills 0:b2219e6e444b 240 */
switches 1:1a770989adcb 241 int readALS(void);
switches 1:1a770989adcb 242
kgills 0:b2219e6e444b 243
kgills 0:b2219e6e444b 244 private:
kgills 0:b2219e6e444b 245
kgills 0:b2219e6e444b 246 I2C *i2c_;
kgills 0:b2219e6e444b 247 bool i2c_owner;
kgills 0:b2219e6e444b 248
kgills 0:b2219e6e444b 249 };
kgills 0:b2219e6e444b 250
switches 1:1a770989adcb 251 #endif /* _MAX44000_H_ */