Version finale
Dependencies: mbed
Fork of Main_V3_Old by
TSL2561.cpp@3:534235b99bc6, 2017-10-02 (annotated)
- Committer:
- ramialjed
- Date:
- Mon Oct 02 12:47:09 2017 +0000
- Revision:
- 3:534235b99bc6
- Parent:
- 2:17591031447b
Hello
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 | 2:17591031447b | 6 | * Copyright (c) 2015,'17 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 | 2:17591031447b | 9 | * Created: Feburary 21st, 2015 |
kenjiArai | 2:17591031447b | 10 | * Revised: August 23rd, 2017 |
kenjiArai | 0:eec7bcd27c52 | 11 | */ |
ramialjed | 3:534235b99bc6 | 12 | |
kenjiArai | 0:eec7bcd27c52 | 13 | #include "TSL2561.h" |
ramialjed | 3:534235b99bc6 | 14 | |
kenjiArai | 2:17591031447b | 15 | TSL2561::TSL2561 (PinName p_sda, PinName p_scl) |
kenjiArai | 2:17591031447b | 16 | : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) |
kenjiArai | 0:eec7bcd27c52 | 17 | { |
kenjiArai | 0:eec7bcd27c52 | 18 | TSL2561_addr = TSL2561_ADDRESS_GND; |
kenjiArai | 0:eec7bcd27c52 | 19 | init(); |
kenjiArai | 0:eec7bcd27c52 | 20 | } |
ramialjed | 3:534235b99bc6 | 21 | |
kenjiArai | 2:17591031447b | 22 | TSL2561::TSL2561 (PinName p_sda, PinName p_scl, uint8_t addr) |
kenjiArai | 2:17591031447b | 23 | : _i2c_p(new I2C(p_sda, p_scl)), _i2c(*_i2c_p) |
kenjiArai | 0:eec7bcd27c52 | 24 | { |
kenjiArai | 0:eec7bcd27c52 | 25 | TSL2561_addr = addr; |
kenjiArai | 0:eec7bcd27c52 | 26 | init(); |
kenjiArai | 0:eec7bcd27c52 | 27 | } |
ramialjed | 3:534235b99bc6 | 28 | |
kenjiArai | 2:17591031447b | 29 | TSL2561::TSL2561 (I2C& p_i2c) |
kenjiArai | 2:17591031447b | 30 | : _i2c(p_i2c) |
kenjiArai | 0:eec7bcd27c52 | 31 | { |
kenjiArai | 0:eec7bcd27c52 | 32 | TSL2561_addr = TSL2561_ADDRESS_GND; |
kenjiArai | 0:eec7bcd27c52 | 33 | init(); |
kenjiArai | 0:eec7bcd27c52 | 34 | } |
ramialjed | 3:534235b99bc6 | 35 | |
kenjiArai | 2:17591031447b | 36 | TSL2561::TSL2561 (I2C& p_i2c, uint8_t addr) |
kenjiArai | 2:17591031447b | 37 | : _i2c(p_i2c) |
kenjiArai | 0:eec7bcd27c52 | 38 | { |
kenjiArai | 0:eec7bcd27c52 | 39 | TSL2561_addr = addr; |
kenjiArai | 0:eec7bcd27c52 | 40 | init(); |
kenjiArai | 0:eec7bcd27c52 | 41 | } |
ramialjed | 3:534235b99bc6 | 42 | |
kenjiArai | 0:eec7bcd27c52 | 43 | /////////////// Read Lux from sensor ////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 44 | /* |
kenjiArai | 0:eec7bcd27c52 | 45 | For 0 < CH1/CH0 < 0.50 Lux = 0.0304 x CH0-0.062 x CH0 x ((CH1/CH0)1.4) |
kenjiArai | 0:eec7bcd27c52 | 46 | For 0.50 < CH1/CH0 < 0.61 Lux = 0.0224 x CH0-0.031 x CH1 |
kenjiArai | 0:eec7bcd27c52 | 47 | For 0.61 < CH1/CH0 < 0.80 Lux = 0.0128 x CH0-0.0153 x CH1 |
kenjiArai | 0:eec7bcd27c52 | 48 | For 0.80 < CH1/CH0 < 1.30 Lux = 0.00146 x CH0-0.00112x CH1 |
kenjiArai | 0:eec7bcd27c52 | 49 | For CH1/CH0 > 1.30 Lux = 0 |
kenjiArai | 0:eec7bcd27c52 | 50 | */ |
kenjiArai | 0:eec7bcd27c52 | 51 | float TSL2561::lux() |
kenjiArai | 0:eec7bcd27c52 | 52 | { |
kenjiArai | 0:eec7bcd27c52 | 53 | double lux0, lux1; |
kenjiArai | 0:eec7bcd27c52 | 54 | double ratio; |
kenjiArai | 0:eec7bcd27c52 | 55 | double dlux; |
ramialjed | 3:534235b99bc6 | 56 | |
kenjiArai | 0:eec7bcd27c52 | 57 | dt[0] = CMD_MULTI + TSL2561_DATA0LOW; |
kenjiArai | 0:eec7bcd27c52 | 58 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 59 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 60 | ch0 = dt[1] << 8 | dt[0]; |
kenjiArai | 0:eec7bcd27c52 | 61 | dt[0] = CMD_MULTI + TSL2561_DATA1LOW; |
kenjiArai | 0:eec7bcd27c52 | 62 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 63 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 64 | ch1 = dt[1] << 8 | dt[0]; |
kenjiArai | 1:25a700e9b8ec | 65 | if (ch0 == 0xFFFF) { |
kenjiArai | 1:25a700e9b8ec | 66 | return 2500.0; |
kenjiArai | 1:25a700e9b8ec | 67 | } |
kenjiArai | 1:25a700e9b8ec | 68 | lux0 = (double)ch0; |
kenjiArai | 1:25a700e9b8ec | 69 | lux1 = (double)ch1; |
kenjiArai | 0:eec7bcd27c52 | 70 | ratio = lux1 / lux0; |
kenjiArai | 1:25a700e9b8ec | 71 | read_timing_reg(); |
kenjiArai | 1:25a700e9b8ec | 72 | lux0 *= (402.0/integ_time); |
kenjiArai | 1:25a700e9b8ec | 73 | lux1 *= (402.0/integ_time); |
kenjiArai | 1:25a700e9b8ec | 74 | lux0 /= gain; |
kenjiArai | 1:25a700e9b8ec | 75 | lux1 /= gain; |
kenjiArai | 0:eec7bcd27c52 | 76 | if (ratio <= 0.5) { |
kenjiArai | 0:eec7bcd27c52 | 77 | dlux = 0.03040 * lux0 - 0.06200 * lux0 * pow(ratio,1.4); |
kenjiArai | 0:eec7bcd27c52 | 78 | } else if (ratio <= 0.61) { |
kenjiArai | 0:eec7bcd27c52 | 79 | dlux = 0.02240 * lux0 - 0.03100 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 80 | } else if (ratio <= 0.80) { |
kenjiArai | 0:eec7bcd27c52 | 81 | dlux = 0.01280 * lux0 - 0.01530 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 82 | } else if (ratio <= 1.30) { |
kenjiArai | 0:eec7bcd27c52 | 83 | dlux = 0.00146 * lux0 - 0.00112 * lux1; |
kenjiArai | 0:eec7bcd27c52 | 84 | } else { |
kenjiArai | 0:eec7bcd27c52 | 85 | dlux = 0; |
kenjiArai | 0:eec7bcd27c52 | 86 | } |
kenjiArai | 0:eec7bcd27c52 | 87 | return (float)dlux; |
kenjiArai | 0:eec7bcd27c52 | 88 | } |
ramialjed | 3:534235b99bc6 | 89 | |
kenjiArai | 0:eec7bcd27c52 | 90 | /////////////// Initialize //////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 91 | void TSL2561::init() |
kenjiArai | 0:eec7bcd27c52 | 92 | { |
kenjiArai | 0:eec7bcd27c52 | 93 | _i2c.frequency(100000); |
kenjiArai | 0:eec7bcd27c52 | 94 | power_up(); |
kenjiArai | 1:25a700e9b8ec | 95 | set_timing_reg(TIMING_DEFAULT); |
kenjiArai | 1:25a700e9b8ec | 96 | } |
ramialjed | 3:534235b99bc6 | 97 | |
kenjiArai | 1:25a700e9b8ec | 98 | /////////////// Timing Register /////////////////////////// |
kenjiArai | 1:25a700e9b8ec | 99 | uint8_t TSL2561::set_timing_reg(uint8_t parameter) |
kenjiArai | 1:25a700e9b8ec | 100 | { |
kenjiArai | 1:25a700e9b8ec | 101 | dt[0] = CMD_SINGLE + TSL2561_TIMING; |
kenjiArai | 1:25a700e9b8ec | 102 | dt[1] = parameter; |
kenjiArai | 1:25a700e9b8ec | 103 | _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 1:25a700e9b8ec | 104 | dt[0] = CMD_SINGLE + TSL2561_TIMING; |
kenjiArai | 1:25a700e9b8ec | 105 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 1:25a700e9b8ec | 106 | _i2c.read(TSL2561_addr, (char *)dt, 1, false); |
kenjiArai | 1:25a700e9b8ec | 107 | return dt[0]; |
kenjiArai | 1:25a700e9b8ec | 108 | } |
ramialjed | 3:534235b99bc6 | 109 | |
kenjiArai | 1:25a700e9b8ec | 110 | uint8_t TSL2561::read_timing_reg(void) |
kenjiArai | 1:25a700e9b8ec | 111 | { |
kenjiArai | 1:25a700e9b8ec | 112 | uint8_t i; |
ramialjed | 3:534235b99bc6 | 113 | |
kenjiArai | 1:25a700e9b8ec | 114 | dt[0] = CMD_SINGLE + TSL2561_TIMING; |
kenjiArai | 1:25a700e9b8ec | 115 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 1:25a700e9b8ec | 116 | _i2c.read(TSL2561_addr, (char *)dt, 1, false); |
kenjiArai | 1:25a700e9b8ec | 117 | if (dt[0] & TIMING_GAIN_16){ |
kenjiArai | 1:25a700e9b8ec | 118 | gain = 16; |
kenjiArai | 1:25a700e9b8ec | 119 | } else { |
kenjiArai | 1:25a700e9b8ec | 120 | gain = 1; |
kenjiArai | 1:25a700e9b8ec | 121 | } |
kenjiArai | 1:25a700e9b8ec | 122 | i = dt[0] & 0x3; |
kenjiArai | 1:25a700e9b8ec | 123 | switch (i) { |
kenjiArai | 1:25a700e9b8ec | 124 | case 0: |
kenjiArai | 1:25a700e9b8ec | 125 | integ_time = 13.7; |
kenjiArai | 1:25a700e9b8ec | 126 | break; |
kenjiArai | 1:25a700e9b8ec | 127 | case 1: |
kenjiArai | 1:25a700e9b8ec | 128 | integ_time = 101.0; |
kenjiArai | 1:25a700e9b8ec | 129 | break; |
kenjiArai | 1:25a700e9b8ec | 130 | case 2: |
kenjiArai | 1:25a700e9b8ec | 131 | integ_time = 402.0; |
kenjiArai | 1:25a700e9b8ec | 132 | break; |
kenjiArai | 1:25a700e9b8ec | 133 | default: |
kenjiArai | 1:25a700e9b8ec | 134 | integ_time = 0; |
kenjiArai | 1:25a700e9b8ec | 135 | break; |
kenjiArai | 1:25a700e9b8ec | 136 | } |
kenjiArai | 1:25a700e9b8ec | 137 | return dt[0]; |
kenjiArai | 0:eec7bcd27c52 | 138 | } |
ramialjed | 3:534235b99bc6 | 139 | |
kenjiArai | 0:eec7bcd27c52 | 140 | /////////////// ID //////////////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 141 | uint16_t TSL2561::read_ID() |
kenjiArai | 0:eec7bcd27c52 | 142 | { |
kenjiArai | 0:eec7bcd27c52 | 143 | dt[0] = CMD_SINGLE + TSL2561_ID; |
kenjiArai | 0:eec7bcd27c52 | 144 | _i2c.write((int)TSL2561_addr, (char *)dt, 1, true); |
kenjiArai | 0:eec7bcd27c52 | 145 | _i2c.read(TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 146 | id_number = dt[0] << 8 | dt[1]; |
kenjiArai | 0:eec7bcd27c52 | 147 | return id_number; |
kenjiArai | 0:eec7bcd27c52 | 148 | } |
ramialjed | 3:534235b99bc6 | 149 | |
kenjiArai | 0:eec7bcd27c52 | 150 | uint8_t TSL2561::who_am_i() |
kenjiArai | 0:eec7bcd27c52 | 151 | { |
kenjiArai | 0:eec7bcd27c52 | 152 | read_ID(); |
kenjiArai | 0:eec7bcd27c52 | 153 | if ((id_number >> 4) == I_AM_TSL2561) { |
kenjiArai | 0:eec7bcd27c52 | 154 | return 1; |
kenjiArai | 0:eec7bcd27c52 | 155 | } else { |
kenjiArai | 0:eec7bcd27c52 | 156 | return 0; |
kenjiArai | 0:eec7bcd27c52 | 157 | } |
kenjiArai | 0:eec7bcd27c52 | 158 | } |
ramialjed | 3:534235b99bc6 | 159 | |
kenjiArai | 0:eec7bcd27c52 | 160 | /////////////// Power ON/OFF ////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 161 | void TSL2561::power_up() |
kenjiArai | 0:eec7bcd27c52 | 162 | { |
kenjiArai | 0:eec7bcd27c52 | 163 | dt[0] = CMD_SINGLE + TSL2561_CONTROL; |
kenjiArai | 0:eec7bcd27c52 | 164 | dt[1] = 3; |
kenjiArai | 0:eec7bcd27c52 | 165 | _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 166 | } |
ramialjed | 3:534235b99bc6 | 167 | |
kenjiArai | 0:eec7bcd27c52 | 168 | void TSL2561::power_down() |
kenjiArai | 0:eec7bcd27c52 | 169 | { |
kenjiArai | 0:eec7bcd27c52 | 170 | dt[0] = CMD_SINGLE + TSL2561_CONTROL; |
kenjiArai | 0:eec7bcd27c52 | 171 | dt[1] = 0; |
kenjiArai | 0:eec7bcd27c52 | 172 | _i2c.write((int)TSL2561_addr, (char *)dt, 2, false); |
kenjiArai | 0:eec7bcd27c52 | 173 | } |
ramialjed | 3:534235b99bc6 | 174 | |
kenjiArai | 0:eec7bcd27c52 | 175 | /////////////// I2C Freq. ///////////////////////////////// |
kenjiArai | 0:eec7bcd27c52 | 176 | void TSL2561::frequency(int hz) |
kenjiArai | 0:eec7bcd27c52 | 177 | { |
kenjiArai | 0:eec7bcd27c52 | 178 | _i2c.frequency(hz); |
kenjiArai | 0:eec7bcd27c52 | 179 | } |
ramialjed | 3:534235b99bc6 | 180 | |
ramialjed | 3:534235b99bc6 | 181 | |
ramialjed | 3:534235b99bc6 | 182 |