APDS-9130 library

Committer:
sleighton
Date:
Mon Jan 04 12:28:26 2016 +0000
Revision:
0:10fe9a5a884f
Child:
1:159407640167
Working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sleighton 0:10fe9a5a884f 1 #include "Prox.h"
sleighton 0:10fe9a5a884f 2
sleighton 0:10fe9a5a884f 3 Prox::Prox(PinName sda, PinName scl) {
sleighton 0:10fe9a5a884f 4 _sda = sda;
sleighton 0:10fe9a5a884f 5 _scl = scl;
sleighton 0:10fe9a5a884f 6
sleighton 0:10fe9a5a884f 7 //WriteByte(0x72,0xE5); // Clear interrupt
sleighton 0:10fe9a5a884f 8
sleighton 0:10fe9a5a884f 9 char PTIME;
sleighton 0:10fe9a5a884f 10 char WTIME;
sleighton 0:10fe9a5a884f 11 char PPULSE;
sleighton 0:10fe9a5a884f 12
sleighton 0:10fe9a5a884f 13 WTIME = 0xff; // 2.7 ms - minimum Wait time
sleighton 0:10fe9a5a884f 14 PTIME = 0xff; // 2.7 ms - minimum Prox integration time
sleighton 0:10fe9a5a884f 15 PPULSE = 1; // Minimum prox pulse count
sleighton 0:10fe9a5a884f 16
sleighton 0:10fe9a5a884f 17 WriteByte(0x72, 0, 0); //Disable and Powerdown
sleighton 0:10fe9a5a884f 18 WriteByte (0x72, 2, PTIME);
sleighton 0:10fe9a5a884f 19 WriteByte (0x72, 3, WTIME);
sleighton 0:10fe9a5a884f 20 WriteByte (0x72, 0xe, PPULSE);
sleighton 0:10fe9a5a884f 21
sleighton 0:10fe9a5a884f 22 char PDRIVE;
sleighton 0:10fe9a5a884f 23 char PDIODE;
sleighton 0:10fe9a5a884f 24 char PGAIN;
sleighton 0:10fe9a5a884f 25
sleighton 0:10fe9a5a884f 26 PDRIVE = 0; //100mA of LED Power
sleighton 0:10fe9a5a884f 27 PDIODE = 0x20; // CH1 Diode
sleighton 0:10fe9a5a884f 28 PGAIN = 0x08; //4x Prox gain
sleighton 0:10fe9a5a884f 29
sleighton 0:10fe9a5a884f 30 WriteByte (0x72, 0xf, PDRIVE | PDIODE | PGAIN );
sleighton 0:10fe9a5a884f 31
sleighton 0:10fe9a5a884f 32 char WEN, PEN, PON, PIEN;
sleighton 0:10fe9a5a884f 33 WEN = 8; // Enable Wait
sleighton 0:10fe9a5a884f 34 PIEN = 5; //Enable interrupt
sleighton 0:10fe9a5a884f 35 PEN = 4; // Enable Prox
sleighton 0:10fe9a5a884f 36 PON = 1; // Enable Power On
sleighton 0:10fe9a5a884f 37
sleighton 0:10fe9a5a884f 38 WriteByte (0x72, 0, WEN | PIEN | PEN | PON);
sleighton 0:10fe9a5a884f 39
sleighton 0:10fe9a5a884f 40 char PIHTL, PIHTH; //Upper interrupt threshold low and high bytes
sleighton 0:10fe9a5a884f 41
sleighton 0:10fe9a5a884f 42 PIHTL = 0;
sleighton 0:10fe9a5a884f 43 PIHTH = 0x80; //trigger at 256
sleighton 0:10fe9a5a884f 44
sleighton 0:10fe9a5a884f 45 WriteByte (0x72, 0x0A, PIHTL);
sleighton 0:10fe9a5a884f 46 WriteByte (0x72, 0x0B, PIHTH);
sleighton 0:10fe9a5a884f 47
sleighton 0:10fe9a5a884f 48 wait(0.12); //Wait for 120 ms
sleighton 0:10fe9a5a884f 49 }
sleighton 0:10fe9a5a884f 50
sleighton 0:10fe9a5a884f 51 uint8_t Prox::readProx(){
sleighton 0:10fe9a5a884f 52 WriteByte (0x72, 0xE5); //clear interrupt pin (untested)
sleighton 0:10fe9a5a884f 53 uint8_t prox;
sleighton 0:10fe9a5a884f 54 ReadWord(0x72, 0x18, &prox);
sleighton 0:10fe9a5a884f 55 return prox;
sleighton 0:10fe9a5a884f 56 }
sleighton 0:10fe9a5a884f 57
sleighton 0:10fe9a5a884f 58
sleighton 0:10fe9a5a884f 59
sleighton 0:10fe9a5a884f 60
sleighton 0:10fe9a5a884f 61
sleighton 0:10fe9a5a884f 62 // Read a byte on the i2c interface
sleighton 0:10fe9a5a884f 63
sleighton 0:10fe9a5a884f 64 void Prox::ReadWord(char addr, char reg, uint8_t *data) {
sleighton 0:10fe9a5a884f 65 I2C i2c(_sda,_scl);
sleighton 0:10fe9a5a884f 66 char reg1 = 0xA0 | reg;
sleighton 0:10fe9a5a884f 67 char reading[2];
sleighton 0:10fe9a5a884f 68 i2c.write(addr,&reg1,1);
sleighton 0:10fe9a5a884f 69 i2c.read(addr,reading,2);
sleighton 0:10fe9a5a884f 70 *data = reading[0] | (reading[1]<<8);
sleighton 0:10fe9a5a884f 71 wait(0.07);
sleighton 0:10fe9a5a884f 72 }
sleighton 0:10fe9a5a884f 73
sleighton 0:10fe9a5a884f 74 void Prox::ReadByte(char addr, char reg, char *bytedata) {
sleighton 0:10fe9a5a884f 75 I2C i2c(_sda,_scl);
sleighton 0:10fe9a5a884f 76 char tempdata;
sleighton 0:10fe9a5a884f 77 char reg1 = 0x80 | reg;
sleighton 0:10fe9a5a884f 78 i2c.write(addr,&reg1,1);
sleighton 0:10fe9a5a884f 79 i2c.read(addr,&tempdata,1);
sleighton 0:10fe9a5a884f 80 *bytedata = tempdata;
sleighton 0:10fe9a5a884f 81 wait(0.07);
sleighton 0:10fe9a5a884f 82 }
sleighton 0:10fe9a5a884f 83
sleighton 0:10fe9a5a884f 84
sleighton 0:10fe9a5a884f 85
sleighton 0:10fe9a5a884f 86 // Write a byte on the i2c interface
sleighton 0:10fe9a5a884f 87
sleighton 0:10fe9a5a884f 88 void Prox::WriteByte(uint8_t addr, uint8_t reg, char data) {
sleighton 0:10fe9a5a884f 89 I2C i2c(_sda,_scl);
sleighton 0:10fe9a5a884f 90 char data1[2];
sleighton 0:10fe9a5a884f 91 data1[0] = 0x80 | reg;
sleighton 0:10fe9a5a884f 92 data1[1] = data;
sleighton 0:10fe9a5a884f 93 i2c.write(addr,data1,2);
sleighton 0:10fe9a5a884f 94 wait(0.07);
sleighton 0:10fe9a5a884f 95
sleighton 0:10fe9a5a884f 96 }
sleighton 0:10fe9a5a884f 97
sleighton 0:10fe9a5a884f 98 void Prox::WriteByte(uint8_t addr, uint8_t reg) {
sleighton 0:10fe9a5a884f 99 I2C i2c(_sda,_scl);
sleighton 0:10fe9a5a884f 100 char data1;
sleighton 0:10fe9a5a884f 101 data1 = 0x80 | reg;
sleighton 0:10fe9a5a884f 102 i2c.write(addr,&data1,1);
sleighton 0:10fe9a5a884f 103 wait(0.07);
sleighton 0:10fe9a5a884f 104
sleighton 0:10fe9a5a884f 105 }