Uses the APDS_9960 Digital Proximity, Ambient Light, RGB and Gesture Sensor library to play detected gesture sounds on a speaker from the SDcard

Dependencies:   mbed SDFileSystem wave_player

Files at this revision

API Documentation at this revision

Comitter:
kbhagat6
Date:
Fri Mar 06 02:06:15 2015 +0000
Parent:
4:84545b0e63a9
Child:
6:687dff16904e
Commit message:
added more functions.. need to look readgesture function

Changed in this revision

glibr.cpp Show annotated file Show diff for this revision Revisions of this file
glibr.h Show annotated file Show diff for this revision Revisions of this file
--- a/glibr.cpp	Thu Mar 05 22:42:17 2015 +0000
+++ b/glibr.cpp	Fri Mar 06 02:06:15 2015 +0000
@@ -375,7 +375,7 @@
     
     /* Read value from GSTATUS register */
     val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_GSTATUS);
-    if( val==ERROR) ) {
+    if( val==ERROR) {
         return ERROR;
     }
     
@@ -408,7 +408,7 @@
     while(1) {
     
         /* Wait some time to collect next batch of FIFO data */
-        delay(FIFO_PAUSE_TIME);
+        wait(FIFO_PAUSE_TIME);
         
         /* Get the contents of the STATUS register. Is data still valid? */
         
@@ -431,8 +431,9 @@
 //#endif
 
             /* If there's stuff in the FIFO, read it into our data block */                 //NEED TO FIGURE OUT WHAT THIS IS DOING.
+        
             if( fifo_level > 0) {
-                bytes_read = wireReadDataBlock(  APDS9960_GFIFO_U, 
+                bytes_read = I2CReadDataBlock(APDS9960_I2C_ADDR,APDS9960_GFIFO_U, 
                                                 (uint8_t*)fifo_data, 
                                                 (fifo_level * 4) );
                 if( bytes_read == -1 ) {
@@ -463,12 +464,12 @@
                     }
                     
 #if DEBUG
-                Serial.print("Up Data: ");
+               // Serial.print("Up Data: ");
                 for ( i = 0; i < gesture_data_.total_gestures; i++ ) {
-                    Serial.print(gesture_data_.u_data[i]);
-                    Serial.print(" ");
+                 //   Serial.print(gesture_data_.u_data[i]);
+                  //  Serial.print(" ");
                 }
-                Serial.println();
+              //  Serial.println();
 #endif
 
                     /* Filter and process gesture data. Decode near/far state */
@@ -489,12 +490,12 @@
         } else {
     
             /* Determine best guessed gesture and clean up */
-            delay(FIFO_PAUSE_TIME);
+            wait(FIFO_PAUSE_TIME);
             decodeGesture();
             motion = gesture_motion_;
 #if DEBUG
-            Serial.print("END: ");
-            Serial.println(gesture_motion_);
+        //    Serial.print("END: ");
+        //    Serial.println(gesture_motion_);
 #endif
             resetGestureParameters();
             return motion;
@@ -508,7 +509,7 @@
     
     /* Read value from CONTROL register */
    
-    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
+    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
     
     if(val==ERROR){
         return false;   
@@ -534,7 +535,7 @@
     
     /* Read value from CONTROL register */
    
-    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val);
+    val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL);
     
     if(val==ERROR){
         return false;   
@@ -554,7 +555,7 @@
 }
 
 
-bool glibr::setProxIntLowThresh(uint8_t threshold)
+bool setProxIntLowThresh(uint8_t threshold)
 {
     if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PILT, threshold) ) {
         return false;
@@ -577,18 +578,6 @@
 
 
 
-
-
-
-int glibr::I2CwriteByte(char address, char subAddress, char data)
-{   
-    int ret;
-    char cmd[2] = {subAddress, data};
-    ret=i2c.write(address<<1, cmd, 2);  //if ret is 1, then not acked.
-    return ret;
-}
-
-
 bool glibr::setLightIntLowThreshold(uint16_t threshold)
 {
     uint8_t val_low;
@@ -611,6 +600,20 @@
     return true;
 }
 
+
+
+
+
+int glibr::I2CwriteByte(char address, char subAddress, char data)
+{   
+    int ret;
+    char cmd[2] = {subAddress, data};
+    ret=i2c.write(address<<1, cmd, 2);  //if ret is 1, then not acked.
+    return ret;
+}
+
+
+
 uint8_t glibr::I2CreadByte(char address, char subAddress)
 {
     char data; // store the register data
@@ -622,3 +625,36 @@
     return data;
 
 }
+
+/**
+ * @brief Reads a block (array) of bytes from the I2C device and register
+ *
+ * @param[in] reg the register to read from
+ * @param[out] val pointer to the beginning of the data
+ * @param[in] len number of bytes to read
+ * @return Number of bytes read. -1 on read error.
+ */
+int glibr::I2CReadDataBlock(char address, char subAddress, unsigned int len)
+{
+  //  unsigned char i = 0;
+    char *data
+    /* Indicate which register we want to read from */
+   
+      if(i2c.write(address<<1, subAddress, 1, true)){
+        return -1;   //7 bit   //not acked
+    } 
+    
+    /* Read block data */
+    i2c.read(address<<1, &data, len);
+    
+    //Wire.requestFrom(APDS9960_I2C_ADDR, len);
+    /*while (Wire.available()) {
+        if (i >= len) {
+            return -1;
+        }
+        val[i] = Wire.read();
+        i++;
+    }*/
+
+    return data;
+}
\ No newline at end of file
--- a/glibr.h	Thu Mar 05 22:42:17 2015 +0000
+++ b/glibr.h	Fri Mar 06 02:06:15 2015 +0000
@@ -73,14 +73,14 @@
 #define APDS9960_GFIFO_R        0xFF
 
 /* Bit fields */
-#define APDS9960_PON            0b00000001
-#define APDS9960_AEN            0b00000010
-#define APDS9960_PEN            0b00000100
-#define APDS9960_WEN            0b00001000
-#define APSD9960_AIEN           0b00010000
-#define APDS9960_PIEN           0b00100000
-#define APDS9960_GEN            0b01000000
-#define APDS9960_GVALID         0b00000001
+#define APDS9960_PON            0x01
+#define APDS9960_AEN            0x02
+#define APDS9960_PEN            0x04
+#define APDS9960_WEN            0x08
+#define APSD9960_AIEN           0x10
+#define APDS9960_PIEN           0x20
+#define APDS9960_GEN            0x40
+#define APDS9960_GVALID         0x01
 
 /* On/Off definitions */
 #define OFF                     0
@@ -165,6 +165,37 @@
 #define DEFAULT_GCONF3          0       // All photodiodes active during gesture
 #define DEFAULT_GIEN            0       // Disable gesture interrupts
 
+/* Direction definitions */
+enum {
+  DIR_NONE,
+  DIR_LEFT,
+  DIR_RIGHT,
+  DIR_UP,
+  DIR_DOWN,
+  DIR_NEAR,
+  DIR_FAR,
+  DIR_ALL
+};
+
+/* State definitions */
+enum {
+  NA_STATE,
+  NEAR_STATE,
+  FAR_STATE,
+  ALL_STATE
+};
+
+/* Container for gesture data */
+typedef struct gesture_data_type {
+    uint8_t u_data[32];
+    uint8_t d_data[32];
+    uint8_t l_data[32];
+    uint8_t r_data[32];
+    uint8_t index;
+    uint8_t total_gestures;
+    uint8_t in_threshold;
+    uint8_t out_threshold;
+} gesture_data_type;
 
 
 class glibr{
@@ -177,31 +208,126 @@
 
     
     uint8_t ginit();
-    bool setMode(uint8_t mode, uint8_t enable);
+    /* Initialization methods */
+    
     uint8_t getMode();
-    bool setLEDDrive(uint8_t drive); 
-    bool setProximityGain(uint8_t drive);
-     uint8_t getProximityGain();
+    bool setMode(uint8_t mode, uint8_t enable);
+    
+    /* Turn the APDS-9960 on and off */
+    bool enablePower();
+    bool disablePower();
+    
+    /* Enable or disable specific sensors */
+    bool enableLightSensor(bool interrupts = false);
+    bool disableLightSensor();
+    bool enableProximitySensor(bool interrupts = false);
+    bool disableProximitySensor();
+    bool enableGestureSensor(bool interrupts = true);
+    bool disableGestureSensor();
+    
+    /* LED drive strength control */
+    uint8_t getLEDDrive();
+    bool setLEDDrive(uint8_t drive);
+    uint8_t getGestureLEDDrive();
+    bool setGestureLEDDrive(uint8_t drive);
+    
+    /* Gain control */
     uint8_t getAmbientLightGain();
-    bool setAmbientLightGain(uint8_t drive);
+    bool setAmbientLightGain(uint8_t gain);
+    uint8_t getProximityGain();
+    bool setProximityGain(uint8_t gain);
     uint8_t getGestureGain();
     bool setGestureGain(uint8_t gain);
     
-    bool enableLightSensor(bool interrupts);
-    bool disableLightSensor();
-    bool enableProximitySensor(bool interrupts);
-    bool disableProximitySensor();
+    /* Get and set light interrupt thresholds */
+    bool getLightIntLowThreshold(uint16_t &threshold);
+    bool setLightIntLowThreshold(uint16_t threshold);
+    bool getLightIntHighThreshold(uint16_t &threshold);
+    bool setLightIntHighThreshold(uint16_t threshold);
+    
+    /* Get and set proximity interrupt thresholds */
+    bool getProximityIntLowThreshold(uint8_t &threshold);
+    bool setProximityIntLowThreshold(uint8_t threshold);
+    bool getProximityIntHighThreshold(uint8_t &threshold);
+    bool setProximityIntHighThreshold(uint8_t threshold);
     
-    bool enableGestureSensor(bool interrupts);
-    bool disableGestureSensor();
+    /* Get and set interrupt enables */
+    uint8_t getAmbientLightIntEnable();
+    bool setAmbientLightIntEnable(uint8_t enable);
+    uint8_t getProximityIntEnable();
+    bool setProximityIntEnable(uint8_t enable);
+    uint8_t getGestureIntEnable();
+    bool setGestureIntEnable(uint8_t enable);
+    
+    /* Clear interrupts */
+    bool clearAmbientLightInt();
+    bool clearProximityInt();
+    
+    /* Ambient light methods */
+    bool readAmbientLight(uint16_t &val);
+    bool readRedLight(uint16_t &val);
+    bool readGreenLight(uint16_t &val);
+    bool readBlueLight(uint16_t &val);
+    
+    /* Proximity methods */
+    bool readProximity(uint8_t &val);
+    
+    /* Gesture methods */
     bool isGestureAvailable();
     int readGesture();
      
     private:  
+    /* Gesture processing */
+    void resetGestureParameters();
+    bool processGestureData();
+    bool decodeGesture();
+
+    /* Proximity Interrupt Threshold */
+    uint8_t getProxIntLowThresh();
+    bool setProxIntLowThresh(uint8_t threshold);
+    uint8_t getProxIntHighThresh();
+    bool setProxIntHighThresh(uint8_t threshold);
+    
+    /* LED Boost Control */
+    uint8_t getLEDBoost();
+    bool setLEDBoost(uint8_t boost);
+    
+    /* Proximity photodiode select */
+    uint8_t getProxGainCompEnable();
+    bool setProxGainCompEnable(uint8_t enable);
+    uint8_t getProxPhotoMask();
+    bool setProxPhotoMask(uint8_t mask);
+    
+    /* Gesture threshold control */
+    uint8_t getGestureEnterThresh();
+    bool setGestureEnterThresh(uint8_t threshold);
+    uint8_t getGestureExitThresh();
+    bool setGestureExitThresh(uint8_t threshold);
+    
+    /* Gesture LED, gain, and time control */
+    uint8_t getGestureWaitTime();
+    bool setGestureWaitTime(uint8_t time);
+    
+    /* Gesture mode */
+    uint8_t getGestureMode();
+    bool setGestureMode(uint8_t mode);
+    
+    /* Members */
+    gesture_data_type gesture_data_;  //instanciation from struct 
+    int gesture_ud_delta_;
+    int gesture_lr_delta_;
+    int gesture_ud_count_;
+    int gesture_lr_count_;
+    int gesture_near_count_;
+    int gesture_far_count_;
+    int gesture_state_;
+    int gesture_motion_;
+    
+    
+    
     uint8_t I2CreadByte(char address, char subAddress);
     int I2CwriteByte(char address, char subAddress, char data); 
-    bool setProxIntLowThresh(uint8_t threshold);
-    bool setProxIntHighThresh(uint8_t threshold);
+    int I2CReadDataBlock(uint8_t reg, uint8_t *val, unsigned int len);
     I2C i2c;
     
 };