source: rtos_arduino/trunk/examples/MultiTaskText/libraries/Grove_Digital_Light_Sensor/Digital_Light_TSL2561.cpp@ 244

Last change on this file since 244 was 244, checked in by ertl-honda, 8 years ago

追加

File size: 4.9 KB
Line 
1/*
2 * Digital_Light_TSL2561.cpp
3 * A library for TSL2561
4 *
5 * Copyright (c) 2012 seeed technology inc.
6 * Website : www.seeed.cc
7 * Author : zhangkun
8 * Create Time:
9 * Change Log : Jack Shao, Nov 2014, bug fix and update for user experience
10 *
11 * The MIT License (MIT)
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * THE SOFTWARE.
30 */
31#include <Digital_Light_TSL2561.h>
32#include <Arduino.h>
33#include <Wire.h>
34uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
35{
36
37 uint8_t value;
38 Wire.beginTransmission(deviceAddress);
39 Wire.write(address); // register to read
40 Wire.endTransmission();
41 Wire.requestFrom(deviceAddress, 1); // read a byte
42 while(!Wire.available());
43 value = Wire.read();
44 //delay(100);
45 return value;
46}
47
48void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val)
49{
50 Wire.beginTransmission(deviceAddress); // start transmission to device
51 Wire.write(address); // send register address
52 Wire.write(val); // send value to write
53 Wire.endTransmission(); // end transmission
54 //delay(100);
55}
56void TSL2561_CalculateLux::getLux(void)
57{
58 CH0_LOW=readRegister(TSL2561_Address,TSL2561_Channal0L);
59 CH0_HIGH=readRegister(TSL2561_Address,TSL2561_Channal0H);
60 //read two bytes from registers 0x0E and 0x0F
61 CH1_LOW=readRegister(TSL2561_Address,TSL2561_Channal1L);
62 CH1_HIGH=readRegister(TSL2561_Address,TSL2561_Channal1H);
63
64 ch0 = (CH0_HIGH<<8) | CH0_LOW;
65 ch1 = (CH1_HIGH<<8) | CH1_LOW;
66}
67void TSL2561_CalculateLux::init()
68{
69 writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
70 writeRegister(TSL2561_Address,TSL2561_Timing,0x00); //No High Gain (1x), integration time of 13ms
71 writeRegister(TSL2561_Address,TSL2561_Interrupt,0x00);
72 writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
73}
74signed long TSL2561_CalculateLux::readVisibleLux()
75{
76 writeRegister(TSL2561_Address,TSL2561_Control,0x03); // POWER UP
77 delay(14);
78 getLux();
79
80 writeRegister(TSL2561_Address,TSL2561_Control,0x00); // POWER Down
81 if(ch0/ch1 < 2 && ch0 > 4900)
82 {
83 return -1; //ch0 out of range, but ch1 not. the lux is not valid in this situation.
84 }
85 return calculateLux(0, 0, 0); //T package, no gain, 13ms
86}
87unsigned long TSL2561_CalculateLux::calculateLux(unsigned int iGain, unsigned int tInt,int iType)
88{
89 switch (tInt)
90 {
91 case 0: // 13.7 msec
92 chScale = CHSCALE_TINT0;
93 break;
94 case 1: // 101 msec
95 chScale = CHSCALE_TINT1;
96 break;
97 default: // assume no scaling
98 chScale = (1 << CH_SCALE);
99 break;
100}
101if (!iGain) chScale = chScale << 4; // scale 1X to 16X
102// scale the channel values
103channel0 = (ch0 * chScale) >> CH_SCALE;
104channel1 = (ch1 * chScale) >> CH_SCALE;
105
106 ratio1 = 0;
107 if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
108// round the ratio value
109 unsigned long ratio = (ratio1 + 1) >> 1;
110
111 switch (iType)
112 {
113 case 0: // T package
114 if ((ratio >= 0) && (ratio <= K1T))
115 {b=B1T; m=M1T;}
116 else if (ratio <= K2T)
117 {b=B2T; m=M2T;}
118 else if (ratio <= K3T)
119 {b=B3T; m=M3T;}
120 else if (ratio <= K4T)
121 {b=B4T; m=M4T;}
122 else if (ratio <= K5T)
123 {b=B5T; m=M5T;}
124 else if (ratio <= K6T)
125 {b=B6T; m=M6T;}
126 else if (ratio <= K7T)
127 {b=B7T; m=M7T;}
128 else if (ratio > K8T)
129 {b=B8T; m=M8T;}
130 break;
131 case 1:// CS package
132 if ((ratio >= 0) && (ratio <= K1C))
133 {b=B1C; m=M1C;}
134 else if (ratio <= K2C)
135 {b=B2C; m=M2C;}
136 else if (ratio <= K3C)
137 {b=B3C; m=M3C;}
138 else if (ratio <= K4C)
139 {b=B4C; m=M4C;}
140 else if (ratio <= K5C)
141 {b=B5C; m=M5C;}
142 else if (ratio <= K6C)
143 {b=B6C; m=M6C;}
144 else if (ratio <= K7C)
145 {b=B7C; m=M7C;}
146 }
147 temp=((channel0*b)-(channel1*m));
148 if(temp<0) temp=0;
149 temp+=(1<<(LUX_SCALE-1));
150 // strip off fractional portion
151 lux=temp>>LUX_SCALE;
152 return (lux);
153 }
154 TSL2561_CalculateLux TSL2561;
155
156
Note: See TracBrowser for help on using the repository browser.