Kenji Arai / APDS_9960
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers glibr.cpp Source File

glibr.cpp

00001 #include "glibr.h"
00002 #include "mbed.h"
00003 #include <iostream>
00004 
00005 
00006 
00007 
00008 glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){
00009     gesture_ud_delta_ = 0;
00010     gesture_lr_delta_ = 0;
00011     
00012     gesture_ud_count_ = 0;
00013     gesture_lr_count_ = 0;
00014     
00015     gesture_near_count_ = 0;
00016     gesture_far_count_ = 0;
00017     
00018     gesture_state_ = 0;
00019     gesture_motion_ = DIR_NONE;
00020 }    
00021    
00022 glibr::~glibr(){
00023        
00024 } 
00025 
00026  bool  glibr::ginit(){
00027     uint8_t id;
00028         
00029      id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID);
00030     
00031     if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) {
00032          //printf("%c\r\n", id);
00033          std::cout<<"fuck"<<std::endl;
00034          return false;
00035     }
00036     
00037     if(!setMode(ALL, Off)) {
00038         return false;
00039     }
00040     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){
00041         return false;
00042     }
00043     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){
00044         return false;
00045     }
00046     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){
00047         return false;
00048     }
00049     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){
00050         return false;
00051     }
00052     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){
00053         return false;
00054     }
00055     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){
00056         return false;
00057     }
00058     
00059     if( !setLEDDrive(DEFAULT_LDRIVE) ) {
00060         return false;
00061     }
00062     
00063     if( !setProximityGain(DEFAULT_PGAIN) ) {
00064         return false;
00065     }
00066     if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
00067         return false;
00068     }
00069     if( !setProxIntLowThresh(DEFAULT_PILT) ) {
00070         return false;
00071     }
00072     if( !setProxIntHighThresh(DEFAULT_PIHT) ) {
00073         return false;
00074     }
00075     if( !setLightIntLowThreshold(DEFAULT_AILT) ) {
00076         return false;
00077     }
00078     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, DEFAULT_CONFIG2) ) {
00079         return false;
00080     }
00081     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, DEFAULT_CONFIG3) ) {
00082         return false;
00083     }
00084     
00085     if( !setGestureEnterThresh(DEFAULT_GPENTH) ) {
00086         return false;
00087     }
00088     if( !setGestureExitThresh(DEFAULT_GEXTH) ) {
00089         return false;
00090     }
00091     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF1, DEFAULT_GCONF1) ) {
00092         return false;
00093     }
00094     if( !setGestureGain(DEFAULT_GGAIN) ) {
00095         return false;
00096     }
00097     if( !setGestureLEDDrive(DEFAULT_GLDRIVE) ) {
00098         return false;
00099     }
00100     if( !setGestureWaitTime(DEFAULT_GWTIME) ) {
00101         return false;
00102     }
00103     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_U, DEFAULT_GOFFSET) ) {
00104         return false;
00105     }
00106     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_D, DEFAULT_GOFFSET) ) {
00107         return false;
00108     }
00109     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_L, DEFAULT_GOFFSET) ) {
00110         return false;
00111     }
00112     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GOFFSET_R, DEFAULT_GOFFSET) ) {
00113         return false;
00114     }
00115     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GPULSE, DEFAULT_GPULSE) ) {
00116         return false;
00117     }
00118     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF3, DEFAULT_GCONF3) ) {
00119         return false;
00120     }
00121     if( !setGestureIntEnable(DEFAULT_GIEN) ) {
00122         return false;
00123     }
00124     
00125     return true;
00126     
00127 }
00128 
00129 //#if 0
00130 //    /* Gesture config register dump */
00131 //    uint8_t reg;
00132 //    uint8_t val;
00133 //  
00134 //    for(reg = 0x80; reg <= 0xAF; reg++) {
00135 //        if( (reg != 0x82) && \
00136 //            (reg != 0x8A) && \
00137 //            (reg != 0x91) && \
00138 //            (reg != 0xA8) && \
00139 //            (reg != 0xAC) && \
00140 //            (reg != 0xAD) )
00141 //        {
00142 //         val= I2CreadByte(APDS9960_I2C_ADDR, reg);
00143 //          if(val==ERROR){
00144 //              printf("ERROR");
00145 //          }
00146 //            /*
00147 //            print(reg, HEX);
00148 //            print(": 0x");
00149 //            println(val, HEX);*/
00150 //        }
00151 //    }
00152 //
00153 //    for(reg = 0xE4; reg <= 0xE7; reg++) {
00154 //        val= I2CreadByte(APDS9960_I2C_ADDR, reg);
00155 //     /*   Serial.print(reg, HEX);
00156 //        Serial.print(": 0x");
00157 //        Serial.println(val, HEX);*/
00158 //    }
00159 //#endif 
00160 
00161   //  return true;
00162  
00163 
00164 
00165 
00166 /**
00167  * @brief Enables or disables a feature in the APDS-9960
00168  *
00169  * @param[in] mode which feature to enable
00170  * @param[in] enable On (1) or Off (0)
00171  * @return True if operation success. False otherwise.
00172  */
00173 bool glibr::setMode(uint8_t mode, uint8_t enable)
00174 {
00175     uint8_t reg_val;
00176 
00177     /* Read current ENABLE register */
00178     reg_val = getMode();
00179     if( reg_val == ERROR ) {
00180         return false;
00181     }
00182     
00183     /* Change bit(s) in ENABLE register */
00184     enable = enable & 0x01;
00185     if( mode >= 0 && mode <= 6 ) {
00186         if (enable) {
00187             reg_val |= (1 << mode);
00188         } else {
00189             reg_val &= ~(1 << mode);
00190         }
00191     } else if( mode == ALL ) {
00192         if (enable) {
00193             reg_val = 0x7F;
00194         } else {
00195             reg_val = 0x00;
00196         }
00197     }
00198     
00199     /* Write value back to ENABLE register */     
00200     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){
00201         return false;
00202     }
00203    
00204 
00205         
00206     return true;
00207 }
00208 
00209 uint8_t glibr::getMode()
00210 {
00211     uint8_t val;
00212     val= I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
00213     if(val==ERROR){
00214         return ERROR;
00215     }
00216     return val;
00217 }
00218 
00219 
00220 
00221 bool glibr::enableLightSensor(bool interrupts)
00222 {
00223     
00224     /* Set default gain, interrupts, enable power, and enable sensor */
00225     if( !setAmbientLightGain(DEFAULT_AGAIN) ) {
00226         return false;
00227     }
00228     if( interrupts ) {
00229         if( !setAmbientLightIntEnable(1) ) {
00230             return false;
00231         }
00232     } else {
00233         if( !setAmbientLightIntEnable(0) ) {
00234             return false;
00235         }
00236     }
00237     if( !enablePower() ){
00238         return false;
00239     }
00240     if( !setMode(AMBIENT_LIGHT, 1) ) {
00241         return false;
00242     }
00243     
00244     return true;
00245 
00246 }
00247 
00248 /**
00249  * @brief Ends the light sensor on the APDS-9960
00250  *
00251  * @return True if sensor disabled correctly. False on error.
00252  */
00253 bool glibr::disableLightSensor()
00254 {
00255     if( !setAmbientLightIntEnable(0) ) {
00256         return false;
00257     }
00258     if( !setMode(AMBIENT_LIGHT, 0) ) {
00259         return false;
00260     }
00261     
00262     return true;
00263 }
00264 
00265 /**
00266  * @brief Starts the proximity sensor on the APDS-9960
00267  *
00268  * @param[in] interrupts true to enable hardware external interrupt on proximity
00269  * @return True if sensor enabled correctly. False on error.
00270  */
00271 bool glibr::enableProximitySensor(bool interrupts)
00272 {
00273     /* Set default gain, LED, interrupts, enable power, and enable sensor */
00274     if( !setProximityGain(DEFAULT_PGAIN) ) {
00275         return false;
00276     }
00277     if( !setLEDDrive(DEFAULT_LDRIVE) ) {
00278         return false;
00279     }
00280     if( interrupts ) {
00281         if( !setProximityIntEnable(1) ) {
00282             return false;
00283         }
00284     } else {
00285         if( !setProximityIntEnable(0) ) {
00286             return false;
00287         }
00288     }
00289     if( !enablePower() ){
00290         return false;
00291     }
00292     if( !setMode(PROXIMITY, 1) ) {
00293         return false;
00294     }
00295     
00296     return true;
00297 }
00298 
00299 /**
00300  * @brief Ends the proximity sensor on the APDS-9960
00301  *
00302  * @return True if sensor disabled correctly. False on error.
00303  */
00304 bool glibr::disableProximitySensor()
00305 {
00306     if( !setProximityIntEnable(0) ) {
00307         return false;
00308     }
00309     if( !setMode(PROXIMITY, 0) ) {
00310         return false;
00311     }
00312 
00313     return true;
00314 }
00315 
00316 
00317 /**
00318  * @brief Starts the gesture recognition engine on the APDS-9960
00319  *
00320  * @param[in] interrupts true to enable hardware external interrupt on gesture
00321  * @return True if engine enabled correctly. False on error.
00322  */
00323 bool glibr::enableGestureSensor(bool interrupts)
00324 {
00325     
00326     /* Enable gesture mode
00327        Set ENABLE to 0 (power off)
00328        Set WTIME to 0xFF
00329        Set AUX to LED_BOOST_300
00330        Enable PON, WEN, PEN, GEN in ENABLE 
00331     */
00332 
00333     resetGestureParameters();
00334     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, 0xFF) ) {
00335         return false;
00336     }
00337     if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_GESTURE_PPULSE) ) {
00338         return false;
00339     }
00340     if( !setLEDBoost(LED_BOOST_300) ) {
00341         return false;
00342     }
00343     if( interrupts ) {
00344         if( !setGestureIntEnable(1) ) {
00345             return false;
00346         }
00347     } else {
00348         if( !setGestureIntEnable(0) ) {
00349             return false;
00350         }
00351     }
00352     if( !setGestureMode(1) ) {
00353         return false;
00354     }
00355     if( !enablePower() ){
00356         return false;
00357     }
00358     if( !setMode(WAIT, 1) ) {
00359         return false;
00360     }
00361     if( !setMode(PROXIMITY, 1) ) {
00362         return false;
00363     }
00364     if( !setMode(GESTURE, 1) ) {
00365         return false;
00366     }
00367     
00368     return true;
00369 }
00370 
00371 /**
00372  * @brief Ends the gesture recognition engine on the APDS-9960
00373  *
00374  * @return True if engine disabled correctly. False on error.
00375  */
00376 bool glibr::disableGestureSensor()
00377 {
00378     resetGestureParameters();
00379     if( !setGestureIntEnable(0) ) {
00380         return false;
00381     }
00382     if( !setGestureMode(0) ) {
00383         return false;
00384     }
00385     if( !setMode(GESTURE, 0) ) {
00386         return false;
00387     }
00388     
00389     return true;
00390 }
00391 
00392 
00393 /**
00394  * @brief Determines if there is a gesture available for reading
00395  *
00396  * @return True if gesture available. False otherwise.
00397  */
00398 bool glibr::isGestureAvailable()
00399 {
00400     uint8_t val;
00401     
00402     /* Read value from GSTATUS register */
00403     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
00404     if( val==ERROR) {
00405         return ERROR;
00406     }
00407     
00408     /* Shift and mask out GVALID bit */
00409     val &= APDS9960_GVALID;
00410     
00411     /* Return true/false based on GVALID bit */
00412     if( val == 1) {
00413         return true;
00414     } else {
00415         return false;
00416     }
00417 }
00418 
00419 int glibr::readGesture()
00420 {
00421     uint8_t fifo_level = 0;
00422    // uint8_t bytes_expected= 0;
00423     int check;
00424     //char fifo_data[128];
00425     char fifo_data[128];
00426    char *fptr;
00427    fptr= fifo_data;
00428     
00429     uint8_t gstatus;
00430     int motion;
00431     int i;
00432     
00433     /* Make sure that power and gesture is on and data is valid */
00434     if( !isGestureAvailable() || !(getMode() & 0x41) ) {
00435         return DIR_NONE;
00436     }
00437 
00438     
00439     /* Keep looping as long as gesture data is valid */
00440     while(1) {
00441       
00442         /* Wait some time to collect next batch of FIFO data */
00443         // by Arai / JH1PJL
00444         /* wait(FIFO_PAUSE_TIME); */
00445         ThisThread::sleep_for(FIFO_PAUSE_TIME);
00446         
00447         /* Get the contents of the STATUS register. Is data still valid? */
00448           
00449         gstatus=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
00450         if( gstatus==ERROR ) {
00451             return ERROR;
00452         }
00453         /* If we have valid data, read in FIFO */
00454         if( (gstatus & APDS9960_GVALID) == APDS9960_GVALID ) {
00455         
00456             /* Read the current FIFO level */
00457             fifo_level=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GFLVL);
00458             if( fifo_level==ERROR ) {
00459                 return ERROR;
00460             }
00461             
00462 //#if DEBUG
00463 //            Serial.print("FIFO Level: ");
00464 //            Serial.println(fifo_level);
00465 //#endif
00466 
00467             /* If there's stuff in the FIFO, read it into our data block */                 //NEED TO FIGURE OUT WHAT THIS IS DOING.
00468         
00469             if( fifo_level > 0) {
00470                 check = I2CReadDataBlock(APDS9960_I2C_ADDR,APDS9960_GFIFO_U, 
00471                                                 fptr, 
00472                                                 (fifo_level * 4) );
00473                 
00474                 if( check == -1 ) {
00475                     return ERROR;
00476                 }
00477                 
00478 #if DEBUG
00479                 //Serial.print("FIFO Dump: ");
00480                 for ( i = 0; i < (fifo_level * 4); i++ ) {
00481                  //   Serial.print(fifo_data[i]);
00482                   //  Serial.print(" ");
00483                 }
00484                 //Serial.println();
00485 #endif
00486 
00487                 /* If at least 1 set of data, sort the data into U/D/L/R */
00488                 if((fifo_level * 4)  >= 4 ) {
00489                     for( i = 0; i < (fifo_level * 4); i += 4 ) {
00490                         gesture_data_.u_data[gesture_data_.sindex] = \
00491                                                             fifo_data[i + 0];
00492                         gesture_data_.d_data[gesture_data_.sindex] = \
00493                                                             fifo_data[i + 1];
00494                         gesture_data_.l_data[gesture_data_.sindex] = \
00495                                                             fifo_data[i + 2];
00496                         gesture_data_.r_data[gesture_data_.sindex] = \
00497                                                             fifo_data[i + 3];
00498                         gesture_data_.sindex++;
00499                         gesture_data_.total_gestures++;
00500                     }
00501                     
00502 #if DEBUG
00503                // Serial.print("Up Data: ");
00504                 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
00505                  //   Serial.print(gesture_data_.u_data[i]);
00506                   //  Serial.print(" ");
00507                 }
00508               //  Serial.println();
00509 #endif
00510 
00511                     /* Filter and process gesture data. Decode near/far state */
00512                     if( processGestureData() ) {
00513                         if( decodeGesture() ) {
00514                             //***TODO: U-Turn Gestures
00515 #if DEBUG
00516                             //Serial.println(gesture_motion_);
00517 #endif
00518                         }
00519                     }
00520                     
00521                     /* Reset data */
00522                     gesture_data_.sindex = 0;
00523                     gesture_data_.total_gestures = 0;
00524                 }
00525             }
00526         } else {
00527     
00528             /* Determine best guessed gesture and clean up */
00529             // by Arai / JH1PJL
00530             /* wait(FIFO_PAUSE_TIME); */
00531             ThisThread::sleep_for(FIFO_PAUSE_TIME);
00532             decodeGesture();
00533             motion = gesture_motion_;
00534 #if DEBUG
00535         //    Serial.print("END: ");
00536         //    Serial.println(gesture_motion_);
00537 #endif
00538             resetGestureParameters();
00539             return motion;
00540         }
00541     }
00542    // delete fptr;
00543 }
00544 /**
00545  * Turn the APDS-9960 on
00546  *
00547  * @return True if operation successful. False otherwise.
00548  */
00549 bool glibr::enablePower()
00550 {
00551     if( !setMode(Power, 1) ) {
00552         return false;
00553     }
00554     
00555     return true;
00556 }
00557 
00558 /**
00559  * Turn the APDS-9960 off
00560  *
00561  * @return True if operation successful. False otherwise.
00562  */
00563 bool glibr::disablePower()
00564 {
00565     if( !setMode(Power, 0) ) {
00566         return false;
00567     }
00568     
00569     return true;
00570 }
00571 
00572 /*******************************************************************************
00573  * Ambient light and color sensor controls
00574  ******************************************************************************/
00575 
00576 /**
00577  * @brief Reads the ambient (clear) light level as a 16-bit value
00578  *
00579  * @param[out] val value of the light sensor.
00580  * @return True if operation successful. False otherwise.
00581  */
00582 bool glibr::readAmbientLight(uint16_t &val)
00583 {
00584     uint8_t val_byte;
00585     val = 0;
00586     
00587     /* Read value from clear channel, low byte register */
00588     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAL);
00589     if( val_byte==ERROR) {
00590         return false;
00591     }
00592     val = val_byte;
00593     
00594     /* Read value from clear channel, high byte register */
00595    
00596     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CDATAH);
00597     if( val_byte==ERROR) {
00598         return false;
00599     }
00600     val = val + ((uint16_t)val_byte << 8);
00601     return true;
00602 }
00603 
00604 /**
00605  * @brief Reads the red light level as a 16-bit value
00606  *
00607  * @param[out] val value of the light sensor.
00608  * @return True if operation successful. False otherwise.
00609  */
00610 bool glibr::readRedLight(uint16_t &val)
00611 {
00612     uint8_t val_byte;
00613     val = 0;
00614     
00615     /* Read value from clear channel, low byte register */
00616     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAL);
00617     if( val_byte==ERROR) {
00618         return false;
00619     }
00620     
00621     val = val_byte;
00622     
00623     /* Read value from clear channel, high byte register */
00624     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_RDATAH);
00625     if( val_byte==ERROR) {
00626         return false;
00627     }
00628     val = val + ((uint16_t)val_byte << 8);
00629     
00630     return true;
00631 }
00632 
00633 /**
00634  * @brief Reads the green light level as a 16-bit value
00635  *
00636  * @param[out] val value of the light sensor.
00637  * @return True if operation successful. False otherwise.
00638  */
00639 
00640 bool glibr::readGreenLight(uint16_t &val)
00641 {
00642     uint8_t val_byte;
00643     val = 0;
00644     
00645     /* Read value from clear channel, low byte register */
00646     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAL);
00647     if( val_byte==ERROR) {
00648         return false;
00649     }
00650     
00651     val = val_byte;
00652     
00653     /* Read value from clear channel, high byte register */
00654     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GDATAH);
00655     if( val_byte==ERROR) {
00656         return false;
00657     }
00658     val = val + ((uint16_t)val_byte << 8);
00659     
00660     return true;
00661 }
00662 
00663 /**
00664  * @brief Reads the red light level as a 16-bit value
00665  *
00666  * @param[out] val value of the light sensor.
00667  * @return True if operation successful. False otherwise.
00668 */
00669 
00670 bool glibr::readBlueLight(uint16_t &val)
00671 {
00672     uint8_t val_byte;
00673     val = 0;
00674     
00675     /* Read value from clear channel, low byte register */
00676     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAL);
00677     if( val_byte==ERROR) {
00678         return false;
00679     }
00680     
00681     val = val_byte;
00682     
00683     /* Read value from clear channel, high byte register */
00684     val_byte=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_BDATAH);
00685     if( val_byte==ERROR) {
00686         return false;
00687     }
00688     val = val + ((uint16_t)val_byte << 8);
00689     
00690     return true;
00691 }
00692 
00693 /*******************************************************************************
00694  * Proximity sensor controls
00695  ******************************************************************************/
00696 
00697 /**
00698  * @brief Reads the proximity level as an 8-bit value
00699  *
00700  * @param[out] val value of the proximity sensor.
00701  * @return True if operation successful. False otherwise.
00702  */
00703 bool glibr::readProximity(uint8_t &val)
00704 {
00705     val = 0;
00706     
00707     /* Read value from proximity data register */
00708      val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PDATA);
00709     
00710     if(val==ERROR){
00711         return false;   
00712     }
00713     
00714     return true;
00715 }
00716 
00717 /*******************************************************************************
00718  * High-level gesture controls
00719  ******************************************************************************/
00720 
00721 /**
00722  * @brief Resets all the parameters in the gesture data member
00723  */
00724 void glibr::resetGestureParameters()
00725 {
00726     gesture_data_.sindex = 0;
00727     gesture_data_.total_gestures = 0;
00728     
00729     gesture_ud_delta_ = 0;
00730     gesture_lr_delta_ = 0;
00731     
00732     gesture_ud_count_ = 0;
00733     gesture_lr_count_ = 0;
00734     
00735     gesture_near_count_ = 0;
00736     gesture_far_count_ = 0;
00737     
00738     gesture_state_ = 0;
00739     gesture_motion_ = DIR_NONE;
00740 }
00741 
00742 bool glibr::processGestureData()
00743 {
00744     uint8_t u_first = 0;
00745     uint8_t d_first = 0;
00746     uint8_t l_first = 0;
00747     uint8_t r_first = 0;
00748     uint8_t u_last = 0;
00749     uint8_t d_last = 0;
00750     uint8_t l_last = 0;
00751     uint8_t r_last = 0;
00752     int ud_ratio_first;
00753     int lr_ratio_first;
00754     int ud_ratio_last;
00755     int lr_ratio_last;
00756     int ud_delta;
00757     int lr_delta;
00758     int i;
00759 
00760     /* If we have less than 4 total gestures, that's not enough */
00761     if( gesture_data_.total_gestures <= 4 ) {
00762         return false;
00763     }
00764     
00765     /* Check to make sure our data isn't out of bounds */
00766     if( (gesture_data_.total_gestures <= 32) && \
00767         (gesture_data_.total_gestures > 0) ) {
00768         
00769         /* Find the first value in U/D/L/R above the threshold */
00770         for( i = 0; i < gesture_data_.total_gestures; i++ ) {
00771             if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
00772                 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
00773                 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
00774                 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
00775                 
00776                 u_first = gesture_data_.u_data[i];
00777                 d_first = gesture_data_.d_data[i];
00778                 l_first = gesture_data_.l_data[i];
00779                 r_first = gesture_data_.r_data[i];
00780                 break;
00781             }
00782         }
00783         
00784         /* If one of the _first values is 0, then there is no good data */
00785         if( (u_first == 0) || (d_first == 0) || \
00786             (l_first == 0) || (r_first == 0) ) {
00787             
00788             return false;
00789         }
00790         /* Find the last value in U/D/L/R above the threshold */
00791         for( i = gesture_data_.total_gestures - 1; i >= 0; i-- ) {
00792 /* #if DEBUG
00793             Serial.print(F("Finding last: "));
00794             Serial.print(F("U:"));
00795             Serial.print(gesture_data_.u_data[i]);
00796             Serial.print(F(" D:"));
00797             Serial.print(gesture_data_.d_data[i]);
00798             Serial.print(F(" L:"));
00799             Serial.print(gesture_data_.l_data[i]);
00800             Serial.print(F(" R:"));
00801             Serial.println(gesture_data_.r_data[i]);
00802 #endif */
00803             if( (gesture_data_.u_data[i] > GESTURE_THRESHOLD_OUT) &&
00804                 (gesture_data_.d_data[i] > GESTURE_THRESHOLD_OUT) &&
00805                 (gesture_data_.l_data[i] > GESTURE_THRESHOLD_OUT) &&
00806                 (gesture_data_.r_data[i] > GESTURE_THRESHOLD_OUT) ) {
00807                 
00808                 u_last = gesture_data_.u_data[i];
00809                 d_last = gesture_data_.d_data[i];
00810                 l_last = gesture_data_.l_data[i];
00811                 r_last = gesture_data_.r_data[i];
00812                 break;
00813             }
00814         }
00815     }
00816     
00817     /* Calculate the first vs. last ratio of up/down and left/right */
00818     ud_ratio_first = ((u_first - d_first) * 100) / (u_first + d_first);
00819     lr_ratio_first = ((l_first - r_first) * 100) / (l_first + r_first);
00820     ud_ratio_last = ((u_last - d_last) * 100) / (u_last + d_last);
00821     lr_ratio_last = ((l_last - r_last) * 100) / (l_last + r_last);
00822        
00823 /* #if DEBUG
00824     Serial.print(F("Last Values: "));
00825     Serial.print(F("U:"));
00826     Serial.print(u_last);
00827     Serial.print(F(" D:"));
00828     Serial.print(d_last);
00829     Serial.print(F(" L:"));
00830     Serial.print(l_last);
00831     Serial.print(F(" R:"));
00832     Serial.println(r_last);
00833 
00834     Serial.print(F("Ratios: "));
00835     Serial.print(F("UD Fi: "));
00836     Serial.print(ud_ratio_first);
00837     Serial.print(F(" UD La: "));
00838     Serial.print(ud_ratio_last);
00839     Serial.print(F(" LR Fi: "));
00840     Serial.print(lr_ratio_first);
00841     Serial.print(F(" LR La: "));
00842     Serial.println(lr_ratio_last);
00843 #endif */
00844        
00845     /* Determine the difference between the first and last ratios */
00846     ud_delta = ud_ratio_last - ud_ratio_first;
00847     lr_delta = lr_ratio_last - lr_ratio_first;
00848     
00849 /* #if DEBUG
00850     Serial.print("Deltas: ");
00851     Serial.print("UD: ");
00852     Serial.print(ud_delta);
00853     Serial.print(" LR: ");
00854     Serial.println(lr_delta);
00855 #endif */
00856 
00857     /* Accumulate the UD and LR delta values */
00858     gesture_ud_delta_ += ud_delta;
00859     gesture_lr_delta_ += lr_delta;
00860     
00861 /* #if DEBUG
00862     Serial.print("Accumulations: ");
00863     Serial.print("UD: ");
00864     Serial.print(gesture_ud_delta_);
00865     Serial.print(" LR: ");
00866     Serial.println(gesture_lr_delta_);
00867 #endif */
00868     
00869     /* Determine U/D gesture */
00870     if( gesture_ud_delta_ >= GESTURE_SENSITIVITY_1 ) {
00871         gesture_ud_count_ = 1;
00872     } else if( gesture_ud_delta_ <= -GESTURE_SENSITIVITY_1 ) {
00873         gesture_ud_count_ = -1;
00874     } else {
00875         gesture_ud_count_ = 0;
00876     }
00877     
00878     /* Determine L/R gesture */
00879     if( gesture_lr_delta_ >= GESTURE_SENSITIVITY_1 ) {
00880         gesture_lr_count_ = 1;
00881     } else if( gesture_lr_delta_ <= -GESTURE_SENSITIVITY_1 ) {
00882         gesture_lr_count_ = -1;
00883     } else {
00884         gesture_lr_count_ = 0;
00885     }
00886     
00887     /* Determine Near/Far gesture */
00888     if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 0) ) {
00889         if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
00890             (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
00891             
00892             if( (ud_delta == 0) && (lr_delta == 0) ) {
00893                 gesture_near_count_++;
00894             } else if( (ud_delta != 0) || (lr_delta != 0) ) {
00895                 gesture_far_count_++;
00896             }
00897             
00898             if( (gesture_near_count_ >= 10) && (gesture_far_count_ >= 2) ) {
00899                 if( (ud_delta == 0) && (lr_delta == 0) ) {
00900                     gesture_state_ = NEAR_STATE;
00901                 } else if( (ud_delta != 0) && (lr_delta != 0) ) {
00902                     gesture_state_ = FAR_STATE;
00903                 }
00904                 return true;
00905             }
00906         }
00907     } else {
00908         if( (abs(ud_delta) < GESTURE_SENSITIVITY_2) && \
00909             (abs(lr_delta) < GESTURE_SENSITIVITY_2) ) {
00910                 
00911             if( (ud_delta == 0) && (lr_delta == 0) ) {
00912                 gesture_near_count_++;
00913             }
00914             
00915             if( gesture_near_count_ >= 5 ) {
00916                 gesture_ud_count_ = 0;
00917                 gesture_lr_count_ = 0;
00918                 gesture_ud_delta_ = 0;
00919                 gesture_lr_delta_ = 0;
00920             }
00921         }
00922     }
00923     
00924 // #if DEBUG
00925   /*    printf("UD_CT: %d\n",gesture_ud_count_);
00926       printf("LR_CT: %d\n",gesture_lr_count_);
00927       printf("NEAR_CT: %d\n",gesture_near_count_);
00928       printf(" FAR_CT: %d\n",gesture_far_count_);
00929       printf("----------"); */
00930 //#endif */
00931     
00932     return false;
00933 }
00934 
00935 /**
00936  * @brief Determines swipe direction or near/far state
00937  *
00938  * @return True if near/far event. False otherwise.
00939  */
00940 bool glibr::decodeGesture()
00941 {
00942     /* Return if near or far event is detected */
00943     if( gesture_state_ == NEAR_STATE ) {
00944         gesture_motion_ = DIR_NEAR;
00945         return true;
00946     } else if ( gesture_state_ == FAR_STATE ) {
00947         gesture_motion_ = DIR_FAR;
00948         return true;
00949     }
00950     
00951     /* Determine swipe direction */
00952     if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 0) ) {
00953         gesture_motion_ = DIR_UP;
00954     } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 0) ) {
00955         gesture_motion_ = DIR_DOWN;
00956     } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == 1) ) {
00957         gesture_motion_ = DIR_RIGHT;
00958     } else if( (gesture_ud_count_ == 0) && (gesture_lr_count_ == -1) ) {
00959         gesture_motion_ = DIR_LEFT;
00960     } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == 1) ) {
00961         if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
00962             gesture_motion_ = DIR_UP;
00963         } else {
00964             gesture_motion_ = DIR_RIGHT;
00965         }
00966     } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == -1) ) {
00967         if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
00968             gesture_motion_ = DIR_DOWN;
00969         } else {
00970             gesture_motion_ = DIR_LEFT;
00971         }
00972     } else if( (gesture_ud_count_ == -1) && (gesture_lr_count_ == -1) ) {
00973         if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
00974             gesture_motion_ = DIR_UP;
00975         } else {
00976             gesture_motion_ = DIR_LEFT;
00977         }
00978     } else if( (gesture_ud_count_ == 1) && (gesture_lr_count_ == 1) ) {
00979         if( abs(gesture_ud_delta_) > abs(gesture_lr_delta_) ) {
00980             gesture_motion_ = DIR_DOWN;
00981         } else {
00982             gesture_motion_ = DIR_RIGHT;
00983         }
00984     } else {
00985         return false;
00986     }
00987     
00988     return true;
00989 }
00990 
00991 /*******************************************************************************
00992  * Getters and setters for register values
00993  ******************************************************************************/
00994 
00995 /**
00996  * @brief Returns the lower threshold for proximity detection
00997  *
00998  * @return lower threshold
00999  */
01000  
01001  uint8_t glibr::getProxIntLowThresh()
01002 {
01003     uint8_t val;
01004     
01005     /* Read value from PILT register */
01006    /* if( !wireReadDataByte(APDS9960_PILT, val) ) {
01007         val = 0;
01008     }*/
01009     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
01010     if(val==ERROR){
01011         val=0;   
01012     }
01013     
01014     return val;
01015 }
01016  
01017  /**
01018  * @brief Sets the lower threshold for proximity detection
01019  *
01020  * @param[in] threshold the lower proximity threshold
01021  * @return True if operation successful. False otherwise.
01022  */
01023  bool glibr::setProxIntLowThresh(uint8_t threshold)
01024 {
01025     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
01026         return false;
01027     }
01028     
01029     return true;
01030 }
01031 
01032 /**
01033  * @brief Returns the high threshold for proximity detection
01034  *
01035  * @return high threshold
01036  */
01037 uint8_t glibr::getProxIntHighThresh()
01038 {
01039     uint8_t val;
01040     
01041     /* Read value from PIHT register */
01042     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_PILT);
01043     if( val==ERROR ) {
01044         val = 0;
01045     }
01046     
01047     return val;
01048 }
01049 
01050 /**
01051  * @brief Sets the high threshold for proximity detection
01052  *
01053  * @param[in] threshold the high proximity threshold
01054  * @return True if operation successful. False otherwise.
01055  */
01056 bool glibr::setProxIntHighThresh(uint8_t threshold)
01057 {
01058    
01059     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT, threshold) ) {
01060         return false;
01061     }
01062     
01063     return true;
01064 }
01065 
01066  /**
01067  * @brief Returns LED drive strength for proximity and ALS
01068  *
01069  * Value    LED Current
01070  *   0        100 mA
01071  *   1         50 mA
01072  *   2         25 mA
01073  *   3         12.5 mA
01074  *
01075  * @return the value of the LED drive strength. 0xFF on failure.
01076  */
01077 uint8_t glibr::getLEDDrive()
01078 {
01079     uint8_t val;
01080     
01081     /* Read value from CONTROL register */
01082     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01083     if(  val == ERROR ){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
01084         return ERROR;
01085     }
01086     
01087     /* Shift and mask out LED drive bits */
01088     val = (val >> 6) & 0x03;//0b00000011;
01089     
01090     return val;
01091 }
01092  
01093  /**
01094  * @brief Sets the LED drive strength for proximity and ALS
01095  *
01096  * Value    LED Current
01097  *   0        100 mA
01098  *   1         50 mA
01099  *   2         25 mA
01100  *   3         12.5 mA
01101  *
01102  * @param[in] drive the value (0-3) for the LED drive strength
01103  * @return True if operation successful. False otherwise.
01104  */
01105  
01106 bool glibr::setLEDDrive(uint8_t drive)
01107 {
01108     uint8_t val;
01109     
01110     /* Read value from CONTROL register */
01111     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01112     if(val==ERROR){
01113         return false;   
01114     }
01115     /* Set bits in register to given value */
01116     //drive &= 0b00000011
01117     drive &= 0x03;
01118     drive = drive << 6;
01119     //val &= 0b00111111;
01120     val &= 0x3F;
01121     val |= drive;
01122     
01123     /* Write register value back into CONTROL register */
01124     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
01125         return false;
01126     }
01127     
01128     return true;
01129 }
01130 
01131 /**
01132  * @brief Returns receiver gain for proximity detection
01133  *
01134  * Value    Gain
01135  *   0       1x
01136  *   1       2x
01137  *   2       4x
01138  *   3       8x
01139  *
01140  * @return the value of the proximity gain. 0xFF on failure.
01141  */
01142 uint8_t glibr::getProximityGain()
01143 {
01144     uint8_t val;
01145     
01146     /* Read value from CONTROL register */
01147     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01148     if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
01149         return ERROR;
01150     }
01151     
01152     /* Shift and mask out PDRIVE bits */
01153     val = (val >> 2) & 0x03;//0b00000011;
01154     
01155     return val;
01156 }
01157 
01158 /**
01159  * @brief Sets the receiver gain for proximity detection
01160  *
01161  * Value    Gain
01162  *   0       1x
01163  *   1       2x
01164  *   2       4x
01165  *   3       8x
01166  *
01167  * @param[in] drive the value (0-3) for the gain
01168  * @return True if operation successful. False otherwise.
01169  */
01170 bool glibr::setProximityGain(uint8_t drive)
01171 {
01172     uint8_t val;
01173     
01174     /* Read value from CONTROL register */
01175    
01176     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01177     
01178     if(val==ERROR){
01179         return false;   
01180     }
01181     /* Set bits in register to given value */
01182     //drive &= 0b00000011;
01183     drive &=0x03;
01184     drive = drive << 2;
01185     //val &= 0b11110011
01186     val &= 0xF3;
01187     val |= drive;
01188     
01189     /* Write register value back into CONTROL register */
01190     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
01191         return false;
01192     }
01193     return true;
01194 }
01195 
01196 /**
01197  * @brief Returns receiver gain for the ambient light sensor (ALS)
01198  *
01199  * Value    Gain
01200  *   0        1x
01201  *   1        4x
01202  *   2       16x
01203  *   3       64x
01204  *
01205  * @return the value of the ALS gain. 0xFF on failure.
01206  */
01207 uint8_t glibr::getAmbientLightGain()
01208 {
01209     uint8_t val;
01210     
01211     /* Read value from CONTROL register */
01212     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01213     if( val == ERROR){//!wireReadDataByte(APDS9960_CONTROL, val) ) {
01214         return ERROR;
01215     }
01216     
01217     /* Shift and mask out ADRIVE bits */
01218     val &= 0x03;//0b00000011;
01219     
01220     return val;
01221 }
01222 
01223 /**
01224  * @brief Sets the receiver gain for the ambient light sensor (ALS)
01225  *
01226  * Value    Gain
01227  *   0        1x
01228  *   1        4x
01229  *   2       16x
01230  *   3       64x
01231  *
01232  * @param[in] drive the value (0-3) for the gain
01233  * @return True if operation successful. False otherwise.
01234  */
01235 bool glibr::setAmbientLightGain(uint8_t drive){
01236 
01237     uint8_t val;
01238     
01239     /* Read value from CONTROL register */
01240    
01241     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
01242     
01243     if(val==ERROR){
01244         return false;   
01245     }
01246     /* Set bits in register to given value */
01247     //drive &= 0b00000011;
01248     drive &=0x03;
01249     drive = drive << 2;
01250     //val &=0b11111100
01251     val &= 0xF3;
01252     val |= drive;
01253     
01254     /* Write register value back into CONTROL register */
01255     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) {
01256         return false;
01257     }
01258     return true;
01259 }
01260 
01261 /**
01262  * @brief Get the current LED boost value
01263  * 
01264  * Value  Boost Current
01265  *   0        100%
01266  *   1        150%
01267  *   2        200%
01268  *   3        300%
01269  *
01270  * @return The LED boost value. 0xFF on failure.
01271  */
01272 uint8_t glibr::getLEDBoost() {
01273     uint8_t val;
01274     
01275     /* Read value from CONFIG2 register */
01276     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
01277     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
01278         return ERROR;
01279     }
01280     
01281     /* Shift and mask out LED_BOOST bits */
01282     val = (val >> 4) & 0x03;//0b00000011;
01283     
01284     return val;
01285 }
01286 
01287 /**
01288  * @brief Sets the LED current boost value
01289  *
01290  * Value  Boost Current
01291  *   0        100%
01292  *   1        150%
01293  *   2        200%
01294  *   3        300%
01295  *
01296  * @param[in] drive the value (0-3) for current boost (100-300%)
01297  * @return True if operation successful. False otherwise.
01298  */
01299 bool glibr::setLEDBoost(uint8_t boost)
01300 {
01301     uint8_t val;
01302     
01303     /* Read value from CONFIG2 register */
01304     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG2);
01305     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG2, val) ) {
01306         return false;
01307     }
01308     
01309     /* Set bits in register to given value */
01310     boost &= 0x03;//0b00000011;
01311     boost = boost << 4;
01312     val &= 0xCF;//0b11001111;
01313     val |= boost;
01314     
01315     /* Write register value back into CONFIG2 register */
01316     
01317     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG2, val)){//!wireWriteDataByte(APDS9960_CONFIG2, val) ) {
01318         return false;
01319     }
01320     
01321     return true;
01322 }    
01323 
01324 /**
01325  * @brief Gets proximity gain compensation enable
01326  *
01327  * @return 1 if compensation is enabled. 0 if not. 0xFF on error.
01328  */
01329 uint8_t glibr::getProxGainCompEnable()
01330 {
01331     uint8_t val;
01332     
01333     /* Read value from CONFIG3 register */
01334     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3); 
01335     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
01336         return ERROR;
01337     }
01338     
01339     /* Shift and mask out PCMP bits */
01340     val = (val >> 5) & 0x01;//0b00000001;
01341     
01342     return val;
01343 }
01344 
01345 /**
01346  * @brief Sets the proximity gain compensation enable
01347  *
01348  * @param[in] enable 1 to enable compensation. 0 to disable compensation.
01349  * @return True if operation successful. False otherwise.
01350  */
01351  bool glibr::setProxGainCompEnable(uint8_t enable)
01352 {
01353     uint8_t val;
01354     
01355     /* Read value from CONFIG3 register */
01356     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
01357     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
01358         return false;
01359     }
01360     
01361     /* Set bits in register to given value */
01362     enable &= 0x01;//0b00000001;
01363     enable = enable << 5;
01364     val &= 0xCF;//0b11011111;
01365     val |= enable;
01366     
01367     /* Write register value back into CONFIG3 register */
01368     
01369     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG3, val)){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
01370         return false;
01371     }
01372     
01373     return true;
01374 }
01375 
01376 /**
01377  * @brief Gets the current mask for enabled/disabled proximity photodiodes
01378  *
01379  * 1 = disabled, 0 = enabled
01380  * Bit    Photodiode
01381  *  3       UP
01382  *  2       DOWN
01383  *  1       LEFT
01384  *  0       RIGHT
01385  *
01386  * @return Current proximity mask for photodiodes. 0xFF on error.
01387  */
01388 uint8_t glibr::getProxPhotoMask()
01389 {
01390     uint8_t val;
01391     
01392     /* Read value from CONFIG3 register */
01393     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
01394     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
01395         return ERROR;
01396     }
01397     
01398     /* Mask out photodiode enable mask bits */
01399     val &= 0x0F;//0b00001111;
01400     
01401     return val;
01402 }
01403 
01404 /**
01405  * @brief Sets the mask for enabling/disabling proximity photodiodes
01406  *
01407  * 1 = disabled, 0 = enabled
01408  * Bit    Photodiode
01409  *  3       UP
01410  *  2       DOWN
01411  *  1       LEFT
01412  *  0       RIGHT
01413  *
01414  * @param[in] mask 4-bit mask value
01415  * @return True if operation successful. False otherwise.
01416  */
01417 bool glibr::setProxPhotoMask(uint8_t mask)
01418 {
01419     uint8_t val;
01420     
01421     /* Read value from CONFIG3 register */
01422     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3);
01423     if( val == ERROR){//!wireReadDataByte(APDS9960_CONFIG3, val) ) {
01424         return false;
01425     }
01426     
01427     /* Set bits in register to given value */
01428     mask &= 0x0F;//0b00001111;
01429     val &= 0xF0;//0b11110000;
01430     val |= mask;
01431     
01432     /* Write register value back into CONFIG3 register */
01433     I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val);
01434     if( val == ERROR){//!wireWriteDataByte(APDS9960_CONFIG3, val) ) {
01435         return false;
01436     }
01437     
01438     return true;
01439 }
01440 
01441 /**
01442  * @brief Gets the entry proximity threshold for gesture sensing
01443  *
01444  * @return Current entry proximity threshold.
01445  */
01446 uint8_t glibr::getGestureEnterThresh()
01447 {
01448     uint8_t val;
01449     
01450     /* Read value from GPENTH register */
01451     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GPENTH);
01452     if( val == ERROR){//!wireReadDataByte(APDS9960_GPENTH, val) ) {
01453         val = 0;
01454     }
01455     
01456     return val;
01457 }
01458 
01459 /**
01460  * @brief Sets the entry proximity threshold for gesture sensing
01461  *
01462  * @param[in] threshold proximity value needed to start gesture mode
01463  * @return True if operation successful. False otherwise.
01464  */
01465 bool glibr::setGestureEnterThresh(uint8_t threshold)
01466 {
01467     
01468     if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GPENTH, threshold)){;//!wireWriteDataByte(APDS9960_GPENTH, threshold) ) {
01469         return false;
01470     }
01471     
01472     return true;
01473 }
01474 
01475 /**
01476  * @brief Gets the exit proximity threshold for gesture sensing
01477  *
01478  * @return Current exit proximity threshold.
01479  */
01480 uint8_t glibr::getGestureExitThresh()
01481 {
01482     uint8_t val;
01483     
01484     /* Read value from GEXTH register */
01485     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GEXTH);
01486     if( val == ERROR){//!wireReadDataByte(APDS9960_GEXTH, val) ) {
01487         val = 0;
01488     }
01489     
01490     return val;
01491 }
01492 
01493 /**
01494  * @brief Sets the exit proximity threshold for gesture sensing
01495  *
01496  * @param[in] threshold proximity value needed to end gesture mode
01497  * @return True if operation successful. False otherwise.
01498  */
01499 bool glibr::setGestureExitThresh(uint8_t threshold)
01500 {
01501     if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GEXTH, threshold)){//!wireWriteDataByte(APDS9960_GEXTH, threshold) ) {
01502         return false;
01503     }
01504     
01505     return true;
01506 }
01507 
01508 /**
01509  * @brief Gets the gain of the photodiode during gesture mode
01510  *
01511  * Value    Gain
01512  *   0       1x
01513  *   1       2x
01514  *   2       4x
01515  *   3       8x
01516  *
01517  * @return the current photodiode gain. 0xFF on error.
01518  */
01519 uint8_t glibr::getGestureGain()
01520 {
01521     uint8_t val;
01522     
01523     /* Read value from GCONF2 register */
01524     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01525     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01526         return ERROR;
01527     }
01528     
01529     /* Shift and mask out GGAIN bits */
01530     val = (val >> 5) & 0x03;//0b00000011;
01531     
01532     return val;
01533 }
01534 
01535 /**
01536  * @brief Sets the gain of the photodiode during gesture mode
01537  *
01538  * Value    Gain
01539  *   0       1x
01540  *   1       2x
01541  *   2       4x
01542  *   3       8x
01543  *
01544  * @param[in] gain the value for the photodiode gain
01545  * @return True if operation successful. False otherwise.
01546  */
01547 bool glibr::setGestureGain(uint8_t gain)
01548 {
01549     uint8_t val;
01550     
01551     /* Read value from GCONF2 register */
01552     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01553     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01554         return false;
01555     }
01556     
01557     /* Set bits in register to given value */
01558     gain &= 0x03;//0b00000011;
01559     gain = gain << 5;
01560     val &= 0x9F;//0b10011111;
01561     val |= gain;
01562     
01563     /* Write register value back into GCONF2 register */
01564     if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
01565         return false;
01566     }
01567     
01568     return true;
01569 }
01570 
01571 /**
01572  * @brief Gets the drive current of the LED during gesture mode
01573  *
01574  * Value    LED Current
01575  *   0        100 mA
01576  *   1         50 mA
01577  *   2         25 mA
01578  *   3         12.5 mA
01579  *
01580  * @return the LED drive current value. 0xFF on error.
01581  */
01582 uint8_t glibr::getGestureLEDDrive()
01583 {
01584     uint8_t val;
01585     
01586     /* Read value from GCONF2 register */
01587     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01588     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01589         return ERROR;
01590     }
01591     
01592     /* Shift and mask out GLDRIVE bits */
01593     val = (val >> 3) & 0x03;//0b00000011;
01594     
01595     return val;
01596 }
01597 
01598 /**
01599  * @brief Sets the LED drive current during gesture mode
01600  *
01601  * Value    LED Current
01602  *   0        100 mA
01603  *   1         50 mA
01604  *   2         25 mA
01605  *   3         12.5 mA
01606  *
01607  * @param[in] drive the value for the LED drive current
01608  * @return True if operation successful. False otherwise.
01609  */
01610 bool glibr::setGestureLEDDrive(uint8_t drive)
01611 {
01612     uint8_t val;
01613     
01614     /* Read value from GCONF2 register */
01615     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01616     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01617         return false;
01618     }
01619     
01620     /* Set bits in register to given value */
01621     drive &= 0x03;//0b00000011;
01622     drive = drive << 3;
01623     val &= 0xE7;//0b11100111;
01624     val |= drive;
01625     
01626     /* Write register value back into GCONF2 register */
01627     if( I2CwriteByte(APDS9960_I2C_ADDR, APDS9960_GCONF2, val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
01628         return false;
01629     }
01630     
01631     return true;
01632 }
01633 
01634 /**
01635  * @brief Gets the time in low power mode between gesture detections
01636  *
01637  * Value    Wait time
01638  *   0          0 ms
01639  *   1          2.8 ms
01640  *   2          5.6 ms
01641  *   3          8.4 ms
01642  *   4         14.0 ms
01643  *   5         22.4 ms
01644  *   6         30.8 ms
01645  *   7         39.2 ms
01646  *
01647  * @return the current wait time between gestures. 0xFF on error.
01648  */
01649 uint8_t glibr::getGestureWaitTime()
01650 {
01651     uint8_t val;
01652     
01653     /* Read value from GCONF2 register */
01654     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01655     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01656         return ERROR;
01657     }
01658     
01659     /* Mask out GWTIME bits */
01660     val &= 0x07;//0b00000111;
01661     
01662     return val;
01663 }
01664 
01665 /*
01666 *
01667 *
01668 *
01669 *LEFT Off HERE AT 3:47PM On 3/6/15
01670 *
01671 *
01672 *
01673 *
01674 */
01675 
01676 
01677 /**
01678  * @brief Sets the time in low power mode between gesture detections
01679  *
01680  * Value    Wait time
01681  *   0          0 ms
01682  *   1          2.8 ms
01683  *   2          5.6 ms
01684  *   3          8.4 ms
01685  *   4         14.0 ms
01686  *   5         22.4 ms
01687  *   6         30.8 ms
01688  *   7         39.2 ms
01689  *
01690  * @param[in] the value for the wait time
01691  * @return True if operation successful. False otherwise.
01692  */
01693 bool glibr::setGestureWaitTime(uint8_t time)
01694 {
01695     uint8_t val;
01696     
01697     /* Read value from GCONF2 register */
01698     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF2);
01699     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF2, val) ) {
01700         return false;
01701     }
01702     /* if( !wireReadDataByte(APDS9960_GCONF2, val) ) {
01703         return false;
01704     } */
01705     
01706     /* Set bits in register to given value */
01707     time &= 0x07;//0b00000111;
01708     val &= 0xF8;//0b11111000;
01709     val |= time;
01710     
01711     /* Write register value back into GCONF2 register */
01712     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF2,val)){//!wireWriteDataByte(APDS9960_GCONF2, val) ) {
01713         return false;
01714     }
01715     /*if( !wireWriteDataByte(APDS9960_GCONF2, val) ) {
01716         return false;
01717     }*/
01718     return true;
01719 }
01720 
01721 /**
01722  * @brief Gets the low threshold for ambient light interrupts
01723  *
01724  * @param[out] threshold current low threshold stored on the APDS-9960
01725  * @return True if operation successful. False otherwise.
01726  */
01727 bool glibr::getLightIntLowThreshold(uint16_t &threshold)
01728 {
01729     uint8_t val_byte;
01730     threshold = 0;
01731     
01732     /* Read value from ambient light low threshold, low byte register */
01733     val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTL);
01734     if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTL, val_byte) ) {
01735         return false;
01736     }
01737     threshold = val_byte;
01738     
01739     /* Read value from ambient light low threshold, high byte register */
01740     val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AILTH);
01741     if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AILTH, val_byte) ) {
01742         return false;
01743     }
01744     threshold = threshold + ((uint16_t)val_byte << 8);
01745     
01746     return true;
01747 }
01748 
01749 /**
01750  * @brief Sets the low threshold for ambient light interrupts
01751  *
01752  * @param[in] threshold low threshold value for interrupt to trigger
01753  * @return True if operation successful. False otherwise.
01754  */
01755 bool glibr::setLightIntLowThreshold(uint16_t threshold)
01756 {
01757     uint8_t val_low;
01758     uint8_t val_high;
01759     
01760     /* Break 16-bit threshold into 2 8-bit values */
01761     val_low = threshold & 0x00FF;
01762     val_high = (threshold & 0xFF00) >> 8;
01763     
01764     /* Write low byte */
01765     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTL,val_low)){//!wireWriteDataByte(APDS9960_AILTL, val_low) ) {
01766         return false;
01767     }
01768     
01769     /* Write high byte */
01770     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AILTH,val_high)){//!wireWriteDataByte(APDS9960_AILTH, val_high) ) {
01771         return false;
01772     }
01773     
01774     return true;
01775 }
01776 
01777 /**
01778  * @brief Gets the high threshold for ambient light interrupts
01779  *
01780  * @param[out] threshold current low threshold stored on the APDS-9960
01781  * @return True if operation successful. False otherwise.
01782  */
01783 bool glibr::getLightIntHighThreshold(uint16_t &threshold)
01784 {
01785     uint8_t val_byte;
01786     threshold = 0;
01787     
01788     /* Read value from ambient light high threshold, low byte register */
01789     val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTL);
01790     if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTL, val_byte) ) {
01791         return false;
01792     }
01793     threshold = val_byte;
01794     
01795     /* Read value from ambient light high threshold, high byte register */
01796     val_byte = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AIHTH);
01797     if( val_byte == ERROR){//!wireReadDataByte(APDS9960_AIHTH, val_byte) ) {
01798         return false;
01799     }
01800     threshold = threshold + ((uint16_t)val_byte << 8);
01801     
01802     return true;
01803 }
01804 
01805 /**
01806  * @brief Sets the high threshold for ambient light interrupts
01807  *
01808  * @param[in] threshold high threshold value for interrupt to trigger
01809  * @return True if operation successful. False otherwise.
01810  */
01811 bool glibr::setLightIntHighThreshold(uint16_t threshold)
01812 {
01813     uint8_t val_low;
01814     uint8_t val_high;
01815     
01816     /* Break 16-bit threshold into 2 8-bit values */
01817     val_low = threshold & 0x00FF;
01818     val_high = (threshold & 0xFF00) >> 8;
01819     
01820     /* Write low byte */
01821     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTL,val_low)){//!wireWriteDataByte(APDS9960_AIHTL, val_low) ) {
01822         return false;
01823     }
01824     
01825     /* Write high byte */
01826     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_AIHTH,val_high)){//!wireWriteDataByte(APDS9960_AIHTH, val_high) ) {
01827         return false;
01828     }
01829     
01830     return true;
01831 }
01832 
01833 /**
01834  * @brief Gets the low threshold for proximity interrupts
01835  *
01836  * @param[out] threshold current low threshold stored on the APDS-9960
01837  * @return True if operation successful. False otherwise.
01838  */
01839 bool glibr::getProximityIntLowThreshold(uint8_t &threshold)
01840 {
01841     threshold = 0;
01842     
01843     /* Read value from proximity low threshold register */
01844     threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PILT);
01845     if( threshold == ERROR){//!wireReadDataByte(APDS9960_PILT, threshold) ) {
01846         return false;
01847     }
01848     
01849     return true;
01850 }
01851 
01852 /**
01853  * @brief Sets the low threshold for proximity interrupts
01854  *
01855  * @param[in] threshold low threshold value for interrupt to trigger
01856  * @return True if operation successful. False otherwise.
01857  */
01858 bool glibr::setProximityIntLowThreshold(uint8_t threshold)
01859 {
01860     
01861     /* Write threshold value to register */
01862     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT,threshold)){//!wireWriteDataByte(APDS9960_PILT, threshold) ) {
01863         return false;
01864     }
01865     
01866     return true;
01867 }
01868     
01869 /**
01870  * @brief Gets the high threshold for proximity interrupts
01871  *
01872  * @param[out] threshold current low threshold stored on the APDS-9960
01873  * @return True if operation successful. False otherwise.
01874  */
01875 bool glibr::getProximityIntHighThreshold(uint8_t &threshold)
01876 {
01877     threshold = 0;
01878     
01879     /* Read value from proximity low threshold register */
01880     threshold = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PIHT);
01881     if( threshold == ERROR){//!wireReadDataByte(APDS9960_PIHT, threshold) ) {
01882         return false;
01883     }
01884     
01885     return true;
01886 }
01887 
01888 /**
01889  * @brief Sets the high threshold for proximity interrupts
01890  *
01891  * @param[in] threshold high threshold value for interrupt to trigger
01892  * @return True if operation successful. False otherwise.
01893  */
01894 bool glibr::setProximityIntHighThreshold(uint8_t threshold)
01895 {
01896     
01897     /* Write threshold value to register */
01898     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PIHT,threshold)){//!wireWriteDataByte(APDS9960_PIHT, threshold) ) {
01899         return false;
01900     }
01901     
01902     return true;
01903 }
01904 
01905 /**
01906  * @brief Gets if ambient light interrupts are enabled or not
01907  *
01908  * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
01909  */
01910 uint8_t glibr::getAmbientLightIntEnable()
01911 {
01912     uint8_t val;
01913     
01914     /* Read value from ENABLE register */
01915     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
01916     if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
01917         return ERROR;
01918     }
01919     
01920     /* Shift and mask out AIEN bit */
01921     val = (val >> 4) & 0x01;//0b00000001;
01922     
01923     return val;
01924 }
01925 
01926 /**
01927  * @brief Turns ambient light interrupts on or off
01928  *
01929  * @param[in] enable 1 to enable interrupts, 0 to turn them off
01930  * @return True if operation successful. False otherwise.
01931  */
01932 bool glibr::setAmbientLightIntEnable(uint8_t enable)
01933 {
01934     uint8_t val;
01935     
01936     /* Read value from ENABLE register */
01937     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
01938     if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
01939         return false;
01940     }
01941     
01942     /* Set bits in register to given value */
01943     enable &= 0x01;//0b00000001;
01944     enable = enable << 4;
01945     val &= 0xEF;//0b11101111;
01946     val |= enable;
01947     
01948     /* Write register value back into ENABLE register */
01949     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
01950         return false;
01951     }
01952     
01953     return true;
01954 }
01955 
01956 /**
01957  * @brief Gets if proximity interrupts are enabled or not
01958  *
01959  * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
01960  */
01961 uint8_t glibr::getProximityIntEnable()
01962 {
01963     uint8_t val;
01964     
01965     /* Read value from ENABLE register */
01966     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
01967     if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
01968         return ERROR;
01969     }
01970     
01971     /* Shift and mask out PIEN bit */
01972     val = (val >> 5) & 0x01;//0b00000001;
01973     
01974     return val;
01975 }
01976 
01977 /**
01978  * @brief Turns proximity interrupts on or off
01979  *
01980  * @param[in] enable 1 to enable interrupts, 0 to turn them off
01981  * @return True if operation successful. False otherwise.
01982  */
01983 bool glibr::setProximityIntEnable(uint8_t enable)
01984 {
01985     uint8_t val;
01986     
01987     /* Read value from ENABLE register */
01988     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE);
01989     if( val == ERROR){//!wireReadDataByte(APDS9960_ENABLE, val) ) {
01990         return false;
01991     }
01992     
01993     /* Set bits in register to given value */
01994     enable &= 0x01;//0b00000001;
01995     enable = enable << 5;
01996     val &= 0xDF;//0b11011111;
01997     val |= enable;
01998     
01999     /* Write register value back into ENABLE register */
02000     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE,val)){//!wireWriteDataByte(APDS9960_ENABLE, val) ) {
02001         return false;
02002     }
02003     
02004     return true;
02005 }
02006 
02007 /**
02008  * @brief Gets if gesture interrupts are enabled or not
02009  *
02010  * @return 1 if interrupts are enabled, 0 if not. 0xFF on error.
02011  */
02012 uint8_t glibr::getGestureIntEnable()
02013 {
02014     uint8_t val;
02015     
02016     /* Read value from GCONF4 register */
02017     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
02018     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
02019         return ERROR;
02020     }
02021     
02022     /* Shift and mask out GIEN bit */
02023     val = (val >> 1) & 0x01;//0b00000001;
02024     
02025     return val;
02026 }
02027 
02028 /**
02029  * @brief Turns gesture-related interrupts on or off
02030  *
02031  * @param[in] enable 1 to enable interrupts, 0 to turn them off
02032  * @return True if operation successful. False otherwise.
02033  */
02034 bool glibr::setGestureIntEnable(uint8_t enable)
02035 {
02036     uint8_t val;
02037     
02038     /* Read value from GCONF4 register */
02039     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
02040     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
02041         return false;
02042     }
02043     
02044     /* Set bits in register to given value */
02045     enable &= 0x01;//0b00000001;
02046     enable = enable << 1;
02047     val &= 0xFD;//0b11111101;
02048     val |= enable;
02049     
02050     /* Write register value back into GCONF4 register */
02051     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
02052         return false;
02053     }
02054     
02055     return true;
02056 }
02057 
02058 /**
02059  * @brief Clears the ambient light interrupt
02060  *
02061  * @return True if operation completed successfully. False otherwise.
02062  */
02063 bool glibr::clearAmbientLightInt()
02064 {
02065     uint8_t throwaway;
02066     throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_AICLEAR);
02067     if( throwaway == ERROR){//!wireReadDataByte(APDS9960_AICLEAR, throwaway) ) {
02068         return false;
02069     }
02070     
02071     return true;
02072 }
02073 
02074 /**
02075  * @brief Clears the proximity interrupt
02076  *
02077  * @return True if operation completed successfully. False otherwise.
02078  */
02079 bool glibr::clearProximityInt()
02080 {
02081     uint8_t throwaway;
02082     throwaway = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_PICLEAR);
02083     if( throwaway == ERROR){//!wireReadDataByte(APDS9960_PICLEAR, throwaway) ) {
02084         return false;
02085     }
02086     
02087     return true;
02088 }
02089 
02090 /**
02091  * @brief Tells if the gesture state machine is currently running
02092  *
02093  * @return 1 if gesture state machine is running, 0 if not. 0xFF on error.
02094  */
02095 uint8_t glibr::getGestureMode()
02096 {
02097     uint8_t val;
02098     
02099     /* Read value from GCONF4 register */
02100     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
02101     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
02102         return ERROR;
02103     }
02104     
02105     /* Mask out GMODE bit */
02106     val &= 0x01;//0b00000001;
02107     
02108     return val;
02109 }
02110 
02111 /**
02112  * @brief Tells the state machine to either enter or exit gesture state machine
02113  *
02114  * @param[in] mode 1 to enter gesture state machine, 0 to exit.
02115  * @return True if operation successful. False otherwise.
02116  */
02117 bool glibr::setGestureMode(uint8_t mode)
02118 {
02119     uint8_t val;
02120     
02121     /* Read value from GCONF4 register */
02122     val = I2CreadByte(APDS9960_I2C_ADDR, APDS9960_GCONF4);
02123     if( val == ERROR){//!wireReadDataByte(APDS9960_GCONF4, val) ) {
02124         return false;
02125     }
02126     
02127     /* Set bits in register to given value */
02128     mode &= 0x01;//0b00000001;
02129     val &= 0xFE;//0b11111110;
02130     val |= mode;
02131     
02132     /* Write register value back into GCONF4 register */
02133     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_GCONF4,val)){//!wireWriteDataByte(APDS9960_GCONF4, val) ) {
02134         return false;
02135     }
02136     
02137     return true;
02138 }
02139 
02140 
02141 
02142 
02143 
02144 int glibr::I2CwriteByte(char address, char subAddress, char data)
02145 {   
02146     int ret;
02147     char cmd[2] = {subAddress, data};
02148     ret=i2c.write(address<<1, cmd, 2);  //if ret is 1, then not acked.
02149     return ret;
02150 }
02151 
02152 
02153 
02154 uint8_t glibr::I2CreadByte(char address, char subAddress)
02155 {
02156     char data; // store the register data
02157     
02158     if(i2c.write(address<<1, &subAddress, 1, true)){
02159         //std::cout<<"hello"<<std::endl;
02160         return ERROR;   //7 bit   //not acked
02161     } 
02162     if(i2c.read(address<<1, &data, 1)){    /////CHANGED THIS NEED TO TEST.
02163         //std::cout<<"aloha"<<std::endl;
02164         return ERROR;
02165     }
02166         
02167     //std::cout<<"Cheupei"<<std::endl;
02168     
02169     //i2c.read(address<<1, &data, 1);
02170     return data;
02171 
02172 }
02173 
02174 
02175 // * @brief Reads a block (array) of bytes from the I2C device and register
02176 // *
02177 // * @param[in] reg the register to read from
02178 // * @param[out] val pointer to the beginning of the data
02179 // * @param[in] len number of bytes to read
02180 // * @return Number of bytes read. -1 on read error.
02181 // */
02182 int glibr::I2CReadDataBlock(char address, char subAddress, char *data, unsigned int len)
02183 {
02184   //  unsigned char i = 0;
02185 
02186     /* Indicate which register we want to read from */
02187    
02188       if(i2c.write(address<<1, &subAddress, 1, true)){
02189         return -1;   //7 bit   //not acked
02190       } 
02191     
02192     /* Read block data */
02193      
02194     if(i2c.read(address<<1, data, len)){
02195         return -1;
02196     }
02197     
02198     return 1;
02199     //Wire.requestFrom(APDS9960_I2C_ADDR, len);
02200     /*while (Wire.available()) {
02201         if (i >= len) {
02202             return -1;
02203         }
02204         val[i] = Wire.read();
02205         i++;
02206     }*/
02207 }