Skip to content

Update to the test file of the TCS34725 color sensor #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 30, 2021
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
6 changes: 6 additions & 0 deletions adafruit_tcs34725.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ def color_rgb_bytes(self):
red, green, blue component values as bytes (0-255).
"""
r, g, b, clear = self.color_raw

# Avoid divide by zero errors ... if clear = 0 return black
if clear == 0:
return (0, 0, 0)

# Each color value is normalized to clear, to obtain int values between 0 and 255.
# A gamma correction of 2.5 is applied to each value as well, first dividing by 255,
# since gamma is applied to values between 0 and 1
red = int(pow((int((r / clear) * 256) / 255), 2.5) * 255)
green = int(pow((int((g / clear) * 256) / 255), 2.5) * 255)
blue = int(pow((int((b / clear) * 256) / 255), 2.5) * 255)

# Handle possible 8-bit overflow
if red > 255:
red = 255
Expand Down
19 changes: 18 additions & 1 deletion examples/tcs34725_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = adafruit_tcs34725.TCS34725(i2c)

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

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

# Main loop reading color and printing it every second.
while True:
# Raw data from the sensor in a 4-tuple of red, green, blue, clear light component values
# print(sensor.color_raw)

color = sensor.color
color_rgb = sensor.color_rgb_bytes
print(
"RGB color as 8 bits per channel int: #{0:02X} or as 3-tuple: {1}".format(
color, color_rgb
)
)

# Read the color temperature and lux of the sensor too.
temp = sensor.color_temperature
lux = sensor.lux
print("Temperature: {0}K Lux: {1}".format(temp, lux))
print("Temperature: {0}K Lux: {1}\n".format(temp, lux))
# Delay for a second and repeat.
time.sleep(1.0)