Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/TCS34725Config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# example configuration for couning rate on GPIO pins
# example configuration for counting rate on GPIO pins

DAQModule: TCS34725Config

Expand Down
Binary file not shown.
86 changes: 30 additions & 56 deletions phypidaq/TCS34725Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
from __future__ import print_function, division, unicode_literals
from __future__ import absolute_import

import sys

# import relevant pieces for TCS34725 RGB (color) sensor
import Adafruit_TCS34725 as TCS34725
import board
import adafruit_tcs34725


class TCS34725Config(object):
Expand All @@ -22,92 +21,67 @@ def __init__(self, confdict=None):
self.NChannels = 3

if 'Gain' in confdict:
gains = (TCS34725.TCS34725_GAIN_1X,
TCS34725.TCS34725_GAIN_4X,
TCS34725.TCS34725_GAIN_16X,
TCS34725.TCS34725_GAIN_60X)
self.gain = gains(confdict['Gain'])
gains = (1, 4, 16, 60)
if isinstance(confdict["Gain"], int):
self.gain = gains(confdict['Gain'])
else:
self.gain = 4
else:
self.gain = TCS34725.TCS34725_GAIN_60X
self.gain = 4
# Possible gain values:
# - TCS34725_GAIN_1X
# - TCS34725_GAIN_4X (default)
# - TCS34725_GAIN_16X
# - TCS34725_GAIN_60X

if 'IntegrationTime' in confdict:
IntTs = (TCS34725.TCS34725_INTEGRATIONTIME_2_4MS,
TCS34725.TCS34725_INTEGRATIONTIME_24MS,
TCS34725.TCS34725_INTEGRATIONTIME_50MS,
TCS34725.TCS34725_INTEGRATIONTIME_101MS,
TCS34725.TCS34725_INTEGRATIONTIME_154MS,
TCS34725.TCS34725_INTEGRATIONTIME_700MS)
maxVals = (1024, 10240, 20480, 43008, 65535, 65535)
self.IntT = IntTs(confdict['IntegrationTime'])
self.maxCount = maxVals(confdict['IntegrationTime'])
Int_Times = (2.4, 24, 50, 101, 154, 700)
self.IntTime = Int_Times(confdict['IntegrationTime'])
else:
self.IntT = TCS34725.TCS34725_INTEGRATIONTIME_2_4MS
self.maxCount = 1024. # 10 bit
# Possible integration time values:
# - TCS34725_INTEGRATIONTIME_2_4MS = 2.4ms - 1 cycle - Max Count: 1024 (default)
# - TCS34725_INTEGRATIONTIME_24MS = 24ms - 10 cycles - Max Count: 10240
# - TCS34725_INTEGRATIONTIME_50MS = 50ms - 20 cycles - Max Count: 20480
# - TCS34725_INTEGRATIONTIME_101MS = 101ms - 42 cycles - Max Count: 43008
# - TCS34725_INTEGRATIONTIME_154MS = 154ms - 64 cycles - Max Count: 65535
# - TCS34725_INTEGRATIONTIME_700MS = 700ms - 256 cycles - Max Count: 65535
self.IntT = 2.4

if 'I2CADDR' in confdict:
self.I2CAddr = confdict['I2CADDR']
print("TCS34725: I2C address set to %x " % (self.I2CAddr))
print("TCS34725: I2C address set to %x " % self.I2CAddr)
else:
self.I2CAddr = 0x29 # use default

if 'busnum' in confdict:
self.busnum = confdict['busnum']
print("TCS34725: bus number set to %x " % (self.busnum))
print("TCS34725: bus number set to %x " % self.busnum)
else:
self.busnum = 1 # use default

# provide configuration parameters
self.ChanNams = ['R', 'G', 'B', 'c']
self.ChanLims = [[0., 1.]] * self.NChannels
self.sensor = None

def init(self):
# Hardware configuration:
try:
# Create a TCS34725 instance
self.tcs = TCS34725.TCS34725(address=self.I2CAddr, busnum=self.busnum,
integration_time=self.IntT, gain=self.gain)
except Exception as e:
print("TCS34725Config: Error initialising device - exit")
print(str(e))
sys.exit(1)

# Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too).
self.tcs.set_interrupt(False)

# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
self.sensor = adafruit_tcs34725.TCS34725(i2c, address=self.I2CAddr)

# Change sensor integration time to values between 2.4 and 614.4 milliseconds
self.sensor.integration_time = self.IntTime

# Change sensor gain to 1, 4, 16, or 60
self.sensor.gain = self.gain

def acquireData(self, buf):

# Read the R, G, B, C color data.
r, g, b, c = self.tcs.get_raw_data()
if self.NChannels == 1:
buf[0] = c / self.maxCount
buf[0] = self.sensor.lux
else:
buf[0] = r / self.maxCount
buf[1] = g / self.maxCount
buf[2] = b / self.maxCount
colors = self.sensor.color_rgb_bytes
buf[0] = colors[0]
buf[1] = colors[1]
buf[2] = colors[2]
if self.NChannels > 3:
buf[3] = c / self.maxCount

# there are some additional functions in Adafruit driver:
# calculate color temperature
# color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
# calculate lux with another utility function.
# lux = Adafruit_TCS34725.calculate_lux(r, g, b)

# Enable interrupts and put the chip back to low power sleep/disabled.
# self.tcs.set_interrupt(True)
# self.tcs.disable()
buf[3] = self.sensor.lux

def closeDevice(self):
# nothing to do here
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ adafruit-circuitpython-max31865==2.2.12
adafruit-circuitpython-ads1x15==2.2.21
adafruit-circuitpython-adxl34x==1.12.8
adafruit-circuitpython-mcp4725==1.4.11
adafruit-circuitpython-tcs34725==3.3.16
Adafruit-PlatformDetect==3.15.3
Adafruit-PureIO==1.1.9
pyftdi==0.53.2
Expand Down