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
glibr.cpp@0:437ae08befe3, 2015-03-05 (annotated)
- Committer:
- kbhagat6
- Date:
- Thu Mar 05 04:53:07 2015 +0000
- Revision:
- 0:437ae08befe3
- Child:
- 1:c7215f5f9a72
current
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kbhagat6 | 0:437ae08befe3 | 1 | #include "glibr.h" |
kbhagat6 | 0:437ae08befe3 | 2 | #include "mbed.h" |
kbhagat6 | 0:437ae08befe3 | 3 | |
kbhagat6 | 0:437ae08befe3 | 4 | |
kbhagat6 | 0:437ae08befe3 | 5 | |
kbhagat6 | 0:437ae08befe3 | 6 | |
kbhagat6 | 0:437ae08befe3 | 7 | glibr::glibr(PinName sda, PinName scl):i2c(sda, scl){ |
kbhagat6 | 0:437ae08befe3 | 8 | |
kbhagat6 | 0:437ae08befe3 | 9 | } |
kbhagat6 | 0:437ae08befe3 | 10 | |
kbhagat6 | 0:437ae08befe3 | 11 | glibr::~glibr(){ |
kbhagat6 | 0:437ae08befe3 | 12 | |
kbhagat6 | 0:437ae08befe3 | 13 | } |
kbhagat6 | 0:437ae08befe3 | 14 | |
kbhagat6 | 0:437ae08befe3 | 15 | uint8_t glibr::ginit(){ |
kbhagat6 | 0:437ae08befe3 | 16 | uint8_t id; |
kbhagat6 | 0:437ae08befe3 | 17 | |
kbhagat6 | 0:437ae08befe3 | 18 | // if( !wireReadDataByte(APDS9960_ID, id) ) { |
kbhagat6 | 0:437ae08befe3 | 19 | |
kbhagat6 | 0:437ae08befe3 | 20 | id=I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ID); |
kbhagat6 | 0:437ae08befe3 | 21 | |
kbhagat6 | 0:437ae08befe3 | 22 | if( (!(id == APDS9960_ID_1 || id == APDS9960_ID_2))||id==ERROR) { |
kbhagat6 | 0:437ae08befe3 | 23 | return false; |
kbhagat6 | 0:437ae08befe3 | 24 | } |
kbhagat6 | 0:437ae08befe3 | 25 | |
kbhagat6 | 0:437ae08befe3 | 26 | if(!setMode(ALL, OFF)) { |
kbhagat6 | 0:437ae08befe3 | 27 | return false; |
kbhagat6 | 0:437ae08befe3 | 28 | } |
kbhagat6 | 0:437ae08befe3 | 29 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ATIME, DEFAULT_ATIME)){ |
kbhagat6 | 0:437ae08befe3 | 30 | return false; |
kbhagat6 | 0:437ae08befe3 | 31 | } |
kbhagat6 | 0:437ae08befe3 | 32 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_WTIME, DEFAULT_WTIME)){ |
kbhagat6 | 0:437ae08befe3 | 33 | return false; |
kbhagat6 | 0:437ae08befe3 | 34 | } |
kbhagat6 | 0:437ae08befe3 | 35 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_PPULSE, DEFAULT_PROX_PPULSE)){ |
kbhagat6 | 0:437ae08befe3 | 36 | return false; |
kbhagat6 | 0:437ae08befe3 | 37 | } |
kbhagat6 | 0:437ae08befe3 | 38 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_UR, DEFAULT_POFFSET_UR)){ |
kbhagat6 | 0:437ae08befe3 | 39 | return false; |
kbhagat6 | 0:437ae08befe3 | 40 | } |
kbhagat6 | 0:437ae08befe3 | 41 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_POFFSET_DL, DEFAULT_POFFSET_DL)){ |
kbhagat6 | 0:437ae08befe3 | 42 | return false; |
kbhagat6 | 0:437ae08befe3 | 43 | } |
kbhagat6 | 0:437ae08befe3 | 44 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONFIG1, DEFAULT_CONFIG1)){ |
kbhagat6 | 0:437ae08befe3 | 45 | return false; |
kbhagat6 | 0:437ae08befe3 | 46 | } |
kbhagat6 | 0:437ae08befe3 | 47 | |
kbhagat6 | 0:437ae08befe3 | 48 | if( !setLEDDrive(DEFAULT_LDRIVE) ) { |
kbhagat6 | 0:437ae08befe3 | 49 | return false; |
kbhagat6 | 0:437ae08befe3 | 50 | } |
kbhagat6 | 0:437ae08befe3 | 51 | |
kbhagat6 | 0:437ae08befe3 | 52 | if( !setProximityGain(DEFAULT_PGAIN) ) { |
kbhagat6 | 0:437ae08befe3 | 53 | return false; |
kbhagat6 | 0:437ae08befe3 | 54 | } |
kbhagat6 | 0:437ae08befe3 | 55 | if( !setAmbientLightGain(DEFAULT_AGAIN) ) { |
kbhagat6 | 0:437ae08befe3 | 56 | return false; |
kbhagat6 | 0:437ae08befe3 | 57 | } |
kbhagat6 | 0:437ae08befe3 | 58 | if( !setProxIntLowThresh(DEFAULT_PILT) ) { |
kbhagat6 | 0:437ae08befe3 | 59 | return false; |
kbhagat6 | 0:437ae08befe3 | 60 | } |
kbhagat6 | 0:437ae08befe3 | 61 | |
kbhagat6 | 0:437ae08befe3 | 62 | |
kbhagat6 | 0:437ae08befe3 | 63 | |
kbhagat6 | 0:437ae08befe3 | 64 | |
kbhagat6 | 0:437ae08befe3 | 65 | |
kbhagat6 | 0:437ae08befe3 | 66 | return id; |
kbhagat6 | 0:437ae08befe3 | 67 | } |
kbhagat6 | 0:437ae08befe3 | 68 | |
kbhagat6 | 0:437ae08befe3 | 69 | |
kbhagat6 | 0:437ae08befe3 | 70 | |
kbhagat6 | 0:437ae08befe3 | 71 | |
kbhagat6 | 0:437ae08befe3 | 72 | /** |
kbhagat6 | 0:437ae08befe3 | 73 | * @brief Enables or disables a feature in the APDS-9960 |
kbhagat6 | 0:437ae08befe3 | 74 | * |
kbhagat6 | 0:437ae08befe3 | 75 | * @param[in] mode which feature to enable |
kbhagat6 | 0:437ae08befe3 | 76 | * @param[in] enable ON (1) or OFF (0) |
kbhagat6 | 0:437ae08befe3 | 77 | * @return True if operation success. False otherwise. |
kbhagat6 | 0:437ae08befe3 | 78 | */ |
kbhagat6 | 0:437ae08befe3 | 79 | bool glibr::setMode(uint8_t mode, uint8_t enable) |
kbhagat6 | 0:437ae08befe3 | 80 | { |
kbhagat6 | 0:437ae08befe3 | 81 | uint8_t reg_val; |
kbhagat6 | 0:437ae08befe3 | 82 | |
kbhagat6 | 0:437ae08befe3 | 83 | /* Read current ENABLE register */ |
kbhagat6 | 0:437ae08befe3 | 84 | reg_val = getMode(); |
kbhagat6 | 0:437ae08befe3 | 85 | if( reg_val == ERROR ) { |
kbhagat6 | 0:437ae08befe3 | 86 | return false; |
kbhagat6 | 0:437ae08befe3 | 87 | } |
kbhagat6 | 0:437ae08befe3 | 88 | |
kbhagat6 | 0:437ae08befe3 | 89 | /* Change bit(s) in ENABLE register */ |
kbhagat6 | 0:437ae08befe3 | 90 | enable = enable & 0x01; |
kbhagat6 | 0:437ae08befe3 | 91 | if( mode >= 0 && mode <= 6 ) { |
kbhagat6 | 0:437ae08befe3 | 92 | if (enable) { |
kbhagat6 | 0:437ae08befe3 | 93 | reg_val |= (1 << mode); |
kbhagat6 | 0:437ae08befe3 | 94 | } else { |
kbhagat6 | 0:437ae08befe3 | 95 | reg_val &= ~(1 << mode); |
kbhagat6 | 0:437ae08befe3 | 96 | } |
kbhagat6 | 0:437ae08befe3 | 97 | } else if( mode == ALL ) { |
kbhagat6 | 0:437ae08befe3 | 98 | if (enable) { |
kbhagat6 | 0:437ae08befe3 | 99 | reg_val = 0x7F; |
kbhagat6 | 0:437ae08befe3 | 100 | } else { |
kbhagat6 | 0:437ae08befe3 | 101 | reg_val = 0x00; |
kbhagat6 | 0:437ae08befe3 | 102 | } |
kbhagat6 | 0:437ae08befe3 | 103 | } |
kbhagat6 | 0:437ae08befe3 | 104 | |
kbhagat6 | 0:437ae08befe3 | 105 | /* Write value back to ENABLE register */ |
kbhagat6 | 0:437ae08befe3 | 106 | if(I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_ENABLE, reg_val)){ |
kbhagat6 | 0:437ae08befe3 | 107 | return false; |
kbhagat6 | 0:437ae08befe3 | 108 | } |
kbhagat6 | 0:437ae08befe3 | 109 | |
kbhagat6 | 0:437ae08befe3 | 110 | |
kbhagat6 | 0:437ae08befe3 | 111 | |
kbhagat6 | 0:437ae08befe3 | 112 | return true; |
kbhagat6 | 0:437ae08befe3 | 113 | } |
kbhagat6 | 0:437ae08befe3 | 114 | |
kbhagat6 | 0:437ae08befe3 | 115 | uint8_t glibr::getMode() |
kbhagat6 | 0:437ae08befe3 | 116 | { |
kbhagat6 | 0:437ae08befe3 | 117 | return I2CreadByte(APDS9960_I2C_ADDR, APDS9960_ENABLE); |
kbhagat6 | 0:437ae08befe3 | 118 | } |
kbhagat6 | 0:437ae08befe3 | 119 | |
kbhagat6 | 0:437ae08befe3 | 120 | bool glibr::setLEDDrive(uint8_t drive) |
kbhagat6 | 0:437ae08befe3 | 121 | { |
kbhagat6 | 0:437ae08befe3 | 122 | uint8_t val; |
kbhagat6 | 0:437ae08befe3 | 123 | |
kbhagat6 | 0:437ae08befe3 | 124 | /* Read value from CONTROL register */ |
kbhagat6 | 0:437ae08befe3 | 125 | |
kbhagat6 | 0:437ae08befe3 | 126 | val=I2CreadByte(APDS9960_I2C_ADDR,APDS9960_CONTROL); |
kbhagat6 | 0:437ae08befe3 | 127 | if(val==ERROR){ |
kbhagat6 | 0:437ae08befe3 | 128 | return false; |
kbhagat6 | 0:437ae08befe3 | 129 | } |
kbhagat6 | 0:437ae08befe3 | 130 | /* Set bits in register to given value */ |
kbhagat6 | 0:437ae08befe3 | 131 | drive &= 0x03; |
kbhagat6 | 0:437ae08befe3 | 132 | drive = drive << 6; |
kbhagat6 | 0:437ae08befe3 | 133 | //val &= 0b00111111; |
kbhagat6 | 0:437ae08befe3 | 134 | val &= 0x3F; |
kbhagat6 | 0:437ae08befe3 | 135 | val |= drive; |
kbhagat6 | 0:437ae08befe3 | 136 | |
kbhagat6 | 0:437ae08befe3 | 137 | /* Write register value back into CONTROL register */ |
kbhagat6 | 0:437ae08befe3 | 138 | if( I2CwriteByte(APDS9960_I2C_ADDR,APDS9960_CONTROL, val) ) { |
kbhagat6 | 0:437ae08befe3 | 139 | return false; |
kbhagat6 | 0:437ae08befe3 | 140 | } |
kbhagat6 | 0:437ae08befe3 | 141 | |
kbhagat6 | 0:437ae08befe3 | 142 | return true; |
kbhagat6 | 0:437ae08befe3 | 143 | } |
kbhagat6 | 0:437ae08befe3 | 144 | |
kbhagat6 | 0:437ae08befe3 | 145 | bool glibr::setProximityGain(uint8_t drive) |
kbhagat6 | 0:437ae08befe3 | 146 | { |
kbhagat6 | 0:437ae08befe3 | 147 | uint8_t val; |
kbhagat6 | 0:437ae08befe3 | 148 | |
kbhagat6 | 0:437ae08befe3 | 149 | /* Read value from CONTROL register */ |
kbhagat6 | 0:437ae08befe3 | 150 | if( !wireReadDataByte(APDS9960_CONTROL, val) ) { |
kbhagat6 | 0:437ae08befe3 | 151 | return false; |
kbhagat6 | 0:437ae08befe3 | 152 | } |
kbhagat6 | 0:437ae08befe3 | 153 | |
kbhagat6 | 0:437ae08befe3 | 154 | /* Set bits in register to given value */ |
kbhagat6 | 0:437ae08befe3 | 155 | drive &= 0b00000011; |
kbhagat6 | 0:437ae08befe3 | 156 | drive = drive << 2; |
kbhagat6 | 0:437ae08befe3 | 157 | val &= 0b11110011; |
kbhagat6 | 0:437ae08befe3 | 158 | val |= drive; |
kbhagat6 | 0:437ae08befe3 | 159 | |
kbhagat6 | 0:437ae08befe3 | 160 | /* Write register value back into CONTROL register */ |
kbhagat6 | 0:437ae08befe3 | 161 | if( !wireWriteDataByte(APDS9960_CONTROL, val) ) { |
kbhagat6 | 0:437ae08befe3 | 162 | return false; |
kbhagat6 | 0:437ae08befe3 | 163 | } |
kbhagat6 | 0:437ae08befe3 | 164 | |
kbhagat6 | 0:437ae08befe3 | 165 | return true; |
kbhagat6 | 0:437ae08befe3 | 166 | } |
kbhagat6 | 0:437ae08befe3 | 167 | |
kbhagat6 | 0:437ae08befe3 | 168 | |
kbhagat6 | 0:437ae08befe3 | 169 | |
kbhagat6 | 0:437ae08befe3 | 170 | |
kbhagat6 | 0:437ae08befe3 | 171 | int glibr::I2CwriteByte(char address, char subAddress, char data) |
kbhagat6 | 0:437ae08befe3 | 172 | { |
kbhagat6 | 0:437ae08befe3 | 173 | int ret; |
kbhagat6 | 0:437ae08befe3 | 174 | char cmd[2] = {subAddress, data}; |
kbhagat6 | 0:437ae08befe3 | 175 | ret=i2c.write(address<<1, cmd, 2); //if ret is 1, then not acked. |
kbhagat6 | 0:437ae08befe3 | 176 | return ret; |
kbhagat6 | 0:437ae08befe3 | 177 | } |
kbhagat6 | 0:437ae08befe3 | 178 | |
kbhagat6 | 0:437ae08befe3 | 179 | uint8_t glibr::I2CreadByte(char address, char subAddress) |
kbhagat6 | 0:437ae08befe3 | 180 | { |
kbhagat6 | 0:437ae08befe3 | 181 | char data; // store the register data |
kbhagat6 | 0:437ae08befe3 | 182 | |
kbhagat6 | 0:437ae08befe3 | 183 | if(i2c.write(address<<1, &subAddress, 1, true)){ |
kbhagat6 | 0:437ae08befe3 | 184 | return ERROR; //7 bit //not acked |
kbhagat6 | 0:437ae08befe3 | 185 | } |
kbhagat6 | 0:437ae08befe3 | 186 | i2c.read(address<<1, &data, 1); |
kbhagat6 | 0:437ae08befe3 | 187 | return data; |
kbhagat6 | 0:437ae08befe3 | 188 | |
kbhagat6 | 0:437ae08befe3 | 189 | } |