APDS-9130 library
Prox.cpp@2:9db88ca586db, 2016-03-04 (annotated)
- Committer:
- sleighton
- Date:
- Fri Mar 04 18:46:17 2016 +0000
- Revision:
- 2:9db88ca586db
- Parent:
- 1:159407640167
Before Tidy up
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 | 2:9db88ca586db | 7 | WriteByte(0x72,0xE7); // 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 | 2:9db88ca586db | 24 | PIHTL = 0x01; |
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 | 2:9db88ca586db | 51 | |
sleighton | 2:9db88ca586db | 52 | |
sleighton | 2:9db88ca586db | 53 | |
sleighton | 1:159407640167 | 54 | uint16_t Prox::readProx(){ |
sleighton | 2:9db88ca586db | 55 | WriteByte (0x72, 0xE7); //clear interrupt pin (untested) |
sleighton | 1:159407640167 | 56 | uint16_t prox; |
sleighton | 0:10fe9a5a884f | 57 | ReadWord(0x72, 0x18, &prox); |
sleighton | 0:10fe9a5a884f | 58 | return prox; |
sleighton | 0:10fe9a5a884f | 59 | } |
sleighton | 0:10fe9a5a884f | 60 | |
sleighton | 0:10fe9a5a884f | 61 | |
sleighton | 0:10fe9a5a884f | 62 | |
sleighton | 0:10fe9a5a884f | 63 | |
sleighton | 0:10fe9a5a884f | 64 | |
sleighton | 0:10fe9a5a884f | 65 | // Read a byte on the i2c interface |
sleighton | 0:10fe9a5a884f | 66 | |
sleighton | 1:159407640167 | 67 | void Prox::ReadWord(char addr, char reg, uint16_t *data) { |
sleighton | 0:10fe9a5a884f | 68 | I2C i2c(_sda,_scl); |
sleighton | 0:10fe9a5a884f | 69 | char reg1 = 0xA0 | reg; |
sleighton | 0:10fe9a5a884f | 70 | char reading[2]; |
sleighton | 0:10fe9a5a884f | 71 | i2c.write(addr,®1,1); |
sleighton | 0:10fe9a5a884f | 72 | i2c.read(addr,reading,2); |
sleighton | 0:10fe9a5a884f | 73 | *data = reading[0] | (reading[1]<<8); |
sleighton | 0:10fe9a5a884f | 74 | wait(0.07); |
sleighton | 0:10fe9a5a884f | 75 | } |
sleighton | 0:10fe9a5a884f | 76 | |
sleighton | 0:10fe9a5a884f | 77 | void Prox::ReadByte(char addr, char reg, char *bytedata) { |
sleighton | 0:10fe9a5a884f | 78 | I2C i2c(_sda,_scl); |
sleighton | 0:10fe9a5a884f | 79 | char tempdata; |
sleighton | 0:10fe9a5a884f | 80 | char reg1 = 0x80 | reg; |
sleighton | 0:10fe9a5a884f | 81 | i2c.write(addr,®1,1); |
sleighton | 0:10fe9a5a884f | 82 | i2c.read(addr,&tempdata,1); |
sleighton | 0:10fe9a5a884f | 83 | *bytedata = tempdata; |
sleighton | 0:10fe9a5a884f | 84 | wait(0.07); |
sleighton | 0:10fe9a5a884f | 85 | } |
sleighton | 0:10fe9a5a884f | 86 | |
sleighton | 0:10fe9a5a884f | 87 | |
sleighton | 0:10fe9a5a884f | 88 | |
sleighton | 0:10fe9a5a884f | 89 | // Write a byte on the i2c interface |
sleighton | 0:10fe9a5a884f | 90 | |
sleighton | 0:10fe9a5a884f | 91 | void Prox::WriteByte(uint8_t addr, uint8_t reg, char data) { |
sleighton | 0:10fe9a5a884f | 92 | I2C i2c(_sda,_scl); |
sleighton | 0:10fe9a5a884f | 93 | char data1[2]; |
sleighton | 0:10fe9a5a884f | 94 | data1[0] = 0x80 | reg; |
sleighton | 0:10fe9a5a884f | 95 | data1[1] = data; |
sleighton | 0:10fe9a5a884f | 96 | i2c.write(addr,data1,2); |
sleighton | 0:10fe9a5a884f | 97 | wait(0.07); |
sleighton | 0:10fe9a5a884f | 98 | |
sleighton | 0:10fe9a5a884f | 99 | } |
sleighton | 0:10fe9a5a884f | 100 | |
sleighton | 0:10fe9a5a884f | 101 | void Prox::WriteByte(uint8_t addr, uint8_t reg) { |
sleighton | 0:10fe9a5a884f | 102 | I2C i2c(_sda,_scl); |
sleighton | 0:10fe9a5a884f | 103 | char data1; |
sleighton | 0:10fe9a5a884f | 104 | data1 = 0x80 | reg; |
sleighton | 0:10fe9a5a884f | 105 | i2c.write(addr,&data1,1); |
sleighton | 0:10fe9a5a884f | 106 | wait(0.07); |
sleighton | 0:10fe9a5a884f | 107 | |
sleighton | 0:10fe9a5a884f | 108 | } |