Gsma Version
Dependents: GSMACode stripReader
att160826.h@1:fc2dbccffe34, 2017-04-21 (annotated)
- Committer:
- elmkom
- Date:
- Fri Apr 21 17:58:08 2017 +0000
- Revision:
- 1:fc2dbccffe34
- Parent:
- 0:5e15a306a518
Proximity strip reader
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elmkom | 0:5e15a306a518 | 1 | #include "pca9548.h" |
elmkom | 0:5e15a306a518 | 2 | #include "sfh7779.h" |
elmkom | 0:5e15a306a518 | 3 | |
elmkom | 1:fc2dbccffe34 | 4 | #define SENSORS_PER_STRIP 8 |
elmkom | 0:5e15a306a518 | 5 | |
elmkom | 0:5e15a306a518 | 6 | typedef struct { |
elmkom | 0:5e15a306a518 | 7 | bool present; |
elmkom | 0:5e15a306a518 | 8 | sfh7779_measurements_t sample; |
elmkom | 0:5e15a306a518 | 9 | } sfh7779_data_t; |
elmkom | 0:5e15a306a518 | 10 | |
elmkom | 0:5e15a306a518 | 11 | typedef struct { |
elmkom | 0:5e15a306a518 | 12 | sfh7779_data_t sensor[SENSORS_PER_STRIP]; |
elmkom | 0:5e15a306a518 | 13 | } att160826_data_t; |
elmkom | 0:5e15a306a518 | 14 | |
elmkom | 0:5e15a306a518 | 15 | class ATT160826 |
elmkom | 0:5e15a306a518 | 16 | { |
elmkom | 0:5e15a306a518 | 17 | public: |
elmkom | 0:5e15a306a518 | 18 | /** |
elmkom | 0:5e15a306a518 | 19 | * Constructor |
elmkom | 0:5e15a306a518 | 20 | * |
elmkom | 0:5e15a306a518 | 21 | * @param i2c I2C class servicing the strip |
elmkom | 0:5e15a306a518 | 22 | * @param addr_3bit address of the strip (A0-A2 of I2C multiplexer) |
elmkom | 0:5e15a306a518 | 23 | * Valid values are 0-7 |
elmkom | 0:5e15a306a518 | 24 | * @param reset optional DigitalOut class that resets the strip |
elmkom | 0:5e15a306a518 | 25 | * Not needed if hardware reset is not used or if reset |
elmkom | 0:5e15a306a518 | 26 | * is not managed by the class. |
elmkom | 0:5e15a306a518 | 27 | */ |
elmkom | 0:5e15a306a518 | 28 | ATT160826(I2C * i2c, uint8_t addr_3bit, DigitalOut * reset = NULL) { |
elmkom | 0:5e15a306a518 | 29 | _pca9548 = new PCA9548(i2c, addr_3bit, reset); |
elmkom | 0:5e15a306a518 | 30 | for (int x = 0; x < SENSORS_PER_STRIP; x++) { |
elmkom | 0:5e15a306a518 | 31 | _sfh7779[x] = new SFH7779(i2c); |
elmkom | 0:5e15a306a518 | 32 | } |
elmkom | 0:5e15a306a518 | 33 | } |
elmkom | 0:5e15a306a518 | 34 | |
elmkom | 0:5e15a306a518 | 35 | /** |
elmkom | 0:5e15a306a518 | 36 | * Destructor |
elmkom | 0:5e15a306a518 | 37 | */ |
elmkom | 0:5e15a306a518 | 38 | ~ATT160826() { |
elmkom | 0:5e15a306a518 | 39 | if (_pca9548) { |
elmkom | 0:5e15a306a518 | 40 | delete _pca9548; |
elmkom | 0:5e15a306a518 | 41 | } |
elmkom | 0:5e15a306a518 | 42 | for (int x = 0; x < SENSORS_PER_STRIP; x++) { |
elmkom | 0:5e15a306a518 | 43 | if (_sfh7779[x]) { |
elmkom | 0:5e15a306a518 | 44 | delete _sfh7779[x]; |
elmkom | 0:5e15a306a518 | 45 | } |
elmkom | 0:5e15a306a518 | 46 | } |
elmkom | 0:5e15a306a518 | 47 | } |
elmkom | 0:5e15a306a518 | 48 | |
elmkom | 0:5e15a306a518 | 49 | /** |
elmkom | 0:5e15a306a518 | 50 | * Sample all sensors on the strip (proximity, visual ambient light, and infrared ambient light). |
elmkom | 0:5e15a306a518 | 51 | * Sensors are all activated, sampled, and then placed in standby mode to conserve power consumption. |
elmkom | 0:5e15a306a518 | 52 | * |
elmkom | 0:5e15a306a518 | 53 | * @param att160826_data pointer to a buffer which will be filled with a sensor data |
elmkom | 0:5e15a306a518 | 54 | * |
elmkom | 0:5e15a306a518 | 55 | * @returns true if successfull |
elmkom | 0:5e15a306a518 | 56 | */ |
elmkom | 0:5e15a306a518 | 57 | bool scan(att160826_data_t * att160826_data) { |
elmkom | 0:5e15a306a518 | 58 | |
elmkom | 0:5e15a306a518 | 59 | // Clear all previous measurements before starting the scan |
elmkom | 0:5e15a306a518 | 60 | memset(att160826_data, 0, sizeof(att160826_data_t)); |
elmkom | 0:5e15a306a518 | 61 | |
elmkom | 0:5e15a306a518 | 62 | if (_pca9548) { |
elmkom | 0:5e15a306a518 | 63 | // Enable all sensors |
elmkom | 0:5e15a306a518 | 64 | for (int x = 0; x < SENSORS_PER_STRIP; x++) { |
elmkom | 0:5e15a306a518 | 65 | int channel_idx = x; |
elmkom | 0:5e15a306a518 | 66 | bool ok = _pca9548->select_channel(channel_idx) |
elmkom | 0:5e15a306a518 | 67 | && _sfh7779[x] |
elmkom | 0:5e15a306a518 | 68 | && _sfh7779[x]->enable(); |
elmkom | 0:5e15a306a518 | 69 | att160826_data->sensor[x].present = ok; |
elmkom | 0:5e15a306a518 | 70 | } |
elmkom | 0:5e15a306a518 | 71 | |
elmkom | 0:5e15a306a518 | 72 | // Collect data from all sensors (throw away 1st sample and keep 2nd) |
elmkom | 0:5e15a306a518 | 73 | for (int y = 0; y < 1; y++) { |
elmkom | 0:5e15a306a518 | 74 | for (int x = 0; x < SENSORS_PER_STRIP; x++) { |
elmkom | 0:5e15a306a518 | 75 | int channel_idx = x; |
elmkom | 0:5e15a306a518 | 76 | if (att160826_data->sensor[x].present && |
elmkom | 0:5e15a306a518 | 77 | _pca9548->select_channel(channel_idx)) { |
elmkom | 0:5e15a306a518 | 78 | _sfh7779[x]->read(&(att160826_data->sensor[x].sample), 500); |
elmkom | 0:5e15a306a518 | 79 | } |
elmkom | 0:5e15a306a518 | 80 | } |
elmkom | 0:5e15a306a518 | 81 | } |
elmkom | 0:5e15a306a518 | 82 | /* |
elmkom | 0:5e15a306a518 | 83 | // Disable all sensors |
elmkom | 0:5e15a306a518 | 84 | for (int x = 0; x < SENSORS_PER_STRIP; x++) { |
elmkom | 0:5e15a306a518 | 85 | int channel_idx = x; |
elmkom | 0:5e15a306a518 | 86 | bool ok = _pca9548->select_channel(channel_idx) |
elmkom | 0:5e15a306a518 | 87 | && _sfh7779[x]->disable(); |
elmkom | 0:5e15a306a518 | 88 | } |
elmkom | 0:5e15a306a518 | 89 | */ |
elmkom | 0:5e15a306a518 | 90 | // Disconnect all sensors from the I2C bus |
elmkom | 0:5e15a306a518 | 91 | _pca9548->reset(); |
elmkom | 0:5e15a306a518 | 92 | } |
elmkom | 0:5e15a306a518 | 93 | |
elmkom | 0:5e15a306a518 | 94 | return true; |
elmkom | 0:5e15a306a518 | 95 | } |
elmkom | 0:5e15a306a518 | 96 | |
elmkom | 0:5e15a306a518 | 97 | protected: |
elmkom | 0:5e15a306a518 | 98 | PCA9548 * _pca9548; |
elmkom | 0:5e15a306a518 | 99 | SFH7779 * _sfh7779[SENSORS_PER_STRIP]; |
elmkom | 0:5e15a306a518 | 100 | }; |