Skip to content

Commit 8a4bd09

Browse files
committed
add oneshot example and more comments.
1 parent 3ae8e96 commit 8a4bd09

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

examples/opt4048_fulltest.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
22
# SPDX-License-Identifier: MIT
3+
"""
4+
A comprehensive demo for using the OPT4048 tristimulus XYZ color sensor,
5+
showing all capabilities.
6+
7+
This example reads the sensor values from all four channels (X, Y, Z, W),
8+
demonstrates setting and getting threshold values, and displays the results.
9+
"""
10+
311
import time
4-
from time import sleep
512

613
import board
714

@@ -22,12 +29,13 @@
2229

2330
print("Adafruit OPT4048 Tristimulus XYZ Color Sensor Test")
2431

32+
# Initialize the sensor
2533
i2c = board.I2C() # uses board.SCL and board.SDA
2634
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
2735
sensor = OPT4048(i2c)
28-
2936
print("OPT4048 sensor found!")
3037

38+
# Set low and high thresholds for interrupts
3139
low_threshold_val = 1000
3240
print(f"Setting low threshold to: {low_threshold_val}")
3341
sensor.threshold_low = low_threshold_val
@@ -36,11 +44,15 @@
3644
print(f"Setting high threshold to: {high_threshold_val}")
3745
sensor.threshold_high = high_threshold_val
3846

47+
# Read back the thresholds to verify
3948
print(f"Read back low threshold: {sensor.threshold_low}")
4049
print(f"Read back high threshold: {sensor.threshold_high}")
4150

51+
# Enable Quick Wake feature
4252
print("\nEnabling Quick Wake feature...")
4353
sensor.qick_wake = True
54+
55+
# Read back Quick Wake status
4456
print(f"Quick Wake status: {sensor.quick_wake}")
4557

4658
# Set range to auto
@@ -69,16 +81,21 @@
6981
print("\nConfiguring interrupt settings...")
7082
sensor.interrupt_latch = True
7183
sensor.interrupt_polarity = True
84+
85+
# Read back interrupt settings
7286
print(f"Interrupt latch mode: {'Latched' if sensor.interrupt_latch else 'Transparent'}")
7387
print(f"Interrupt polarity: {'Active-high' if sensor.interrupt_polarity else 'Active-low'}")
7488

89+
# Configure fault count
7590
print("\nSetting fault count to 4 consecutive faults...")
7691
sensor.fault_count = FaultCount.COUNT_4
92+
# Read back fault count setting
7793
print(
7894
f"Fault count setting value: {sensor.fault_count} "
7995
+ f"name: {FaultCount.get_name(sensor.fault_count)}"
8096
)
8197

98+
# Configure threshold channel
8299
print("\nSetting threshold channel to Channel 1 (Y)...")
83100
sensor.threshold_channel = 1
84101
channels = {
@@ -87,10 +104,14 @@
87104
2: "(Z)",
88105
3: "(W)",
89106
}
107+
# Read back threshold channel setting
90108
print(f"Threshold channel setting: Channel {channels[sensor.threshold_channel]}")
91109

110+
# Configure interrupt configuration
92111
print("\nSetting interrupt configuration to data ready for all channels...")
93112
sensor.interrupt_config = IntConfig.DATA_READY_ALL
113+
114+
# Read back interrupt configuration setting
94115
print(
95116
f"Interrupt configuration value: {sensor.interrupt_config} "
96117
+ f"name: {IntConfig.get_name(sensor.interrupt_config)}"
@@ -135,4 +156,4 @@
135156
except RuntimeError:
136157
print("Error reading sensor data")
137158

138-
time.sleep(1)
159+
time.sleep(1) # read once per second

examples/opt4048_oneshot.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
A basic one shotdemo for using the OPT4048 tristimulus XYZ color sensor
5+
6+
This example reads the sensor values from all four channels (X, Y, Z, W),
7+
demonstrates setting and getting threshold values, and displays the results.
8+
9+
Readints are taken in oneshot mode and then the next one is triggered when
10+
the power down mode status is detected.
11+
"""
12+
13+
import time
14+
from time import sleep
15+
16+
import board
17+
18+
from adafruit_opt4048 import OPT4048, ConversionTime, Mode, Range
19+
20+
timestamp = 0
21+
22+
print("Adafruit OPT4048 Tristimulus XYZ Color Sensor Oneshot Test")
23+
24+
i2c = board.I2C() # uses board.SCL and board.SDA
25+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
26+
sensor = OPT4048(i2c)
27+
print("OPT4048 sensor found!")
28+
29+
sensor.range = Range.AUTO
30+
sensor.conversion_time = ConversionTime.TIME_100MS
31+
sensor.mode = Mode.AUTO_ONESHOT
32+
while True:
33+
if sensor.mode == Mode.POWERDOWN:
34+
# ok we finished the reading!
35+
try:
36+
CIEx, CIEy, lux = sensor.get_cie()
37+
except RuntimeError:
38+
print("Error reading sensor data")
39+
40+
print("\nCIE Coordinates:")
41+
print(f"CIE x:{CIEx}, y:{CIEy}, lux: {lux}", end=" ")
42+
43+
# Calculate and display color temperature
44+
color_temp = sensor.calculate_color_temperature(CIEx, CIEy)
45+
print(f"Color Temperature: {color_temp} K")
46+
print(f"Time since last read: {time.monotonic() - timestamp} sec")
47+
timestamp = time.monotonic()
48+
49+
sensor.mode = Mode.AUTO_ONESHOT

examples/opt4048_simpletest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
22
# SPDX-License-Identifier: MIT
3+
"""
4+
A basic demo for using the OPT4048 tristimulus XYZ color sensor
5+
6+
This example reads the sensor values from all four channels (X, Y, Z, W),
7+
demonstrates setting and getting threshold values, and displays the results.
8+
"""
9+
310
import time
411
from time import sleep
512

examples/opt4048_webserial.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
22
# SPDX-License-Identifier: MIT
3+
"""
4+
This example reads color data from the OPT4048 sensor and outputs it
5+
in a format suitable for displaying on a web page using Web Serial API.
6+
7+
It continuously measures CIE x,y coordinates, lux, and color temperature.
8+
9+
This example works with the web interface in the /webserial directory of the
10+
gh-pages branch of the Arduino driver repo: https://github.com/adafruit/Adafruit_OPT4048/tree/gh-pages,
11+
which can be accessed at: https://adafruit.github.io/Adafruit_OPT4048/webserial/
12+
"""
13+
314
import time
415
from time import sleep
516

0 commit comments

Comments
 (0)