APDS-9130 library
Prox.cpp@1:159407640167, 2016-01-30 (annotated)
- Committer:
- sleighton
- Date:
- Sat Jan 30 20:33:17 2016 +0000
- Revision:
- 1:159407640167
- Parent:
- 0:10fe9a5a884f
- Child:
- 2:9db88ca586db
no changes that I know of
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:159407640167 | 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 | 1:159407640167 | 22 | char PIHTL, PIHTH; //Upper interrupt threshold low and high bytes |
sleighton | 1:159407640167 | 23 | |
sleighton | 1:159407640167 | 24 | PIHTL = 0xFF; |
sleighton | 1:159407640167 | 25 | PIHTH = 0x01; //trigger at 256 |
sleighton | 1:159407640167 | 26 | |
sleighton | 1:159407640167 | 27 | WriteByte (0x72, 0x0A, PIHTL); |
sleighton | 1:159407640167 | 28 | WriteByte (0x72, 0x0B, PIHTH); |
sleighton | 1:159407640167 | 29 | |
sleighton | 0:10fe9a5a884f | 30 | char PDRIVE; |
sleighton | 0:10fe9a5a884f | 31 | char PDIODE; |
sleighton | 0:10fe9a5a884f | 32 | char PGAIN; |
sleighton | 0:10fe9a5a884f | 33 | |
sleighton | 0:10fe9a5a884f | 34 | PDRIVE = 0; //100mA of LED Power |
sleighton | 0:10fe9a5a884f | 35 | PDIODE = 0x20; // CH1 Diode |
sleighton | 0:10fe9a5a884f | 36 | PGAIN = 0x08; //4x Prox gain |
sleighton | 0:10fe9a5a884f | 37 | |
sleighton | 0:10fe9a5a884f | 38 | WriteByte (0x72, 0xf, PDRIVE | PDIODE | PGAIN ); |
sleighton | 0:10fe9a5a884f | 39 | |
sleighton | 0:10fe9a5a884f | 40 | char WEN, PEN, PON, PIEN; |
sleighton | 0:10fe9a5a884f | 41 | WEN = 8; // Enable Wait |
sleighton | 1:159407640167 | 42 | PIEN = 32; //Enable interrupt |
sleighton | 0:10fe9a5a884f | 43 | PEN = 4; // Enable Prox |
sleighton | 0:10fe9a5a884f | 44 | PON = 1; // Enable Power On |
sleighton | 0:10fe9a5a884f | 45 | |
sleighton | 0:10fe9a5a884f | 46 | WriteByte (0x72, 0, WEN | PIEN | PEN | PON); |
sleighton | 0:10fe9a5a884f | 47 | |
sleighton | 0:10fe9a5a884f | 48 | wait(0.12); //Wait for 120 ms |
sleighton | 0:10fe9a5a884f | 49 | } |
sleighton | 0:10fe9a5a884f | 50 | |
sleighton | 1:159407640167 | 51 | uint16_t Prox::readProx(){ |
sleighton | 0:10fe9a5a884f | 52 | WriteByte (0x72, 0xE5); //clear interrupt pin (untested) |
sleighton | 1:159407640167 | 53 | uint16_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 | 1:159407640167 | 64 | void Prox::ReadWord(char addr, char reg, uint16_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,®1,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,®1,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 | } |