Yet Another WebIOPi+
 All Classes Namespaces Files Functions Variables Macros Pages
tslXXXX.py
Go to the documentation of this file.
1 # Copyright 2013 - 2014 Andreas Riegg
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 #
16 # Changelog
17 #
18 # 1.0 2013/02/28 Initial release
19 # 1.1 2014/04/03 Fixed issue 90, applied same update to __setTime__
20 # Corrected typo channel1value to channel1_value
21 # Corrected typo channelRatio to channel_ratio
22 # Moved robustness checks to avoid division by zero
23 #
24 
25 from webiopi.utils.types import toint
26 from webiopi.devices.i2c import I2C
27 from webiopi.devices.sensor import Luminosity
28 
29 
31  VAL_COMMAND = 0x80
32  REG_CONTROL = 0x00 | VAL_COMMAND
33  REG_CONFIG = 0x01 | VAL_COMMAND
34 
35  VAL_PWON = 0x03
36  VAL_PWOFF = 0x00
37  VAL_INVALID = -1
38 
39  def __init__(self, slave, time, name="TSL_LIGHT_X"):
40  I2C.__init__(self, toint(slave))
41  self.name = name
42  self.wake() # devices are powered down after power reset, wake them
43  self.setTime(toint(time))
44 
45  def __str__(self):
46  return "%s(slave=0x%02X)" % (self.name, self.slave)
47 
48  def wake(self):
49  self.__wake__()
50 
51  def __wake__(self):
52  self.writeRegister(self.REG_CONTROL, self.VAL_PWON)
53 
54  def sleep(self):
55  self.__sleep__()
56 
57  def __sleep__(self):
58  self.writeRegister(self.REG_CONTROL, self.VAL_PWOFF)
59 
60  def setTime(self, time):
61  self.__setTime__(time)
62 
63  def getTime(self):
64  return self.__getTime__()
65 
67  VAL_TIME_402_MS = 0x02
68  VAL_TIME_101_MS = 0x01
69  VAL_TIME_14_MS = 0x00
70 
71  REG_CHANNEL_0_LOW = 0x0C | TSL_LIGHT_X.VAL_COMMAND
72  REG_CHANNEL_1_LOW = 0x0E | TSL_LIGHT_X.VAL_COMMAND
73 
74  MASK_GAIN = 0x10
75  MASK_TIME = 0x03
76 
77  def __init__(self, slave, time, gain, name="TSL2561X"):
78  TSL_LIGHT_X.__init__(self, slave, time, name)
79  self.setGain(toint(gain))
80 
81  def __getLux__(self):
82  ch0_bytes = self.readRegisters(self.REG_CHANNEL_0_LOW, 2)
83  ch1_bytes = self.readRegisters(self.REG_CHANNEL_1_LOW, 2)
84  ch0_word = ch0_bytes[1] << 8 | ch0_bytes[0]
85  ch1_word = ch1_bytes[1] << 8 | ch1_bytes[0]
86  scaling = self.time_multiplier * self.gain_multiplier
87  return self.__calculateLux__(scaling * ch0_word, scaling * ch1_word)
88 
89  def setGain(self, gain):
90  if gain == 1:
91  bit_gain = 0
92  self.gain_multiplier = 16
93  elif gain == 16:
94  bit_gain = 1
95  self.gain_multiplier = 1
96  else:
97  raise ValueError("Gain %d out of range [%d,%d]" % (gain, 1, 16))
98  new_byte_gain = (bit_gain << 4) & self.MASK_GAIN
99 
100  current_byte_config = self.readRegister(self.REG_CONFIG)
101  new_byte_config = (current_byte_config & ~self.MASK_GAIN) | new_byte_gain
102  self.writeRegister(self.REG_CONFIG, new_byte_config)
103 
104  def getGain(self):
105  current_byte_config = self.readRegister(self.REG_CONFIG)
106  if (current_byte_config & self.MASK_GAIN):
107  return 16
108  else:
109  return 1
110 
111  def __setTime__(self, time):
112  if not time in [14, 101, 402]:
113  raise ValueError("Time %d out of range [%d,%d,%d]" % (time, 14, 101, 402))
114  if time == 402:
115  bits_time = self.VAL_TIME_402_MS
117  elif time == 101:
118  bits_time = self.VAL_TIME_101_MS
119  self.time_multiplier = 322 / 81.0
120  elif time == 14:
121  bits_time = self.VAL_TIME_14_MS
122  self.time_multiplier = 322 / 11.0
123  new_byte_time = bits_time & self.MASK_TIME
124 
125  current_byte_config = self.readRegister(self.REG_CONFIG)
126  new_byte_config = (current_byte_config & ~self.MASK_TIME) | new_byte_time
127  self.writeRegister(self.REG_CONFIG, new_byte_config)
128 
129  def __getTime__(self):
130  current_byte_config = self.readRegister(self.REG_CONFIG)
131  bits_time = (current_byte_config & self.MASK_TIME)
132  if bits_time == self.VAL_TIME_402_MS:
133  t = 402
134  elif bits_time == self.VAL_TIME_101_MS:
135  t = 101
136  elif bits_time == self.VAL_TIME_14_MS:
137  t = 14
138  else:
139  t = TSL_LIGHT_X.VAL_INVALID # indicates undefined
140  return t
141 
143  # Package CS (Chipscale) chip version
144  def __init__(self, slave=0x39, time=402, gain=1):
145  TSL2561X.__init__(self, slave, time, gain, "TSL2561CS")
146 
147  def __calculateLux__(self, channel0_value, channel1_value):
148  if float(channel0_value) == 0.0: # driver robustness, avoid division by zero
149  return self.VAL_INVALID
150 
151  channel_ratio = channel1_value / float(channel0_value)
152  if 0 < channel_ratio <= 0.52:
153  lux = 0.0315 * channel0_value - 0.0593 * channel0_value *(channel_ratio**1.4)
154  elif 0.52 < channel_ratio <= 0.65:
155  lux = 0.0229 * channel0_value - 0.0291 * channel1_value
156  elif 0.65 < channel_ratio <= 0.80:
157  lux = 0.0157 * channel0_value - 0.0180 * channel1_value
158  elif 0.80 < channel_ratio <= 1.30:
159  lux = 0.00338 * channel0_value - 0.00260 * channel1_value
160  else: # if channel_ratio > 1.30
161  lux = 0
162  return lux
163 
165  # Package T (TMB-6) chip version
166  def __init__(self, slave=0x39, time=402, gain=1):
167  TSL2561X.__init__(self, slave, time, gain, "TSL2561T")
168 
169  def __calculateLux__(self, channel0_value, channel1_value):
170  if float(channel0_value) == 0.0: # driver robustness, avoid division by zero
171  return self.VAL_INVALID
172 
173  channel_ratio = channel1_value / float(channel0_value)
174  if 0 < channel_ratio <= 0.50:
175  lux = 0.0304 * channel0_value - 0.062 * channel0_value * (channel_ratio**1.4)
176  elif 0.50 < channel_ratio <= 0.61:
177  lux = 0.0224 * channel0_value - 0.031 * channel1_value
178  elif 0.61 < channel_ratio <= 0.80:
179  lux = 0.0128 * channel0_value - 0.0153 * channel1_value
180  elif 0.80 < channel_ratio <= 1.30:
181  lux = 0.00146 * channel0_value - 0.00112 * channel1_value
182  else: # if channel_ratio > 1.30
183  lux = 0
184  return lux
185 
187  # Default version for unknown packages, uses T Package class lux calculation
188  def __init__(self, slave=0x39, time=402, gain=1):
189  TSL2561X.__init__(self, slave, time, gain, "TSL2561")
190 
191 
193  # Default version for unknown subtypes, uses 0x29 as slave address
194  VAL_TIME_400_MS = 0x00
195  VAL_TIME_200_MS = 0x01
196  VAL_TIME_100_MS = 0x02
197 
198  REG_DATA_LOW = 0x04 | TSL_LIGHT_X.VAL_COMMAND
199 
200  MASK_TCNTRL = 0x03
201 
202  def __init__(self, slave=0x29, time=400, name="TSL4531"):
203  TSL_LIGHT_X.__init__(self, slave, time, name)
204 
205  def __setTime__(self, time):
206  if not time in [100, 200, 400]:
207  raise ValueError("Time %d out of range [%d,%d,%d]" % (time, 100, 200, 400))
208  if time == 400:
209  bits_time = self.VAL_TIME_400_MS
211  elif time == 200:
212  bits_time = self.VAL_TIME_200_MS
213  self.time_multiplier = 2
214  elif time == 100:
215  bits_time = self.VAL_TIME_100_MS
216  self.time_multiplier = 4
217  new_byte_time = bits_time & self.MASK_TCNTRL
218 
219  current_byte_config = self.readRegister(self.REG_CONFIG)
220  new_byte_config = (current_byte_config & ~self.MASK_TCNTRL) | new_byte_time
221  self.writeRegister(self.REG_CONFIG, new_byte_config)
222 
223  def __getTime__(self):
224  current_byte_config = self.readRegister(self.REG_CONFIG)
225  bits_time = (current_byte_config & self.MASK_TCNTRL)
226  if bits_time == self.VAL_TIME_400_MS:
227  t = 400
228  elif bits_time == self.VAL_TIME_200_MS:
229  t = 200
230  elif bits_time == self.VAL_TIME_100_MS:
231  t = 100
232  else:
233  t = TSL_LIGHT_X.VAL_INVALID # indicates undefined
234  return t
235 
236  def __getLux__(self):
237  data_bytes = self.readRegisters(self.REG_DATA_LOW, 2)
238  return self.time_multiplier * (data_bytes[1] << 8 | data_bytes[0])
239 
241  def __init__(self, slave=0x39, time=400):
242  TSL4531.__init__(self, slave, time, "TSL45311")
243 
245  def __init__(self, slave=0x39, time=400):
246  TSL4531.__init__(self, slave, time, "TSL45313")
247 
249  def __init__(self, slave=0x29, time=400):
250  TSL4531.__init__(self, slave, time, "TSL45315")
251 
253  def __init__(self, slave=0x29, time=400):
254  TSL4531.__init__(self, slave, time, "TSL45317")
255