Version finale
Dependencies: mbed
Fork of Main_V3_Old by
TSL2561.cpp@0:eec7bcd27c52, 2015-02-22 (annotated)
- Committer:
- kenjiArai
- Date:
- Sun Feb 22 01:19:56 2015 +0000
- Revision:
- 0:eec7bcd27c52
- Child:
- 1:25a700e9b8ec
Luminosity sensor by Texas Advanced Optoelectronic Solutions Inc.. Device combines one broadband photodiode (visible plus infrared) and one infrared-responding photodiode. Set Gain x1 and 402mS as default.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:eec7bcd27c52 | 1 | /* |
kenjiArai | 0:eec7bcd27c52 | 2 | * mbed library program |
kenjiArai | 0:eec7bcd27c52 | 3 | * Luminosity sensor -- LIGHT-TO-DIGITAL CONVERTER (light intensity to a digital signal output) |
kenjiArai | 0:eec7bcd27c52 | 4 | * TSL2561 by Texas Advanced Optoelectronic Solutions Inc. |
kenjiArai | 0:eec7bcd27c52 | 5 | * |
kenjiArai | 0:eec7bcd27c52 | 6 | * Copyright (c) 2015 Kenji Arai / JH1PJL |
kenjiArai | 0:eec7bcd27c52 | 7 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:eec7bcd27c52 | 8 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:eec7bcd27c52 | 9 | * Created: Feburary 21st, 2015 |
kenjiArai | 0:eec7bcd27c52 | 10 | * Revised: Feburary 22nd, 2015 |
kenjiArai | 0:eec7bcd27c52 | 11 | * |
kenjiArai | 0:eec7bcd27c52 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 0:eec7bcd27c52 | 13 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 0:eec7bcd27c52 | 14 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 0:eec7bcd27c52 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:eec7bcd27c52 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 0:eec7bcd27c52 | 17 | */ |
kenjiArai | 0:eec7bcd27c52 | 18 | |
kenjiArai | 0:eec7bcd27c52 | 19 | #include "TSL2561.h" |
kenjiArai | 0:eec7bcd27c52 | 20 | |
kenjiArai | 0:eec7bcd27c52 | 21 | TSL2561::TSL2561 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl) |
kenjiArai | 0:eec7bcd27c52 | 22 | { |
kenjiArai | 0:eec7bcd27c52 | 23 | TSL2561_addr = TSL2561_ADDRESS_GND; |
kenjiArai | 0:eec7bcd27c52 | 24 | init(); |
kenjiArai | 0:eec7bcd27c52 | 25 | } |
kenjiArai | 0:eec7bcd27c52 | 26 | |
kenjiArai | 0:eec7bcd27c52 | 27 | TSL2561::TSL2561 (PinName p_sda, PinName p_scl, uint8_t addr) : _i2c(p_sda, p_scl) |
kenjiArai | 0:eec7bcd27c52 | 28 | { |
kenjiArai | 0:eec7bcd27c52 | 29 | TSL2561_addr = addr; |
kenjiArai | 0:eec7bcd27c52 | 30 | init(); |
kenjiArai | 0:eec7bcd27c52 | 31 | } |
kenjiArai | 0:eec7bcd27c52 | 32 | |
kenjiArai | 0:eec7bcd27c52 | 33 | TSL2561::TSL2561 (I2C& p_i2c) : _i2c(p_i2c) |
kenjiArai | 0:eec7bcd27c52 | 34 | { |
kenjiArai | 0:eec7bcd27c52 | 35 | TSL2561_addr = TSL2561_ADDRESS_GND; |
kenjiArai | 0:eec7bcd27c52 | 36 | init(); |
kenjiArai | 0:eec7bcd27c52 | 37 | } |
kenjiArai | 0:eec7bcd27c52 | 38 | |
kenjiArai | 0:eec7bcd27c52 | 39 | TSL2561::TSL2561 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c) |
kenjiArai | 0:eec7bcd27c52 | 40 | { |
kenjiArai | 0:eec7bcd27c52 | 41 | TSL2561_addr = addr; |
kenjiArai | 0:eec7bcd27c52 | 42 | init(); |
kenjiArai | 0:eec7bcd27c52 | 43 | } |
kenjiArai | 0:eec7bcd27c52 | 44 | |
kenjiArai | 0:eec7bcd27c52 | 45 | /////////////// Read Lux from sensor ////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 46 | /* |
kenjiArai | 0:eec7bcd27c52 | 47 | For 0 < CH1/CH0 < 0.50 Lux = 0.0304 x CH0-0.062 x CH0 x ((CH1/CH0)1.4) |
kenjiArai | 0:eec7bcd27c52 | 48 | For 0.50 < CH1/CH0 < 0.61 Lux = 0.0224 x CH0-0.031 x CH1 |
kenjiArai | 0:eec7bcd27c52 | 49 | For 0.61 < CH1/CH0 < 0.80 Lux = 0.0128 x CH0-0.0153 x CH1 |
kenjiArai | 0:eec7bcd27c52 | 50 | For 0.80 < CH1/CH0 < 1.30 Lux = 0.00146 x CH0-0.00112x CH1 |
kenjiArai | 0:eec7bcd27c52 | 51 | For CH1/CH0 > 1.30 Lux = 0 |
kenjiArai | 0:eec7bcd27c52 | 52 | */ |
kenjiArai | 0:eec7bcd27c52 | 53 | float TSL2561::lux() |
kenjiArai | 0:eec7bcd27c52 | 54 | { |
kenjiArai | 0:eec7bcd27c52 | 55 | double lux0, lux1; |
kenjiArai | 0:eec7bcd27c52 | 56 | double ratio; |
kenjiArai | 0:eec7bcd27c52 | 57 | double dlux; |
kenjiArai | 0:eec7bcd27c52 | 58 | |
kenjiArai | 0:eec7bcd27c52 | 59 | dt[0] = CMD_MULTI + TSL2561_DATA0LOW; |
kenjiArai | 0:eec7bcd27c52 | 60 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 61 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 62 | ch0 = dt[1] << 8 | dt[0]; |
kenjiArai | 0:eec7bcd27c52 | 63 | dt[0] = CMD_MULTI + TSL2561_DATA1LOW; |
kenjiArai | 0:eec7bcd27c52 | 64 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 65 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 66 | ch1 = dt[1] << 8 | dt[0]; |
kenjiArai | 0:eec7bcd27c52 | 67 | lux0 = ch0; |
kenjiArai | 0:eec7bcd27c52 | 68 | lux1 = ch1; |
kenjiArai | 0:eec7bcd27c52 | 69 | ratio = lux1 / lux0; |
kenjiArai | 0:eec7bcd27c52 | 70 | if (ratio <= 0.5) { |
kenjiArai | 0:eec7bcd27c52 | 71 | dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4); |
kenjiArai | 0:eec7bcd27c52 | 72 | } else if (ratio <= 0.61) { |
kenjiArai | 0:eec7bcd27c52 | 73 | dlux = 0.02240 * lux0 - 0.03100 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 74 | } else if (ratio <= 0.80) { |
kenjiArai | 0:eec7bcd27c52 | 75 | dlux = 0.01280 * lux0 - 0.01530 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 76 | } else if (ratio <= 1.30) { |
kenjiArai | 0:eec7bcd27c52 | 77 | dlux = 0.00146 * lux0 - 0.00112 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 78 | } else { |
kenjiArai | 0:eec7bcd27c52 | 79 | dlux = 0; |
kenjiArai | 0:eec7bcd27c52 | 80 | } |
kenjiArai | 0:eec7bcd27c52 | 81 | return (float)dlux; |
kenjiArai | 0:eec7bcd27c52 | 82 | } |
kenjiArai | 0:eec7bcd27c52 | 83 | |
kenjiArai | 0:eec7bcd27c52 | 84 | /////////////// Initialize //////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 85 | void TSL2561::init() |
kenjiArai | 0:eec7bcd27c52 | 86 | { |
kenjiArai | 0:eec7bcd27c52 | 87 | _i2c.frequency(100000); |
kenjiArai | 0:eec7bcd27c52 | 88 | power_up(); |
kenjiArai | 0:eec7bcd27c52 | 89 | read_ID(); |
kenjiArai | 0:eec7bcd27c52 | 90 | } |
kenjiArai | 0:eec7bcd27c52 | 91 | |
kenjiArai | 0:eec7bcd27c52 | 92 | /////////////// ID //////////////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 93 | uint16_t TSL2561::read_ID() |
kenjiArai | 0:eec7bcd27c52 | 94 | { |
kenjiArai | 0:eec7bcd27c52 | 95 | dt[0] = CMD_SINGLE + TSL2561_ID; |
kenjiArai | 0:eec7bcd27c52 | 96 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 97 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 98 | id_number = dt[0] << 8 | dt[1]; |
kenjiArai | 0:eec7bcd27c52 | 99 | return id_number; |
kenjiArai | 0:eec7bcd27c52 | 100 | } |
kenjiArai | 0:eec7bcd27c52 | 101 | |
kenjiArai | 0:eec7bcd27c52 | 102 | uint8_t TSL2561::who_am_i() |
kenjiArai | 0:eec7bcd27c52 | 103 | { |
kenjiArai | 0:eec7bcd27c52 | 104 | read_ID(); |
kenjiArai | 0:eec7bcd27c52 | 105 | if ((id_number >> 4) == I_AM_TSL2561) { |
kenjiArai | 0:eec7bcd27c52 | 106 | return 1; |
kenjiArai | 0:eec7bcd27c52 | 107 | } else { |
kenjiArai | 0:eec7bcd27c52 | 108 | return 0; |
kenjiArai | 0:eec7bcd27c52 | 109 | } |
kenjiArai | 0:eec7bcd27c52 | 110 | } |
kenjiArai | 0:eec7bcd27c52 | 111 | |
kenjiArai | 0:eec7bcd27c52 | 112 | /////////////// Power ON/OFF ////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 113 | void TSL2561::power_up() |
kenjiArai | 0:eec7bcd27c52 | 114 | { |
kenjiArai | 0:eec7bcd27c52 | 115 | dt[0] = CMD_SINGLE + TSL2561_CONTROL; |
kenjiArai | 0:eec7bcd27c52 | 116 | dt[1] = 3; |
kenjiArai | 0:eec7bcd27c52 | 117 | _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 118 | } |
kenjiArai | 0:eec7bcd27c52 | 119 | |
kenjiArai | 0:eec7bcd27c52 | 120 | void TSL2561::power_down() |
kenjiArai | 0:eec7bcd27c52 | 121 | { |
kenjiArai | 0:eec7bcd27c52 | 122 | dt[0] = CMD_SINGLE + TSL2561_CONTROL; |
kenjiArai | 0:eec7bcd27c52 | 123 | dt[1] = 0; |
kenjiArai | 0:eec7bcd27c52 | 124 | _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 125 | } |
kenjiArai | 0:eec7bcd27c52 | 126 | |
kenjiArai | 0:eec7bcd27c52 | 127 | /////////////// I2C Freq. ///////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 128 | void TSL2561::frequency(int hz) |
kenjiArai | 0:eec7bcd27c52 | 129 | { |
kenjiArai | 0:eec7bcd27c52 | 130 | _i2c.frequency(hz); |
kenjiArai | 0:eec7bcd27c52 | 131 | } |