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:
Mon Apr 27 02:05:49 2015 +0000
Revision:
2:ba051af6731a
Parent:
0:1aac12a5f1e0
gesture control

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 2:ba051af6731a 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 2:ba051af6731a 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 2:ba051af6731a 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 2:ba051af6731a 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 return true;
kbhagat6 0:1aac12a5f1e0 595 }
kbhagat6 0:1aac12a5f1e0 596
kbhagat6 0:1aac12a5f1e0 597 /**
kbhagat6 0:1aac12a5f1e0 598 * @brief Reads the red light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 599 *
kbhagat6 0:1aac12a5f1e0 600 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 601 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 602 */
kbhagat6 0:1aac12a5f1e0 603 bool glibr::readRedLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 604 {
kbhagat6 0:1aac12a5f1e0 605 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 606 val = 0;
kbhagat6 0:1aac12a5f1e0 607
kbhagat6 0:1aac12a5f1e0 608 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 609 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL);
kbhagat6 0:1aac12a5f1e0 610 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 611 return false;
kbhagat6 0:1aac12a5f1e0 612 }
kbhagat6 0:1aac12a5f1e0 613
kbhagat6 0:1aac12a5f1e0 614 val = val_byte;
kbhagat6 0:1aac12a5f1e0 615
kbhagat6 0:1aac12a5f1e0 616 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 617 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH);
kbhagat6 0:1aac12a5f1e0 618 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 619 return false;
kbhagat6 0:1aac12a5f1e0 620 }
kbhagat6 0:1aac12a5f1e0 621 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 622
kbhagat6 0:1aac12a5f1e0 623 return true;
kbhagat6 0:1aac12a5f1e0 624 }
kbhagat6 0:1aac12a5f1e0 625
kbhagat6 0:1aac12a5f1e0 626 /**
kbhagat6 0:1aac12a5f1e0 627 * @brief Reads the green light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 628 *
kbhagat6 0:1aac12a5f1e0 629 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 630 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 631 */
kbhagat6 0:1aac12a5f1e0 632
kbhagat6 0:1aac12a5f1e0 633 bool glibr::readGreenLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 634 {
kbhagat6 0:1aac12a5f1e0 635 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 636 val = 0;
kbhagat6 0:1aac12a5f1e0 637
kbhagat6 0:1aac12a5f1e0 638 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 639 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL);
kbhagat6 0:1aac12a5f1e0 640 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 641 return false;
kbhagat6 0:1aac12a5f1e0 642 }
kbhagat6 0:1aac12a5f1e0 643
kbhagat6 0:1aac12a5f1e0 644 val = val_byte;
kbhagat6 0:1aac12a5f1e0 645
kbhagat6 0:1aac12a5f1e0 646 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 647 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH);
kbhagat6 0:1aac12a5f1e0 648 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 649 return false;
kbhagat6 0:1aac12a5f1e0 650 }
kbhagat6 0:1aac12a5f1e0 651 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 652
kbhagat6 0:1aac12a5f1e0 653 return true;
kbhagat6 0:1aac12a5f1e0 654 }
kbhagat6 0:1aac12a5f1e0 655
kbhagat6 0:1aac12a5f1e0 656 /**
kbhagat6 0:1aac12a5f1e0 657 * @brief Reads the red light level as a 16-bit value
kbhagat6 0:1aac12a5f1e0 658 *
kbhagat6 0:1aac12a5f1e0 659 * @param[out] val value of the light sensor.
kbhagat6 0:1aac12a5f1e0 660 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 661 */
kbhagat6 0:1aac12a5f1e0 662
kbhagat6 0:1aac12a5f1e0 663 bool glibr::readBlueLight(uint16_t &val)
kbhagat6 0:1aac12a5f1e0 664 {
kbhagat6 0:1aac12a5f1e0 665 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 666 val = 0;
kbhagat6 0:1aac12a5f1e0 667
kbhagat6 0:1aac12a5f1e0 668 /* Read value from clear channel, low byte register */
kbhagat6 0:1aac12a5f1e0 669 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL);
kbhagat6 0:1aac12a5f1e0 670 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 671 return false;
kbhagat6 0:1aac12a5f1e0 672 }
kbhagat6 0:1aac12a5f1e0 673
kbhagat6 0:1aac12a5f1e0 674 val = val_byte;
kbhagat6 0:1aac12a5f1e0 675
kbhagat6 0:1aac12a5f1e0 676 /* Read value from clear channel, high byte register */
kbhagat6 0:1aac12a5f1e0 677 val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH);
kbhagat6 0:1aac12a5f1e0 678 if( val_byte==ERROR) {
kbhagat6 0:1aac12a5f1e0 679 return false;
kbhagat6 0:1aac12a5f1e0 680 }
kbhagat6 0:1aac12a5f1e0 681 val = val + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 682
kbhagat6 0:1aac12a5f1e0 683 return true;
kbhagat6 0:1aac12a5f1e0 684 }
kbhagat6 0:1aac12a5f1e0 685
kbhagat6 0:1aac12a5f1e0 686 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 687 * Proximity sensor controls
kbhagat6 0:1aac12a5f1e0 688 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 689
kbhagat6 0:1aac12a5f1e0 690 /**
kbhagat6 0:1aac12a5f1e0 691 * @brief Reads the proximity level as an 8-bit value
kbhagat6 0:1aac12a5f1e0 692 *
kbhagat6 0:1aac12a5f1e0 693 * @param[out] val value of the proximity sensor.
kbhagat6 0:1aac12a5f1e0 694 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 695 */
kbhagat6 0:1aac12a5f1e0 696 bool glibr::readProximity(uint8_t &val)
kbhagat6 0:1aac12a5f1e0 697 {
kbhagat6 0:1aac12a5f1e0 698 val = 0;
kbhagat6 0:1aac12a5f1e0 699
kbhagat6 0:1aac12a5f1e0 700 /* Read value from proximity data register */
kbhagat6 0:1aac12a5f1e0 701 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA);
kbhagat6 0:1aac12a5f1e0 702
kbhagat6 0:1aac12a5f1e0 703 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 704 return false;
kbhagat6 0:1aac12a5f1e0 705 }
kbhagat6 0:1aac12a5f1e0 706
kbhagat6 0:1aac12a5f1e0 707 return true;
kbhagat6 0:1aac12a5f1e0 708 }
kbhagat6 0:1aac12a5f1e0 709
kbhagat6 0:1aac12a5f1e0 710 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 711 * High-level gesture controls
kbhagat6 0:1aac12a5f1e0 712 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 713
kbhagat6 0:1aac12a5f1e0 714 /**
kbhagat6 0:1aac12a5f1e0 715 * @brief Resets all the parameters in the gesture data member
kbhagat6 0:1aac12a5f1e0 716 */
kbhagat6 0:1aac12a5f1e0 717 void glibr::resetGestureParameters()
kbhagat6 0:1aac12a5f1e0 718 {
kbhagat6 0:1aac12a5f1e0 719 gesture_data_.sindex = 0;
kbhagat6 0:1aac12a5f1e0 720 gesture_data_.total_gestures = 0;
kbhagat6 0:1aac12a5f1e0 721
kbhagat6 0:1aac12a5f1e0 722 gesture_ud_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 723 gesture_lr_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 724
kbhagat6 0:1aac12a5f1e0 725 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 726 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 727
kbhagat6 0:1aac12a5f1e0 728 gesture_near_count_ = 0;
kbhagat6 0:1aac12a5f1e0 729 gesture_far_count_ = 0;
kbhagat6 0:1aac12a5f1e0 730
kbhagat6 0:1aac12a5f1e0 731 gesture_state_ = 0;
kbhagat6 0:1aac12a5f1e0 732 gesture_motion_ = DIR_NONE;
kbhagat6 0:1aac12a5f1e0 733 }
kbhagat6 0:1aac12a5f1e0 734
kbhagat6 0:1aac12a5f1e0 735 bool glibr::processGestureData()
kbhagat6 0:1aac12a5f1e0 736 {
kbhagat6 0:1aac12a5f1e0 737 uint8_t u_first = 0;
kbhagat6 0:1aac12a5f1e0 738 uint8_t d_first = 0;
kbhagat6 0:1aac12a5f1e0 739 uint8_t l_first = 0;
kbhagat6 0:1aac12a5f1e0 740 uint8_t r_first = 0;
kbhagat6 0:1aac12a5f1e0 741 uint8_t u_last = 0;
kbhagat6 0:1aac12a5f1e0 742 uint8_t d_last = 0;
kbhagat6 0:1aac12a5f1e0 743 uint8_t l_last = 0;
kbhagat6 0:1aac12a5f1e0 744 uint8_t r_last = 0;
kbhagat6 0:1aac12a5f1e0 745 int ud_ratio_first;
kbhagat6 0:1aac12a5f1e0 746 int lr_ratio_first;
kbhagat6 0:1aac12a5f1e0 747 int ud_ratio_last;
kbhagat6 0:1aac12a5f1e0 748 int lr_ratio_last;
kbhagat6 0:1aac12a5f1e0 749 int ud_delta;
kbhagat6 0:1aac12a5f1e0 750 int lr_delta;
kbhagat6 0:1aac12a5f1e0 751 int i;
kbhagat6 0:1aac12a5f1e0 752
kbhagat6 0:1aac12a5f1e0 753 /* If we have less than 4 total gestures, that's not enough */
kbhagat6 0:1aac12a5f1e0 754 if( gesture_data_.total_gestures <= 4 ) {
kbhagat6 0:1aac12a5f1e0 755 return false;
kbhagat6 0:1aac12a5f1e0 756 }
kbhagat6 0:1aac12a5f1e0 757
kbhagat6 0:1aac12a5f1e0 758 /* Check to make sure our data isn't out of bounds */
kbhagat6 0:1aac12a5f1e0 759 if( (gesture_data_.total_gestures <= 32) && \
kbhagat6 0:1aac12a5f1e0 760 (gesture_data_.total_gestures > 0) ) {
kbhagat6 0:1aac12a5f1e0 761
kbhagat6 0:1aac12a5f1e0 762 /* Find the first value in U/D/L/R above the threshold */
kbhagat6 0:1aac12a5f1e0 763 for( i = 0; i < gesture_data_.total_gestures; i++ ) {
kbhagat6 0:1aac12a5f1e0 764 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 765 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 766 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 767 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 0:1aac12a5f1e0 768
kbhagat6 0:1aac12a5f1e0 769 u_first = gesture_data_.u_data[i];
kbhagat6 0:1aac12a5f1e0 770 d_first = gesture_data_.d_data[i];
kbhagat6 0:1aac12a5f1e0 771 l_first = gesture_data_.l_data[i];
kbhagat6 0:1aac12a5f1e0 772 r_first = gesture_data_.r_data[i];
kbhagat6 0:1aac12a5f1e0 773 break;
kbhagat6 0:1aac12a5f1e0 774 }
kbhagat6 0:1aac12a5f1e0 775 }
kbhagat6 0:1aac12a5f1e0 776
kbhagat6 0:1aac12a5f1e0 777 /* If one of the _first values is 0, then there is no good data */
kbhagat6 0:1aac12a5f1e0 778 if( (u_first == 0) || (d_first == 0) || \
kbhagat6 0:1aac12a5f1e0 779 (l_first == 0) || (r_first == 0) ) {
kbhagat6 0:1aac12a5f1e0 780
kbhagat6 0:1aac12a5f1e0 781 return false;
kbhagat6 0:1aac12a5f1e0 782 }
kbhagat6 0:1aac12a5f1e0 783 /* Find the last value in U/D/L/R above the threshold */
kbhagat6 0:1aac12a5f1e0 784 for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
kbhagat6 0:1aac12a5f1e0 785 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 786 Serial.print(F("Finding last: "));
kbhagat6 0:1aac12a5f1e0 787 Serial.print(F("U:"));
kbhagat6 0:1aac12a5f1e0 788 Serial.print(gesture_data_.u_data[i]);
kbhagat6 0:1aac12a5f1e0 789 Serial.print(F(" D:"));
kbhagat6 0:1aac12a5f1e0 790 Serial.print(gesture_data_.d_data[i]);
kbhagat6 0:1aac12a5f1e0 791 Serial.print(F(" L:"));
kbhagat6 0:1aac12a5f1e0 792 Serial.print(gesture_data_.l_data[i]);
kbhagat6 0:1aac12a5f1e0 793 Serial.print(F(" R:"));
kbhagat6 0:1aac12a5f1e0 794 Serial.println(gesture_data_.r_data[i]);
kbhagat6 0:1aac12a5f1e0 795 #endif */
kbhagat6 0:1aac12a5f1e0 796 if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 797 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 798 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
kbhagat6 0:1aac12a5f1e0 799 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
kbhagat6 0:1aac12a5f1e0 800
kbhagat6 0:1aac12a5f1e0 801 u_last = gesture_data_.u_data[i];
kbhagat6 0:1aac12a5f1e0 802 d_last = gesture_data_.d_data[i];
kbhagat6 0:1aac12a5f1e0 803 l_last = gesture_data_.l_data[i];
kbhagat6 0:1aac12a5f1e0 804 r_last = gesture_data_.r_data[i];
kbhagat6 0:1aac12a5f1e0 805 break;
kbhagat6 0:1aac12a5f1e0 806 }
kbhagat6 0:1aac12a5f1e0 807 }
kbhagat6 0:1aac12a5f1e0 808 }
kbhagat6 0:1aac12a5f1e0 809
kbhagat6 0:1aac12a5f1e0 810 /* Calculate the first vs. last ratio of up/down and left/right */
kbhagat6 0:1aac12a5f1e0 811 ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
kbhagat6 0:1aac12a5f1e0 812 lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
kbhagat6 0:1aac12a5f1e0 813 ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
kbhagat6 0:1aac12a5f1e0 814 lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
kbhagat6 0:1aac12a5f1e0 815
kbhagat6 0:1aac12a5f1e0 816 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 817 Serial.print(F("Last Values: "));
kbhagat6 0:1aac12a5f1e0 818 Serial.print(F("U:"));
kbhagat6 0:1aac12a5f1e0 819 Serial.print(u_last);
kbhagat6 0:1aac12a5f1e0 820 Serial.print(F(" D:"));
kbhagat6 0:1aac12a5f1e0 821 Serial.print(d_last);
kbhagat6 0:1aac12a5f1e0 822 Serial.print(F(" L:"));
kbhagat6 0:1aac12a5f1e0 823 Serial.print(l_last);
kbhagat6 0:1aac12a5f1e0 824 Serial.print(F(" R:"));
kbhagat6 0:1aac12a5f1e0 825 Serial.println(r_last);
kbhagat6 0:1aac12a5f1e0 826
kbhagat6 0:1aac12a5f1e0 827 Serial.print(F("Ratios: "));
kbhagat6 0:1aac12a5f1e0 828 Serial.print(F("UD Fi: "));
kbhagat6 0:1aac12a5f1e0 829 Serial.print(ud_ratio_first);
kbhagat6 0:1aac12a5f1e0 830 Serial.print(F(" UD La: "));
kbhagat6 0:1aac12a5f1e0 831 Serial.print(ud_ratio_last);
kbhagat6 0:1aac12a5f1e0 832 Serial.print(F(" LR Fi: "));
kbhagat6 0:1aac12a5f1e0 833 Serial.print(lr_ratio_first);
kbhagat6 0:1aac12a5f1e0 834 Serial.print(F(" LR La: "));
kbhagat6 0:1aac12a5f1e0 835 Serial.println(lr_ratio_last);
kbhagat6 0:1aac12a5f1e0 836 #endif */
kbhagat6 0:1aac12a5f1e0 837
kbhagat6 0:1aac12a5f1e0 838 /* Determine the difference between the first and last ratios */
kbhagat6 0:1aac12a5f1e0 839 ud_delta = ud_ratio_last - ud_ratio_first;
kbhagat6 0:1aac12a5f1e0 840 lr_delta = lr_ratio_last - lr_ratio_first;
kbhagat6 0:1aac12a5f1e0 841
kbhagat6 0:1aac12a5f1e0 842 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 843 Serial.print("Deltas: ");
kbhagat6 0:1aac12a5f1e0 844 Serial.print("UD: ");
kbhagat6 0:1aac12a5f1e0 845 Serial.print(ud_delta);
kbhagat6 0:1aac12a5f1e0 846 Serial.print(" LR: ");
kbhagat6 0:1aac12a5f1e0 847 Serial.println(lr_delta);
kbhagat6 0:1aac12a5f1e0 848 #endif */
kbhagat6 0:1aac12a5f1e0 849
kbhagat6 0:1aac12a5f1e0 850 /* Accumulate the UD and LR delta values */
kbhagat6 0:1aac12a5f1e0 851 gesture_ud_delta_ += ud_delta;
kbhagat6 0:1aac12a5f1e0 852 gesture_lr_delta_ += lr_delta;
kbhagat6 0:1aac12a5f1e0 853
kbhagat6 0:1aac12a5f1e0 854 /* #if DEBUG
kbhagat6 0:1aac12a5f1e0 855 Serial.print("Accumulations: ");
kbhagat6 0:1aac12a5f1e0 856 Serial.print("UD: ");
kbhagat6 0:1aac12a5f1e0 857 Serial.print(gesture_ud_delta_);
kbhagat6 0:1aac12a5f1e0 858 Serial.print(" LR: ");
kbhagat6 0:1aac12a5f1e0 859 Serial.println(gesture_lr_delta_);
kbhagat6 0:1aac12a5f1e0 860 #endif */
kbhagat6 0:1aac12a5f1e0 861
kbhagat6 0:1aac12a5f1e0 862 /* Determine U/D gesture */
kbhagat6 0:1aac12a5f1e0 863 if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 864 gesture_ud_count_ = 1;
kbhagat6 0:1aac12a5f1e0 865 } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 866 gesture_ud_count_ = -1;
kbhagat6 0:1aac12a5f1e0 867 } else {
kbhagat6 0:1aac12a5f1e0 868 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 869 }
kbhagat6 0:1aac12a5f1e0 870
kbhagat6 0:1aac12a5f1e0 871 /* Determine L/R gesture */
kbhagat6 0:1aac12a5f1e0 872 if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 873 gesture_lr_count_ = 1;
kbhagat6 0:1aac12a5f1e0 874 } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
kbhagat6 0:1aac12a5f1e0 875 gesture_lr_count_ = -1;
kbhagat6 0:1aac12a5f1e0 876 } else {
kbhagat6 0:1aac12a5f1e0 877 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 878 }
kbhagat6 0:1aac12a5f1e0 879
kbhagat6 0:1aac12a5f1e0 880 /* Determine Near/Far gesture */
kbhagat6 0:1aac12a5f1e0 881 if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 882 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 0:1aac12a5f1e0 883 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 0:1aac12a5f1e0 884
kbhagat6 0:1aac12a5f1e0 885 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 886 gesture_near_count_++;
kbhagat6 0:1aac12a5f1e0 887 } else if( (ud_delta != 0) || (lr_delta != 0) ) {
kbhagat6 0:1aac12a5f1e0 888 gesture_far_count_++;
kbhagat6 0:1aac12a5f1e0 889 }
kbhagat6 0:1aac12a5f1e0 890
kbhagat6 0:1aac12a5f1e0 891 if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
kbhagat6 0:1aac12a5f1e0 892 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 893 gesture_state_ = NEAR_STATE;
kbhagat6 0:1aac12a5f1e0 894 } else if( (ud_delta != 0) && (lr_delta != 0) ) {
kbhagat6 0:1aac12a5f1e0 895 gesture_state_ = FAR_STATE;
kbhagat6 0:1aac12a5f1e0 896 }
kbhagat6 0:1aac12a5f1e0 897 return true;
kbhagat6 0:1aac12a5f1e0 898 }
kbhagat6 0:1aac12a5f1e0 899 }
kbhagat6 0:1aac12a5f1e0 900 } else {
kbhagat6 0:1aac12a5f1e0 901 if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
kbhagat6 0:1aac12a5f1e0 902 (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
kbhagat6 0:1aac12a5f1e0 903
kbhagat6 0:1aac12a5f1e0 904 if( (ud_delta == 0) && (lr_delta == 0) ) {
kbhagat6 0:1aac12a5f1e0 905 gesture_near_count_++;
kbhagat6 0:1aac12a5f1e0 906 }
kbhagat6 0:1aac12a5f1e0 907
kbhagat6 0:1aac12a5f1e0 908 if( gesture_near_count_ >= 5 ) {
kbhagat6 0:1aac12a5f1e0 909 gesture_ud_count_ = 0;
kbhagat6 0:1aac12a5f1e0 910 gesture_lr_count_ = 0;
kbhagat6 0:1aac12a5f1e0 911 gesture_ud_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 912 gesture_lr_delta_ = 0;
kbhagat6 0:1aac12a5f1e0 913 }
kbhagat6 0:1aac12a5f1e0 914 }
kbhagat6 0:1aac12a5f1e0 915 }
kbhagat6 0:1aac12a5f1e0 916
kbhagat6 0:1aac12a5f1e0 917 // #if DEBUG
kbhagat6 0:1aac12a5f1e0 918 /* printf("UD_CT: %d\n",gesture_ud_count_);
kbhagat6 0:1aac12a5f1e0 919 printf("LR_CT: %d\n",gesture_lr_count_);
kbhagat6 0:1aac12a5f1e0 920 printf("NEAR_CT: %d\n",gesture_near_count_);
kbhagat6 0:1aac12a5f1e0 921 printf(" FAR_CT: %d\n",gesture_far_count_);
kbhagat6 0:1aac12a5f1e0 922 printf("----------"); */
kbhagat6 0:1aac12a5f1e0 923 //#endif */
kbhagat6 0:1aac12a5f1e0 924
kbhagat6 0:1aac12a5f1e0 925 return false;
kbhagat6 0:1aac12a5f1e0 926 }
kbhagat6 0:1aac12a5f1e0 927
kbhagat6 0:1aac12a5f1e0 928 /**
kbhagat6 0:1aac12a5f1e0 929 * @brief Determines swipe direction or near/far state
kbhagat6 0:1aac12a5f1e0 930 *
kbhagat6 0:1aac12a5f1e0 931 * @return True if near/far event. False otherwise.
kbhagat6 0:1aac12a5f1e0 932 */
kbhagat6 0:1aac12a5f1e0 933 bool glibr::decodeGesture()
kbhagat6 0:1aac12a5f1e0 934 {
kbhagat6 0:1aac12a5f1e0 935 /* Return if near or far event is detected */
kbhagat6 0:1aac12a5f1e0 936 if( gesture_state_ == NEAR_STATE ) {
kbhagat6 0:1aac12a5f1e0 937 gesture_motion_ = DIR_NEAR;
kbhagat6 0:1aac12a5f1e0 938 return true;
kbhagat6 0:1aac12a5f1e0 939 } else if ( gesture_state_ == FAR_STATE ) {
kbhagat6 0:1aac12a5f1e0 940 gesture_motion_ = DIR_FAR;
kbhagat6 0:1aac12a5f1e0 941 return true;
kbhagat6 0:1aac12a5f1e0 942 }
kbhagat6 0:1aac12a5f1e0 943
kbhagat6 0:1aac12a5f1e0 944 /* Determine swipe direction */
kbhagat6 0:1aac12a5f1e0 945 if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 946 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 947 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
kbhagat6 0:1aac12a5f1e0 948 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 949 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 950 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 951 } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 952 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 953 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 954 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 955 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 956 } else {
kbhagat6 0:1aac12a5f1e0 957 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 958 }
kbhagat6 0:1aac12a5f1e0 959 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 960 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 961 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 962 } else {
kbhagat6 0:1aac12a5f1e0 963 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 964 }
kbhagat6 0:1aac12a5f1e0 965 } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
kbhagat6 0:1aac12a5f1e0 966 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 967 gesture_motion_ = DIR_UP;
kbhagat6 0:1aac12a5f1e0 968 } else {
kbhagat6 0:1aac12a5f1e0 969 gesture_motion_ = DIR_LEFT;
kbhagat6 0:1aac12a5f1e0 970 }
kbhagat6 0:1aac12a5f1e0 971 } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
kbhagat6 0:1aac12a5f1e0 972 if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
kbhagat6 0:1aac12a5f1e0 973 gesture_motion_ = DIR_DOWN;
kbhagat6 0:1aac12a5f1e0 974 } else {
kbhagat6 0:1aac12a5f1e0 975 gesture_motion_ = DIR_RIGHT;
kbhagat6 0:1aac12a5f1e0 976 }
kbhagat6 0:1aac12a5f1e0 977 } else {
kbhagat6 0:1aac12a5f1e0 978 return false;
kbhagat6 0:1aac12a5f1e0 979 }
kbhagat6 0:1aac12a5f1e0 980
kbhagat6 0:1aac12a5f1e0 981 return true;
kbhagat6 0:1aac12a5f1e0 982 }
kbhagat6 0:1aac12a5f1e0 983
kbhagat6 0:1aac12a5f1e0 984 /*******************************************************************************
kbhagat6 0:1aac12a5f1e0 985 * Getters and setters for register values
kbhagat6 0:1aac12a5f1e0 986 ******************************************************************************/
kbhagat6 0:1aac12a5f1e0 987
kbhagat6 0:1aac12a5f1e0 988 /**
kbhagat6 0:1aac12a5f1e0 989 * @brief Returns the lower threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 990 *
kbhagat6 0:1aac12a5f1e0 991 * @return lower threshold
kbhagat6 0:1aac12a5f1e0 992 */
kbhagat6 0:1aac12a5f1e0 993
kbhagat6 0:1aac12a5f1e0 994 uint8_t glibr::getProxIntLowThresh()
kbhagat6 0:1aac12a5f1e0 995 {
kbhagat6 0:1aac12a5f1e0 996 uint8_t val;
kbhagat6 0:1aac12a5f1e0 997
kbhagat6 0:1aac12a5f1e0 998 /* Read value from PILT register */
kbhagat6 0:1aac12a5f1e0 999 /* if( !wireReadDataByte(APDS9960_PILT, val) ) {
kbhagat6 0:1aac12a5f1e0 1000 val = 0;
kbhagat6 0:1aac12a5f1e0 1001 }*/
kbhagat6 0:1aac12a5f1e0 1002 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1003 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1004 val=0;
kbhagat6 0:1aac12a5f1e0 1005 }
kbhagat6 0:1aac12a5f1e0 1006
kbhagat6 0:1aac12a5f1e0 1007 return val;
kbhagat6 0:1aac12a5f1e0 1008 }
kbhagat6 0:1aac12a5f1e0 1009
kbhagat6 0:1aac12a5f1e0 1010 /**
kbhagat6 0:1aac12a5f1e0 1011 * @brief Sets the lower threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1012 *
kbhagat6 0:1aac12a5f1e0 1013 * @param[in] threshold the lower proximity threshold
kbhagat6 0:1aac12a5f1e0 1014 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1015 */
kbhagat6 0:1aac12a5f1e0 1016 bool glibr::setProxIntLowThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1017 {
kbhagat6 0:1aac12a5f1e0 1018 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1019 return false;
kbhagat6 0:1aac12a5f1e0 1020 }
kbhagat6 0:1aac12a5f1e0 1021
kbhagat6 0:1aac12a5f1e0 1022 return true;
kbhagat6 0:1aac12a5f1e0 1023 }
kbhagat6 0:1aac12a5f1e0 1024
kbhagat6 0:1aac12a5f1e0 1025 /**
kbhagat6 0:1aac12a5f1e0 1026 * @brief Returns the high threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1027 *
kbhagat6 0:1aac12a5f1e0 1028 * @return high threshold
kbhagat6 0:1aac12a5f1e0 1029 */
kbhagat6 0:1aac12a5f1e0 1030 uint8_t glibr::getProxIntHighThresh()
kbhagat6 0:1aac12a5f1e0 1031 {
kbhagat6 0:1aac12a5f1e0 1032 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1033
kbhagat6 0:1aac12a5f1e0 1034 /* Read value from PIHT register */
kbhagat6 0:1aac12a5f1e0 1035 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1036 if( val==ERROR ) {
kbhagat6 0:1aac12a5f1e0 1037 val = 0;
kbhagat6 0:1aac12a5f1e0 1038 }
kbhagat6 0:1aac12a5f1e0 1039
kbhagat6 0:1aac12a5f1e0 1040 return val;
kbhagat6 0:1aac12a5f1e0 1041 }
kbhagat6 0:1aac12a5f1e0 1042
kbhagat6 0:1aac12a5f1e0 1043 /**
kbhagat6 0:1aac12a5f1e0 1044 * @brief Sets the high threshold for proximity detection
kbhagat6 0:1aac12a5f1e0 1045 *
kbhagat6 0:1aac12a5f1e0 1046 * @param[in] threshold the high proximity threshold
kbhagat6 0:1aac12a5f1e0 1047 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1048 */
kbhagat6 0:1aac12a5f1e0 1049 bool glibr::setProxIntHighThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1050 {
kbhagat6 0:1aac12a5f1e0 1051
kbhagat6 0:1aac12a5f1e0 1052 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1053 return false;
kbhagat6 0:1aac12a5f1e0 1054 }
kbhagat6 0:1aac12a5f1e0 1055
kbhagat6 0:1aac12a5f1e0 1056 return true;
kbhagat6 0:1aac12a5f1e0 1057 }
kbhagat6 0:1aac12a5f1e0 1058
kbhagat6 0:1aac12a5f1e0 1059 /**
kbhagat6 0:1aac12a5f1e0 1060 * @brief Returns LED drive strength for proximity and ALS
kbhagat6 0:1aac12a5f1e0 1061 *
kbhagat6 0:1aac12a5f1e0 1062 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1063 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1064 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1065 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1066 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1067 *
kbhagat6 0:1aac12a5f1e0 1068 * @return the value of the LED drive strength. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1069 */
kbhagat6 0:1aac12a5f1e0 1070 uint8_t glibr::getLEDDrive()
kbhagat6 0:1aac12a5f1e0 1071 {
kbhagat6 0:1aac12a5f1e0 1072 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1073
kbhagat6 0:1aac12a5f1e0 1074 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1075 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1076 if( val == ERROR ){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1077 return ERROR;
kbhagat6 0:1aac12a5f1e0 1078 }
kbhagat6 0:1aac12a5f1e0 1079
kbhagat6 0:1aac12a5f1e0 1080 /* Shift and mask out LED drive bits */
kbhagat6 0:1aac12a5f1e0 1081 val = (val >> 6) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1082
kbhagat6 0:1aac12a5f1e0 1083 return val;
kbhagat6 0:1aac12a5f1e0 1084 }
kbhagat6 0:1aac12a5f1e0 1085
kbhagat6 0:1aac12a5f1e0 1086 /**
kbhagat6 0:1aac12a5f1e0 1087 * @brief Sets the LED drive strength for proximity and ALS
kbhagat6 0:1aac12a5f1e0 1088 *
kbhagat6 0:1aac12a5f1e0 1089 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1090 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1091 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1092 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1093 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1094 *
kbhagat6 0:1aac12a5f1e0 1095 * @param[in] drive the value (0-3) for the LED drive strength
kbhagat6 0:1aac12a5f1e0 1096 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1097 */
kbhagat6 0:1aac12a5f1e0 1098
kbhagat6 0:1aac12a5f1e0 1099 bool glibr::setLEDDrive(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1100 {
kbhagat6 0:1aac12a5f1e0 1101 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1102
kbhagat6 0:1aac12a5f1e0 1103 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1104 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1105 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1106 return false;
kbhagat6 0:1aac12a5f1e0 1107 }
kbhagat6 0:1aac12a5f1e0 1108 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1109 //drive &= 0b00000011
kbhagat6 0:1aac12a5f1e0 1110 drive &= 0x03;
kbhagat6 0:1aac12a5f1e0 1111 drive = drive << 6;
kbhagat6 0:1aac12a5f1e0 1112 //val &= 0b00111111;
kbhagat6 0:1aac12a5f1e0 1113 val &= 0x3F;
kbhagat6 0:1aac12a5f1e0 1114 val |= drive;
kbhagat6 0:1aac12a5f1e0 1115
kbhagat6 0:1aac12a5f1e0 1116 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1117 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1118 return false;
kbhagat6 0:1aac12a5f1e0 1119 }
kbhagat6 0:1aac12a5f1e0 1120
kbhagat6 0:1aac12a5f1e0 1121 return true;
kbhagat6 0:1aac12a5f1e0 1122 }
kbhagat6 0:1aac12a5f1e0 1123
kbhagat6 0:1aac12a5f1e0 1124 /**
kbhagat6 0:1aac12a5f1e0 1125 * @brief Returns receiver gain for proximity detection
kbhagat6 0:1aac12a5f1e0 1126 *
kbhagat6 0:1aac12a5f1e0 1127 * Value Gain
kbhagat6 0:1aac12a5f1e0 1128 * 0 1x
kbhagat6 0:1aac12a5f1e0 1129 * 1 2x
kbhagat6 0:1aac12a5f1e0 1130 * 2 4x
kbhagat6 0:1aac12a5f1e0 1131 * 3 8x
kbhagat6 0:1aac12a5f1e0 1132 *
kbhagat6 0:1aac12a5f1e0 1133 * @return the value of the proximity gain. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1134 */
kbhagat6 0:1aac12a5f1e0 1135 uint8_t glibr::getProximityGain()
kbhagat6 0:1aac12a5f1e0 1136 {
kbhagat6 0:1aac12a5f1e0 1137 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1138
kbhagat6 0:1aac12a5f1e0 1139 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1140 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1141 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1142 return ERROR;
kbhagat6 0:1aac12a5f1e0 1143 }
kbhagat6 0:1aac12a5f1e0 1144
kbhagat6 0:1aac12a5f1e0 1145 /* Shift and mask out PDRIVE bits */
kbhagat6 0:1aac12a5f1e0 1146 val = (val >> 2) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1147
kbhagat6 0:1aac12a5f1e0 1148 return val;
kbhagat6 0:1aac12a5f1e0 1149 }
kbhagat6 0:1aac12a5f1e0 1150
kbhagat6 0:1aac12a5f1e0 1151 /**
kbhagat6 0:1aac12a5f1e0 1152 * @brief Sets the receiver gain for proximity detection
kbhagat6 0:1aac12a5f1e0 1153 *
kbhagat6 0:1aac12a5f1e0 1154 * Value Gain
kbhagat6 0:1aac12a5f1e0 1155 * 0 1x
kbhagat6 0:1aac12a5f1e0 1156 * 1 2x
kbhagat6 0:1aac12a5f1e0 1157 * 2 4x
kbhagat6 0:1aac12a5f1e0 1158 * 3 8x
kbhagat6 0:1aac12a5f1e0 1159 *
kbhagat6 0:1aac12a5f1e0 1160 * @param[in] drive the value (0-3) for the gain
kbhagat6 0:1aac12a5f1e0 1161 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1162 */
kbhagat6 0:1aac12a5f1e0 1163 bool glibr::setProximityGain(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1164 {
kbhagat6 0:1aac12a5f1e0 1165 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1166
kbhagat6 0:1aac12a5f1e0 1167 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1168
kbhagat6 0:1aac12a5f1e0 1169 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1170
kbhagat6 0:1aac12a5f1e0 1171 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1172 return false;
kbhagat6 0:1aac12a5f1e0 1173 }
kbhagat6 0:1aac12a5f1e0 1174 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1175 //drive &= 0b00000011;
kbhagat6 0:1aac12a5f1e0 1176 drive &=0x03;
kbhagat6 0:1aac12a5f1e0 1177 drive = drive << 2;
kbhagat6 0:1aac12a5f1e0 1178 //val &= 0b11110011
kbhagat6 0:1aac12a5f1e0 1179 val &= 0xF3;
kbhagat6 0:1aac12a5f1e0 1180 val |= drive;
kbhagat6 0:1aac12a5f1e0 1181
kbhagat6 0:1aac12a5f1e0 1182 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1183 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1184 return false;
kbhagat6 0:1aac12a5f1e0 1185 }
kbhagat6 0:1aac12a5f1e0 1186 return true;
kbhagat6 0:1aac12a5f1e0 1187 }
kbhagat6 0:1aac12a5f1e0 1188
kbhagat6 0:1aac12a5f1e0 1189 /**
kbhagat6 0:1aac12a5f1e0 1190 * @brief Returns receiver gain for the ambient light sensor (ALS)
kbhagat6 0:1aac12a5f1e0 1191 *
kbhagat6 0:1aac12a5f1e0 1192 * Value Gain
kbhagat6 0:1aac12a5f1e0 1193 * 0 1x
kbhagat6 0:1aac12a5f1e0 1194 * 1 4x
kbhagat6 0:1aac12a5f1e0 1195 * 2 16x
kbhagat6 0:1aac12a5f1e0 1196 * 3 64x
kbhagat6 0:1aac12a5f1e0 1197 *
kbhagat6 0:1aac12a5f1e0 1198 * @return the value of the ALS gain. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1199 */
kbhagat6 0:1aac12a5f1e0 1200 uint8_t glibr::getAmbientLightGain()
kbhagat6 0:1aac12a5f1e0 1201 {
kbhagat6 0:1aac12a5f1e0 1202 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1203
kbhagat6 0:1aac12a5f1e0 1204 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1205 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1206 if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1207 return ERROR;
kbhagat6 0:1aac12a5f1e0 1208 }
kbhagat6 0:1aac12a5f1e0 1209
kbhagat6 0:1aac12a5f1e0 1210 /* Shift and mask out ADRIVE bits */
kbhagat6 0:1aac12a5f1e0 1211 val &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1212
kbhagat6 0:1aac12a5f1e0 1213 return val;
kbhagat6 0:1aac12a5f1e0 1214 }
kbhagat6 0:1aac12a5f1e0 1215
kbhagat6 0:1aac12a5f1e0 1216 /**
kbhagat6 0:1aac12a5f1e0 1217 * @brief Sets the receiver gain for the ambient light sensor (ALS)
kbhagat6 0:1aac12a5f1e0 1218 *
kbhagat6 0:1aac12a5f1e0 1219 * Value Gain
kbhagat6 0:1aac12a5f1e0 1220 * 0 1x
kbhagat6 0:1aac12a5f1e0 1221 * 1 4x
kbhagat6 0:1aac12a5f1e0 1222 * 2 16x
kbhagat6 0:1aac12a5f1e0 1223 * 3 64x
kbhagat6 0:1aac12a5f1e0 1224 *
kbhagat6 0:1aac12a5f1e0 1225 * @param[in] drive the value (0-3) for the gain
kbhagat6 0:1aac12a5f1e0 1226 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1227 */
kbhagat6 0:1aac12a5f1e0 1228 bool glibr::setAmbientLightGain(uint8_t drive){
kbhagat6 0:1aac12a5f1e0 1229
kbhagat6 0:1aac12a5f1e0 1230 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1231
kbhagat6 0:1aac12a5f1e0 1232 /* Read value from CONTROL register */
kbhagat6 0:1aac12a5f1e0 1233
kbhagat6 0:1aac12a5f1e0 1234 val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
kbhagat6 0:1aac12a5f1e0 1235
kbhagat6 0:1aac12a5f1e0 1236 if(val==ERROR){
kbhagat6 0:1aac12a5f1e0 1237 return false;
kbhagat6 0:1aac12a5f1e0 1238 }
kbhagat6 0:1aac12a5f1e0 1239 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1240 //drive &= 0b00000011;
kbhagat6 0:1aac12a5f1e0 1241 drive &=0x03;
kbhagat6 0:1aac12a5f1e0 1242 drive = drive << 2;
kbhagat6 0:1aac12a5f1e0 1243 //val &=0b11111100
kbhagat6 0:1aac12a5f1e0 1244 val &= 0xF3;
kbhagat6 0:1aac12a5f1e0 1245 val |= drive;
kbhagat6 0:1aac12a5f1e0 1246
kbhagat6 0:1aac12a5f1e0 1247 /* Write register value back into CONTROL register */
kbhagat6 0:1aac12a5f1e0 1248 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
kbhagat6 0:1aac12a5f1e0 1249 return false;
kbhagat6 0:1aac12a5f1e0 1250 }
kbhagat6 0:1aac12a5f1e0 1251 return true;
kbhagat6 0:1aac12a5f1e0 1252 }
kbhagat6 0:1aac12a5f1e0 1253
kbhagat6 0:1aac12a5f1e0 1254 /**
kbhagat6 0:1aac12a5f1e0 1255 * @brief Get the current LED boost value
kbhagat6 0:1aac12a5f1e0 1256 *
kbhagat6 0:1aac12a5f1e0 1257 * Value Boost Current
kbhagat6 0:1aac12a5f1e0 1258 * 0 100%
kbhagat6 0:1aac12a5f1e0 1259 * 1 150%
kbhagat6 0:1aac12a5f1e0 1260 * 2 200%
kbhagat6 0:1aac12a5f1e0 1261 * 3 300%
kbhagat6 0:1aac12a5f1e0 1262 *
kbhagat6 0:1aac12a5f1e0 1263 * @return The LED boost value. 0xFF on failure.
kbhagat6 0:1aac12a5f1e0 1264 */
kbhagat6 0:1aac12a5f1e0 1265 uint8_t glibr::getLEDBoost() {
kbhagat6 0:1aac12a5f1e0 1266 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1267
kbhagat6 0:1aac12a5f1e0 1268 /* Read value from CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1269 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 0:1aac12a5f1e0 1270 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1271 return ERROR;
kbhagat6 0:1aac12a5f1e0 1272 }
kbhagat6 0:1aac12a5f1e0 1273
kbhagat6 0:1aac12a5f1e0 1274 /* Shift and mask out LED_BOOST bits */
kbhagat6 0:1aac12a5f1e0 1275 val = (val >> 4) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1276
kbhagat6 0:1aac12a5f1e0 1277 return val;
kbhagat6 0:1aac12a5f1e0 1278 }
kbhagat6 0:1aac12a5f1e0 1279
kbhagat6 0:1aac12a5f1e0 1280 /**
kbhagat6 0:1aac12a5f1e0 1281 * @brief Sets the LED current boost value
kbhagat6 0:1aac12a5f1e0 1282 *
kbhagat6 0:1aac12a5f1e0 1283 * Value Boost Current
kbhagat6 0:1aac12a5f1e0 1284 * 0 100%
kbhagat6 0:1aac12a5f1e0 1285 * 1 150%
kbhagat6 0:1aac12a5f1e0 1286 * 2 200%
kbhagat6 0:1aac12a5f1e0 1287 * 3 300%
kbhagat6 0:1aac12a5f1e0 1288 *
kbhagat6 0:1aac12a5f1e0 1289 * @param[in] drive the value (0-3) for current boost (100-300%)
kbhagat6 0:1aac12a5f1e0 1290 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1291 */
kbhagat6 0:1aac12a5f1e0 1292 bool glibr::setLEDBoost(uint8_t boost)
kbhagat6 0:1aac12a5f1e0 1293 {
kbhagat6 0:1aac12a5f1e0 1294 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1295
kbhagat6 0:1aac12a5f1e0 1296 /* Read value from CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1297 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
kbhagat6 0:1aac12a5f1e0 1298 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1299 return false;
kbhagat6 0:1aac12a5f1e0 1300 }
kbhagat6 0:1aac12a5f1e0 1301
kbhagat6 0:1aac12a5f1e0 1302 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1303 boost &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1304 boost = boost << 4;
kbhagat6 0:1aac12a5f1e0 1305 val &= 0xCF;//0b11001111;
kbhagat6 0:1aac12a5f1e0 1306 val |= boost;
kbhagat6 0:1aac12a5f1e0 1307
kbhagat6 0:1aac12a5f1e0 1308 /* Write register value back into CONFIG2 register */
kbhagat6 0:1aac12a5f1e0 1309
kbhagat6 0:1aac12a5f1e0 1310 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, val)){//!wireWriteDataByte(APDS9960_CONFIG2, val) ) {
kbhagat6 0:1aac12a5f1e0 1311 return false;
kbhagat6 0:1aac12a5f1e0 1312 }
kbhagat6 0:1aac12a5f1e0 1313
kbhagat6 0:1aac12a5f1e0 1314 return true;
kbhagat6 0:1aac12a5f1e0 1315 }
kbhagat6 0:1aac12a5f1e0 1316
kbhagat6 0:1aac12a5f1e0 1317 /**
kbhagat6 0:1aac12a5f1e0 1318 * @brief Gets proximity gain compensation enable
kbhagat6 0:1aac12a5f1e0 1319 *
kbhagat6 0:1aac12a5f1e0 1320 * @return 1 if compensation is enabled. 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1321 */
kbhagat6 0:1aac12a5f1e0 1322 uint8_t glibr::getProxGainCompEnable()
kbhagat6 0:1aac12a5f1e0 1323 {
kbhagat6 0:1aac12a5f1e0 1324 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1325
kbhagat6 0:1aac12a5f1e0 1326 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1327 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1328 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1329 return ERROR;
kbhagat6 0:1aac12a5f1e0 1330 }
kbhagat6 0:1aac12a5f1e0 1331
kbhagat6 0:1aac12a5f1e0 1332 /* Shift and mask out PCMP bits */
kbhagat6 0:1aac12a5f1e0 1333 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1334
kbhagat6 0:1aac12a5f1e0 1335 return val;
kbhagat6 0:1aac12a5f1e0 1336 }
kbhagat6 0:1aac12a5f1e0 1337
kbhagat6 0:1aac12a5f1e0 1338 /**
kbhagat6 0:1aac12a5f1e0 1339 * @brief Sets the proximity gain compensation enable
kbhagat6 0:1aac12a5f1e0 1340 *
kbhagat6 0:1aac12a5f1e0 1341 * @param[in] enable 1 to enable compensation. 0 to disable compensation.
kbhagat6 0:1aac12a5f1e0 1342 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1343 */
kbhagat6 0:1aac12a5f1e0 1344 bool glibr::setProxGainCompEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1345 {
kbhagat6 0:1aac12a5f1e0 1346 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1347
kbhagat6 0:1aac12a5f1e0 1348 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1349 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1350 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1351 return false;
kbhagat6 0:1aac12a5f1e0 1352 }
kbhagat6 0:1aac12a5f1e0 1353
kbhagat6 0:1aac12a5f1e0 1354 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1355 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1356 enable = enable << 5;
kbhagat6 0:1aac12a5f1e0 1357 val &= 0xCF;//0b11011111;
kbhagat6 0:1aac12a5f1e0 1358 val |= enable;
kbhagat6 0:1aac12a5f1e0 1359
kbhagat6 0:1aac12a5f1e0 1360 /* Write register value back into CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1361
kbhagat6 0:1aac12a5f1e0 1362 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, val)){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1363 return false;
kbhagat6 0:1aac12a5f1e0 1364 }
kbhagat6 0:1aac12a5f1e0 1365
kbhagat6 0:1aac12a5f1e0 1366 return true;
kbhagat6 0:1aac12a5f1e0 1367 }
kbhagat6 0:1aac12a5f1e0 1368
kbhagat6 0:1aac12a5f1e0 1369 /**
kbhagat6 0:1aac12a5f1e0 1370 * @brief Gets the current mask for enabled/disabled proximity photodiodes
kbhagat6 0:1aac12a5f1e0 1371 *
kbhagat6 0:1aac12a5f1e0 1372 * 1 = disabled, 0 = enabled
kbhagat6 0:1aac12a5f1e0 1373 * Bit Photodiode
kbhagat6 0:1aac12a5f1e0 1374 * 3 UP
kbhagat6 0:1aac12a5f1e0 1375 * 2 DOWN
kbhagat6 0:1aac12a5f1e0 1376 * 1 LEFT
kbhagat6 0:1aac12a5f1e0 1377 * 0 RIGHT
kbhagat6 0:1aac12a5f1e0 1378 *
kbhagat6 0:1aac12a5f1e0 1379 * @return Current proximity mask for photodiodes. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1380 */
kbhagat6 0:1aac12a5f1e0 1381 uint8_t glibr::getProxPhotoMask()
kbhagat6 0:1aac12a5f1e0 1382 {
kbhagat6 0:1aac12a5f1e0 1383 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1384
kbhagat6 0:1aac12a5f1e0 1385 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1386 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1387 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1388 return ERROR;
kbhagat6 0:1aac12a5f1e0 1389 }
kbhagat6 0:1aac12a5f1e0 1390
kbhagat6 0:1aac12a5f1e0 1391 /* Mask out photodiode enable mask bits */
kbhagat6 0:1aac12a5f1e0 1392 val &= 0x0F;//0b00001111;
kbhagat6 0:1aac12a5f1e0 1393
kbhagat6 0:1aac12a5f1e0 1394 return val;
kbhagat6 0:1aac12a5f1e0 1395 }
kbhagat6 0:1aac12a5f1e0 1396
kbhagat6 0:1aac12a5f1e0 1397 /**
kbhagat6 0:1aac12a5f1e0 1398 * @brief Sets the mask for enabling/disabling proximity photodiodes
kbhagat6 0:1aac12a5f1e0 1399 *
kbhagat6 0:1aac12a5f1e0 1400 * 1 = disabled, 0 = enabled
kbhagat6 0:1aac12a5f1e0 1401 * Bit Photodiode
kbhagat6 0:1aac12a5f1e0 1402 * 3 UP
kbhagat6 0:1aac12a5f1e0 1403 * 2 DOWN
kbhagat6 0:1aac12a5f1e0 1404 * 1 LEFT
kbhagat6 0:1aac12a5f1e0 1405 * 0 RIGHT
kbhagat6 0:1aac12a5f1e0 1406 *
kbhagat6 0:1aac12a5f1e0 1407 * @param[in] mask 4-bit mask value
kbhagat6 0:1aac12a5f1e0 1408 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1409 */
kbhagat6 0:1aac12a5f1e0 1410 bool glibr::setProxPhotoMask(uint8_t mask)
kbhagat6 0:1aac12a5f1e0 1411 {
kbhagat6 0:1aac12a5f1e0 1412 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1413
kbhagat6 0:1aac12a5f1e0 1414 /* Read value from CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1415 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
kbhagat6 0:1aac12a5f1e0 1416 if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1417 return false;
kbhagat6 0:1aac12a5f1e0 1418 }
kbhagat6 0:1aac12a5f1e0 1419
kbhagat6 0:1aac12a5f1e0 1420 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1421 mask &= 0x0F;//0b00001111;
kbhagat6 0:1aac12a5f1e0 1422 val &= 0xF0;//0b11110000;
kbhagat6 0:1aac12a5f1e0 1423 val |= mask;
kbhagat6 0:1aac12a5f1e0 1424
kbhagat6 0:1aac12a5f1e0 1425 /* Write register value back into CONFIG3 register */
kbhagat6 0:1aac12a5f1e0 1426 I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val);
kbhagat6 0:1aac12a5f1e0 1427 if( val == ERROR){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
kbhagat6 0:1aac12a5f1e0 1428 return false;
kbhagat6 0:1aac12a5f1e0 1429 }
kbhagat6 0:1aac12a5f1e0 1430
kbhagat6 0:1aac12a5f1e0 1431 return true;
kbhagat6 0:1aac12a5f1e0 1432 }
kbhagat6 0:1aac12a5f1e0 1433
kbhagat6 0:1aac12a5f1e0 1434 /**
kbhagat6 0:1aac12a5f1e0 1435 * @brief Gets the entry proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1436 *
kbhagat6 0:1aac12a5f1e0 1437 * @return Current entry proximity threshold.
kbhagat6 0:1aac12a5f1e0 1438 */
kbhagat6 0:1aac12a5f1e0 1439 uint8_t glibr::getGestureEnterThresh()
kbhagat6 0:1aac12a5f1e0 1440 {
kbhagat6 0:1aac12a5f1e0 1441 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1442
kbhagat6 0:1aac12a5f1e0 1443 /* Read value from GPENTH register */
kbhagat6 0:1aac12a5f1e0 1444 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GPENTH);
kbhagat6 0:1aac12a5f1e0 1445 if( val == ERROR){//!wireReadDataByte(APDS9960_GPENTH, val) ) {
kbhagat6 0:1aac12a5f1e0 1446 val = 0;
kbhagat6 0:1aac12a5f1e0 1447 }
kbhagat6 0:1aac12a5f1e0 1448
kbhagat6 0:1aac12a5f1e0 1449 return val;
kbhagat6 0:1aac12a5f1e0 1450 }
kbhagat6 0:1aac12a5f1e0 1451
kbhagat6 0:1aac12a5f1e0 1452 /**
kbhagat6 0:1aac12a5f1e0 1453 * @brief Sets the entry proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1454 *
kbhagat6 0:1aac12a5f1e0 1455 * @param[in] threshold proximity value needed to start gesture mode
kbhagat6 0:1aac12a5f1e0 1456 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1457 */
kbhagat6 0:1aac12a5f1e0 1458 bool glibr::setGestureEnterThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1459 {
kbhagat6 0:1aac12a5f1e0 1460
kbhagat6 0:1aac12a5f1e0 1461 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold)){;//!wireWriteDataByte(APDS9960_GPENTH, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1462 return false;
kbhagat6 0:1aac12a5f1e0 1463 }
kbhagat6 0:1aac12a5f1e0 1464
kbhagat6 0:1aac12a5f1e0 1465 return true;
kbhagat6 0:1aac12a5f1e0 1466 }
kbhagat6 0:1aac12a5f1e0 1467
kbhagat6 0:1aac12a5f1e0 1468 /**
kbhagat6 0:1aac12a5f1e0 1469 * @brief Gets the exit proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1470 *
kbhagat6 0:1aac12a5f1e0 1471 * @return Current exit proximity threshold.
kbhagat6 0:1aac12a5f1e0 1472 */
kbhagat6 0:1aac12a5f1e0 1473 uint8_t glibr::getGestureExitThresh()
kbhagat6 0:1aac12a5f1e0 1474 {
kbhagat6 0:1aac12a5f1e0 1475 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1476
kbhagat6 0:1aac12a5f1e0 1477 /* Read value from GEXTH register */
kbhagat6 0:1aac12a5f1e0 1478 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GEXTH);
kbhagat6 0:1aac12a5f1e0 1479 if( val == ERROR){//!wireReadDataByte(APDS9960_GEXTH, val) ) {
kbhagat6 0:1aac12a5f1e0 1480 val = 0;
kbhagat6 0:1aac12a5f1e0 1481 }
kbhagat6 0:1aac12a5f1e0 1482
kbhagat6 0:1aac12a5f1e0 1483 return val;
kbhagat6 0:1aac12a5f1e0 1484 }
kbhagat6 0:1aac12a5f1e0 1485
kbhagat6 0:1aac12a5f1e0 1486 /**
kbhagat6 0:1aac12a5f1e0 1487 * @brief Sets the exit proximity threshold for gesture sensing
kbhagat6 0:1aac12a5f1e0 1488 *
kbhagat6 0:1aac12a5f1e0 1489 * @param[in] threshold proximity value needed to end gesture mode
kbhagat6 0:1aac12a5f1e0 1490 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1491 */
kbhagat6 0:1aac12a5f1e0 1492 bool glibr::setGestureExitThresh(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1493 {
kbhagat6 0:1aac12a5f1e0 1494 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold)){//!wireWriteDataByte(APDS9960_GEXTH, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1495 return false;
kbhagat6 0:1aac12a5f1e0 1496 }
kbhagat6 0:1aac12a5f1e0 1497
kbhagat6 0:1aac12a5f1e0 1498 return true;
kbhagat6 0:1aac12a5f1e0 1499 }
kbhagat6 0:1aac12a5f1e0 1500
kbhagat6 0:1aac12a5f1e0 1501 /**
kbhagat6 0:1aac12a5f1e0 1502 * @brief Gets the gain of the photodiode during gesture mode
kbhagat6 0:1aac12a5f1e0 1503 *
kbhagat6 0:1aac12a5f1e0 1504 * Value Gain
kbhagat6 0:1aac12a5f1e0 1505 * 0 1x
kbhagat6 0:1aac12a5f1e0 1506 * 1 2x
kbhagat6 0:1aac12a5f1e0 1507 * 2 4x
kbhagat6 0:1aac12a5f1e0 1508 * 3 8x
kbhagat6 0:1aac12a5f1e0 1509 *
kbhagat6 0:1aac12a5f1e0 1510 * @return the current photodiode gain. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1511 */
kbhagat6 0:1aac12a5f1e0 1512 uint8_t glibr::getGestureGain()
kbhagat6 0:1aac12a5f1e0 1513 {
kbhagat6 0:1aac12a5f1e0 1514 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1515
kbhagat6 0:1aac12a5f1e0 1516 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1517 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1518 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1519 return ERROR;
kbhagat6 0:1aac12a5f1e0 1520 }
kbhagat6 0:1aac12a5f1e0 1521
kbhagat6 0:1aac12a5f1e0 1522 /* Shift and mask out GGAIN bits */
kbhagat6 0:1aac12a5f1e0 1523 val = (val >> 5) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1524
kbhagat6 0:1aac12a5f1e0 1525 return val;
kbhagat6 0:1aac12a5f1e0 1526 }
kbhagat6 0:1aac12a5f1e0 1527
kbhagat6 0:1aac12a5f1e0 1528 /**
kbhagat6 0:1aac12a5f1e0 1529 * @brief Sets the gain of the photodiode during gesture mode
kbhagat6 0:1aac12a5f1e0 1530 *
kbhagat6 0:1aac12a5f1e0 1531 * Value Gain
kbhagat6 0:1aac12a5f1e0 1532 * 0 1x
kbhagat6 0:1aac12a5f1e0 1533 * 1 2x
kbhagat6 0:1aac12a5f1e0 1534 * 2 4x
kbhagat6 0:1aac12a5f1e0 1535 * 3 8x
kbhagat6 0:1aac12a5f1e0 1536 *
kbhagat6 0:1aac12a5f1e0 1537 * @param[in] gain the value for the photodiode gain
kbhagat6 0:1aac12a5f1e0 1538 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1539 */
kbhagat6 0:1aac12a5f1e0 1540 bool glibr::setGestureGain(uint8_t gain)
kbhagat6 0:1aac12a5f1e0 1541 {
kbhagat6 0:1aac12a5f1e0 1542 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1543
kbhagat6 0:1aac12a5f1e0 1544 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1545 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1546 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1547 return false;
kbhagat6 0:1aac12a5f1e0 1548 }
kbhagat6 0:1aac12a5f1e0 1549
kbhagat6 0:1aac12a5f1e0 1550 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1551 gain &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1552 gain = gain << 5;
kbhagat6 0:1aac12a5f1e0 1553 val &= 0x9F;//0b10011111;
kbhagat6 0:1aac12a5f1e0 1554 val |= gain;
kbhagat6 0:1aac12a5f1e0 1555
kbhagat6 0:1aac12a5f1e0 1556 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1557 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1558 return false;
kbhagat6 0:1aac12a5f1e0 1559 }
kbhagat6 0:1aac12a5f1e0 1560
kbhagat6 0:1aac12a5f1e0 1561 return true;
kbhagat6 0:1aac12a5f1e0 1562 }
kbhagat6 0:1aac12a5f1e0 1563
kbhagat6 0:1aac12a5f1e0 1564 /**
kbhagat6 0:1aac12a5f1e0 1565 * @brief Gets the drive current of the LED during gesture mode
kbhagat6 0:1aac12a5f1e0 1566 *
kbhagat6 0:1aac12a5f1e0 1567 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1568 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1569 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1570 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1571 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1572 *
kbhagat6 0:1aac12a5f1e0 1573 * @return the LED drive current value. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1574 */
kbhagat6 0:1aac12a5f1e0 1575 uint8_t glibr::getGestureLEDDrive()
kbhagat6 0:1aac12a5f1e0 1576 {
kbhagat6 0:1aac12a5f1e0 1577 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1578
kbhagat6 0:1aac12a5f1e0 1579 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1580 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1581 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1582 return ERROR;
kbhagat6 0:1aac12a5f1e0 1583 }
kbhagat6 0:1aac12a5f1e0 1584
kbhagat6 0:1aac12a5f1e0 1585 /* Shift and mask out GLDRIVE bits */
kbhagat6 0:1aac12a5f1e0 1586 val = (val >> 3) & 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1587
kbhagat6 0:1aac12a5f1e0 1588 return val;
kbhagat6 0:1aac12a5f1e0 1589 }
kbhagat6 0:1aac12a5f1e0 1590
kbhagat6 0:1aac12a5f1e0 1591 /**
kbhagat6 0:1aac12a5f1e0 1592 * @brief Sets the LED drive current during gesture mode
kbhagat6 0:1aac12a5f1e0 1593 *
kbhagat6 0:1aac12a5f1e0 1594 * Value LED Current
kbhagat6 0:1aac12a5f1e0 1595 * 0 100 mA
kbhagat6 0:1aac12a5f1e0 1596 * 1 50 mA
kbhagat6 0:1aac12a5f1e0 1597 * 2 25 mA
kbhagat6 0:1aac12a5f1e0 1598 * 3 12.5 mA
kbhagat6 0:1aac12a5f1e0 1599 *
kbhagat6 0:1aac12a5f1e0 1600 * @param[in] drive the value for the LED drive current
kbhagat6 0:1aac12a5f1e0 1601 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1602 */
kbhagat6 0:1aac12a5f1e0 1603 bool glibr::setGestureLEDDrive(uint8_t drive)
kbhagat6 0:1aac12a5f1e0 1604 {
kbhagat6 0:1aac12a5f1e0 1605 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1606
kbhagat6 0:1aac12a5f1e0 1607 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1608 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1609 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1610 return false;
kbhagat6 0:1aac12a5f1e0 1611 }
kbhagat6 0:1aac12a5f1e0 1612
kbhagat6 0:1aac12a5f1e0 1613 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1614 drive &= 0x03;//0b00000011;
kbhagat6 0:1aac12a5f1e0 1615 drive = drive << 3;
kbhagat6 0:1aac12a5f1e0 1616 val &= 0xE7;//0b11100111;
kbhagat6 0:1aac12a5f1e0 1617 val |= drive;
kbhagat6 0:1aac12a5f1e0 1618
kbhagat6 0:1aac12a5f1e0 1619 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1620 if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1621 return false;
kbhagat6 0:1aac12a5f1e0 1622 }
kbhagat6 0:1aac12a5f1e0 1623
kbhagat6 0:1aac12a5f1e0 1624 return true;
kbhagat6 0:1aac12a5f1e0 1625 }
kbhagat6 0:1aac12a5f1e0 1626
kbhagat6 0:1aac12a5f1e0 1627 /**
kbhagat6 0:1aac12a5f1e0 1628 * @brief Gets the time in low power mode between gesture detections
kbhagat6 0:1aac12a5f1e0 1629 *
kbhagat6 0:1aac12a5f1e0 1630 * Value Wait time
kbhagat6 0:1aac12a5f1e0 1631 * 0 0 ms
kbhagat6 0:1aac12a5f1e0 1632 * 1 2.8 ms
kbhagat6 0:1aac12a5f1e0 1633 * 2 5.6 ms
kbhagat6 0:1aac12a5f1e0 1634 * 3 8.4 ms
kbhagat6 0:1aac12a5f1e0 1635 * 4 14.0 ms
kbhagat6 0:1aac12a5f1e0 1636 * 5 22.4 ms
kbhagat6 0:1aac12a5f1e0 1637 * 6 30.8 ms
kbhagat6 0:1aac12a5f1e0 1638 * 7 39.2 ms
kbhagat6 0:1aac12a5f1e0 1639 *
kbhagat6 0:1aac12a5f1e0 1640 * @return the current wait time between gestures. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1641 */
kbhagat6 0:1aac12a5f1e0 1642 uint8_t glibr::getGestureWaitTime()
kbhagat6 0:1aac12a5f1e0 1643 {
kbhagat6 0:1aac12a5f1e0 1644 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1645
kbhagat6 0:1aac12a5f1e0 1646 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1647 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1648 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1649 return ERROR;
kbhagat6 0:1aac12a5f1e0 1650 }
kbhagat6 0:1aac12a5f1e0 1651
kbhagat6 0:1aac12a5f1e0 1652 /* Mask out GWTIME bits */
kbhagat6 0:1aac12a5f1e0 1653 val &= 0x07;//0b00000111;
kbhagat6 0:1aac12a5f1e0 1654
kbhagat6 0:1aac12a5f1e0 1655 return val;
kbhagat6 0:1aac12a5f1e0 1656 }
kbhagat6 0:1aac12a5f1e0 1657
kbhagat6 0:1aac12a5f1e0 1658 /*
kbhagat6 0:1aac12a5f1e0 1659 *
kbhagat6 0:1aac12a5f1e0 1660 *
kbhagat6 0:1aac12a5f1e0 1661 *
kbhagat6 2:ba051af6731a 1662 *LEFT Off HERE AT 3:47PM On 3/6/15
kbhagat6 0:1aac12a5f1e0 1663 *
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 * @brief Sets the time in low power mode between gesture detections
kbhagat6 0:1aac12a5f1e0 1672 *
kbhagat6 0:1aac12a5f1e0 1673 * Value Wait time
kbhagat6 0:1aac12a5f1e0 1674 * 0 0 ms
kbhagat6 0:1aac12a5f1e0 1675 * 1 2.8 ms
kbhagat6 0:1aac12a5f1e0 1676 * 2 5.6 ms
kbhagat6 0:1aac12a5f1e0 1677 * 3 8.4 ms
kbhagat6 0:1aac12a5f1e0 1678 * 4 14.0 ms
kbhagat6 0:1aac12a5f1e0 1679 * 5 22.4 ms
kbhagat6 0:1aac12a5f1e0 1680 * 6 30.8 ms
kbhagat6 0:1aac12a5f1e0 1681 * 7 39.2 ms
kbhagat6 0:1aac12a5f1e0 1682 *
kbhagat6 0:1aac12a5f1e0 1683 * @param[in] the value for the wait time
kbhagat6 0:1aac12a5f1e0 1684 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1685 */
kbhagat6 0:1aac12a5f1e0 1686 bool glibr::setGestureWaitTime(uint8_t time)
kbhagat6 0:1aac12a5f1e0 1687 {
kbhagat6 0:1aac12a5f1e0 1688 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1689
kbhagat6 0:1aac12a5f1e0 1690 /* Read value from GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1691 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
kbhagat6 0:1aac12a5f1e0 1692 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1693 return false;
kbhagat6 0:1aac12a5f1e0 1694 }
kbhagat6 0:1aac12a5f1e0 1695 /* if( !wireReadDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1696 return false;
kbhagat6 0:1aac12a5f1e0 1697 } */
kbhagat6 0:1aac12a5f1e0 1698
kbhagat6 0:1aac12a5f1e0 1699 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1700 time &= 0x07;//0b00000111;
kbhagat6 0:1aac12a5f1e0 1701 val &= 0xF8;//0b11111000;
kbhagat6 0:1aac12a5f1e0 1702 val |= time;
kbhagat6 0:1aac12a5f1e0 1703
kbhagat6 0:1aac12a5f1e0 1704 /* Write register value back into GCONF2 register */
kbhagat6 0:1aac12a5f1e0 1705 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF2,val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1706 return false;
kbhagat6 0:1aac12a5f1e0 1707 }
kbhagat6 0:1aac12a5f1e0 1708 /*if( !wireWriteDataByte(APDS9960_GCONF2, val) ) {
kbhagat6 0:1aac12a5f1e0 1709 return false;
kbhagat6 0:1aac12a5f1e0 1710 }*/
kbhagat6 0:1aac12a5f1e0 1711 return true;
kbhagat6 0:1aac12a5f1e0 1712 }
kbhagat6 0:1aac12a5f1e0 1713
kbhagat6 0:1aac12a5f1e0 1714 /**
kbhagat6 0:1aac12a5f1e0 1715 * @brief Gets the low threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1716 *
kbhagat6 0:1aac12a5f1e0 1717 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1718 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1719 */
kbhagat6 0:1aac12a5f1e0 1720 bool glibr::getLightIntLowThreshold(uint16_t &threshold)
kbhagat6 0:1aac12a5f1e0 1721 {
kbhagat6 0:1aac12a5f1e0 1722 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 1723 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1724
kbhagat6 0:1aac12a5f1e0 1725 /* Read value from ambient light low threshold, low byte register */
kbhagat6 0:1aac12a5f1e0 1726 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTL);
kbhagat6 0:1aac12a5f1e0 1727 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTL, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1728 return false;
kbhagat6 0:1aac12a5f1e0 1729 }
kbhagat6 0:1aac12a5f1e0 1730 threshold = val_byte;
kbhagat6 0:1aac12a5f1e0 1731
kbhagat6 0:1aac12a5f1e0 1732 /* Read value from ambient light low threshold, high byte register */
kbhagat6 0:1aac12a5f1e0 1733 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTH);
kbhagat6 0:1aac12a5f1e0 1734 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTH, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1735 return false;
kbhagat6 0:1aac12a5f1e0 1736 }
kbhagat6 0:1aac12a5f1e0 1737 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 1738
kbhagat6 0:1aac12a5f1e0 1739 return true;
kbhagat6 0:1aac12a5f1e0 1740 }
kbhagat6 0:1aac12a5f1e0 1741
kbhagat6 0:1aac12a5f1e0 1742 /**
kbhagat6 0:1aac12a5f1e0 1743 * @brief Sets the low threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1744 *
kbhagat6 0:1aac12a5f1e0 1745 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1746 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1747 */
kbhagat6 0:1aac12a5f1e0 1748 bool glibr::setLightIntLowThreshold(uint16_t threshold)
kbhagat6 0:1aac12a5f1e0 1749 {
kbhagat6 0:1aac12a5f1e0 1750 uint8_t val_low;
kbhagat6 0:1aac12a5f1e0 1751 uint8_t val_high;
kbhagat6 0:1aac12a5f1e0 1752
kbhagat6 0:1aac12a5f1e0 1753 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 0:1aac12a5f1e0 1754 val_low = threshold & 0x00FF;
kbhagat6 0:1aac12a5f1e0 1755 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 0:1aac12a5f1e0 1756
kbhagat6 0:1aac12a5f1e0 1757 /* Write low byte */
kbhagat6 0:1aac12a5f1e0 1758 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL,val_low)){//!wireWriteDataByte(APDS9960_AILTL, val_low) ) {
kbhagat6 0:1aac12a5f1e0 1759 return false;
kbhagat6 0:1aac12a5f1e0 1760 }
kbhagat6 0:1aac12a5f1e0 1761
kbhagat6 0:1aac12a5f1e0 1762 /* Write high byte */
kbhagat6 0:1aac12a5f1e0 1763 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH,val_high)){//!wireWriteDataByte(APDS9960_AILTH, val_high) ) {
kbhagat6 0:1aac12a5f1e0 1764 return false;
kbhagat6 0:1aac12a5f1e0 1765 }
kbhagat6 0:1aac12a5f1e0 1766
kbhagat6 0:1aac12a5f1e0 1767 return true;
kbhagat6 0:1aac12a5f1e0 1768 }
kbhagat6 0:1aac12a5f1e0 1769
kbhagat6 0:1aac12a5f1e0 1770 /**
kbhagat6 0:1aac12a5f1e0 1771 * @brief Gets the high threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1772 *
kbhagat6 0:1aac12a5f1e0 1773 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1774 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1775 */
kbhagat6 0:1aac12a5f1e0 1776 bool glibr::getLightIntHighThreshold(uint16_t &threshold)
kbhagat6 0:1aac12a5f1e0 1777 {
kbhagat6 0:1aac12a5f1e0 1778 uint8_t val_byte;
kbhagat6 0:1aac12a5f1e0 1779 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1780
kbhagat6 0:1aac12a5f1e0 1781 /* Read value from ambient light high threshold, low byte register */
kbhagat6 0:1aac12a5f1e0 1782 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTL);
kbhagat6 0:1aac12a5f1e0 1783 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTL, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1784 return false;
kbhagat6 0:1aac12a5f1e0 1785 }
kbhagat6 0:1aac12a5f1e0 1786 threshold = val_byte;
kbhagat6 0:1aac12a5f1e0 1787
kbhagat6 0:1aac12a5f1e0 1788 /* Read value from ambient light high threshold, high byte register */
kbhagat6 0:1aac12a5f1e0 1789 val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTH);
kbhagat6 0:1aac12a5f1e0 1790 if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTH, val_byte) ) {
kbhagat6 0:1aac12a5f1e0 1791 return false;
kbhagat6 0:1aac12a5f1e0 1792 }
kbhagat6 0:1aac12a5f1e0 1793 threshold = threshold + ((uint16_t)val_byte << 8);
kbhagat6 0:1aac12a5f1e0 1794
kbhagat6 0:1aac12a5f1e0 1795 return true;
kbhagat6 0:1aac12a5f1e0 1796 }
kbhagat6 0:1aac12a5f1e0 1797
kbhagat6 0:1aac12a5f1e0 1798 /**
kbhagat6 0:1aac12a5f1e0 1799 * @brief Sets the high threshold for ambient light interrupts
kbhagat6 0:1aac12a5f1e0 1800 *
kbhagat6 0:1aac12a5f1e0 1801 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1802 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1803 */
kbhagat6 0:1aac12a5f1e0 1804 bool glibr::setLightIntHighThreshold(uint16_t threshold)
kbhagat6 0:1aac12a5f1e0 1805 {
kbhagat6 0:1aac12a5f1e0 1806 uint8_t val_low;
kbhagat6 0:1aac12a5f1e0 1807 uint8_t val_high;
kbhagat6 0:1aac12a5f1e0 1808
kbhagat6 0:1aac12a5f1e0 1809 /* Break 16-bit threshold into 2 8-bit values */
kbhagat6 0:1aac12a5f1e0 1810 val_low = threshold & 0x00FF;
kbhagat6 0:1aac12a5f1e0 1811 val_high = (threshold & 0xFF00) >> 8;
kbhagat6 0:1aac12a5f1e0 1812
kbhagat6 0:1aac12a5f1e0 1813 /* Write low byte */
kbhagat6 0:1aac12a5f1e0 1814 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTL,val_low)){//!wireWriteDataByte(APDS9960_AIHTL, val_low) ) {
kbhagat6 0:1aac12a5f1e0 1815 return false;
kbhagat6 0:1aac12a5f1e0 1816 }
kbhagat6 0:1aac12a5f1e0 1817
kbhagat6 0:1aac12a5f1e0 1818 /* Write high byte */
kbhagat6 0:1aac12a5f1e0 1819 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTH,val_high)){//!wireWriteDataByte(APDS9960_AIHTH, val_high) ) {
kbhagat6 0:1aac12a5f1e0 1820 return false;
kbhagat6 0:1aac12a5f1e0 1821 }
kbhagat6 0:1aac12a5f1e0 1822
kbhagat6 0:1aac12a5f1e0 1823 return true;
kbhagat6 0:1aac12a5f1e0 1824 }
kbhagat6 0:1aac12a5f1e0 1825
kbhagat6 0:1aac12a5f1e0 1826 /**
kbhagat6 0:1aac12a5f1e0 1827 * @brief Gets the low threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1828 *
kbhagat6 0:1aac12a5f1e0 1829 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1830 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1831 */
kbhagat6 0:1aac12a5f1e0 1832 bool glibr::getProximityIntLowThreshold(uint8_t &threshold)
kbhagat6 0:1aac12a5f1e0 1833 {
kbhagat6 0:1aac12a5f1e0 1834 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1835
kbhagat6 0:1aac12a5f1e0 1836 /* Read value from proximity low threshold register */
kbhagat6 0:1aac12a5f1e0 1837 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PILT);
kbhagat6 0:1aac12a5f1e0 1838 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1839 return false;
kbhagat6 0:1aac12a5f1e0 1840 }
kbhagat6 0:1aac12a5f1e0 1841
kbhagat6 0:1aac12a5f1e0 1842 return true;
kbhagat6 0:1aac12a5f1e0 1843 }
kbhagat6 0:1aac12a5f1e0 1844
kbhagat6 0:1aac12a5f1e0 1845 /**
kbhagat6 0:1aac12a5f1e0 1846 * @brief Sets the low threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1847 *
kbhagat6 0:1aac12a5f1e0 1848 * @param[in] threshold low threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1849 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1850 */
kbhagat6 0:1aac12a5f1e0 1851 bool glibr::setProximityIntLowThreshold(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1852 {
kbhagat6 0:1aac12a5f1e0 1853
kbhagat6 0:1aac12a5f1e0 1854 /* Write threshold value to register */
kbhagat6 0:1aac12a5f1e0 1855 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT,threshold)){//!wireWriteDataByte(APDS9960_PILT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1856 return false;
kbhagat6 0:1aac12a5f1e0 1857 }
kbhagat6 0:1aac12a5f1e0 1858
kbhagat6 0:1aac12a5f1e0 1859 return true;
kbhagat6 0:1aac12a5f1e0 1860 }
kbhagat6 0:1aac12a5f1e0 1861
kbhagat6 0:1aac12a5f1e0 1862 /**
kbhagat6 0:1aac12a5f1e0 1863 * @brief Gets the high threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1864 *
kbhagat6 0:1aac12a5f1e0 1865 * @param[out] threshold current low threshold stored on the APDS-9960
kbhagat6 0:1aac12a5f1e0 1866 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1867 */
kbhagat6 0:1aac12a5f1e0 1868 bool glibr::getProximityIntHighThreshold(uint8_t &threshold)
kbhagat6 0:1aac12a5f1e0 1869 {
kbhagat6 0:1aac12a5f1e0 1870 threshold = 0;
kbhagat6 0:1aac12a5f1e0 1871
kbhagat6 0:1aac12a5f1e0 1872 /* Read value from proximity low threshold register */
kbhagat6 0:1aac12a5f1e0 1873 threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PIHT);
kbhagat6 0:1aac12a5f1e0 1874 if( threshold == ERROR){//!wireReadDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1875 return false;
kbhagat6 0:1aac12a5f1e0 1876 }
kbhagat6 0:1aac12a5f1e0 1877
kbhagat6 0:1aac12a5f1e0 1878 return true;
kbhagat6 0:1aac12a5f1e0 1879 }
kbhagat6 0:1aac12a5f1e0 1880
kbhagat6 0:1aac12a5f1e0 1881 /**
kbhagat6 0:1aac12a5f1e0 1882 * @brief Sets the high threshold for proximity interrupts
kbhagat6 0:1aac12a5f1e0 1883 *
kbhagat6 0:1aac12a5f1e0 1884 * @param[in] threshold high threshold value for interrupt to trigger
kbhagat6 0:1aac12a5f1e0 1885 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1886 */
kbhagat6 0:1aac12a5f1e0 1887 bool glibr::setProximityIntHighThreshold(uint8_t threshold)
kbhagat6 0:1aac12a5f1e0 1888 {
kbhagat6 0:1aac12a5f1e0 1889
kbhagat6 0:1aac12a5f1e0 1890 /* Write threshold value to register */
kbhagat6 0:1aac12a5f1e0 1891 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT,threshold)){//!wireWriteDataByte(APDS9960_PIHT, threshold) ) {
kbhagat6 0:1aac12a5f1e0 1892 return false;
kbhagat6 0:1aac12a5f1e0 1893 }
kbhagat6 0:1aac12a5f1e0 1894
kbhagat6 0:1aac12a5f1e0 1895 return true;
kbhagat6 0:1aac12a5f1e0 1896 }
kbhagat6 0:1aac12a5f1e0 1897
kbhagat6 0:1aac12a5f1e0 1898 /**
kbhagat6 0:1aac12a5f1e0 1899 * @brief Gets if ambient light interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 1900 *
kbhagat6 0:1aac12a5f1e0 1901 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1902 */
kbhagat6 0:1aac12a5f1e0 1903 uint8_t glibr::getAmbientLightIntEnable()
kbhagat6 0:1aac12a5f1e0 1904 {
kbhagat6 0:1aac12a5f1e0 1905 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1906
kbhagat6 0:1aac12a5f1e0 1907 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1908 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1909 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1910 return ERROR;
kbhagat6 0:1aac12a5f1e0 1911 }
kbhagat6 0:1aac12a5f1e0 1912
kbhagat6 0:1aac12a5f1e0 1913 /* Shift and mask out AIEN bit */
kbhagat6 0:1aac12a5f1e0 1914 val = (val >> 4) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1915
kbhagat6 0:1aac12a5f1e0 1916 return val;
kbhagat6 0:1aac12a5f1e0 1917 }
kbhagat6 0:1aac12a5f1e0 1918
kbhagat6 0:1aac12a5f1e0 1919 /**
kbhagat6 0:1aac12a5f1e0 1920 * @brief Turns ambient light interrupts on or off
kbhagat6 0:1aac12a5f1e0 1921 *
kbhagat6 0:1aac12a5f1e0 1922 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 1923 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1924 */
kbhagat6 0:1aac12a5f1e0 1925 bool glibr::setAmbientLightIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1926 {
kbhagat6 0:1aac12a5f1e0 1927 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1928
kbhagat6 0:1aac12a5f1e0 1929 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1930 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1931 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1932 return false;
kbhagat6 0:1aac12a5f1e0 1933 }
kbhagat6 0:1aac12a5f1e0 1934
kbhagat6 0:1aac12a5f1e0 1935 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1936 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1937 enable = enable << 4;
kbhagat6 0:1aac12a5f1e0 1938 val &= 0xEF;//0b11101111;
kbhagat6 0:1aac12a5f1e0 1939 val |= enable;
kbhagat6 0:1aac12a5f1e0 1940
kbhagat6 0:1aac12a5f1e0 1941 /* Write register value back into ENABLE register */
kbhagat6 0:1aac12a5f1e0 1942 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1943 return false;
kbhagat6 0:1aac12a5f1e0 1944 }
kbhagat6 0:1aac12a5f1e0 1945
kbhagat6 0:1aac12a5f1e0 1946 return true;
kbhagat6 0:1aac12a5f1e0 1947 }
kbhagat6 0:1aac12a5f1e0 1948
kbhagat6 0:1aac12a5f1e0 1949 /**
kbhagat6 0:1aac12a5f1e0 1950 * @brief Gets if proximity interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 1951 *
kbhagat6 0:1aac12a5f1e0 1952 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 1953 */
kbhagat6 0:1aac12a5f1e0 1954 uint8_t glibr::getProximityIntEnable()
kbhagat6 0:1aac12a5f1e0 1955 {
kbhagat6 0:1aac12a5f1e0 1956 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1957
kbhagat6 0:1aac12a5f1e0 1958 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1959 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1960 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1961 return ERROR;
kbhagat6 0:1aac12a5f1e0 1962 }
kbhagat6 0:1aac12a5f1e0 1963
kbhagat6 0:1aac12a5f1e0 1964 /* Shift and mask out PIEN bit */
kbhagat6 0:1aac12a5f1e0 1965 val = (val >> 5) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1966
kbhagat6 0:1aac12a5f1e0 1967 return val;
kbhagat6 0:1aac12a5f1e0 1968 }
kbhagat6 0:1aac12a5f1e0 1969
kbhagat6 0:1aac12a5f1e0 1970 /**
kbhagat6 0:1aac12a5f1e0 1971 * @brief Turns proximity interrupts on or off
kbhagat6 0:1aac12a5f1e0 1972 *
kbhagat6 0:1aac12a5f1e0 1973 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 1974 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 1975 */
kbhagat6 0:1aac12a5f1e0 1976 bool glibr::setProximityIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 1977 {
kbhagat6 0:1aac12a5f1e0 1978 uint8_t val;
kbhagat6 0:1aac12a5f1e0 1979
kbhagat6 0:1aac12a5f1e0 1980 /* Read value from ENABLE register */
kbhagat6 0:1aac12a5f1e0 1981 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
kbhagat6 0:1aac12a5f1e0 1982 if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1983 return false;
kbhagat6 0:1aac12a5f1e0 1984 }
kbhagat6 0:1aac12a5f1e0 1985
kbhagat6 0:1aac12a5f1e0 1986 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 1987 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 1988 enable = enable << 5;
kbhagat6 0:1aac12a5f1e0 1989 val &= 0xDF;//0b11011111;
kbhagat6 0:1aac12a5f1e0 1990 val |= enable;
kbhagat6 0:1aac12a5f1e0 1991
kbhagat6 0:1aac12a5f1e0 1992 /* Write register value back into ENABLE register */
kbhagat6 0:1aac12a5f1e0 1993 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
kbhagat6 0:1aac12a5f1e0 1994 return false;
kbhagat6 0:1aac12a5f1e0 1995 }
kbhagat6 0:1aac12a5f1e0 1996
kbhagat6 0:1aac12a5f1e0 1997 return true;
kbhagat6 0:1aac12a5f1e0 1998 }
kbhagat6 0:1aac12a5f1e0 1999
kbhagat6 0:1aac12a5f1e0 2000 /**
kbhagat6 0:1aac12a5f1e0 2001 * @brief Gets if gesture interrupts are enabled or not
kbhagat6 0:1aac12a5f1e0 2002 *
kbhagat6 0:1aac12a5f1e0 2003 * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 2004 */
kbhagat6 0:1aac12a5f1e0 2005 uint8_t glibr::getGestureIntEnable()
kbhagat6 0:1aac12a5f1e0 2006 {
kbhagat6 0:1aac12a5f1e0 2007 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2008
kbhagat6 0:1aac12a5f1e0 2009 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2010 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2011 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2012 return ERROR;
kbhagat6 0:1aac12a5f1e0 2013 }
kbhagat6 0:1aac12a5f1e0 2014
kbhagat6 0:1aac12a5f1e0 2015 /* Shift and mask out GIEN bit */
kbhagat6 0:1aac12a5f1e0 2016 val = (val >> 1) & 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2017
kbhagat6 0:1aac12a5f1e0 2018 return val;
kbhagat6 0:1aac12a5f1e0 2019 }
kbhagat6 0:1aac12a5f1e0 2020
kbhagat6 0:1aac12a5f1e0 2021 /**
kbhagat6 0:1aac12a5f1e0 2022 * @brief Turns gesture-related interrupts on or off
kbhagat6 0:1aac12a5f1e0 2023 *
kbhagat6 0:1aac12a5f1e0 2024 * @param[in] enable 1 to enable interrupts, 0 to turn them off
kbhagat6 0:1aac12a5f1e0 2025 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 2026 */
kbhagat6 0:1aac12a5f1e0 2027 bool glibr::setGestureIntEnable(uint8_t enable)
kbhagat6 0:1aac12a5f1e0 2028 {
kbhagat6 0:1aac12a5f1e0 2029 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2030
kbhagat6 0:1aac12a5f1e0 2031 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2032 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2033 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2034 return false;
kbhagat6 0:1aac12a5f1e0 2035 }
kbhagat6 0:1aac12a5f1e0 2036
kbhagat6 0:1aac12a5f1e0 2037 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 2038 enable &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2039 enable = enable << 1;
kbhagat6 0:1aac12a5f1e0 2040 val &= 0xFD;//0b11111101;
kbhagat6 0:1aac12a5f1e0 2041 val |= enable;
kbhagat6 0:1aac12a5f1e0 2042
kbhagat6 0:1aac12a5f1e0 2043 /* Write register value back into GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2044 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2045 return false;
kbhagat6 0:1aac12a5f1e0 2046 }
kbhagat6 0:1aac12a5f1e0 2047
kbhagat6 0:1aac12a5f1e0 2048 return true;
kbhagat6 0:1aac12a5f1e0 2049 }
kbhagat6 0:1aac12a5f1e0 2050
kbhagat6 0:1aac12a5f1e0 2051 /**
kbhagat6 0:1aac12a5f1e0 2052 * @brief Clears the ambient light interrupt
kbhagat6 0:1aac12a5f1e0 2053 *
kbhagat6 0:1aac12a5f1e0 2054 * @return True if operation completed successfully. False otherwise.
kbhagat6 0:1aac12a5f1e0 2055 */
kbhagat6 0:1aac12a5f1e0 2056 bool glibr::clearAmbientLightInt()
kbhagat6 0:1aac12a5f1e0 2057 {
kbhagat6 0:1aac12a5f1e0 2058 uint8_t throwaway;
kbhagat6 0:1aac12a5f1e0 2059 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AICLEAR);
kbhagat6 0:1aac12a5f1e0 2060 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_AICLEAR, throwaway) ) {
kbhagat6 0:1aac12a5f1e0 2061 return false;
kbhagat6 0:1aac12a5f1e0 2062 }
kbhagat6 0:1aac12a5f1e0 2063
kbhagat6 0:1aac12a5f1e0 2064 return true;
kbhagat6 0:1aac12a5f1e0 2065 }
kbhagat6 0:1aac12a5f1e0 2066
kbhagat6 0:1aac12a5f1e0 2067 /**
kbhagat6 0:1aac12a5f1e0 2068 * @brief Clears the proximity interrupt
kbhagat6 0:1aac12a5f1e0 2069 *
kbhagat6 0:1aac12a5f1e0 2070 * @return True if operation completed successfully. False otherwise.
kbhagat6 0:1aac12a5f1e0 2071 */
kbhagat6 0:1aac12a5f1e0 2072 bool glibr::clearProximityInt()
kbhagat6 0:1aac12a5f1e0 2073 {
kbhagat6 0:1aac12a5f1e0 2074 uint8_t throwaway;
kbhagat6 0:1aac12a5f1e0 2075 throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PICLEAR);
kbhagat6 0:1aac12a5f1e0 2076 if( throwaway == ERROR){//!wireReadDataByte(APDS9960_PICLEAR, throwaway) ) {
kbhagat6 0:1aac12a5f1e0 2077 return false;
kbhagat6 0:1aac12a5f1e0 2078 }
kbhagat6 0:1aac12a5f1e0 2079
kbhagat6 0:1aac12a5f1e0 2080 return true;
kbhagat6 0:1aac12a5f1e0 2081 }
kbhagat6 0:1aac12a5f1e0 2082
kbhagat6 0:1aac12a5f1e0 2083 /**
kbhagat6 0:1aac12a5f1e0 2084 * @brief Tells if the gesture state machine is currently running
kbhagat6 0:1aac12a5f1e0 2085 *
kbhagat6 0:1aac12a5f1e0 2086 * @return 1 if gesture state machine is running, 0 if not. 0xFF on error.
kbhagat6 0:1aac12a5f1e0 2087 */
kbhagat6 0:1aac12a5f1e0 2088 uint8_t glibr::getGestureMode()
kbhagat6 0:1aac12a5f1e0 2089 {
kbhagat6 0:1aac12a5f1e0 2090 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2091
kbhagat6 0:1aac12a5f1e0 2092 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2093 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2094 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2095 return ERROR;
kbhagat6 0:1aac12a5f1e0 2096 }
kbhagat6 0:1aac12a5f1e0 2097
kbhagat6 0:1aac12a5f1e0 2098 /* Mask out GMODE bit */
kbhagat6 0:1aac12a5f1e0 2099 val &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2100
kbhagat6 0:1aac12a5f1e0 2101 return val;
kbhagat6 0:1aac12a5f1e0 2102 }
kbhagat6 0:1aac12a5f1e0 2103
kbhagat6 0:1aac12a5f1e0 2104 /**
kbhagat6 0:1aac12a5f1e0 2105 * @brief Tells the state machine to either enter or exit gesture state machine
kbhagat6 0:1aac12a5f1e0 2106 *
kbhagat6 0:1aac12a5f1e0 2107 * @param[in] mode 1 to enter gesture state machine, 0 to exit.
kbhagat6 0:1aac12a5f1e0 2108 * @return True if operation successful. False otherwise.
kbhagat6 0:1aac12a5f1e0 2109 */
kbhagat6 0:1aac12a5f1e0 2110 bool glibr::setGestureMode(uint8_t mode)
kbhagat6 0:1aac12a5f1e0 2111 {
kbhagat6 0:1aac12a5f1e0 2112 uint8_t val;
kbhagat6 0:1aac12a5f1e0 2113
kbhagat6 0:1aac12a5f1e0 2114 /* Read value from GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2115 val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
kbhagat6 0:1aac12a5f1e0 2116 if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2117 return false;
kbhagat6 0:1aac12a5f1e0 2118 }
kbhagat6 0:1aac12a5f1e0 2119
kbhagat6 0:1aac12a5f1e0 2120 /* Set bits in register to given value */
kbhagat6 0:1aac12a5f1e0 2121 mode &= 0x01;//0b00000001;
kbhagat6 0:1aac12a5f1e0 2122 val &= 0xFE;//0b11111110;
kbhagat6 0:1aac12a5f1e0 2123 val |= mode;
kbhagat6 0:1aac12a5f1e0 2124
kbhagat6 0:1aac12a5f1e0 2125 /* Write register value back into GCONF4 register */
kbhagat6 0:1aac12a5f1e0 2126 if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
kbhagat6 0:1aac12a5f1e0 2127 return false;
kbhagat6 0:1aac12a5f1e0 2128 }
kbhagat6 0:1aac12a5f1e0 2129
kbhagat6 0:1aac12a5f1e0 2130 return true;
kbhagat6 0:1aac12a5f1e0 2131 }
kbhagat6 0:1aac12a5f1e0 2132
kbhagat6 0:1aac12a5f1e0 2133
kbhagat6 0:1aac12a5f1e0 2134
kbhagat6 0:1aac12a5f1e0 2135
kbhagat6 0:1aac12a5f1e0 2136
kbhagat6 0:1aac12a5f1e0 2137 int glibr::I2CwriteByte(char address, char subAddress, char data)
kbhagat6 0:1aac12a5f1e0 2138 {
kbhagat6 0:1aac12a5f1e0 2139 int ret;
kbhagat6 0:1aac12a5f1e0 2140 char cmd[2] = {subAddress, data};
kbhagat6 0:1aac12a5f1e0 2141 ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked.
kbhagat6 0:1aac12a5f1e0 2142 return ret;
kbhagat6 0:1aac12a5f1e0 2143 }
kbhagat6 0:1aac12a5f1e0 2144
kbhagat6 0:1aac12a5f1e0 2145
kbhagat6 0:1aac12a5f1e0 2146
kbhagat6 0:1aac12a5f1e0 2147 uint8_t glibr::I2CreadByte(char address, char subAddress)
kbhagat6 0:1aac12a5f1e0 2148 {
kbhagat6 0:1aac12a5f1e0 2149 char data; // store the register data
kbhagat6 0:1aac12a5f1e0 2150
kbhagat6 0:1aac12a5f1e0 2151 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:1aac12a5f1e0 2152 return ERROR; //7 bit //not acked
kbhagat6 0:1aac12a5f1e0 2153 }
kbhagat6 0:1aac12a5f1e0 2154 if(i2c.read(address<<1, &data, 1)){ /////CHANGED THIS NEED TO TEST.
kbhagat6 0:1aac12a5f1e0 2155 return ERROR;
kbhagat6 0:1aac12a5f1e0 2156 }
kbhagat6 0:1aac12a5f1e0 2157
kbhagat6 0:1aac12a5f1e0 2158
kbhagat6 0:1aac12a5f1e0 2159 //i2c.read(address<<1, &data, 1);
kbhagat6 0:1aac12a5f1e0 2160 return data;
kbhagat6 0:1aac12a5f1e0 2161
kbhagat6 0:1aac12a5f1e0 2162 }
kbhagat6 0:1aac12a5f1e0 2163
kbhagat6 0:1aac12a5f1e0 2164
kbhagat6 0:1aac12a5f1e0 2165 // * @brief Reads a block (array) of bytes from the I2C device and register
kbhagat6 0:1aac12a5f1e0 2166 // *
kbhagat6 0:1aac12a5f1e0 2167 // * @param[in] reg the register to read from
kbhagat6 0:1aac12a5f1e0 2168 // * @param[out] val pointer to the beginning of the data
kbhagat6 0:1aac12a5f1e0 2169 // * @param[in] len number of bytes to read
kbhagat6 0:1aac12a5f1e0 2170 // * @return Number of bytes read. -1 on read error.
kbhagat6 0:1aac12a5f1e0 2171 // */
kbhagat6 0:1aac12a5f1e0 2172 int glibr::I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len)
kbhagat6 0:1aac12a5f1e0 2173 {
kbhagat6 0:1aac12a5f1e0 2174 // unsigned char i = 0;
kbhagat6 0:1aac12a5f1e0 2175
kbhagat6 0:1aac12a5f1e0 2176 /* Indicate which register we want to read from */
kbhagat6 0:1aac12a5f1e0 2177
kbhagat6 0:1aac12a5f1e0 2178 if(i2c.write(address<<1, &subAddress, 1, true)){
kbhagat6 0:1aac12a5f1e0 2179 return -1; //7 bit //not acked
kbhagat6 0:1aac12a5f1e0 2180 }
kbhagat6 0:1aac12a5f1e0 2181
kbhagat6 0:1aac12a5f1e0 2182 /* Read block data */
kbhagat6 0:1aac12a5f1e0 2183
kbhagat6 0:1aac12a5f1e0 2184 if(i2c.read(address<<1, data, len)){
kbhagat6 0:1aac12a5f1e0 2185 return -1;
kbhagat6 0:1aac12a5f1e0 2186 }
kbhagat6 0:1aac12a5f1e0 2187
kbhagat6 0:1aac12a5f1e0 2188 return 1;
kbhagat6 0:1aac12a5f1e0 2189 //Wire.requestFrom(APDS9960_I2C_ADDR, len);
kbhagat6 0:1aac12a5f1e0 2190 /*while (Wire.available()) {
kbhagat6 0:1aac12a5f1e0 2191 if (i >= len) {
kbhagat6 0:1aac12a5f1e0 2192 return -1;
kbhagat6 0:1aac12a5f1e0 2193 }
kbhagat6 0:1aac12a5f1e0 2194 val[i] = Wire.read();
kbhagat6 0:1aac12a5f1e0 2195 i++;
kbhagat6 0:1aac12a5f1e0 2196 }*/
kbhagat6 0:1aac12a5f1e0 2197 }