Library for Si114x optical sensors. Currently intended for the Si1146, although it will work also for the Si1145, and without full functionality for the Si1147
Dependents: Si114x_HelloWorld Hello-Uzuki-sensor-shield
Si114x.h@0:971d705818e7, 2015-08-23 (annotated)
- Committer:
- Sissors
- Date:
- Sun Aug 23 16:58:36 2015 +0000
- Revision:
- 0:971d705818e7
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 0:971d705818e7 | 1 | #ifndef SI114X_H |
Sissors | 0:971d705818e7 | 2 | #define SI114X_H |
Sissors | 0:971d705818e7 | 3 | |
Sissors | 0:971d705818e7 | 4 | |
Sissors | 0:971d705818e7 | 5 | #include "mbed.h" |
Sissors | 0:971d705818e7 | 6 | /* |
Sissors | 0:971d705818e7 | 7 | * Class to interface with Si114x sensors |
Sissors | 0:971d705818e7 | 8 | * |
Sissors | 0:971d705818e7 | 9 | * Currently only intended for Si1146. Should work also on the Si1145, |
Sissors | 0:971d705818e7 | 10 | * but the extra LED of the Si1147 is not supported. Everyone is welcome |
Sissors | 0:971d705818e7 | 11 | * to add this support and submit a pull request. |
Sissors | 0:971d705818e7 | 12 | * |
Sissors | 0:971d705818e7 | 13 | * Aditionally only synchronous mode is for now supported (no async I2C API is used |
Sissors | 0:971d705818e7 | 14 | * and the sensor is not placed in automatic measurement mode: Each request starts a new |
Sissors | 0:971d705818e7 | 15 | * measurement. Also for this and other issues: Everyone is welcome to submit |
Sissors | 0:971d705818e7 | 16 | * pull requests |
Sissors | 0:971d705818e7 | 17 | * |
Sissors | 0:971d705818e7 | 18 | * For now this is just returning raw values, no scaling is done by the library code |
Sissors | 0:971d705818e7 | 19 | */ |
Sissors | 0:971d705818e7 | 20 | class Si114x { |
Sissors | 0:971d705818e7 | 21 | public: |
Sissors | 0:971d705818e7 | 22 | /* Constructor to create new Si114x instance |
Sissors | 0:971d705818e7 | 23 | * |
Sissors | 0:971d705818e7 | 24 | * @param sda SDA pin of the I2C interface |
Sissors | 0:971d705818e7 | 25 | * @param scl SCL pin of the I2C interface |
Sissors | 0:971d705818e7 | 26 | */ |
Sissors | 0:971d705818e7 | 27 | Si114x(PinName sda, PinName scl); |
Sissors | 0:971d705818e7 | 28 | |
Sissors | 0:971d705818e7 | 29 | /* Get the measure visible light intensity |
Sissors | 0:971d705818e7 | 30 | * |
Sissors | 0:971d705818e7 | 31 | * @return unsigned short (uint16_t) representing visible light intensity |
Sissors | 0:971d705818e7 | 32 | */ |
Sissors | 0:971d705818e7 | 33 | unsigned short getVisibleLight(void); |
Sissors | 0:971d705818e7 | 34 | |
Sissors | 0:971d705818e7 | 35 | /* Get the measure IR light intensity |
Sissors | 0:971d705818e7 | 36 | * |
Sissors | 0:971d705818e7 | 37 | * @return unsigned short (uint16_t) representing IR light intensity |
Sissors | 0:971d705818e7 | 38 | */ |
Sissors | 0:971d705818e7 | 39 | unsigned short getIRLight(void); |
Sissors | 0:971d705818e7 | 40 | |
Sissors | 0:971d705818e7 | 41 | /* Get the measure UV intensity |
Sissors | 0:971d705818e7 | 42 | * |
Sissors | 0:971d705818e7 | 43 | * The retval is supposed to be 10 times the UV index. It will be modified |
Sissors | 0:971d705818e7 | 44 | * to return a float representing the UV index once it doesn't claim |
Sissors | 0:971d705818e7 | 45 | * the UV index is 50 outside. |
Sissors | 0:971d705818e7 | 46 | * |
Sissors | 0:971d705818e7 | 47 | * @return unsigned short (uint16_t) representing UV intensity |
Sissors | 0:971d705818e7 | 48 | */ |
Sissors | 0:971d705818e7 | 49 | unsigned short getUVIndex(void); |
Sissors | 0:971d705818e7 | 50 | |
Sissors | 0:971d705818e7 | 51 | /* Perform a proximity measurement |
Sissors | 0:971d705818e7 | 52 | * |
Sissors | 0:971d705818e7 | 53 | * This enabled LEDs (set using setPSLEDs) and performs a |
Sissors | 0:971d705818e7 | 54 | * reflection measurement. It can be used to obtain (relative) |
Sissors | 0:971d705818e7 | 55 | * proximity, but also to measure for example heartbeat, as in |
Sissors | 0:971d705818e7 | 56 | * the HelloWorld program. |
Sissors | 0:971d705818e7 | 57 | * |
Sissors | 0:971d705818e7 | 58 | * Currently the code does not substract ambient light (todo) |
Sissors | 0:971d705818e7 | 59 | * |
Sissors | 0:971d705818e7 | 60 | * @param pschannel channel to use for the measurement (1-2) |
Sissors | 0:971d705818e7 | 61 | * @return unsigned short (uint16_t) representing the reflection |
Sissors | 0:971d705818e7 | 62 | */ |
Sissors | 0:971d705818e7 | 63 | unsigned short getProximity(char pschannel); |
Sissors | 0:971d705818e7 | 64 | |
Sissors | 0:971d705818e7 | 65 | /* Set the LEDs for a given proximity measurement |
Sissors | 0:971d705818e7 | 66 | * |
Sissors | 0:971d705818e7 | 67 | * The Si1146 has two channels to do measurements with and can control 2 LEDs. |
Sissors | 0:971d705818e7 | 68 | * This function is used to set which LEDs are used for which channel. |
Sissors | 0:971d705818e7 | 69 | * Multiple LEDs can be active at the same time. By default the |
Sissors | 0:971d705818e7 | 70 | * constructor sets LED1 to be active with channel 1 and LED2 to |
Sissors | 0:971d705818e7 | 71 | * be active with channel 2. |
Sissors | 0:971d705818e7 | 72 | * |
Sissors | 0:971d705818e7 | 73 | * @param pschannel channel to set active LEDs for (1-2) |
Sissors | 0:971d705818e7 | 74 | * @param ledmask LEDs to be active for the set channel (bit 0 is LED1, bit1 is LED2) |
Sissors | 0:971d705818e7 | 75 | */ |
Sissors | 0:971d705818e7 | 76 | void setPSLEDs(char pschannel, char ledmask); |
Sissors | 0:971d705818e7 | 77 | |
Sissors | 0:971d705818e7 | 78 | /* Verify I2C communication can occur with the sensor |
Sissors | 0:971d705818e7 | 79 | * |
Sissors | 0:971d705818e7 | 80 | * @return true when the sensor is found |
Sissors | 0:971d705818e7 | 81 | */ |
Sissors | 0:971d705818e7 | 82 | bool verifyConnection(void); |
Sissors | 0:971d705818e7 | 83 | |
Sissors | 0:971d705818e7 | 84 | protected: |
Sissors | 0:971d705818e7 | 85 | char read_byte(const char reg); |
Sissors | 0:971d705818e7 | 86 | unsigned short read_word(const char reg); |
Sissors | 0:971d705818e7 | 87 | void write_byte(const char reg, const char byte); |
Sissors | 0:971d705818e7 | 88 | void param_set(const char param, const char value); |
Sissors | 0:971d705818e7 | 89 | char param_query(const char param); |
Sissors | 0:971d705818e7 | 90 | void write_cmd(const char cmd); |
Sissors | 0:971d705818e7 | 91 | |
Sissors | 0:971d705818e7 | 92 | I2C i2c; |
Sissors | 0:971d705818e7 | 93 | |
Sissors | 0:971d705818e7 | 94 | bool continious; |
Sissors | 0:971d705818e7 | 95 | }; |
Sissors | 0:971d705818e7 | 96 | |
Sissors | 0:971d705818e7 | 97 | #endif |