APDS-9960 Light, RGB, Proximity, and Gesture Sensor https://www.sparkfun.com/products/12787 This library interfaces the APDS-9960 to Mbed over I2C. The library relies on the I2C library. To use the library, instantiate a glibr object, call ginit(), and call the appropriate functions.

Dependents:   Gesture_User_Interface car_test_v1 Capt_couleur_CAN Capt_couleur_smart

Fork of APDS_9960 by Krishan Bhagat

Committer:
kbhagat6
Date:
Thu Mar 12 20:39:10 2015 +0000
Revision:
0:1aac12a5f1e0
Child:
2:ba051af6731a
final commit of library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbhagat6 0:1aac12a5f1e0 1 #include "glibr.h"
kbhagat6 0:1aac12a5f1e0 2 #include "mbed.h"
kbhagat6 0:1aac12a5f1e0 3
kbhagat6 0:1aac12a5f1e0 4
kbhagat6 0:1aac12a5f1e0 5
kbhagat6 0:1aac12a5f1e0 6
kbhagat6 0:1aac12a5f1e0 7 glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){
kbhagat6 0:1aac12a5f1e0 8 gesture_ud_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 9 gesture_lr_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 10
kbhagat6 0:1aac12a5f1e0 11 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 12 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 13
kbhagat6 0:1aac12a5f1e0 14 gesture_near_count_ = 0;
kbhagat6 0:1aac12a5f1e0 15 gesture_far_count_ = 0;
kbhagat6 0:1aac12a5f1e0 16
kbhagat6 0:1aac12a5f1e0 17 gesture_state_ = 0;
kbhagat6 0:1aac12a5f1e0 18 gesture_motion_ = DIR_NONE;
kbhagat6 0:1aac12a5f1e0 19 }
kbhagat6 0:1aac12a5f1e0 20
kbhagat6 0:1aac12a5f1e0 21 glibr::~glibr(){
kbhagat6 0:1aac12a5f1e0 22
kbhagat6 0:1aac12a5f1e0 23 }
kbhagat6 0:1aac12a5f1e0 24
kbhagat6 0:1aac12a5f1e0 25 bool glibr::ginit(){
kbhagat6 0:1aac12a5f1e0 26 uint8_t id;
kbhagat6 0:1aac12a5f1e0 27
kbhagat6 0:1aac12a5f1e0 28 id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
kbhagat6 0:1aac12a5f1e0 29
kbhagat6 0:1aac12a5f1e0 30 if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
kbhagat6 0:1aac12a5f1e0 31 return false;
kbhagat6 0:1aac12a5f1e0 32 }
kbhagat6 0:1aac12a5f1e0 33
kbhagat6 0:1aac12a5f1e0 34 if(!setMode(ALL, OFF)) {
kbhagat6 0:1aac12a5f1e0 35 return false;
kbhagat6 0:1aac12a5f1e0 36 }
kbhagat6 0:1aac12a5f1e0 37 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
kbhagat6 0:1aac12a5f1e0 38 return false;
kbhagat6 0:1aac12a5f1e0 39 }
kbhagat6 0:1aac12a5f1e0 40 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
kbhagat6 0:1aac12a5f1e0 41 return false;
kbhagat6 0:1aac12a5f1e0 42 }
kbhagat6 0:1aac12a5f1e0 43 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
kbhagat6 0:1aac12a5f1e0 44 return false;
kbhagat6 0:1aac12a5f1e0 45 }
kbhagat6 0:1aac12a5f1e0 46 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
kbhagat6 0:1aac12a5f1e0 47 return false;
kbhagat6 0:1aac12a5f1e0 48 }
kbhagat6 0:1aac12a5f1e0 49 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
kbhagat6 0:1aac12a5f1e0 50 return false;
kbhagat6 0:1aac12a5f1e0 51 }
kbhagat6 0:1aac12a5f1e0 52 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
kbhagat6 0:1aac12a5f1e0 53 return false;
kbhagat6 0:1aac12a5f1e0 54 }
kbhagat6 0:1aac12a5f1e0 55
kbhagat6 0:1aac12a5f1e0 56 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 0:1aac12a5f1e0 57 return false;
kbhagat6 0:1aac12a5f1e0 58 }
kbhagat6 0:1aac12a5f1e0 59
kbhagat6 0:1aac12a5f1e0 60 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 0:1aac12a5f1e0 61 return false;
kbhagat6 0:1aac12a5f1e0 62 }
kbhagat6 0:1aac12a5f1e0 63 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 0:1aac12a5f1e0 64 return false;
kbhagat6 0:1aac12a5f1e0 65 }
kbhagat6 0:1aac12a5f1e0 66 if( !setProxIntLowThresh(DEFAULT_PILT) ) {
kbhagat6 0:1aac12a5f1e0 67 return false;
kbhagat6 0:1aac12a5f1e0 68 }
kbhagat6 0:1aac12a5f1e0 69 if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
kbhagat6 0:1aac12a5f1e0 70 return false;
kbhagat6 0:1aac12a5f1e0 71 }
kbhagat6 0:1aac12a5f1e0 72 if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
kbhagat6 0:1aac12a5f1e0 73 return false;
kbhagat6 0:1aac12a5f1e0 74 }
kbhagat6 0:1aac12a5f1e0 75 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
kbhagat6 0:1aac12a5f1e0 76 return false;
kbhagat6 0:1aac12a5f1e0 77 }
kbhagat6 0:1aac12a5f1e0 78 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
kbhagat6 0:1aac12a5f1e0 79 return false;
kbhagat6 0:1aac12a5f1e0 80 }
kbhagat6 0:1aac12a5f1e0 81
kbhagat6 0:1aac12a5f1e0 82 if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
kbhagat6 0:1aac12a5f1e0 83 return false;
kbhagat6 0:1aac12a5f1e0 84 }
kbhagat6 0:1aac12a5f1e0 85 if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
kbhagat6 0:1aac12a5f1e0 86 return false;
kbhagat6 0:1aac12a5f1e0 87 }
kbhagat6 0:1aac12a5f1e0 88 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
kbhagat6 0:1aac12a5f1e0 89 return false;
kbhagat6 0:1aac12a5f1e0 90 }
kbhagat6 0:1aac12a5f1e0 91 if( !setGestureGain(DEFAULT_GGAIN) ) {
kbhagat6 0:1aac12a5f1e0 92 return false;
kbhagat6 0:1aac12a5f1e0 93 }
kbhagat6 0:1aac12a5f1e0 94 if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
kbhagat6 0:1aac12a5f1e0 95 return false;
kbhagat6 0:1aac12a5f1e0 96 }
kbhagat6 0:1aac12a5f1e0 97 if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
kbhagat6 0:1aac12a5f1e0 98 return false;
kbhagat6 0:1aac12a5f1e0 99 }
kbhagat6 0:1aac12a5f1e0 100 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
kbhagat6 0:1aac12a5f1e0 101 return false;
kbhagat6 0:1aac12a5f1e0 102 }
kbhagat6 0:1aac12a5f1e0 103 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
kbhagat6 0:1aac12a5f1e0 104 return false;
kbhagat6 0:1aac12a5f1e0 105 }
kbhagat6 0:1aac12a5f1e0 106 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
kbhagat6 0:1aac12a5f1e0 107 return false;
kbhagat6 0:1aac12a5f1e0 108 }
kbhagat6 0:1aac12a5f1e0 109 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
kbhagat6 0:1aac12a5f1e0 110 return false;
kbhagat6 0:1aac12a5f1e0 111 }
kbhagat6 0:1aac12a5f1e0 112 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
kbhagat6 0:1aac12a5f1e0 113 return false;
kbhagat6 0:1aac12a5f1e0 114 }
kbhagat6 0:1aac12a5f1e0 115 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
kbhagat6 0:1aac12a5f1e0 116 return false;
kbhagat6 0:1aac12a5f1e0 117 }
kbhagat6 0:1aac12a5f1e0 118 if( !setGestureIntEnable(DEFAULT_GIEN) ) {
kbhagat6 0:1aac12a5f1e0 119 return false;
kbhagat6 0:1aac12a5f1e0 120 }
kbhagat6 0:1aac12a5f1e0 121
kbhagat6 0:1aac12a5f1e0 122 return true;
kbhagat6 0:1aac12a5f1e0 123
kbhagat6 0:1aac12a5f1e0 124 }
kbhagat6 0:1aac12a5f1e0 125
kbhagat6 0:1aac12a5f1e0 126 //#if 0
kbhagat6 0:1aac12a5f1e0 127 // /* Gesture config register dump */
kbhagat6 0:1aac12a5f1e0 128 // uint8_t reg;
kbhagat6 0:1aac12a5f1e0 129 // uint8_t val;
kbhagat6 0:1aac12a5f1e0 130 //
kbhagat6 0:1aac12a5f1e0 131 // for(reg = 0x80; reg <= 0xAF; reg++) {
kbhagat6 0:1aac12a5f1e0 132 // if( (reg != 0x82) && \
kbhagat6 0:1aac12a5f1e0 133 // (reg != 0x8A) && \
kbhagat6 0:1aac12a5f1e0 134 // (reg != 0x91) && \
kbhagat6 0:1aac12a5f1e0 135 // (reg != 0xA8) && \
kbhagat6 0:1aac12a5f1e0 136 // (reg != 0xAC) && \
kbhagat6 0:1aac12a5f1e0 137 // (reg != 0xAD) )
kbhagat6 0:1aac12a5f1e0 138 // {
kbhagat6 0:1aac12a5f1e0 139 // val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 0:1aac12a5f1e0 140 // if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 141 // printf("ERROR");
kbhagat6 0:1aac12a5f1e0 142 // }
kbhagat6 0:1aac12a5f1e0 143 // /*
kbhagat6 0:1aac12a5f1e0 144 // print(reg, HEX);
kbhagat6 0:1aac12a5f1e0 145 // print(": 0x");
kbhagat6 0:1aac12a5f1e0 146 // println(val, HEX);*/
kbhagat6 0:1aac12a5f1e0 147 // }
kbhagat6 0:1aac12a5f1e0 148 // }
kbhagat6 0:1aac12a5f1e0 149 //
kbhagat6 0:1aac12a5f1e0 150 // for(reg = 0xE4; reg <= 0xE7; reg++) {
kbhagat6 0:1aac12a5f1e0 151 // val= I2CreadByte(APDS9960_I2C_ADDR, reg);
kbhagat6 0:1aac12a5f1e0 152 // /* Serial.print(reg, HEX);
kbhagat6 0:1aac12a5f1e0 153 // Serial.print(": 0x");
kbhagat6 0:1aac12a5f1e0 154 // Serial.println(val, HEX);*/
kbhagat6 0:1aac12a5f1e0 155 // }
kbhagat6 0:1aac12a5f1e0 156 //#endif
kbhagat6 0:1aac12a5f1e0 157
kbhagat6 0:1aac12a5f1e0 158 // return true;
kbhagat6 0:1aac12a5f1e0 159
kbhagat6 0:1aac12a5f1e0 160
kbhagat6 0:1aac12a5f1e0 161
kbhagat6 0:1aac12a5f1e0 162
kbhagat6 0:1aac12a5f1e0 163 /**
kbhagat6 0:1aac12a5f1e0 164 * @brief Enables or disables a feature in the APDS-9960
kbhagat6 0:1aac12a5f1e0 165 *
kbhagat6 0:1aac12a5f1e0 166 * @param[in] mode which feature to enable
kbhagat6 0:1aac12a5f1e0 167 * @param[in] enable ON (1) or OFF (0)
kbhagat6 0:1aac12a5f1e0 168 * @return True if operation success. False otherwise.
kbhagat6 0:1aac12a5f1e0 169 */
kbhagat6 0:1aac12a5f1e0 170 bool glibr::setMode(uint8_t mode, uint8_t enable)
kbhagat6 0:1aac12a5f1e0 171 {
kbhagat6 0:1aac12a5f1e0 172 uint8_t reg_val;
kbhagat6 0:1aac12a5f1e0 173
kbhagat6 0:1aac12a5f1e0 174 /* Read current ENABLE register */
kbhagat6 0:1aac12a5f1e0 175 reg_val = getMode();
kbhagat6 0:1aac12a5f1e0 176 if( reg_val == ERROR ) {
kbhagat6 0:1aac12a5f1e0 177 return false;
kbhagat6 0:1aac12a5f1e0 178 }
kbhagat6 0:1aac12a5f1e0 179
kbhagat6 0:1aac12a5f1e0 180 /* Change bit(s) in ENABLE register */
kbhagat6 0:1aac12a5f1e0 181 enable = enable & 0x01;
kbhagat6 0:1aac12a5f1e0 182 if( mode >= 0 && mode <= 6 ) {
kbhagat6 0:1aac12a5f1e0 183 if (enable) {
kbhagat6 0:1aac12a5f1e0 184 reg_val |= (1 << mode);
kbhagat6 0:1aac12a5f1e0 185 } else {
kbhagat6 0:1aac12a5f1e0 186 reg_val &= ~(1 << mode);
kbhagat6 0:1aac12a5f1e0 187 }
kbhagat6 0:1aac12a5f1e0 188 } else if( mode == ALL ) {
kbhagat6 0:1aac12a5f1e0 189 if (enable) {
kbhagat6 0:1aac12a5f1e0 190 reg_val = 0x7F;
kbhagat6 0:1aac12a5f1e0 191 } else {
kbhagat6 0:1aac12a5f1e0 192 reg_val = 0x00;
kbhagat6 0:1aac12a5f1e0 193 }
kbhagat6 0:1aac12a5f1e0 194 }
kbhagat6 0:1aac12a5f1e0 195
kbhagat6 0:1aac12a5f1e0 196 /* Write value back to ENABLE register */
kbhagat6 0:1aac12a5f1e0 197 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
kbhagat6 0:1aac12a5f1e0 198 return false;
kbhagat6 0:1aac12a5f1e0 199 }
kbhagat6 0:1aac12a5f1e0 200
kbhagat6 0:1aac12a5f1e0 201
kbhagat6 0:1aac12a5f1e0 202
kbhagat6 0:1aac12a5f1e0 203 return true;
kbhagat6 0:1aac12a5f1e0 204 }
kbhagat6 0:1aac12a5f1e0 205
kbhagat6 0:1aac12a5f1e0 206 uint8_t glibr::getMode()
kbhagat6 0:1aac12a5f1e0 207 {
kbhagat6 0:1aac12a5f1e0 208 uint8_t val;
kbhagat6 0:1aac12a5f1e0 209 val= I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 210 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 211 return ERROR;
kbhagat6 0:1aac12a5f1e0 212 }
kbhagat6 0:1aac12a5f1e0 213 return val;
kbhagat6 0:1aac12a5f1e0 214 }
kbhagat6 0:1aac12a5f1e0 215
kbhagat6 0:1aac12a5f1e0 216
kbhagat6 0:1aac12a5f1e0 217
kbhagat6 0:1aac12a5f1e0 218 bool glibr::enableLightSensor(bool interrupts)
kbhagat6 0:1aac12a5f1e0 219 {
kbhagat6 0:1aac12a5f1e0 220
kbhagat6 0:1aac12a5f1e0 221 /* Set default gain, interrupts, enable power, and enable sensor */
kbhagat6 0:1aac12a5f1e0 222 if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
kbhagat6 0:1aac12a5f1e0 223 return false;
kbhagat6 0:1aac12a5f1e0 224 }
kbhagat6 0:1aac12a5f1e0 225 if( interrupts ) {
kbhagat6 0:1aac12a5f1e0 226 if( !setAmbientLightIntEnable(1) ) {
kbhagat6 0:1aac12a5f1e0 227 return false;
kbhagat6 0:1aac12a5f1e0 228 }
kbhagat6 0:1aac12a5f1e0 229 } else {
kbhagat6 0:1aac12a5f1e0 230 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 231 return false;
kbhagat6 0:1aac12a5f1e0 232 }
kbhagat6 0:1aac12a5f1e0 233 }
kbhagat6 0:1aac12a5f1e0 234 if( !enablePower() ){
kbhagat6 0:1aac12a5f1e0 235 return false;
kbhagat6 0:1aac12a5f1e0 236 }
kbhagat6 0:1aac12a5f1e0 237 if( !setMode(AMBIENT_LIGHT, 1) ) {
kbhagat6 0:1aac12a5f1e0 238 return false;
kbhagat6 0:1aac12a5f1e0 239 }
kbhagat6 0:1aac12a5f1e0 240
kbhagat6 0:1aac12a5f1e0 241 return true;
kbhagat6 0:1aac12a5f1e0 242
kbhagat6 0:1aac12a5f1e0 243 }
kbhagat6 0:1aac12a5f1e0 244
kbhagat6 0:1aac12a5f1e0 245 /**
kbhagat6 0:1aac12a5f1e0 246 * @brief Ends the light sensor on the APDS-9960
kbhagat6 0:1aac12a5f1e0 247 *
kbhagat6 0:1aac12a5f1e0 248 * @return True if sensor disabled correctly. False on error.
kbhagat6 0:1aac12a5f1e0 249 */
kbhagat6 0:1aac12a5f1e0 250 bool glibr::disableLightSensor()
kbhagat6 0:1aac12a5f1e0 251 {
kbhagat6 0:1aac12a5f1e0 252 if( !setAmbientLightIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 253 return false;
kbhagat6 0:1aac12a5f1e0 254 }
kbhagat6 0:1aac12a5f1e0 255 if( !setMode(AMBIENT_LIGHT, 0) ) {
kbhagat6 0:1aac12a5f1e0 256 return false;
kbhagat6 0:1aac12a5f1e0 257 }
kbhagat6 0:1aac12a5f1e0 258
kbhagat6 0:1aac12a5f1e0 259 return true;
kbhagat6 0:1aac12a5f1e0 260 }
kbhagat6 0:1aac12a5f1e0 261
kbhagat6 0:1aac12a5f1e0 262 /**
kbhagat6 0:1aac12a5f1e0 263 * @brief Starts the proximity sensor on the APDS-9960
kbhagat6 0:1aac12a5f1e0 264 *
kbhagat6 0:1aac12a5f1e0 265 * @param[in] interrupts true to enable hardware external interrupt on proximity
kbhagat6 0:1aac12a5f1e0 266 * @return True if sensor enabled correctly. False on error.
kbhagat6 0:1aac12a5f1e0 267 */
kbhagat6 0:1aac12a5f1e0 268 bool glibr::enableProximitySensor(bool interrupts)
kbhagat6 0:1aac12a5f1e0 269 {
kbhagat6 0:1aac12a5f1e0 270 /* Set default gain, LED, interrupts, enable power, and enable sensor */
kbhagat6 0:1aac12a5f1e0 271 if( !setProximityGain(DEFAULT_PGAIN) ) {
kbhagat6 0:1aac12a5f1e0 272 return false;
kbhagat6 0:1aac12a5f1e0 273 }
kbhagat6 0:1aac12a5f1e0 274 if( !setLEDDrive(DEFAULT_LDRIVE) ) {
kbhagat6 0:1aac12a5f1e0 275 return false;
kbhagat6 0:1aac12a5f1e0 276 }
kbhagat6 0:1aac12a5f1e0 277 if( interrupts ) {
kbhagat6 0:1aac12a5f1e0 278 if( !setProximityIntEnable(1) ) {
kbhagat6 0:1aac12a5f1e0 279 return false;
kbhagat6 0:1aac12a5f1e0 280 }
kbhagat6 0:1aac12a5f1e0 281 } else {
kbhagat6 0:1aac12a5f1e0 282 if( !setProximityIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 283 return false;
kbhagat6 0:1aac12a5f1e0 284 }
kbhagat6 0:1aac12a5f1e0 285 }
kbhagat6 0:1aac12a5f1e0 286 if( !enablePower() ){
kbhagat6 0:1aac12a5f1e0 287 return false;
kbhagat6 0:1aac12a5f1e0 288 }
kbhagat6 0:1aac12a5f1e0 289 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 0:1aac12a5f1e0 290 return false;
kbhagat6 0:1aac12a5f1e0 291 }
kbhagat6 0:1aac12a5f1e0 292
kbhagat6 0:1aac12a5f1e0 293 return true;
kbhagat6 0:1aac12a5f1e0 294 }
kbhagat6 0:1aac12a5f1e0 295
kbhagat6 0:1aac12a5f1e0 296 /**
kbhagat6 0:1aac12a5f1e0 297 * @brief Ends the proximity sensor on the APDS-9960
kbhagat6 0:1aac12a5f1e0 298 *
kbhagat6 0:1aac12a5f1e0 299 * @return True if sensor disabled correctly. False on error.
kbhagat6 0:1aac12a5f1e0 300 */
kbhagat6 0:1aac12a5f1e0 301 bool glibr::disableProximitySensor()
kbhagat6 0:1aac12a5f1e0 302 {
kbhagat6 0:1aac12a5f1e0 303 if( !setProximityIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 304 return false;
kbhagat6 0:1aac12a5f1e0 305 }
kbhagat6 0:1aac12a5f1e0 306 if( !setMode(PROXIMITY, 0) ) {
kbhagat6 0:1aac12a5f1e0 307 return false;
kbhagat6 0:1aac12a5f1e0 308 }
kbhagat6 0:1aac12a5f1e0 309
kbhagat6 0:1aac12a5f1e0 310 return true;
kbhagat6 0:1aac12a5f1e0 311 }
kbhagat6 0:1aac12a5f1e0 312
kbhagat6 0:1aac12a5f1e0 313
kbhagat6 0:1aac12a5f1e0 314 /**
kbhagat6 0:1aac12a5f1e0 315 * @brief Starts the gesture recognition engine on the APDS-9960
kbhagat6 0:1aac12a5f1e0 316 *
kbhagat6 0:1aac12a5f1e0 317 * @param[in] interrupts true to enable hardware external interrupt on gesture
kbhagat6 0:1aac12a5f1e0 318 * @return True if engine enabled correctly. False on error.
kbhagat6 0:1aac12a5f1e0 319 */
kbhagat6 0:1aac12a5f1e0 320 bool glibr::enableGestureSensor(bool interrupts)
kbhagat6 0:1aac12a5f1e0 321 {
kbhagat6 0:1aac12a5f1e0 322
kbhagat6 0:1aac12a5f1e0 323 /* Enable gesture mode
kbhagat6 0:1aac12a5f1e0 324 Set ENABLE to 0 (power off)
kbhagat6 0:1aac12a5f1e0 325 Set WTIME to 0xFF
kbhagat6 0:1aac12a5f1e0 326 Set AUX to LED_BOOST_300
kbhagat6 0:1aac12a5f1e0 327 Enable PON, WEN, PEN, GEN in ENABLE
kbhagat6 0:1aac12a5f1e0 328 */
kbhagat6 0:1aac12a5f1e0 329
kbhagat6 0:1aac12a5f1e0 330 resetGestureParameters();
kbhagat6 0:1aac12a5f1e0 331 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, 0xFF) ) {
kbhagat6 0:1aac12a5f1e0 332 return false;
kbhagat6 0:1aac12a5f1e0 333 }
kbhagat6 0:1aac12a5f1e0 334 if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
kbhagat6 0:1aac12a5f1e0 335 return false;
kbhagat6 0:1aac12a5f1e0 336 }
kbhagat6 0:1aac12a5f1e0 337 if( !setLEDBoost(LED_BOOST_300) ) {
kbhagat6 0:1aac12a5f1e0 338 return false;
kbhagat6 0:1aac12a5f1e0 339 }
kbhagat6 0:1aac12a5f1e0 340 if( interrupts ) {
kbhagat6 0:1aac12a5f1e0 341 if( !setGestureIntEnable(1) ) {
kbhagat6 0:1aac12a5f1e0 342 return false;
kbhagat6 0:1aac12a5f1e0 343 }
kbhagat6 0:1aac12a5f1e0 344 } else {
kbhagat6 0:1aac12a5f1e0 345 if( !setGestureIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 346 return false;
kbhagat6 0:1aac12a5f1e0 347 }
kbhagat6 0:1aac12a5f1e0 348 }
kbhagat6 0:1aac12a5f1e0 349 if( !setGestureMode(1) ) {
kbhagat6 0:1aac12a5f1e0 350 return false;
kbhagat6 0:1aac12a5f1e0 351 }
kbhagat6 0:1aac12a5f1e0 352 if( !enablePower() ){
kbhagat6 0:1aac12a5f1e0 353 return false;
kbhagat6 0:1aac12a5f1e0 354 }
kbhagat6 0:1aac12a5f1e0 355 if( !setMode(WAIT, 1) ) {
kbhagat6 0:1aac12a5f1e0 356 return false;
kbhagat6 0:1aac12a5f1e0 357 }
kbhagat6 0:1aac12a5f1e0 358 if( !setMode(PROXIMITY, 1) ) {
kbhagat6 0:1aac12a5f1e0 359 return false;
kbhagat6 0:1aac12a5f1e0 360 }
kbhagat6 0:1aac12a5f1e0 361 if( !setMode(GESTURE, 1) ) {
kbhagat6 0:1aac12a5f1e0 362 return false;
kbhagat6 0:1aac12a5f1e0 363 }
kbhagat6 0:1aac12a5f1e0 364
kbhagat6 0:1aac12a5f1e0 365 return true;
kbhagat6 0:1aac12a5f1e0 366 }
kbhagat6 0:1aac12a5f1e0 367
kbhagat6 0:1aac12a5f1e0 368 /**
kbhagat6 0:1aac12a5f1e0 369 * @brief Ends the gesture recognition engine on the APDS-9960
kbhagat6 0:1aac12a5f1e0 370 *
kbhagat6 0:1aac12a5f1e0 371 * @return True if engine disabled correctly. False on error.
kbhagat6 0:1aac12a5f1e0 372 */
kbhagat6 0:1aac12a5f1e0 373 bool glibr::disableGestureSensor()
kbhagat6 0:1aac12a5f1e0 374 {
kbhagat6 0:1aac12a5f1e0 375 resetGestureParameters();
kbhagat6 0:1aac12a5f1e0 376 if( !setGestureIntEnable(0) ) {
kbhagat6 0:1aac12a5f1e0 377 return false;
kbhagat6 0:1aac12a5f1e0 378 }
kbhagat6 0:1aac12a5f1e0 379 if( !setGestureMode(0) ) {
kbhagat6 0:1aac12a5f1e0 380 return false;
kbhagat6 0:1aac12a5f1e0 381 }
kbhagat6 0:1aac12a5f1e0 382 if( !setMode(GESTURE, 0) ) {
kbhagat6 0:1aac12a5f1e0 383 return false;
kbhagat6 0:1aac12a5f1e0 384 }
kbhagat6 0:1aac12a5f1e0 385
kbhagat6 0:1aac12a5f1e0 386 return true;
kbhagat6 0:1aac12a5f1e0 387 }
kbhagat6 0:1aac12a5f1e0 388
kbhagat6 0:1aac12a5f1e0 389
kbhagat6 0:1aac12a5f1e0 390 /**
kbhagat6 0:1aac12a5f1e0 391 * @brief Determines if there is a gesture available for reading
kbhagat6 0:1aac12a5f1e0 392 *
kbhagat6 0:1aac12a5f1e0 393 * @return True if gesture available. False otherwise.
kbhagat6 0:1aac12a5f1e0 394 */
kbhagat6 0:1aac12a5f1e0 395 bool glibr::isGestureAvailable()
kbhagat6 0:1aac12a5f1e0 396 {
kbhagat6 0:1aac12a5f1e0 397 uint8_t val;
kbhagat6 0:1aac12a5f1e0 398
kbhagat6 0:1aac12a5f1e0 399 /* Read value from GSTATUS register */
kbhagat6 0:1aac12a5f1e0 400 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 0:1aac12a5f1e0 401 if( val==ERROR) {
kbhagat6 0:1aac12a5f1e0 402 return ERROR;
kbhagat6 0:1aac12a5f1e0 403 }
kbhagat6 0:1aac12a5f1e0 404
kbhagat6 0:1aac12a5f1e0 405 /* Shift and mask out GVALID bit */
kbhagat6 0:1aac12a5f1e0 406 val &= APDS9960_GVALID;
kbhagat6 0:1aac12a5f1e0 407
kbhagat6 0:1aac12a5f1e0 408 /* Return true/false based on GVALID bit */
kbhagat6 0:1aac12a5f1e0 409 if( val == 1) {
kbhagat6 0:1aac12a5f1e0 410 return true;
kbhagat6 0:1aac12a5f1e0 411 } else {
kbhagat6 0:1aac12a5f1e0 412 return false;
kbhagat6 0:1aac12a5f1e0 413 }
kbhagat6 0:1aac12a5f1e0 414 }
kbhagat6 0:1aac12a5f1e0 415
kbhagat6 0:1aac12a5f1e0 416 int glibr::readGesture()
kbhagat6 0:1aac12a5f1e0 417 {
kbhagat6 0:1aac12a5f1e0 418 uint8_t fifo_level = 0;
kbhagat6 0:1aac12a5f1e0 419 // uint8_t bytes_expected= 0;
kbhagat6 0:1aac12a5f1e0 420 int check;
kbhagat6 0:1aac12a5f1e0 421 //char fifo_data[128];
kbhagat6 0:1aac12a5f1e0 422 char fifo_data[128];
kbhagat6 0:1aac12a5f1e0 423 char *fptr;
kbhagat6 0:1aac12a5f1e0 424 fptr= fifo_data;
kbhagat6 0:1aac12a5f1e0 425
kbhagat6 0:1aac12a5f1e0 426 uint8_t gstatus;
kbhagat6 0:1aac12a5f1e0 427 int motion;
kbhagat6 0:1aac12a5f1e0 428 int i;
kbhagat6 0:1aac12a5f1e0 429
kbhagat6 0:1aac12a5f1e0 430 /* Make sure that power and gesture is on and data is valid */
kbhagat6 0:1aac12a5f1e0 431 if( !isGestureAvailable() || !(getMode() & 0x41) ) {
kbhagat6 0:1aac12a5f1e0 432 return DIR_NONE;
kbhagat6 0:1aac12a5f1e0 433 }
kbhagat6 0:1aac12a5f1e0 434
kbhagat6 0:1aac12a5f1e0 435
kbhagat6 0:1aac12a5f1e0 436 /* Keep looping as long as gesture data is valid */
kbhagat6 0:1aac12a5f1e0 437 while(1) {
kbhagat6 0:1aac12a5f1e0 438
kbhagat6 0:1aac12a5f1e0 439 /* Wait some time to collect next batch of FIFO data */
kbhagat6 0:1aac12a5f1e0 440 wait(FIFO_PAUSE_TIME);
kbhagat6 0:1aac12a5f1e0 441
kbhagat6 0:1aac12a5f1e0 442 /* Get the contents of the STATUS register. Is data still valid? */
kbhagat6 0:1aac12a5f1e0 443
kbhagat6 0:1aac12a5f1e0 444 gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
kbhagat6 0:1aac12a5f1e0 445 if( gstatus==ERROR ) {
kbhagat6 0:1aac12a5f1e0 446 return ERROR;
kbhagat6 0:1aac12a5f1e0 447 }
kbhagat6 0:1aac12a5f1e0 448 /* If we have valid data, read in FIFO */
kbhagat6 0:1aac12a5f1e0 449 if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
kbhagat6 0:1aac12a5f1e0 450
kbhagat6 0:1aac12a5f1e0 451 /* Read the current FIFO level */
kbhagat6 0:1aac12a5f1e0 452 fifo_level=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL);
kbhagat6 0:1aac12a5f1e0 453 if( fifo_level==ERROR ) {
kbhagat6 0:1aac12a5f1e0 454 return ERROR;
kbhagat6 0:1aac12a5f1e0 455 }
kbhagat6 0:1aac12a5f1e0 456
kbhagat6 0:1aac12a5f1e0 457 //#if DEBUG
kbhagat6 0:1aac12a5f1e0 458 // Serial.print("FIFO Level: ");
kbhagat6 0:1aac12a5f1e0 459 // Serial.println(fifo_level);
kbhagat6 0:1aac12a5f1e0 460 //#endif
kbhagat6 0:1aac12a5f1e0 461
kbhagat6 0:1aac12a5f1e0 462 /* If there's stuff in the FIFO, read it into our data block */ //NEED TO FIGURE OUT WHAT THIS IS DOING.
kbhagat6 0:1aac12a5f1e0 463
kbhagat6 0:1aac12a5f1e0 464 if( fifo_level > 0) {
kbhagat6 0:1aac12a5f1e0 465 check = I2CReadDataBlock(APDS9960_I2C_ADDR,APDS9960_GFIFO_U,
kbhagat6 0:1aac12a5f1e0 466 fptr,
kbhagat6 0:1aac12a5f1e0 467 (fifo_level * 4) );
kbhagat6 0:1aac12a5f1e0 468
kbhagat6 0:1aac12a5f1e0 469 if( check == -1 ) {
kbhagat6 0:1aac12a5f1e0 470 return ERROR;
kbhagat6 0:1aac12a5f1e0 471 }
kbhagat6 0:1aac12a5f1e0 472
kbhagat6 0:1aac12a5f1e0 473 #if DEBUG
kbhagat6 0:1aac12a5f1e0 474 //Serial.print("FIFO Dump: ");
kbhagat6 0:1aac12a5f1e0 475 for ( i = 0; i < (fifo_level * 4); i++ ) {
kbhagat6 0:1aac12a5f1e0 476 // Serial.print(fifo_data[i]);
kbhagat6 0:1aac12a5f1e0 477 // Serial.print(" ");
kbhagat6 0:1aac12a5f1e0 478 }
kbhagat6 0:1aac12a5f1e0 479 //Serial.println();
kbhagat6 0:1aac12a5f1e0 480 #endif
kbhagat6 0:1aac12a5f1e0 481
kbhagat6 0:1aac12a5f1e0 482 /* If at least 1 set of data, sort the data into U/D/L/R */
kbhagat6 0:1aac12a5f1e0 483 if((fifo_level * 4) >= 4 ) {
kbhagat6 0:1aac12a5f1e0 484 for( i = 0; i < (fifo_level * 4); i += 4 ) {
kbhagat6 0:1aac12a5f1e0 485 gesture_data_.u_data[gesture_data_.sindex] = \
kbhagat6 0:1aac12a5f1e0 486 fifo_data[i + 0];
kbhagat6 0:1aac12a5f1e0 487 gesture_data_.d_data[gesture_data_.sindex] = \
kbhagat6 0:1aac12a5f1e0 488 fifo_data[i + 1];
kbhagat6 0:1aac12a5f1e0 489 gesture_data_.l_data[gesture_data_.sindex] = \
kbhagat6 0:1aac12a5f1e0 490 fifo_data[i + 2];
kbhagat6 0:1aac12a5f1e0 491 gesture_data_.r_data[gesture_data_.sindex] = \
kbhagat6 0:1aac12a5f1e0 492 fifo_data[i + 3];
kbhagat6 0:1aac12a5f1e0 493 gesture_data_.sindex++;
kbhagat6 0:1aac12a5f1e0 494 gesture_data_.total_gestures++;
kbhagat6 0:1aac12a5f1e0 495 }
kbhagat6 0:1aac12a5f1e0 496
kbhagat6 0:1aac12a5f1e0 497 #if DEBUG
kbhagat6 0:1aac12a5f1e0 498 // Serial.print("Up Data: ");
kbhagat6 0:1aac12a5f1e0 499 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 0:1aac12a5f1e0 500 // Serial.print(gesture_data_.u_data[i]);
kbhagat6 0:1aac12a5f1e0 501 // Serial.print(" ");
kbhagat6 0:1aac12a5f1e0 502 }
kbhagat6 0:1aac12a5f1e0 503 // Serial.println();
kbhagat6 0:1aac12a5f1e0 504 #endif
kbhagat6 0:1aac12a5f1e0 505
kbhagat6 0:1aac12a5f1e0 506 /* Filter and process gesture data. Decode near/far state */
kbhagat6 0:1aac12a5f1e0 507 if( processGestureData() ) {
kbhagat6 0:1aac12a5f1e0 508 if( decodeGesture() ) {
kbhagat6 0:1aac12a5f1e0 509 //***TODO: U-Turn Gestures
kbhagat6 0:1aac12a5f1e0 510 #if DEBUG
kbhagat6 0:1aac12a5f1e0 511 //Serial.println(gesture_motion_);
kbhagat6 0:1aac12a5f1e0 512 #endif
kbhagat6 0:1aac12a5f1e0 513 }
kbhagat6 0:1aac12a5f1e0 514 }
kbhagat6 0:1aac12a5f1e0 515
kbhagat6 0:1aac12a5f1e0 516 /* Reset data */
kbhagat6 0:1aac12a5f1e0 517 gesture_data_.sindex = 0;
kbhagat6 0:1aac12a5f1e0 518 gesture_data_.total_gestures = 0;
kbhagat6 0:1aac12a5f1e0 519 }
kbhagat6 0:1aac12a5f1e0 520 }
kbhagat6 0:1aac12a5f1e0 521 } else {
kbhagat6 0:1aac12a5f1e0 522
kbhagat6 0:1aac12a5f1e0 523 /* Determine best guessed gesture and clean up */
kbhagat6 0:1aac12a5f1e0 524 wait(FIFO_PAUSE_TIME);
kbhagat6 0:1aac12a5f1e0 525 decodeGesture();
kbhagat6 0:1aac12a5f1e0 526 motion = gesture_motion_;
kbhagat6 0:1aac12a5f1e0 527 #if DEBUG
kbhagat6 0:1aac12a5f1e0 528 // Serial.print("END: ");
kbhagat6 0:1aac12a5f1e0 529 // Serial.println(gesture_motion_);
kbhagat6 0:1aac12a5f1e0 530 #endif
kbhagat6 0:1aac12a5f1e0 531 resetGestureParameters();
kbhagat6 0:1aac12a5f1e0 532 return motion;
kbhagat6 0:1aac12a5f1e0 533 }
kbhagat6 0:1aac12a5f1e0 534 }
kbhagat6 0:1aac12a5f1e0 535 // delete fptr;
kbhagat6 0:1aac12a5f1e0 536 }
kbhagat6 0:1aac12a5f1e0 537 /**
kbhagat6 0:1aac12a5f1e0 538 * Turn the APDS-9960 on
kbhagat6 0:1aac12a5f1e0 539 *
kbhagat6 0:1aac12a5f1e0 540 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 541 */
kbhagat6 0:1aac12a5f1e0 542 bool glibr::enablePower()
kbhagat6 0:1aac12a5f1e0 543 {
kbhagat6 0:1aac12a5f1e0 544 if( !setMode(POWER, 1) ) {
kbhagat6 0:1aac12a5f1e0 545 return false;
kbhagat6 0:1aac12a5f1e0 546 }
kbhagat6 0:1aac12a5f1e0 547
kbhagat6 0:1aac12a5f1e0 548 return true;
kbhagat6 0:1aac12a5f1e0 549 }
kbhagat6 0:1aac12a5f1e0 550
kbhagat6 0:1aac12a5f1e0 551 /**
kbhagat6 0:1aac12a5f1e0 552 * Turn the APDS-9960 off
kbhagat6 0:1aac12a5f1e0 553 *
kbhagat6 0:1aac12a5f1e0 554 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 555 */
kbhagat6 0:1aac12a5f1e0 556 bool glibr::disablePower()
kbhagat6 0:1aac12a5f1e0 557 {
kbhagat6 0:1aac12a5f1e0 558 if( !setMode(POWER, 0) ) {
kbhagat6 0:1aac12a5f1e0 559 return false;
kbhagat6 0:1aac12a5f1e0 560 }
kbhagat6 0:1aac12a5f1e0 561
kbhagat6 0:1aac12a5f1e0 562 return true;
kbhagat6 0:1aac12a5f1e0 563 }
kbhagat6 0:1aac12a5f1e0 564
kbhagat6 0:1aac12a5f1e0 565 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 566 * Ambient light and color sensor controls
kbhagat6 0:1aac12a5f1e0 567 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 568
kbhagat6 0:1aac12a5f1e0 569 /**
kbhagat6 0:1aac12a5f1e0 570 * @brief Reads the ambient (clear) light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 571 *
kbhagat6 0:1aac12a5f1e0 572 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 573 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 574 */
kbhagat6 0:1aac12a5f1e0 575 bool glibr::readAmbientLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 576 {
kbhagat6 0:1aac12a5f1e0 577 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 578 val = 0;
kbhagat6 0:1aac12a5f1e0 579
kbhagat6 0:1aac12a5f1e0 580 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 581 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL);
kbhagat6 0:1aac12a5f1e0 582 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 583 return false;
kbhagat6 0:1aac12a5f1e0 584 }
kbhagat6 0:1aac12a5f1e0 585 val = val_byte;
kbhagat6 0:1aac12a5f1e0 586
kbhagat6 0:1aac12a5f1e0 587 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 588
kbhagat6 0:1aac12a5f1e0 589 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH);
kbhagat6 0:1aac12a5f1e0 590 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 591 return false;
kbhagat6 0:1aac12a5f1e0 592 }
kbhagat6 0:1aac12a5f1e0 593 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 594
kbhagat6 0:1aac12a5f1e0 595 return true;
kbhagat6 0:1aac12a5f1e0 596 }
kbhagat6 0:1aac12a5f1e0 597
kbhagat6 0:1aac12a5f1e0 598 /**
kbhagat6 0:1aac12a5f1e0 599 * @brief Reads the red light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 600 *
kbhagat6 0:1aac12a5f1e0 601 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 602 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 603 */
kbhagat6 0:1aac12a5f1e0 604 bool glibr::readRedLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 605 {
kbhagat6 0:1aac12a5f1e0 606 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 607 val = 0;
kbhagat6 0:1aac12a5f1e0 608
kbhagat6 0:1aac12a5f1e0 609 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 610 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL);
kbhagat6 0:1aac12a5f1e0 611 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 612 return false;
kbhagat6 0:1aac12a5f1e0 613 }
kbhagat6 0:1aac12a5f1e0 614
kbhagat6 0:1aac12a5f1e0 615 val = val_byte;
kbhagat6 0:1aac12a5f1e0 616
kbhagat6 0:1aac12a5f1e0 617 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 618 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH);
kbhagat6 0:1aac12a5f1e0 619 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 620 return false;
kbhagat6 0:1aac12a5f1e0 621 }
kbhagat6 0:1aac12a5f1e0 622 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 623
kbhagat6 0:1aac12a5f1e0 624 return true;
kbhagat6 0:1aac12a5f1e0 625 }
kbhagat6 0:1aac12a5f1e0 626
kbhagat6 0:1aac12a5f1e0 627 /**
kbhagat6 0:1aac12a5f1e0 628 * @brief Reads the green light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 629 *
kbhagat6 0:1aac12a5f1e0 630 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 631 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 632 */
kbhagat6 0:1aac12a5f1e0 633
kbhagat6 0:1aac12a5f1e0 634 bool glibr::readGreenLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 635 {
kbhagat6 0:1aac12a5f1e0 636 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 637 val = 0;
kbhagat6 0:1aac12a5f1e0 638
kbhagat6 0:1aac12a5f1e0 639 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 640 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL);
kbhagat6 0:1aac12a5f1e0 641 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 642 return false;
kbhagat6 0:1aac12a5f1e0 643 }
kbhagat6 0:1aac12a5f1e0 644
kbhagat6 0:1aac12a5f1e0 645 val = val_byte;
kbhagat6 0:1aac12a5f1e0 646
kbhagat6 0:1aac12a5f1e0 647 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 648 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH);
kbhagat6 0:1aac12a5f1e0 649 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 650 return false;
kbhagat6 0:1aac12a5f1e0 651 }
kbhagat6 0:1aac12a5f1e0 652 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 653
kbhagat6 0:1aac12a5f1e0 654 return true;
kbhagat6 0:1aac12a5f1e0 655 }
kbhagat6 0:1aac12a5f1e0 656
kbhagat6 0:1aac12a5f1e0 657 /**
kbhagat6 0:1aac12a5f1e0 658 * @brief Reads the red light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 659 *
kbhagat6 0:1aac12a5f1e0 660 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 661 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 662 */
kbhagat6 0:1aac12a5f1e0 663
kbhagat6 0:1aac12a5f1e0 664 bool glibr::readBlueLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 665 {
kbhagat6 0:1aac12a5f1e0 666 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 667 val = 0;
kbhagat6 0:1aac12a5f1e0 668
kbhagat6 0:1aac12a5f1e0 669 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 670 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL);
kbhagat6 0:1aac12a5f1e0 671 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 672 return false;
kbhagat6 0:1aac12a5f1e0 673 }
kbhagat6 0:1aac12a5f1e0 674
kbhagat6 0:1aac12a5f1e0 675 val = val_byte;
kbhagat6 0:1aac12a5f1e0 676
kbhagat6 0:1aac12a5f1e0 677 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 678 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH);
kbhagat6 0:1aac12a5f1e0 679 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 680 return false;
kbhagat6 0:1aac12a5f1e0 681 }
kbhagat6 0:1aac12a5f1e0 682 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 683
kbhagat6 0:1aac12a5f1e0 684 return true;
kbhagat6 0:1aac12a5f1e0 685 }
kbhagat6 0:1aac12a5f1e0 686
kbhagat6 0:1aac12a5f1e0 687 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 688 * Proximity sensor controls
kbhagat6 0:1aac12a5f1e0 689 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 690
kbhagat6 0:1aac12a5f1e0 691 /**
kbhagat6 0:1aac12a5f1e0 692 * @brief Reads the proximity level as an 8-bit value
kbhagat6 0:1aac12a5f1e0 693 *
kbhagat6 0:1aac12a5f1e0 694 * @param[out] val value of the proximity sensor.
kbhagat6 0:1aac12a5f1e0 695 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 696 */
kbhagat6 0:1aac12a5f1e0 697 bool glibr::readProximity(uint8_t &val)
kbhagat6 0:1aac12a5f1e0 698 {
kbhagat6 0:1aac12a5f1e0 699 val = 0;
kbhagat6 0:1aac12a5f1e0 700
kbhagat6 0:1aac12a5f1e0 701 /* Read value from proximity data register */
kbhagat6 0:1aac12a5f1e0 702 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA);
kbhagat6 0:1aac12a5f1e0 703
kbhagat6 0:1aac12a5f1e0 704 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 705 return false;
kbhagat6 0:1aac12a5f1e0 706 }
kbhagat6 0:1aac12a5f1e0 707
kbhagat6 0:1aac12a5f1e0 708 return true;
kbhagat6 0:1aac12a5f1e0 709 }
kbhagat6 0:1aac12a5f1e0 710
kbhagat6 0:1aac12a5f1e0 711 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 712 * High-level gesture controls
kbhagat6 0:1aac12a5f1e0 713 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 714
kbhagat6 0:1aac12a5f1e0 715 /**
kbhagat6 0:1aac12a5f1e0 716 * @brief Resets all the parameters in the gesture data member
kbhagat6 0:1aac12a5f1e0 717 */
kbhagat6 0:1aac12a5f1e0 718 void glibr::resetGestureParameters()
kbhagat6 0:1aac12a5f1e0 719 {
kbhagat6 0:1aac12a5f1e0 720 gesture_data_.sindex = 0;
kbhagat6 0:1aac12a5f1e0 721 gesture_data_.total_gestures = 0;
kbhagat6 0:1aac12a5f1e0 722
kbhagat6 0:1aac12a5f1e0 723 gesture_ud_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 724 gesture_lr_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 725
kbhagat6 0:1aac12a5f1e0 726 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 727 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 728
kbhagat6 0:1aac12a5f1e0 729 gesture_near_count_ = 0;
kbhagat6 0:1aac12a5f1e0 730 gesture_far_count_ = 0;
kbhagat6 0:1aac12a5f1e0 731
kbhagat6 0:1aac12a5f1e0 732 gesture_state_ = 0;
kbhagat6 0:1aac12a5f1e0 733 gesture_motion_ = DIR_NONE;
kbhagat6 0:1aac12a5f1e0 734 }
kbhagat6 0:1aac12a5f1e0 735
kbhagat6 0:1aac12a5f1e0 736 bool glibr::processGestureData()
kbhagat6 0:1aac12a5f1e0 737 {
kbhagat6 0:1aac12a5f1e0 738 uint8_t u_first = 0;
kbhagat6 0:1aac12a5f1e0 739 uint8_t d_first = 0;
kbhagat6 0:1aac12a5f1e0 740 uint8_t l_first = 0;
kbhagat6 0:1aac12a5f1e0 741 uint8_t r_first = 0;
kbhagat6 0:1aac12a5f1e0 742 uint8_t u_last = 0;
kbhagat6 0:1aac12a5f1e0 743 uint8_t d_last = 0;
kbhagat6 0:1aac12a5f1e0 744 uint8_t l_last = 0;
kbhagat6 0:1aac12a5f1e0 745 uint8_t r_last = 0;
kbhagat6 0:1aac12a5f1e0 746 int ud_ratio_first;
kbhagat6 0:1aac12a5f1e0 747 int lr_ratio_first;
kbhagat6 0:1aac12a5f1e0 748 int ud_ratio_last;
kbhagat6 0:1aac12a5f1e0 749 int lr_ratio_last;
kbhagat6 0:1aac12a5f1e0 750 int ud_delta;
kbhagat6 0:1aac12a5f1e0 751 int lr_delta;
kbhagat6 0:1aac12a5f1e0 752 int i;
kbhagat6 0:1aac12a5f1e0 753
kbhagat6 0:1aac12a5f1e0 754 /* If we have less than 4 total gestures, that's not enough */
kbhagat6 0:1aac12a5f1e0 755 if( gesture_data_.total_gestures <= 4 ) {
kbhagat6 0:1aac12a5f1e0 756 return false;
kbhagat6 0:1aac12a5f1e0 757 }
kbhagat6 0:1aac12a5f1e0 758
kbhagat6 0:1aac12a5f1e0 759 /* Check to make sure our data isn't out of bounds */
kbhagat6 0:1aac12a5f1e0 760 if( (gesture_data_.total_gestures <= 32) && \
kbhagat6 0:1aac12a5f1e0 761 (gesture_data_.total_gestures > 0) ) {
kbhagat6 0:1aac12a5f1e0 762
kbhagat6 0:1aac12a5f1e0 763 /* Find the first value in U/D/L/R above the threshold */
kbhagat6 0:1aac12a5f1e0 764 for( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 0:1aac12a5f1e0 765 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 766 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 767 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 768 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 0:1aac12a5f1e0 769
kbhagat6 0:1aac12a5f1e0 770 u_first = gesture_data_.u_data[i];
kbhagat6 0:1aac12a5f1e0 771 d_first = gesture_data_.d_data[i];
kbhagat6 0:1aac12a5f1e0 772 l_first = gesture_data_.l_data[i];
kbhagat6 0:1aac12a5f1e0 773 r_first = gesture_data_.r_data[i];
kbhagat6 0:1aac12a5f1e0 774 break;
kbhagat6 0:1aac12a5f1e0 775 }
kbhagat6 0:1aac12a5f1e0 776 }
kbhagat6 0:1aac12a5f1e0 777
kbhagat6 0:1aac12a5f1e0 778 /* If one of the _first values is 0, then there is no good data */
kbhagat6 0:1aac12a5f1e0 779 if( (u_first == 0) || (d_first == 0) || \
kbhagat6 0:1aac12a5f1e0 780 (l_first == 0) || (r_first == 0) ) {
kbhagat6 0:1aac12a5f1e0 781
kbhagat6 0:1aac12a5f1e0 782 return false;
kbhagat6 0:1aac12a5f1e0 783 }
kbhagat6 0:1aac12a5f1e0 784 /* Find the last value in U/D/L/R above the threshold */
kbhagat6 0:1aac12a5f1e0 785 for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
kbhagat6 0:1aac12a5f1e0 786 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 787 Serial.print(F("Finding last: "));
kbhagat6 0:1aac12a5f1e0 788 Serial.print(F("U:"));
kbhagat6 0:1aac12a5f1e0 789 Serial.print(gesture_data_.u_data[i]);
kbhagat6 0:1aac12a5f1e0 790 Serial.print(F(" D:"));
kbhagat6 0:1aac12a5f1e0 791 Serial.print(gesture_data_.d_data[i]);
kbhagat6 0:1aac12a5f1e0 792 Serial.print(F(" L:"));
kbhagat6 0:1aac12a5f1e0 793 Serial.print(gesture_data_.l_data[i]);
kbhagat6 0:1aac12a5f1e0 794 Serial.print(F(" R:"));
kbhagat6 0:1aac12a5f1e0 795 Serial.println(gesture_data_.r_data[i]);
kbhagat6 0:1aac12a5f1e0 796 #endif */
kbhagat6 0:1aac12a5f1e0 797 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 798 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 799 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 800 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 0:1aac12a5f1e0 801
kbhagat6 0:1aac12a5f1e0 802 u_last = gesture_data_.u_data[i];
kbhagat6 0:1aac12a5f1e0 803 d_last = gesture_data_.d_data[i];
kbhagat6 0:1aac12a5f1e0 804 l_last = gesture_data_.l_data[i];
kbhagat6 0:1aac12a5f1e0 805 r_last = gesture_data_.r_data[i];
kbhagat6 0:1aac12a5f1e0 806 break;
kbhagat6 0:1aac12a5f1e0 807 }
kbhagat6 0:1aac12a5f1e0 808 }
kbhagat6 0:1aac12a5f1e0 809 }
kbhagat6 0:1aac12a5f1e0 810
kbhagat6 0:1aac12a5f1e0 811 /* Calculate the first vs. last ratio of up/down and left/right */
kbhagat6 0:1aac12a5f1e0 812 ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
kbhagat6 0:1aac12a5f1e0 813 lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
kbhagat6 0:1aac12a5f1e0 814 ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
kbhagat6 0:1aac12a5f1e0 815 lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
kbhagat6 0:1aac12a5f1e0 816
kbhagat6 0:1aac12a5f1e0 817 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 818 Serial.print(F("Last Values: "));
kbhagat6 0:1aac12a5f1e0 819 Serial.print(F("U:"));
kbhagat6 0:1aac12a5f1e0 820 Serial.print(u_last);
kbhagat6 0:1aac12a5f1e0 821 Serial.print(F(" D:"));
kbhagat6 0:1aac12a5f1e0 822 Serial.print(d_last);
kbhagat6 0:1aac12a5f1e0 823 Serial.print(F(" L:"));
kbhagat6 0:1aac12a5f1e0 824 Serial.print(l_last);
kbhagat6 0:1aac12a5f1e0 825 Serial.print(F(" R:"));
kbhagat6 0:1aac12a5f1e0 826 Serial.println(r_last);
kbhagat6 0:1aac12a5f1e0 827
kbhagat6 0:1aac12a5f1e0 828 Serial.print(F("Ratios: "));
kbhagat6 0:1aac12a5f1e0 829 Serial.print(F("UD Fi: "));
kbhagat6 0:1aac12a5f1e0 830 Serial.print(ud_ratio_first);
kbhagat6 0:1aac12a5f1e0 831 Serial.print(F(" UD La: "));
kbhagat6 0:1aac12a5f1e0 832 Serial.print(ud_ratio_last);
kbhagat6 0:1aac12a5f1e0 833 Serial.print(F(" LR Fi: "));
kbhagat6 0:1aac12a5f1e0 834 Serial.print(lr_ratio_first);
kbhagat6 0:1aac12a5f1e0 835 Serial.print(F(" LR La: "));
kbhagat6 0:1aac12a5f1e0 836 Serial.println(lr_ratio_last);
kbhagat6 0:1aac12a5f1e0 837 #endif */
kbhagat6 0:1aac12a5f1e0 838
kbhagat6 0:1aac12a5f1e0 839 /* Determine the difference between the first and last ratios */
kbhagat6 0:1aac12a5f1e0 840 ud_delta = ud_ratio_last - ud_ratio_first;
kbhagat6 0:1aac12a5f1e0 841 lr_delta = lr_ratio_last - lr_ratio_first;
kbhagat6 0:1aac12a5f1e0 842
kbhagat6 0:1aac12a5f1e0 843 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 844 Serial.print("Deltas: ");
kbhagat6 0:1aac12a5f1e0 845 Serial.print("UD: ");
kbhagat6 0:1aac12a5f1e0 846 Serial.print(ud_delta);
kbhagat6 0:1aac12a5f1e0 847 Serial.print(" LR: ");
kbhagat6 0:1aac12a5f1e0 848 Serial.println(lr_delta);
kbhagat6 0:1aac12a5f1e0 849 #endif */
kbhagat6 0:1aac12a5f1e0 850
kbhagat6 0:1aac12a5f1e0 851 /* Accumulate the UD and LR delta values */
kbhagat6 0:1aac12a5f1e0 852 gesture_ud_delta_ += ud_delta;
kbhagat6 0:1aac12a5f1e0 853 gesture_lr_delta_ += lr_delta;
kbhagat6 0:1aac12a5f1e0 854
kbhagat6 0:1aac12a5f1e0 855 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 856 Serial.print("Accumulations: ");
kbhagat6 0:1aac12a5f1e0 857 Serial.print("UD: ");
kbhagat6 0:1aac12a5f1e0 858 Serial.print(gesture_ud_delta_);
kbhagat6 0:1aac12a5f1e0 859 Serial.print(" LR: ");
kbhagat6 0:1aac12a5f1e0 860 Serial.println(gesture_lr_delta_);
kbhagat6 0:1aac12a5f1e0 861 #endif */
kbhagat6 0:1aac12a5f1e0 862
kbhagat6 0:1aac12a5f1e0 863 /* Determine U/D gesture */
kbhagat6 0:1aac12a5f1e0 864 if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 865 gesture_ud_count_ = 1;
kbhagat6 0:1aac12a5f1e0 866 } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 867 gesture_ud_count_ = -1;
kbhagat6 0:1aac12a5f1e0 868 } else {
kbhagat6 0:1aac12a5f1e0 869 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 870 }
kbhagat6 0:1aac12a5f1e0 871
kbhagat6 0:1aac12a5f1e0 872 /* Determine L/R gesture */
kbhagat6 0:1aac12a5f1e0 873 if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 874 gesture_lr_count_ = 1;
kbhagat6 0:1aac12a5f1e0 875 } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 876 gesture_lr_count_ = -1;
kbhagat6 0:1aac12a5f1e0 877 } else {
kbhagat6 0:1aac12a5f1e0 878 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 879 }
kbhagat6 0:1aac12a5f1e0 880
kbhagat6 0:1aac12a5f1e0 881 /* Determine Near/Far gesture */
kbhagat6 0:1aac12a5f1e0 882 if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 883 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 0:1aac12a5f1e0 884 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 0:1aac12a5f1e0 885
kbhagat6 0:1aac12a5f1e0 886 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 887 gesture_near_count_++;
kbhagat6 0:1aac12a5f1e0 888 } else if( (ud_delta != 0) || (lr_delta != 0) ) {
kbhagat6 0:1aac12a5f1e0 889 gesture_far_count_++;
kbhagat6 0:1aac12a5f1e0 890 }
kbhagat6 0:1aac12a5f1e0 891
kbhagat6 0:1aac12a5f1e0 892 if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
kbhagat6 0:1aac12a5f1e0 893 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 894 gesture_state_ = NEAR_STATE;
kbhagat6 0:1aac12a5f1e0 895 } else if( (ud_delta != 0) && (lr_delta != 0) ) {
kbhagat6 0:1aac12a5f1e0 896 gesture_state_ = FAR_STATE;
kbhagat6 0:1aac12a5f1e0 897 }
kbhagat6 0:1aac12a5f1e0 898 return true;
kbhagat6 0:1aac12a5f1e0 899 }
kbhagat6 0:1aac12a5f1e0 900 }
kbhagat6 0:1aac12a5f1e0 901 } else {
kbhagat6 0:1aac12a5f1e0 902 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 0:1aac12a5f1e0 903 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 0:1aac12a5f1e0 904
kbhagat6 0:1aac12a5f1e0 905 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 906 gesture_near_count_++;
kbhagat6 0:1aac12a5f1e0 907 }
kbhagat6 0:1aac12a5f1e0 908
kbhagat6 0:1aac12a5f1e0 909 if( gesture_near_count_ >= 5 ) {
kbhagat6 0:1aac12a5f1e0 910 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 911 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 912 gesture_ud_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 913 gesture_lr_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 914 }
kbhagat6 0:1aac12a5f1e0 915 }
kbhagat6 0:1aac12a5f1e0 916 }
kbhagat6 0:1aac12a5f1e0 917
kbhagat6 0:1aac12a5f1e0 918 // #if DEBUG
kbhagat6 0:1aac12a5f1e0 919 /* printf("UD_CT: %d\n",gesture_ud_count_);
kbhagat6 0:1aac12a5f1e0 920 printf("LR_CT: %d\n",gesture_lr_count_);
kbhagat6 0:1aac12a5f1e0 921 printf("NEAR_CT: %d\n",gesture_near_count_);
kbhagat6 0:1aac12a5f1e0 922 printf(" FAR_CT: %d\n",gesture_far_count_);
kbhagat6 0:1aac12a5f1e0 923 printf("----------"); */
kbhagat6 0:1aac12a5f1e0 924 //#endif */
kbhagat6 0:1aac12a5f1e0 925
kbhagat6 0:1aac12a5f1e0 926 return false;
kbhagat6 0:1aac12a5f1e0 927 }
kbhagat6 0:1aac12a5f1e0 928
kbhagat6 0:1aac12a5f1e0 929 /**
kbhagat6 0:1aac12a5f1e0 930 * @brief Determines swipe direction or near/far state
kbhagat6 0:1aac12a5f1e0 931 *
kbhagat6 0:1aac12a5f1e0 932 * @return True if near/far event. False otherwise.
kbhagat6 0:1aac12a5f1e0 933 */
kbhagat6 0:1aac12a5f1e0 934 bool glibr::decodeGesture()
kbhagat6 0:1aac12a5f1e0 935 {
kbhagat6 0:1aac12a5f1e0 936 /* Return if near or far event is detected */
kbhagat6 0:1aac12a5f1e0 937 if( gesture_state_ == NEAR_STATE ) {
kbhagat6 0:1aac12a5f1e0 938 gesture_motion_ = DIR_NEAR;
kbhagat6 0:1aac12a5f1e0 939 return true;
kbhagat6 0:1aac12a5f1e0 940 } else if ( gesture_state_ == FAR_STATE ) {
kbhagat6 0:1aac12a5f1e0 941 gesture_motion_ = DIR_FAR;
kbhagat6 0:1aac12a5f1e0 942 return true;
kbhagat6 0:1aac12a5f1e0 943 }
kbhagat6 0:1aac12a5f1e0 944
kbhagat6 0:1aac12a5f1e0 945 /* Determine swipe direction */
kbhagat6 0:1aac12a5f1e0 946 if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 947 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 948 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 949 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 950 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 951 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 952 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 953 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 954 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 955 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 956 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 957 } else {
kbhagat6 0:1aac12a5f1e0 958 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 959 }
kbhagat6 0:1aac12a5f1e0 960 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 961 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 962 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 963 } else {
kbhagat6 0:1aac12a5f1e0 964 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 965 }
kbhagat6 0:1aac12a5f1e0 966 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 967 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 968 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 969 } else {
kbhagat6 0:1aac12a5f1e0 970 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 971 }
kbhagat6 0:1aac12a5f1e0 972 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 973 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 974 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 975 } else {
kbhagat6 0:1aac12a5f1e0 976 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 977 }
kbhagat6 0:1aac12a5f1e0 978 } else {
kbhagat6 0:1aac12a5f1e0 979 return false;
kbhagat6 0:1aac12a5f1e0 980 }
kbhagat6 0:1aac12a5f1e0 981
kbhagat6 0:1aac12a5f1e0 982 return true;
kbhagat6 0:1aac12a5f1e0 983 }
kbhagat6 0:1aac12a5f1e0 984
kbhagat6 0:1aac12a5f1e0 985 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 986 * Getters and setters for register values
kbhagat6 0:1aac12a5f1e0 987 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 988
kbhagat6 0:1aac12a5f1e0 989 /**
kbhagat6 0:1aac12a5f1e0 990 * @brief Returns the lower threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 991 *
kbhagat6 0:1aac12a5f1e0 992 * @return lower threshold
kbhagat6 0:1aac12a5f1e0 993 */
kbhagat6 0:1aac12a5f1e0 994
kbhagat6 0:1aac12a5f1e0 995 uint8_t glibr::getProxIntLowThresh()
kbhagat6 0:1aac12a5f1e0 996 {
kbhagat6 0:1aac12a5f1e0 997 uint8_t val;
kbhagat6 0:1aac12a5f1e0 998
kbhagat6 0:1aac12a5f1e0 999 /* Read value from PILT register */
kbhagat6 0:1aac12a5f1e0 1000 /* if( !wireReadDataByte(APDS9960_PILT, val) ) {
kbhagat6 0:1aac12a5f1e0 1001 val = 0;
kbhagat6 0:1aac12a5f1e0 1002 }*/
kbhagat6 0:1aac12a5f1e0 1003 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1004 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1005 val=0;
kbhagat6 0:1aac12a5f1e0 1006 }
kbhagat6 0:1aac12a5f1e0 1007
kbhagat6 0:1aac12a5f1e0 1008 return val;
kbhagat6 0:1aac12a5f1e0 1009 }
kbhagat6 0:1aac12a5f1e0 1010
kbhagat6 0:1aac12a5f1e0 1011 /**
kbhagat6 0:1aac12a5f1e0 1012 * @brief Sets the lower threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1013 *
kbhagat6 0:1aac12a5f1e0 1014 * @param[in] threshold the lower proximity threshold
kbhagat6 0:1aac12a5f1e0 1015 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1016 */
kbhagat6 0:1aac12a5f1e0 1017 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1018 {
kbhagat6 0:1aac12a5f1e0 1019 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1020 return false;
kbhagat6 0:1aac12a5f1e0 1021 }
kbhagat6 0:1aac12a5f1e0 1022
kbhagat6 0:1aac12a5f1e0 1023 return true;
kbhagat6 0:1aac12a5f1e0 1024 }
kbhagat6 0:1aac12a5f1e0 1025
kbhagat6 0:1aac12a5f1e0 1026 /**
kbhagat6 0:1aac12a5f1e0 1027 * @brief Returns the high threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1028 *
kbhagat6 0:1aac12a5f1e0 1029 * @return high threshold
kbhagat6 0:1aac12a5f1e0 1030 */
kbhagat6 0:1aac12a5f1e0 1031 uint8_t glibr::getProxIntHighThresh()
kbhagat6 0:1aac12a5f1e0 1032 {
kbhagat6 0:1aac12a5f1e0 1033 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1034
kbhagat6 0:1aac12a5f1e0 1035 /* Read value from PIHT register */
kbhagat6 0:1aac12a5f1e0 1036 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1037 if( val==ERROR ) {
kbhagat6 0:1aac12a5f1e0 1038 val = 0;
kbhagat6 0:1aac12a5f1e0 1039 }
kbhagat6 0:1aac12a5f1e0 1040
kbhagat6 0:1aac12a5f1e0 1041 return val;
kbhagat6 0:1aac12a5f1e0 1042 }
kbhagat6 0:1aac12a5f1e0 1043
kbhagat6 0:1aac12a5f1e0 1044 /**
kbhagat6 0:1aac12a5f1e0 1045 * @brief Sets the high threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1046 *
kbhagat6 0:1aac12a5f1e0 1047 * @param[in] threshold the high proximity threshold
kbhagat6 0:1aac12a5f1e0 1048 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1049 */
kbhagat6 0:1aac12a5f1e0 1050 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1051 {
kbhagat6 0:1aac12a5f1e0 1052
kbhagat6 0:1aac12a5f1e0 1053 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1054 return false;
kbhagat6 0:1aac12a5f1e0 1055 }
kbhagat6 0:1aac12a5f1e0 1056
kbhagat6 0:1aac12a5f1e0 1057 return true;
kbhagat6 0:1aac12a5f1e0 1058 }
kbhagat6 0:1aac12a5f1e0 1059
kbhagat6 0:1aac12a5f1e0 1060 /**
kbhagat6 0:1aac12a5f1e0 1061 * @brief Returns LED drive strength for proximity and ALS
kbhagat6 0:1aac12a5f1e0 1062 *
kbhagat6 0:1aac12a5f1e0 1063 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1064 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1065 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1066 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1067 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1068 *
kbhagat6 0:1aac12a5f1e0 1069 * @return the value of the LED drive strength. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1070 */
kbhagat6 0:1aac12a5f1e0 1071 uint8_t glibr::getLEDDrive()
kbhagat6 0:1aac12a5f1e0 1072 {
kbhagat6 0:1aac12a5f1e0 1073 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1074
kbhagat6 0:1aac12a5f1e0 1075 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1076 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1077 if( val == ERROR ){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1078 return ERROR;
kbhagat6 0:1aac12a5f1e0 1079 }
kbhagat6 0:1aac12a5f1e0 1080
kbhagat6 0:1aac12a5f1e0 1081 /* Shift and mask out LED drive bits */
kbhagat6 0:1aac12a5f1e0 1082 val = (val >> 6) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1083
kbhagat6 0:1aac12a5f1e0 1084 return val;
kbhagat6 0:1aac12a5f1e0 1085 }
kbhagat6 0:1aac12a5f1e0 1086
kbhagat6 0:1aac12a5f1e0 1087 /**
kbhagat6 0:1aac12a5f1e0 1088 * @brief Sets the LED drive strength for proximity and ALS
kbhagat6 0:1aac12a5f1e0 1089 *
kbhagat6 0:1aac12a5f1e0 1090 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1091 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1092 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1093 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1094 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1095 *
kbhagat6 0:1aac12a5f1e0 1096 * @param[in] drive the value (0-3) for the LED drive strength
kbhagat6 0:1aac12a5f1e0 1097 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1098 */
kbhagat6 0:1aac12a5f1e0 1099
kbhagat6 0:1aac12a5f1e0 1100 bool glibr::setLEDDrive(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1101 {
kbhagat6 0:1aac12a5f1e0 1102 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1103
kbhagat6 0:1aac12a5f1e0 1104 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1105 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1106 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1107 return false;
kbhagat6 0:1aac12a5f1e0 1108 }
kbhagat6 0:1aac12a5f1e0 1109 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1110 //drive &= 0b00000011
kbhagat6 0:1aac12a5f1e0 1111 drive &= 0x03;
kbhagat6 0:1aac12a5f1e0 1112 drive = drive << 6;
kbhagat6 0:1aac12a5f1e0 1113 //val &= 0b00111111;
kbhagat6 0:1aac12a5f1e0 1114 val &= 0x3F;
kbhagat6 0:1aac12a5f1e0 1115 val |= drive;
kbhagat6 0:1aac12a5f1e0 1116
kbhagat6 0:1aac12a5f1e0 1117 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1118 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1119 return false;
kbhagat6 0:1aac12a5f1e0 1120 }
kbhagat6 0:1aac12a5f1e0 1121
kbhagat6 0:1aac12a5f1e0 1122 return true;
kbhagat6 0:1aac12a5f1e0 1123 }
kbhagat6 0:1aac12a5f1e0 1124
kbhagat6 0:1aac12a5f1e0 1125 /**
kbhagat6 0:1aac12a5f1e0 1126 * @brief Returns receiver gain for proximity detection
kbhagat6 0:1aac12a5f1e0 1127 *
kbhagat6 0:1aac12a5f1e0 1128 * Value Gain
kbhagat6 0:1aac12a5f1e0 1129 * 0 1x
kbhagat6 0:1aac12a5f1e0 1130 * 1 2x
kbhagat6 0:1aac12a5f1e0 1131 * 2 4x
kbhagat6 0:1aac12a5f1e0 1132 * 3 8x
kbhagat6 0:1aac12a5f1e0 1133 *
kbhagat6 0:1aac12a5f1e0 1134 * @return the value of the proximity gain. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1135 */
kbhagat6 0:1aac12a5f1e0 1136 uint8_t glibr::getProximityGain()
kbhagat6 0:1aac12a5f1e0 1137 {
kbhagat6 0:1aac12a5f1e0 1138 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1139
kbhagat6 0:1aac12a5f1e0 1140 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1141 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1142 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1143 return ERROR;
kbhagat6 0:1aac12a5f1e0 1144 }
kbhagat6 0:1aac12a5f1e0 1145
kbhagat6 0:1aac12a5f1e0 1146 /* Shift and mask out PDRIVE bits */
kbhagat6 0:1aac12a5f1e0 1147 val = (val >> 2) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1148
kbhagat6 0:1aac12a5f1e0 1149 return val;
kbhagat6 0:1aac12a5f1e0 1150 }
kbhagat6 0:1aac12a5f1e0 1151
kbhagat6 0:1aac12a5f1e0 1152 /**
kbhagat6 0:1aac12a5f1e0 1153 * @brief Sets the receiver gain for proximity detection
kbhagat6 0:1aac12a5f1e0 1154 *
kbhagat6 0:1aac12a5f1e0 1155 * Value Gain
kbhagat6 0:1aac12a5f1e0 1156 * 0 1x
kbhagat6 0:1aac12a5f1e0 1157 * 1 2x
kbhagat6 0:1aac12a5f1e0 1158 * 2 4x
kbhagat6 0:1aac12a5f1e0 1159 * 3 8x
kbhagat6 0:1aac12a5f1e0 1160 *
kbhagat6 0:1aac12a5f1e0 1161 * @param[in] drive the value (0-3) for the gain
kbhagat6 0:1aac12a5f1e0 1162 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1163 */
kbhagat6 0:1aac12a5f1e0 1164 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1165 {
kbhagat6 0:1aac12a5f1e0 1166 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1167
kbhagat6 0:1aac12a5f1e0 1168 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1169
kbhagat6 0:1aac12a5f1e0 1170 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1171
kbhagat6 0:1aac12a5f1e0 1172 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1173 return false;
kbhagat6 0:1aac12a5f1e0 1174 }
kbhagat6 0:1aac12a5f1e0 1175 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1176 //drive &= 0b00000011;
kbhagat6 0:1aac12a5f1e0 1177 drive &=0x03;
kbhagat6 0:1aac12a5f1e0 1178 drive = drive << 2;
kbhagat6 0:1aac12a5f1e0 1179 //val &= 0b11110011
kbhagat6 0:1aac12a5f1e0 1180 val &= 0xF3;
kbhagat6 0:1aac12a5f1e0 1181 val |= drive;
kbhagat6 0:1aac12a5f1e0 1182
kbhagat6 0:1aac12a5f1e0 1183 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1184 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1185 return false;
kbhagat6 0:1aac12a5f1e0 1186 }
kbhagat6 0:1aac12a5f1e0 1187 return true;
kbhagat6 0:1aac12a5f1e0 1188 }
kbhagat6 0:1aac12a5f1e0 1189
kbhagat6 0:1aac12a5f1e0 1190 /**
kbhagat6 0:1aac12a5f1e0 1191 * @brief Returns receiver gain for the ambient light sensor (ALS)
kbhagat6 0:1aac12a5f1e0 1192 *
kbhagat6 0:1aac12a5f1e0 1193 * Value Gain
kbhagat6 0:1aac12a5f1e0 1194 * 0 1x
kbhagat6 0:1aac12a5f1e0 1195 * 1 4x
kbhagat6 0:1aac12a5f1e0 1196 * 2 16x
kbhagat6 0:1aac12a5f1e0 1197 * 3 64x
kbhagat6 0:1aac12a5f1e0 1198 *
kbhagat6 0:1aac12a5f1e0 1199 * @return the value of the ALS gain. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1200 */
kbhagat6 0:1aac12a5f1e0 1201 uint8_t glibr::getAmbientLightGain()
kbhagat6 0:1aac12a5f1e0 1202 {
kbhagat6 0:1aac12a5f1e0 1203 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1204
kbhagat6 0:1aac12a5f1e0 1205 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1206 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1207 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1208 return ERROR;
kbhagat6 0:1aac12a5f1e0 1209 }
kbhagat6 0:1aac12a5f1e0 1210
kbhagat6 0:1aac12a5f1e0 1211 /* Shift and mask out ADRIVE bits */
kbhagat6 0:1aac12a5f1e0 1212 val &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1213
kbhagat6 0:1aac12a5f1e0 1214 return val;
kbhagat6 0:1aac12a5f1e0 1215 }
kbhagat6 0:1aac12a5f1e0 1216
kbhagat6 0:1aac12a5f1e0 1217 /**
kbhagat6 0:1aac12a5f1e0 1218 * @brief Sets the receiver gain for the ambient light sensor (ALS)
kbhagat6 0:1aac12a5f1e0 1219 *
kbhagat6 0:1aac12a5f1e0 1220 * Value Gain
kbhagat6 0:1aac12a5f1e0 1221 * 0 1x
kbhagat6 0:1aac12a5f1e0 1222 * 1 4x
kbhagat6 0:1aac12a5f1e0 1223 * 2 16x
kbhagat6 0:1aac12a5f1e0 1224 * 3 64x
kbhagat6 0:1aac12a5f1e0 1225 *
kbhagat6 0:1aac12a5f1e0 1226 * @param[in] drive the value (0-3) for the gain
kbhagat6 0:1aac12a5f1e0 1227 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1228 */
kbhagat6 0:1aac12a5f1e0 1229 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 0:1aac12a5f1e0 1230
kbhagat6 0:1aac12a5f1e0 1231 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1232
kbhagat6 0:1aac12a5f1e0 1233 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1234
kbhagat6 0:1aac12a5f1e0 1235 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1236
kbhagat6 0:1aac12a5f1e0 1237 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1238 return false;
kbhagat6 0:1aac12a5f1e0 1239 }
kbhagat6 0:1aac12a5f1e0 1240 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1241 //drive &= 0b00000011;
kbhagat6 0:1aac12a5f1e0 1242 drive &=0x03;
kbhagat6 0:1aac12a5f1e0 1243 drive = drive << 2;
kbhagat6 0:1aac12a5f1e0 1244 //val &=0b11111100
kbhagat6 0:1aac12a5f1e0 1245 val &= 0xF3;
kbhagat6 0:1aac12a5f1e0 1246 val |= drive;
kbhagat6 0:1aac12a5f1e0 1247
kbhagat6 0:1aac12a5f1e0 1248 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1249 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1250 return false;
kbhagat6 0:1aac12a5f1e0 1251 }
kbhagat6 0:1aac12a5f1e0 1252 return true;
kbhagat6 0:1aac12a5f1e0 1253 }
kbhagat6 0:1aac12a5f1e0 1254
kbhagat6 0:1aac12a5f1e0 1255 /**
kbhagat6 0:1aac12a5f1e0 1256 * @brief Get the current LED boost value
kbhagat6 0:1aac12a5f1e0 1257 *
kbhagat6 0:1aac12a5f1e0 1258 * Value Boost Current
kbhagat6 0:1aac12a5f1e0 1259 * 0 100%
kbhagat6 0:1aac12a5f1e0 1260 * 1 150%
kbhagat6 0:1aac12a5f1e0 1261 * 2 200%
kbhagat6 0:1aac12a5f1e0 1262 * 3 300%
kbhagat6 0:1aac12a5f1e0 1263 *
kbhagat6 0:1aac12a5f1e0 1264 * @return The LED boost value. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1265 */
kbhagat6 0:1aac12a5f1e0 1266 uint8_t glibr::getLEDBoost() {
kbhagat6 0:1aac12a5f1e0 1267 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1268
kbhagat6 0:1aac12a5f1e0 1269 /* Read value from CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1270 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 0:1aac12a5f1e0 1271 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1272 return ERROR;
kbhagat6 0:1aac12a5f1e0 1273 }
kbhagat6 0:1aac12a5f1e0 1274
kbhagat6 0:1aac12a5f1e0 1275 /* Shift and mask out LED_BOOST bits */
kbhagat6 0:1aac12a5f1e0 1276 val = (val >> 4) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1277
kbhagat6 0:1aac12a5f1e0 1278 return val;
kbhagat6 0:1aac12a5f1e0 1279 }
kbhagat6 0:1aac12a5f1e0 1280
kbhagat6 0:1aac12a5f1e0 1281 /**
kbhagat6 0:1aac12a5f1e0 1282 * @brief Sets the LED current boost value
kbhagat6 0:1aac12a5f1e0 1283 *
kbhagat6 0:1aac12a5f1e0 1284 * Value Boost Current
kbhagat6 0:1aac12a5f1e0 1285 * 0 100%
kbhagat6 0:1aac12a5f1e0 1286 * 1 150%
kbhagat6 0:1aac12a5f1e0 1287 * 2 200%
kbhagat6 0:1aac12a5f1e0 1288 * 3 300%
kbhagat6 0:1aac12a5f1e0 1289 *
kbhagat6 0:1aac12a5f1e0 1290 * @param[in] drive the value (0-3) for current boost (100-300%)
kbhagat6 0:1aac12a5f1e0 1291 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1292 */
kbhagat6 0:1aac12a5f1e0 1293 bool glibr::setLEDBoost(uint8_t boost)
kbhagat6 0:1aac12a5f1e0 1294 {
kbhagat6 0:1aac12a5f1e0 1295 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1296
kbhagat6 0:1aac12a5f1e0 1297 /* Read value from CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1298 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 0:1aac12a5f1e0 1299 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1300 return false;
kbhagat6 0:1aac12a5f1e0 1301 }
kbhagat6 0:1aac12a5f1e0 1302
kbhagat6 0:1aac12a5f1e0 1303 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1304 boost &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1305 boost = boost << 4;
kbhagat6 0:1aac12a5f1e0 1306 val &= 0xCF;//0b11001111;
kbhagat6 0:1aac12a5f1e0 1307 val |= boost;
kbhagat6 0:1aac12a5f1e0 1308
kbhagat6 0:1aac12a5f1e0 1309 /* Write register value back into CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1310
kbhagat6 0:1aac12a5f1e0 1311 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, val)){//!wireWriteDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1312 return false;
kbhagat6 0:1aac12a5f1e0 1313 }
kbhagat6 0:1aac12a5f1e0 1314
kbhagat6 0:1aac12a5f1e0 1315 return true;
kbhagat6 0:1aac12a5f1e0 1316 }
kbhagat6 0:1aac12a5f1e0 1317
kbhagat6 0:1aac12a5f1e0 1318 /**
kbhagat6 0:1aac12a5f1e0 1319 * @brief Gets proximity gain compensation enable
kbhagat6 0:1aac12a5f1e0 1320 *
kbhagat6 0:1aac12a5f1e0 1321 * @return 1 if compensation is enabled. 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1322 */
kbhagat6 0:1aac12a5f1e0 1323 uint8_t glibr::getProxGainCompEnable()
kbhagat6 0:1aac12a5f1e0 1324 {
kbhagat6 0:1aac12a5f1e0 1325 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1326
kbhagat6 0:1aac12a5f1e0 1327 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1328 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1329 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1330 return ERROR;
kbhagat6 0:1aac12a5f1e0 1331 }
kbhagat6 0:1aac12a5f1e0 1332
kbhagat6 0:1aac12a5f1e0 1333 /* Shift and mask out PCMP bits */
kbhagat6 0:1aac12a5f1e0 1334 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1335
kbhagat6 0:1aac12a5f1e0 1336 return val;
kbhagat6 0:1aac12a5f1e0 1337 }
kbhagat6 0:1aac12a5f1e0 1338
kbhagat6 0:1aac12a5f1e0 1339 /**
kbhagat6 0:1aac12a5f1e0 1340 * @brief Sets the proximity gain compensation enable
kbhagat6 0:1aac12a5f1e0 1341 *
kbhagat6 0:1aac12a5f1e0 1342 * @param[in] enable 1 to enable compensation. 0 to disable compensation.
kbhagat6 0:1aac12a5f1e0 1343 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1344 */
kbhagat6 0:1aac12a5f1e0 1345 bool glibr::setProxGainCompEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1346 {
kbhagat6 0:1aac12a5f1e0 1347 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1348
kbhagat6 0:1aac12a5f1e0 1349 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1350 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1351 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1352 return false;
kbhagat6 0:1aac12a5f1e0 1353 }
kbhagat6 0:1aac12a5f1e0 1354
kbhagat6 0:1aac12a5f1e0 1355 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1356 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1357 enable = enable << 5;
kbhagat6 0:1aac12a5f1e0 1358 val &= 0xCF;//0b11011111;
kbhagat6 0:1aac12a5f1e0 1359 val |= enable;
kbhagat6 0:1aac12a5f1e0 1360
kbhagat6 0:1aac12a5f1e0 1361 /* Write register value back into CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1362
kbhagat6 0:1aac12a5f1e0 1363 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, val)){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1364 return false;
kbhagat6 0:1aac12a5f1e0 1365 }
kbhagat6 0:1aac12a5f1e0 1366
kbhagat6 0:1aac12a5f1e0 1367 return true;
kbhagat6 0:1aac12a5f1e0 1368 }
kbhagat6 0:1aac12a5f1e0 1369
kbhagat6 0:1aac12a5f1e0 1370 /**
kbhagat6 0:1aac12a5f1e0 1371 * @brief Gets the current mask for enabled/disabled proximity photodiodes
kbhagat6 0:1aac12a5f1e0 1372 *
kbhagat6 0:1aac12a5f1e0 1373 * 1 = disabled, 0 = enabled
kbhagat6 0:1aac12a5f1e0 1374 * Bit Photodiode
kbhagat6 0:1aac12a5f1e0 1375 * 3 UP
kbhagat6 0:1aac12a5f1e0 1376 * 2 DOWN
kbhagat6 0:1aac12a5f1e0 1377 * 1 LEFT
kbhagat6 0:1aac12a5f1e0 1378 * 0 RIGHT
kbhagat6 0:1aac12a5f1e0 1379 *
kbhagat6 0:1aac12a5f1e0 1380 * @return Current proximity mask for photodiodes. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1381 */
kbhagat6 0:1aac12a5f1e0 1382 uint8_t glibr::getProxPhotoMask()
kbhagat6 0:1aac12a5f1e0 1383 {
kbhagat6 0:1aac12a5f1e0 1384 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1385
kbhagat6 0:1aac12a5f1e0 1386 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1387 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1388 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1389 return ERROR;
kbhagat6 0:1aac12a5f1e0 1390 }
kbhagat6 0:1aac12a5f1e0 1391
kbhagat6 0:1aac12a5f1e0 1392 /* Mask out photodiode enable mask bits */
kbhagat6 0:1aac12a5f1e0 1393 val &= 0x0F;//0b00001111;
kbhagat6 0:1aac12a5f1e0 1394
kbhagat6 0:1aac12a5f1e0 1395 return val;
kbhagat6 0:1aac12a5f1e0 1396 }
kbhagat6 0:1aac12a5f1e0 1397
kbhagat6 0:1aac12a5f1e0 1398 /**
kbhagat6 0:1aac12a5f1e0 1399 * @brief Sets the mask for enabling/disabling proximity photodiodes
kbhagat6 0:1aac12a5f1e0 1400 *
kbhagat6 0:1aac12a5f1e0 1401 * 1 = disabled, 0 = enabled
kbhagat6 0:1aac12a5f1e0 1402 * Bit Photodiode
kbhagat6 0:1aac12a5f1e0 1403 * 3 UP
kbhagat6 0:1aac12a5f1e0 1404 * 2 DOWN
kbhagat6 0:1aac12a5f1e0 1405 * 1 LEFT
kbhagat6 0:1aac12a5f1e0 1406 * 0 RIGHT
kbhagat6 0:1aac12a5f1e0 1407 *
kbhagat6 0:1aac12a5f1e0 1408 * @param[in] mask 4-bit mask value
kbhagat6 0:1aac12a5f1e0 1409 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1410 */
kbhagat6 0:1aac12a5f1e0 1411 bool glibr::setProxPhotoMask(uint8_t mask)
kbhagat6 0:1aac12a5f1e0 1412 {
kbhagat6 0:1aac12a5f1e0 1413 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1414
kbhagat6 0:1aac12a5f1e0 1415 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1416 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1417 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1418 return false;
kbhagat6 0:1aac12a5f1e0 1419 }
kbhagat6 0:1aac12a5f1e0 1420
kbhagat6 0:1aac12a5f1e0 1421 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1422 mask &= 0x0F;//0b00001111;
kbhagat6 0:1aac12a5f1e0 1423 val &= 0xF0;//0b11110000;
kbhagat6 0:1aac12a5f1e0 1424 val |= mask;
kbhagat6 0:1aac12a5f1e0 1425
kbhagat6 0:1aac12a5f1e0 1426 /* Write register value back into CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1427 I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val);
kbhagat6 0:1aac12a5f1e0 1428 if( val == ERROR){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1429 return false;
kbhagat6 0:1aac12a5f1e0 1430 }
kbhagat6 0:1aac12a5f1e0 1431
kbhagat6 0:1aac12a5f1e0 1432 return true;
kbhagat6 0:1aac12a5f1e0 1433 }
kbhagat6 0:1aac12a5f1e0 1434
kbhagat6 0:1aac12a5f1e0 1435 /**
kbhagat6 0:1aac12a5f1e0 1436 * @brief Gets the entry proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1437 *
kbhagat6 0:1aac12a5f1e0 1438 * @return Current entry proximity threshold.
kbhagat6 0:1aac12a5f1e0 1439 */
kbhagat6 0:1aac12a5f1e0 1440 uint8_t glibr::getGestureEnterThresh()
kbhagat6 0:1aac12a5f1e0 1441 {
kbhagat6 0:1aac12a5f1e0 1442 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1443
kbhagat6 0:1aac12a5f1e0 1444 /* Read value from GPENTH register */
kbhagat6 0:1aac12a5f1e0 1445 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GPENTH);
kbhagat6 0:1aac12a5f1e0 1446 if( val == ERROR){//!wireReadDataByte(APDS9960_GPENTH, val) ) {
kbhagat6 0:1aac12a5f1e0 1447 val = 0;
kbhagat6 0:1aac12a5f1e0 1448 }
kbhagat6 0:1aac12a5f1e0 1449
kbhagat6 0:1aac12a5f1e0 1450 return val;
kbhagat6 0:1aac12a5f1e0 1451 }
kbhagat6 0:1aac12a5f1e0 1452
kbhagat6 0:1aac12a5f1e0 1453 /**
kbhagat6 0:1aac12a5f1e0 1454 * @brief Sets the entry proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1455 *
kbhagat6 0:1aac12a5f1e0 1456 * @param[in] threshold proximity value needed to start gesture mode
kbhagat6 0:1aac12a5f1e0 1457 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1458 */
kbhagat6 0:1aac12a5f1e0 1459 bool glibr::setGestureEnterThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1460 {
kbhagat6 0:1aac12a5f1e0 1461
kbhagat6 0:1aac12a5f1e0 1462 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold)){;//!wireWriteDataByte(APDS9960_GPENTH, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1463 return false;
kbhagat6 0:1aac12a5f1e0 1464 }
kbhagat6 0:1aac12a5f1e0 1465
kbhagat6 0:1aac12a5f1e0 1466 return true;
kbhagat6 0:1aac12a5f1e0 1467 }
kbhagat6 0:1aac12a5f1e0 1468
kbhagat6 0:1aac12a5f1e0 1469 /**
kbhagat6 0:1aac12a5f1e0 1470 * @brief Gets the exit proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1471 *
kbhagat6 0:1aac12a5f1e0 1472 * @return Current exit proximity threshold.
kbhagat6 0:1aac12a5f1e0 1473 */
kbhagat6 0:1aac12a5f1e0 1474 uint8_t glibr::getGestureExitThresh()
kbhagat6 0:1aac12a5f1e0 1475 {
kbhagat6 0:1aac12a5f1e0 1476 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1477
kbhagat6 0:1aac12a5f1e0 1478 /* Read value from GEXTH register */
kbhagat6 0:1aac12a5f1e0 1479 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GEXTH);
kbhagat6 0:1aac12a5f1e0 1480 if( val == ERROR){//!wireReadDataByte(APDS9960_GEXTH, val) ) {
kbhagat6 0:1aac12a5f1e0 1481 val = 0;
kbhagat6 0:1aac12a5f1e0 1482 }
kbhagat6 0:1aac12a5f1e0 1483
kbhagat6 0:1aac12a5f1e0 1484 return val;
kbhagat6 0:1aac12a5f1e0 1485 }
kbhagat6 0:1aac12a5f1e0 1486
kbhagat6 0:1aac12a5f1e0 1487 /**
kbhagat6 0:1aac12a5f1e0 1488 * @brief Sets the exit proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1489 *
kbhagat6 0:1aac12a5f1e0 1490 * @param[in] threshold proximity value needed to end gesture mode
kbhagat6 0:1aac12a5f1e0 1491 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1492 */
kbhagat6 0:1aac12a5f1e0 1493 bool glibr::setGestureExitThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1494 {
kbhagat6 0:1aac12a5f1e0 1495 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold)){//!wireWriteDataByte(APDS9960_GEXTH, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1496 return false;
kbhagat6 0:1aac12a5f1e0 1497 }
kbhagat6 0:1aac12a5f1e0 1498
kbhagat6 0:1aac12a5f1e0 1499 return true;
kbhagat6 0:1aac12a5f1e0 1500 }
kbhagat6 0:1aac12a5f1e0 1501
kbhagat6 0:1aac12a5f1e0 1502 /**
kbhagat6 0:1aac12a5f1e0 1503 * @brief Gets the gain of the photodiode during gesture mode
kbhagat6 0:1aac12a5f1e0 1504 *
kbhagat6 0:1aac12a5f1e0 1505 * Value Gain
kbhagat6 0:1aac12a5f1e0 1506 * 0 1x
kbhagat6 0:1aac12a5f1e0 1507 * 1 2x
kbhagat6 0:1aac12a5f1e0 1508 * 2 4x
kbhagat6 0:1aac12a5f1e0 1509 * 3 8x
kbhagat6 0:1aac12a5f1e0 1510 *
kbhagat6 0:1aac12a5f1e0 1511 * @return the current photodiode gain. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1512 */
kbhagat6 0:1aac12a5f1e0 1513 uint8_t glibr::getGestureGain()
kbhagat6 0:1aac12a5f1e0 1514 {
kbhagat6 0:1aac12a5f1e0 1515 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1516
kbhagat6 0:1aac12a5f1e0 1517 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1518 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1519 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1520 return ERROR;
kbhagat6 0:1aac12a5f1e0 1521 }
kbhagat6 0:1aac12a5f1e0 1522
kbhagat6 0:1aac12a5f1e0 1523 /* Shift and mask out GGAIN bits */
kbhagat6 0:1aac12a5f1e0 1524 val = (val >> 5) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1525
kbhagat6 0:1aac12a5f1e0 1526 return val;
kbhagat6 0:1aac12a5f1e0 1527 }
kbhagat6 0:1aac12a5f1e0 1528
kbhagat6 0:1aac12a5f1e0 1529 /**
kbhagat6 0:1aac12a5f1e0 1530 * @brief Sets the gain of the photodiode during gesture mode
kbhagat6 0:1aac12a5f1e0 1531 *
kbhagat6 0:1aac12a5f1e0 1532 * Value Gain
kbhagat6 0:1aac12a5f1e0 1533 * 0 1x
kbhagat6 0:1aac12a5f1e0 1534 * 1 2x
kbhagat6 0:1aac12a5f1e0 1535 * 2 4x
kbhagat6 0:1aac12a5f1e0 1536 * 3 8x
kbhagat6 0:1aac12a5f1e0 1537 *
kbhagat6 0:1aac12a5f1e0 1538 * @param[in] gain the value for the photodiode gain
kbhagat6 0:1aac12a5f1e0 1539 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1540 */
kbhagat6 0:1aac12a5f1e0 1541 bool glibr::setGestureGain(uint8_t gain)
kbhagat6 0:1aac12a5f1e0 1542 {
kbhagat6 0:1aac12a5f1e0 1543 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1544
kbhagat6 0:1aac12a5f1e0 1545 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1546 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1547 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1548 return false;
kbhagat6 0:1aac12a5f1e0 1549 }
kbhagat6 0:1aac12a5f1e0 1550
kbhagat6 0:1aac12a5f1e0 1551 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1552 gain &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1553 gain = gain << 5;
kbhagat6 0:1aac12a5f1e0 1554 val &= 0x9F;//0b10011111;
kbhagat6 0:1aac12a5f1e0 1555 val |= gain;
kbhagat6 0:1aac12a5f1e0 1556
kbhagat6 0:1aac12a5f1e0 1557 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1558 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1559 return false;
kbhagat6 0:1aac12a5f1e0 1560 }
kbhagat6 0:1aac12a5f1e0 1561
kbhagat6 0:1aac12a5f1e0 1562 return true;
kbhagat6 0:1aac12a5f1e0 1563 }
kbhagat6 0:1aac12a5f1e0 1564
kbhagat6 0:1aac12a5f1e0 1565 /**
kbhagat6 0:1aac12a5f1e0 1566 * @brief Gets the drive current of the LED during gesture mode
kbhagat6 0:1aac12a5f1e0 1567 *
kbhagat6 0:1aac12a5f1e0 1568 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1569 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1570 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1571 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1572 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1573 *
kbhagat6 0:1aac12a5f1e0 1574 * @return the LED drive current value. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1575 */
kbhagat6 0:1aac12a5f1e0 1576 uint8_t glibr::getGestureLEDDrive()
kbhagat6 0:1aac12a5f1e0 1577 {
kbhagat6 0:1aac12a5f1e0 1578 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1579
kbhagat6 0:1aac12a5f1e0 1580 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1581 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1582 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1583 return ERROR;
kbhagat6 0:1aac12a5f1e0 1584 }
kbhagat6 0:1aac12a5f1e0 1585
kbhagat6 0:1aac12a5f1e0 1586 /* Shift and mask out GLDRIVE bits */
kbhagat6 0:1aac12a5f1e0 1587 val = (val >> 3) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1588
kbhagat6 0:1aac12a5f1e0 1589 return val;
kbhagat6 0:1aac12a5f1e0 1590 }
kbhagat6 0:1aac12a5f1e0 1591
kbhagat6 0:1aac12a5f1e0 1592 /**
kbhagat6 0:1aac12a5f1e0 1593 * @brief Sets the LED drive current during gesture mode
kbhagat6 0:1aac12a5f1e0 1594 *
kbhagat6 0:1aac12a5f1e0 1595 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1596 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1597 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1598 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1599 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1600 *
kbhagat6 0:1aac12a5f1e0 1601 * @param[in] drive the value for the LED drive current
kbhagat6 0:1aac12a5f1e0 1602 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1603 */
kbhagat6 0:1aac12a5f1e0 1604 bool glibr::setGestureLEDDrive(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1605 {
kbhagat6 0:1aac12a5f1e0 1606 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1607
kbhagat6 0:1aac12a5f1e0 1608 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1609 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1610 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1611 return false;
kbhagat6 0:1aac12a5f1e0 1612 }
kbhagat6 0:1aac12a5f1e0 1613
kbhagat6 0:1aac12a5f1e0 1614 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1615 drive &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1616 drive = drive << 3;
kbhagat6 0:1aac12a5f1e0 1617 val &= 0xE7;//0b11100111;
kbhagat6 0:1aac12a5f1e0 1618 val |= drive;
kbhagat6 0:1aac12a5f1e0 1619
kbhagat6 0:1aac12a5f1e0 1620 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1621 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1622 return false;
kbhagat6 0:1aac12a5f1e0 1623 }
kbhagat6 0:1aac12a5f1e0 1624
kbhagat6 0:1aac12a5f1e0 1625 return true;
kbhagat6 0:1aac12a5f1e0 1626 }
kbhagat6 0:1aac12a5f1e0 1627
kbhagat6 0:1aac12a5f1e0 1628 /**
kbhagat6 0:1aac12a5f1e0 1629 * @brief Gets the time in low power mode between gesture detections
kbhagat6 0:1aac12a5f1e0 1630 *
kbhagat6 0:1aac12a5f1e0 1631 * Value Wait time
kbhagat6 0:1aac12a5f1e0 1632 * 0 0 ms
kbhagat6 0:1aac12a5f1e0 1633 * 1 2.8 ms
kbhagat6 0:1aac12a5f1e0 1634 * 2 5.6 ms
kbhagat6 0:1aac12a5f1e0 1635 * 3 8.4 ms
kbhagat6 0:1aac12a5f1e0 1636 * 4 14.0 ms
kbhagat6 0:1aac12a5f1e0 1637 * 5 22.4 ms
kbhagat6 0:1aac12a5f1e0 1638 * 6 30.8 ms
kbhagat6 0:1aac12a5f1e0 1639 * 7 39.2 ms
kbhagat6 0:1aac12a5f1e0 1640 *
kbhagat6 0:1aac12a5f1e0 1641 * @return the current wait time between gestures. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1642 */
kbhagat6 0:1aac12a5f1e0 1643 uint8_t glibr::getGestureWaitTime()
kbhagat6 0:1aac12a5f1e0 1644 {
kbhagat6 0:1aac12a5f1e0 1645 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1646
kbhagat6 0:1aac12a5f1e0 1647 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1648 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1649 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1650 return ERROR;
kbhagat6 0:1aac12a5f1e0 1651 }
kbhagat6 0:1aac12a5f1e0 1652
kbhagat6 0:1aac12a5f1e0 1653 /* Mask out GWTIME bits */
kbhagat6 0:1aac12a5f1e0 1654 val &= 0x07;//0b00000111;
kbhagat6 0:1aac12a5f1e0 1655
kbhagat6 0:1aac12a5f1e0 1656 return val;
kbhagat6 0:1aac12a5f1e0 1657 }
kbhagat6 0:1aac12a5f1e0 1658
kbhagat6 0:1aac12a5f1e0 1659 /*
kbhagat6 0:1aac12a5f1e0 1660 *
kbhagat6 0:1aac12a5f1e0 1661 *
kbhagat6 0:1aac12a5f1e0 1662 *
kbhagat6 0:1aac12a5f1e0 1663 *LEFT OFF HERE AT 3:47PM ON 3/6/15
kbhagat6 0:1aac12a5f1e0 1664 *
kbhagat6 0:1aac12a5f1e0 1665 *
kbhagat6 0:1aac12a5f1e0 1666 *
kbhagat6 0:1aac12a5f1e0 1667 *
kbhagat6 0:1aac12a5f1e0 1668 */
kbhagat6 0:1aac12a5f1e0 1669
kbhagat6 0:1aac12a5f1e0 1670
kbhagat6 0:1aac12a5f1e0 1671 /**
kbhagat6 0:1aac12a5f1e0 1672 * @brief Sets the time in low power mode between gesture detections
kbhagat6 0:1aac12a5f1e0 1673 *
kbhagat6 0:1aac12a5f1e0 1674 * Value Wait time
kbhagat6 0:1aac12a5f1e0 1675 * 0 0 ms
kbhagat6 0:1aac12a5f1e0 1676 * 1 2.8 ms
kbhagat6 0:1aac12a5f1e0 1677 * 2 5.6 ms
kbhagat6 0:1aac12a5f1e0 1678 * 3 8.4 ms
kbhagat6 0:1aac12a5f1e0 1679 * 4 14.0 ms
kbhagat6 0:1aac12a5f1e0 1680 * 5 22.4 ms
kbhagat6 0:1aac12a5f1e0 1681 * 6 30.8 ms
kbhagat6 0:1aac12a5f1e0 1682 * 7 39.2 ms
kbhagat6 0:1aac12a5f1e0 1683 *
kbhagat6 0:1aac12a5f1e0 1684 * @param[in] the value for the wait time
kbhagat6 0:1aac12a5f1e0 1685 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1686 */
kbhagat6 0:1aac12a5f1e0 1687 bool glibr::setGestureWaitTime(uint8_t time)
kbhagat6 0:1aac12a5f1e0 1688 {
kbhagat6 0:1aac12a5f1e0 1689 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1690
kbhagat6 0:1aac12a5f1e0 1691 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1692 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1693 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1694 return false;
kbhagat6 0:1aac12a5f1e0 1695 }
kbhagat6 0:1aac12a5f1e0 1696 /* if( !wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1697 return false;
kbhagat6 0:1aac12a5f1e0 1698 } */
kbhagat6 0:1aac12a5f1e0 1699
kbhagat6 0:1aac12a5f1e0 1700 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1701 time &= 0x07;//0b00000111;
kbhagat6 0:1aac12a5f1e0 1702 val &= 0xF8;//0b11111000;
kbhagat6 0:1aac12a5f1e0 1703 val |= time;
kbhagat6 0:1aac12a5f1e0 1704
kbhagat6 0:1aac12a5f1e0 1705 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1706 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF2,val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1707 return false;
kbhagat6 0:1aac12a5f1e0 1708 }
kbhagat6 0:1aac12a5f1e0 1709 /*if( !wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1710 return false;
kbhagat6 0:1aac12a5f1e0 1711 }*/
kbhagat6 0:1aac12a5f1e0 1712 return true;
kbhagat6 0:1aac12a5f1e0 1713 }
kbhagat6 0:1aac12a5f1e0 1714
kbhagat6 0:1aac12a5f1e0 1715 /**
kbhagat6 0:1aac12a5f1e0 1716 * @brief Gets the low threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1717 *
kbhagat6 0:1aac12a5f1e0 1718 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1719 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1720 */
kbhagat6 0:1aac12a5f1e0 1721 bool glibr::getLightIntLowThreshold(uint16_t &threshold)
kbhagat6 0:1aac12a5f1e0 1722 {
kbhagat6 0:1aac12a5f1e0 1723 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 1724 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1725
kbhagat6 0:1aac12a5f1e0 1726 /* Read value from ambient light low threshold, low byte register */
kbhagat6 0:1aac12a5f1e0 1727 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTL);
kbhagat6 0:1aac12a5f1e0 1728 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTL, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1729 return false;
kbhagat6 0:1aac12a5f1e0 1730 }
kbhagat6 0:1aac12a5f1e0 1731 threshold = val_byte;
kbhagat6 0:1aac12a5f1e0 1732
kbhagat6 0:1aac12a5f1e0 1733 /* Read value from ambient light low threshold, high byte register */
kbhagat6 0:1aac12a5f1e0 1734 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTH);
kbhagat6 0:1aac12a5f1e0 1735 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTH, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1736 return false;
kbhagat6 0:1aac12a5f1e0 1737 }
kbhagat6 0:1aac12a5f1e0 1738 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 1739
kbhagat6 0:1aac12a5f1e0 1740 return true;
kbhagat6 0:1aac12a5f1e0 1741 }
kbhagat6 0:1aac12a5f1e0 1742
kbhagat6 0:1aac12a5f1e0 1743 /**
kbhagat6 0:1aac12a5f1e0 1744 * @brief Sets the low threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1745 *
kbhagat6 0:1aac12a5f1e0 1746 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1747 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1748 */
kbhagat6 0:1aac12a5f1e0 1749 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 0:1aac12a5f1e0 1750 {
kbhagat6 0:1aac12a5f1e0 1751 uint8_t val_low;
kbhagat6 0:1aac12a5f1e0 1752 uint8_t val_high;
kbhagat6 0:1aac12a5f1e0 1753
kbhagat6 0:1aac12a5f1e0 1754 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 0:1aac12a5f1e0 1755 val_low = threshold & 0x00FF;
kbhagat6 0:1aac12a5f1e0 1756 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 0:1aac12a5f1e0 1757
kbhagat6 0:1aac12a5f1e0 1758 /* Write low byte */
kbhagat6 0:1aac12a5f1e0 1759 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL,val_low)){//!wireWriteDataByte(APDS9960_AILTL, val_low) ) {
kbhagat6 0:1aac12a5f1e0 1760 return false;
kbhagat6 0:1aac12a5f1e0 1761 }
kbhagat6 0:1aac12a5f1e0 1762
kbhagat6 0:1aac12a5f1e0 1763 /* Write high byte */
kbhagat6 0:1aac12a5f1e0 1764 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH,val_high)){//!wireWriteDataByte(APDS9960_AILTH, val_high) ) {
kbhagat6 0:1aac12a5f1e0 1765 return false;
kbhagat6 0:1aac12a5f1e0 1766 }
kbhagat6 0:1aac12a5f1e0 1767
kbhagat6 0:1aac12a5f1e0 1768 return true;
kbhagat6 0:1aac12a5f1e0 1769 }
kbhagat6 0:1aac12a5f1e0 1770
kbhagat6 0:1aac12a5f1e0 1771 /**
kbhagat6 0:1aac12a5f1e0 1772 * @brief Gets the high threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1773 *
kbhagat6 0:1aac12a5f1e0 1774 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1775 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1776 */
kbhagat6 0:1aac12a5f1e0 1777 bool glibr::getLightIntHighThreshold(uint16_t &threshold)
kbhagat6 0:1aac12a5f1e0 1778 {
kbhagat6 0:1aac12a5f1e0 1779 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 1780 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1781
kbhagat6 0:1aac12a5f1e0 1782 /* Read value from ambient light high threshold, low byte register */
kbhagat6 0:1aac12a5f1e0 1783 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTL);
kbhagat6 0:1aac12a5f1e0 1784 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTL, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1785 return false;
kbhagat6 0:1aac12a5f1e0 1786 }
kbhagat6 0:1aac12a5f1e0 1787 threshold = val_byte;
kbhagat6 0:1aac12a5f1e0 1788
kbhagat6 0:1aac12a5f1e0 1789 /* Read value from ambient light high threshold, high byte register */
kbhagat6 0:1aac12a5f1e0 1790 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTH);
kbhagat6 0:1aac12a5f1e0 1791 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTH, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1792 return false;
kbhagat6 0:1aac12a5f1e0 1793 }
kbhagat6 0:1aac12a5f1e0 1794 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 1795
kbhagat6 0:1aac12a5f1e0 1796 return true;
kbhagat6 0:1aac12a5f1e0 1797 }
kbhagat6 0:1aac12a5f1e0 1798
kbhagat6 0:1aac12a5f1e0 1799 /**
kbhagat6 0:1aac12a5f1e0 1800 * @brief Sets the high threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1801 *
kbhagat6 0:1aac12a5f1e0 1802 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1803 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1804 */
kbhagat6 0:1aac12a5f1e0 1805 bool glibr::setLightIntHighThreshold(uint16_t threshold)
kbhagat6 0:1aac12a5f1e0 1806 {
kbhagat6 0:1aac12a5f1e0 1807 uint8_t val_low;
kbhagat6 0:1aac12a5f1e0 1808 uint8_t val_high;
kbhagat6 0:1aac12a5f1e0 1809
kbhagat6 0:1aac12a5f1e0 1810 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 0:1aac12a5f1e0 1811 val_low = threshold & 0x00FF;
kbhagat6 0:1aac12a5f1e0 1812 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 0:1aac12a5f1e0 1813
kbhagat6 0:1aac12a5f1e0 1814 /* Write low byte */
kbhagat6 0:1aac12a5f1e0 1815 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTL,val_low)){//!wireWriteDataByte(APDS9960_AIHTL, val_low) ) {
kbhagat6 0:1aac12a5f1e0 1816 return false;
kbhagat6 0:1aac12a5f1e0 1817 }
kbhagat6 0:1aac12a5f1e0 1818
kbhagat6 0:1aac12a5f1e0 1819 /* Write high byte */
kbhagat6 0:1aac12a5f1e0 1820 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTH,val_high)){//!wireWriteDataByte(APDS9960_AIHTH, val_high) ) {
kbhagat6 0:1aac12a5f1e0 1821 return false;
kbhagat6 0:1aac12a5f1e0 1822 }
kbhagat6 0:1aac12a5f1e0 1823
kbhagat6 0:1aac12a5f1e0 1824 return true;
kbhagat6 0:1aac12a5f1e0 1825 }
kbhagat6 0:1aac12a5f1e0 1826
kbhagat6 0:1aac12a5f1e0 1827 /**
kbhagat6 0:1aac12a5f1e0 1828 * @brief Gets the low threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1829 *
kbhagat6 0:1aac12a5f1e0 1830 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1831 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1832 */
kbhagat6 0:1aac12a5f1e0 1833 bool glibr::getProximityIntLowThreshold(uint8_t &threshold)
kbhagat6 0:1aac12a5f1e0 1834 {
kbhagat6 0:1aac12a5f1e0 1835 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1836
kbhagat6 0:1aac12a5f1e0 1837 /* Read value from proximity low threshold register */
kbhagat6 0:1aac12a5f1e0 1838 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1839 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1840 return false;
kbhagat6 0:1aac12a5f1e0 1841 }
kbhagat6 0:1aac12a5f1e0 1842
kbhagat6 0:1aac12a5f1e0 1843 return true;
kbhagat6 0:1aac12a5f1e0 1844 }
kbhagat6 0:1aac12a5f1e0 1845
kbhagat6 0:1aac12a5f1e0 1846 /**
kbhagat6 0:1aac12a5f1e0 1847 * @brief Sets the low threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1848 *
kbhagat6 0:1aac12a5f1e0 1849 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1850 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1851 */
kbhagat6 0:1aac12a5f1e0 1852 bool glibr::setProximityIntLowThreshold(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1853 {
kbhagat6 0:1aac12a5f1e0 1854
kbhagat6 0:1aac12a5f1e0 1855 /* Write threshold value to register */
kbhagat6 0:1aac12a5f1e0 1856 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT,threshold)){//!wireWriteDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1857 return false;
kbhagat6 0:1aac12a5f1e0 1858 }
kbhagat6 0:1aac12a5f1e0 1859
kbhagat6 0:1aac12a5f1e0 1860 return true;
kbhagat6 0:1aac12a5f1e0 1861 }
kbhagat6 0:1aac12a5f1e0 1862
kbhagat6 0:1aac12a5f1e0 1863 /**
kbhagat6 0:1aac12a5f1e0 1864 * @brief Gets the high threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1865 *
kbhagat6 0:1aac12a5f1e0 1866 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1867 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1868 */
kbhagat6 0:1aac12a5f1e0 1869 bool glibr::getProximityIntHighThreshold(uint8_t &threshold)
kbhagat6 0:1aac12a5f1e0 1870 {
kbhagat6 0:1aac12a5f1e0 1871 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1872
kbhagat6 0:1aac12a5f1e0 1873 /* Read value from proximity low threshold register */
kbhagat6 0:1aac12a5f1e0 1874 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PIHT);
kbhagat6 0:1aac12a5f1e0 1875 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1876 return false;
kbhagat6 0:1aac12a5f1e0 1877 }
kbhagat6 0:1aac12a5f1e0 1878
kbhagat6 0:1aac12a5f1e0 1879 return true;
kbhagat6 0:1aac12a5f1e0 1880 }
kbhagat6 0:1aac12a5f1e0 1881
kbhagat6 0:1aac12a5f1e0 1882 /**
kbhagat6 0:1aac12a5f1e0 1883 * @brief Sets the high threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1884 *
kbhagat6 0:1aac12a5f1e0 1885 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1886 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1887 */
kbhagat6 0:1aac12a5f1e0 1888 bool glibr::setProximityIntHighThreshold(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1889 {
kbhagat6 0:1aac12a5f1e0 1890
kbhagat6 0:1aac12a5f1e0 1891 /* Write threshold value to register */
kbhagat6 0:1aac12a5f1e0 1892 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT,threshold)){//!wireWriteDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1893 return false;
kbhagat6 0:1aac12a5f1e0 1894 }
kbhagat6 0:1aac12a5f1e0 1895
kbhagat6 0:1aac12a5f1e0 1896 return true;
kbhagat6 0:1aac12a5f1e0 1897 }
kbhagat6 0:1aac12a5f1e0 1898
kbhagat6 0:1aac12a5f1e0 1899 /**
kbhagat6 0:1aac12a5f1e0 1900 * @brief Gets if ambient light interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 1901 *
kbhagat6 0:1aac12a5f1e0 1902 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1903 */
kbhagat6 0:1aac12a5f1e0 1904 uint8_t glibr::getAmbientLightIntEnable()
kbhagat6 0:1aac12a5f1e0 1905 {
kbhagat6 0:1aac12a5f1e0 1906 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1907
kbhagat6 0:1aac12a5f1e0 1908 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1909 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1910 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1911 return ERROR;
kbhagat6 0:1aac12a5f1e0 1912 }
kbhagat6 0:1aac12a5f1e0 1913
kbhagat6 0:1aac12a5f1e0 1914 /* Shift and mask out AIEN bit */
kbhagat6 0:1aac12a5f1e0 1915 val = (val >> 4) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1916
kbhagat6 0:1aac12a5f1e0 1917 return val;
kbhagat6 0:1aac12a5f1e0 1918 }
kbhagat6 0:1aac12a5f1e0 1919
kbhagat6 0:1aac12a5f1e0 1920 /**
kbhagat6 0:1aac12a5f1e0 1921 * @brief Turns ambient light interrupts on or off
kbhagat6 0:1aac12a5f1e0 1922 *
kbhagat6 0:1aac12a5f1e0 1923 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 1924 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1925 */
kbhagat6 0:1aac12a5f1e0 1926 bool glibr::setAmbientLightIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1927 {
kbhagat6 0:1aac12a5f1e0 1928 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1929
kbhagat6 0:1aac12a5f1e0 1930 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1931 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1932 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1933 return false;
kbhagat6 0:1aac12a5f1e0 1934 }
kbhagat6 0:1aac12a5f1e0 1935
kbhagat6 0:1aac12a5f1e0 1936 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1937 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1938 enable = enable << 4;
kbhagat6 0:1aac12a5f1e0 1939 val &= 0xEF;//0b11101111;
kbhagat6 0:1aac12a5f1e0 1940 val |= enable;
kbhagat6 0:1aac12a5f1e0 1941
kbhagat6 0:1aac12a5f1e0 1942 /* Write register value back into ENABLE register */
kbhagat6 0:1aac12a5f1e0 1943 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1944 return false;
kbhagat6 0:1aac12a5f1e0 1945 }
kbhagat6 0:1aac12a5f1e0 1946
kbhagat6 0:1aac12a5f1e0 1947 return true;
kbhagat6 0:1aac12a5f1e0 1948 }
kbhagat6 0:1aac12a5f1e0 1949
kbhagat6 0:1aac12a5f1e0 1950 /**
kbhagat6 0:1aac12a5f1e0 1951 * @brief Gets if proximity interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 1952 *
kbhagat6 0:1aac12a5f1e0 1953 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1954 */
kbhagat6 0:1aac12a5f1e0 1955 uint8_t glibr::getProximityIntEnable()
kbhagat6 0:1aac12a5f1e0 1956 {
kbhagat6 0:1aac12a5f1e0 1957 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1958
kbhagat6 0:1aac12a5f1e0 1959 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1960 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1961 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1962 return ERROR;
kbhagat6 0:1aac12a5f1e0 1963 }
kbhagat6 0:1aac12a5f1e0 1964
kbhagat6 0:1aac12a5f1e0 1965 /* Shift and mask out PIEN bit */
kbhagat6 0:1aac12a5f1e0 1966 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1967
kbhagat6 0:1aac12a5f1e0 1968 return val;
kbhagat6 0:1aac12a5f1e0 1969 }
kbhagat6 0:1aac12a5f1e0 1970
kbhagat6 0:1aac12a5f1e0 1971 /**
kbhagat6 0:1aac12a5f1e0 1972 * @brief Turns proximity interrupts on or off
kbhagat6 0:1aac12a5f1e0 1973 *
kbhagat6 0:1aac12a5f1e0 1974 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 1975 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1976 */
kbhagat6 0:1aac12a5f1e0 1977 bool glibr::setProximityIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1978 {
kbhagat6 0:1aac12a5f1e0 1979 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1980
kbhagat6 0:1aac12a5f1e0 1981 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1982 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1983 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1984 return false;
kbhagat6 0:1aac12a5f1e0 1985 }
kbhagat6 0:1aac12a5f1e0 1986
kbhagat6 0:1aac12a5f1e0 1987 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1988 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1989 enable = enable << 5;
kbhagat6 0:1aac12a5f1e0 1990 val &= 0xDF;//0b11011111;
kbhagat6 0:1aac12a5f1e0 1991 val |= enable;
kbhagat6 0:1aac12a5f1e0 1992
kbhagat6 0:1aac12a5f1e0 1993 /* Write register value back into ENABLE register */
kbhagat6 0:1aac12a5f1e0 1994 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1995 return false;
kbhagat6 0:1aac12a5f1e0 1996 }
kbhagat6 0:1aac12a5f1e0 1997
kbhagat6 0:1aac12a5f1e0 1998 return true;
kbhagat6 0:1aac12a5f1e0 1999 }
kbhagat6 0:1aac12a5f1e0 2000
kbhagat6 0:1aac12a5f1e0 2001 /**
kbhagat6 0:1aac12a5f1e0 2002 * @brief Gets if gesture interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 2003 *
kbhagat6 0:1aac12a5f1e0 2004 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 2005 */
kbhagat6 0:1aac12a5f1e0 2006 uint8_t glibr::getGestureIntEnable()
kbhagat6 0:1aac12a5f1e0 2007 {
kbhagat6 0:1aac12a5f1e0 2008 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2009
kbhagat6 0:1aac12a5f1e0 2010 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2011 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2012 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2013 return ERROR;
kbhagat6 0:1aac12a5f1e0 2014 }
kbhagat6 0:1aac12a5f1e0 2015
kbhagat6 0:1aac12a5f1e0 2016 /* Shift and mask out GIEN bit */
kbhagat6 0:1aac12a5f1e0 2017 val = (val >> 1) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2018
kbhagat6 0:1aac12a5f1e0 2019 return val;
kbhagat6 0:1aac12a5f1e0 2020 }
kbhagat6 0:1aac12a5f1e0 2021
kbhagat6 0:1aac12a5f1e0 2022 /**
kbhagat6 0:1aac12a5f1e0 2023 * @brief Turns gesture-related interrupts on or off
kbhagat6 0:1aac12a5f1e0 2024 *
kbhagat6 0:1aac12a5f1e0 2025 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 2026 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 2027 */
kbhagat6 0:1aac12a5f1e0 2028 bool glibr::setGestureIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 2029 {
kbhagat6 0:1aac12a5f1e0 2030 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2031
kbhagat6 0:1aac12a5f1e0 2032 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2033 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2034 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2035 return false;
kbhagat6 0:1aac12a5f1e0 2036 }
kbhagat6 0:1aac12a5f1e0 2037
kbhagat6 0:1aac12a5f1e0 2038 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 2039 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2040 enable = enable << 1;
kbhagat6 0:1aac12a5f1e0 2041 val &= 0xFD;//0b11111101;
kbhagat6 0:1aac12a5f1e0 2042 val |= enable;
kbhagat6 0:1aac12a5f1e0 2043
kbhagat6 0:1aac12a5f1e0 2044 /* Write register value back into GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2045 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2046 return false;
kbhagat6 0:1aac12a5f1e0 2047 }
kbhagat6 0:1aac12a5f1e0 2048
kbhagat6 0:1aac12a5f1e0 2049 return true;
kbhagat6 0:1aac12a5f1e0 2050 }
kbhagat6 0:1aac12a5f1e0 2051
kbhagat6 0:1aac12a5f1e0 2052 /**
kbhagat6 0:1aac12a5f1e0 2053 * @brief Clears the ambient light interrupt
kbhagat6 0:1aac12a5f1e0 2054 *
kbhagat6 0:1aac12a5f1e0 2055 * @return True if operation completed successfully. False otherwise.
kbhagat6 0:1aac12a5f1e0 2056 */
kbhagat6 0:1aac12a5f1e0 2057 bool glibr::clearAmbientLightInt()
kbhagat6 0:1aac12a5f1e0 2058 {
kbhagat6 0:1aac12a5f1e0 2059 uint8_t throwaway;
kbhagat6 0:1aac12a5f1e0 2060 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AICLEAR);
kbhagat6 0:1aac12a5f1e0 2061 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_AICLEAR, throwaway) ) {
kbhagat6 0:1aac12a5f1e0 2062 return false;
kbhagat6 0:1aac12a5f1e0 2063 }
kbhagat6 0:1aac12a5f1e0 2064
kbhagat6 0:1aac12a5f1e0 2065 return true;
kbhagat6 0:1aac12a5f1e0 2066 }
kbhagat6 0:1aac12a5f1e0 2067
kbhagat6 0:1aac12a5f1e0 2068 /**
kbhagat6 0:1aac12a5f1e0 2069 * @brief Clears the proximity interrupt
kbhagat6 0:1aac12a5f1e0 2070 *
kbhagat6 0:1aac12a5f1e0 2071 * @return True if operation completed successfully. False otherwise.
kbhagat6 0:1aac12a5f1e0 2072 */
kbhagat6 0:1aac12a5f1e0 2073 bool glibr::clearProximityInt()
kbhagat6 0:1aac12a5f1e0 2074 {
kbhagat6 0:1aac12a5f1e0 2075 uint8_t throwaway;
kbhagat6 0:1aac12a5f1e0 2076 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PICLEAR);
kbhagat6 0:1aac12a5f1e0 2077 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_PICLEAR, throwaway) ) {
kbhagat6 0:1aac12a5f1e0 2078 return false;
kbhagat6 0:1aac12a5f1e0 2079 }
kbhagat6 0:1aac12a5f1e0 2080
kbhagat6 0:1aac12a5f1e0 2081 return true;
kbhagat6 0:1aac12a5f1e0 2082 }
kbhagat6 0:1aac12a5f1e0 2083
kbhagat6 0:1aac12a5f1e0 2084 /**
kbhagat6 0:1aac12a5f1e0 2085 * @brief Tells if the gesture state machine is currently running
kbhagat6 0:1aac12a5f1e0 2086 *
kbhagat6 0:1aac12a5f1e0 2087 * @return 1 if gesture state machine is running, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 2088 */
kbhagat6 0:1aac12a5f1e0 2089 uint8_t glibr::getGestureMode()
kbhagat6 0:1aac12a5f1e0 2090 {
kbhagat6 0:1aac12a5f1e0 2091 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2092
kbhagat6 0:1aac12a5f1e0 2093 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2094 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2095 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2096 return ERROR;
kbhagat6 0:1aac12a5f1e0 2097 }
kbhagat6 0:1aac12a5f1e0 2098
kbhagat6 0:1aac12a5f1e0 2099 /* Mask out GMODE bit */
kbhagat6 0:1aac12a5f1e0 2100 val &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2101
kbhagat6 0:1aac12a5f1e0 2102 return val;
kbhagat6 0:1aac12a5f1e0 2103 }
kbhagat6 0:1aac12a5f1e0 2104
kbhagat6 0:1aac12a5f1e0 2105 /**
kbhagat6 0:1aac12a5f1e0 2106 * @brief Tells the state machine to either enter or exit gesture state machine
kbhagat6 0:1aac12a5f1e0 2107 *
kbhagat6 0:1aac12a5f1e0 2108 * @param[in] mode 1 to enter gesture state machine, 0 to exit.
kbhagat6 0:1aac12a5f1e0 2109 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 2110 */
kbhagat6 0:1aac12a5f1e0 2111 bool glibr::setGestureMode(uint8_t mode)
kbhagat6 0:1aac12a5f1e0 2112 {
kbhagat6 0:1aac12a5f1e0 2113 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2114
kbhagat6 0:1aac12a5f1e0 2115 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2116 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2117 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2118 return false;
kbhagat6 0:1aac12a5f1e0 2119 }
kbhagat6 0:1aac12a5f1e0 2120
kbhagat6 0:1aac12a5f1e0 2121 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 2122 mode &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2123 val &= 0xFE;//0b11111110;
kbhagat6 0:1aac12a5f1e0 2124 val |= mode;
kbhagat6 0:1aac12a5f1e0 2125
kbhagat6 0:1aac12a5f1e0 2126 /* Write register value back into GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2127 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2128 return false;
kbhagat6 0:1aac12a5f1e0 2129 }
kbhagat6 0:1aac12a5f1e0 2130
kbhagat6 0:1aac12a5f1e0 2131 return true;
kbhagat6 0:1aac12a5f1e0 2132 }
kbhagat6 0:1aac12a5f1e0 2133
kbhagat6 0:1aac12a5f1e0 2134
kbhagat6 0:1aac12a5f1e0 2135
kbhagat6 0:1aac12a5f1e0 2136
kbhagat6 0:1aac12a5f1e0 2137
kbhagat6 0:1aac12a5f1e0 2138 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 0:1aac12a5f1e0 2139 {
kbhagat6 0:1aac12a5f1e0 2140 int ret;
kbhagat6 0:1aac12a5f1e0 2141 char cmd[2] = {subAddress, data};
kbhagat6 0:1aac12a5f1e0 2142 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 0:1aac12a5f1e0 2143 return ret;
kbhagat6 0:1aac12a5f1e0 2144 }
kbhagat6 0:1aac12a5f1e0 2145
kbhagat6 0:1aac12a5f1e0 2146
kbhagat6 0:1aac12a5f1e0 2147
kbhagat6 0:1aac12a5f1e0 2148 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:1aac12a5f1e0 2149 {
kbhagat6 0:1aac12a5f1e0 2150 char data; // store the register data
kbhagat6 0:1aac12a5f1e0 2151
kbhagat6 0:1aac12a5f1e0 2152 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:1aac12a5f1e0 2153 return ERROR; //7 bit //not acked
kbhagat6 0:1aac12a5f1e0 2154 }
kbhagat6 0:1aac12a5f1e0 2155 if(i2c.read(address<<1, &data, 1)){ /////CHANGED THIS NEED TO TEST.
kbhagat6 0:1aac12a5f1e0 2156 return ERROR;
kbhagat6 0:1aac12a5f1e0 2157 }
kbhagat6 0:1aac12a5f1e0 2158
kbhagat6 0:1aac12a5f1e0 2159
kbhagat6 0:1aac12a5f1e0 2160 //i2c.read(address<<1, &data, 1);
kbhagat6 0:1aac12a5f1e0 2161 return data;
kbhagat6 0:1aac12a5f1e0 2162
kbhagat6 0:1aac12a5f1e0 2163 }
kbhagat6 0:1aac12a5f1e0 2164
kbhagat6 0:1aac12a5f1e0 2165
kbhagat6 0:1aac12a5f1e0 2166 // * @brief Reads a block (array) of bytes from the I2C device and register
kbhagat6 0:1aac12a5f1e0 2167 // *
kbhagat6 0:1aac12a5f1e0 2168 // * @param[in] reg the register to read from
kbhagat6 0:1aac12a5f1e0 2169 // * @param[out] val pointer to the beginning of the data
kbhagat6 0:1aac12a5f1e0 2170 // * @param[in] len number of bytes to read
kbhagat6 0:1aac12a5f1e0 2171 // * @return Number of bytes read. -1 on read error.
kbhagat6 0:1aac12a5f1e0 2172 // */
kbhagat6 0:1aac12a5f1e0 2173 int glibr::I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len)
kbhagat6 0:1aac12a5f1e0 2174 {
kbhagat6 0:1aac12a5f1e0 2175 // unsigned char i = 0;
kbhagat6 0:1aac12a5f1e0 2176
kbhagat6 0:1aac12a5f1e0 2177 /* Indicate which register we want to read from */
kbhagat6 0:1aac12a5f1e0 2178
kbhagat6 0:1aac12a5f1e0 2179 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:1aac12a5f1e0 2180 return -1; //7 bit //not acked
kbhagat6 0:1aac12a5f1e0 2181 }
kbhagat6 0:1aac12a5f1e0 2182
kbhagat6 0:1aac12a5f1e0 2183 /* Read block data */
kbhagat6 0:1aac12a5f1e0 2184
kbhagat6 0:1aac12a5f1e0 2185 if(i2c.read(address<<1, data, len)){
kbhagat6 0:1aac12a5f1e0 2186 return -1;
kbhagat6 0:1aac12a5f1e0 2187 }
kbhagat6 0:1aac12a5f1e0 2188
kbhagat6 0:1aac12a5f1e0 2189 return 1;
kbhagat6 0:1aac12a5f1e0 2190 //Wire.requestFrom(APDS9960_I2C_ADDR, len);
kbhagat6 0:1aac12a5f1e0 2191 /*while (Wire.available()) {
kbhagat6 0:1aac12a5f1e0 2192 if (i >= len) {
kbhagat6 0:1aac12a5f1e0 2193 return -1;
kbhagat6 0:1aac12a5f1e0 2194 }
kbhagat6 0:1aac12a5f1e0 2195 val[i] = Wire.read();
kbhagat6 0:1aac12a5f1e0 2196 i++;
kbhagat6 0:1aac12a5f1e0 2197 }*/
kbhagat6 0:1aac12a5f1e0 2198 }