|
1 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
|
2 | 2 | # 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 | + |
3 | 11 | import time
|
4 |
| -from time import sleep |
5 | 12 |
|
6 | 13 | import board
|
7 | 14 |
|
|
22 | 29 |
|
23 | 30 | print("Adafruit OPT4048 Tristimulus XYZ Color Sensor Test")
|
24 | 31 |
|
| 32 | +# Initialize the sensor |
25 | 33 | i2c = board.I2C() # uses board.SCL and board.SDA
|
26 | 34 | # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
|
27 | 35 | sensor = OPT4048(i2c)
|
28 |
| - |
29 | 36 | print("OPT4048 sensor found!")
|
30 | 37 |
|
| 38 | +# Set low and high thresholds for interrupts |
31 | 39 | low_threshold_val = 1000
|
32 | 40 | print(f"Setting low threshold to: {low_threshold_val}")
|
33 | 41 | sensor.threshold_low = low_threshold_val
|
|
36 | 44 | print(f"Setting high threshold to: {high_threshold_val}")
|
37 | 45 | sensor.threshold_high = high_threshold_val
|
38 | 46 |
|
| 47 | +# Read back the thresholds to verify |
39 | 48 | print(f"Read back low threshold: {sensor.threshold_low}")
|
40 | 49 | print(f"Read back high threshold: {sensor.threshold_high}")
|
41 | 50 |
|
| 51 | +# Enable Quick Wake feature |
42 | 52 | print("\nEnabling Quick Wake feature...")
|
43 | 53 | sensor.qick_wake = True
|
| 54 | + |
| 55 | +# Read back Quick Wake status |
44 | 56 | print(f"Quick Wake status: {sensor.quick_wake}")
|
45 | 57 |
|
46 | 58 | # Set range to auto
|
|
69 | 81 | print("\nConfiguring interrupt settings...")
|
70 | 82 | sensor.interrupt_latch = True
|
71 | 83 | sensor.interrupt_polarity = True
|
| 84 | + |
| 85 | +# Read back interrupt settings |
72 | 86 | print(f"Interrupt latch mode: {'Latched' if sensor.interrupt_latch else 'Transparent'}")
|
73 | 87 | print(f"Interrupt polarity: {'Active-high' if sensor.interrupt_polarity else 'Active-low'}")
|
74 | 88 |
|
| 89 | +# Configure fault count |
75 | 90 | print("\nSetting fault count to 4 consecutive faults...")
|
76 | 91 | sensor.fault_count = FaultCount.COUNT_4
|
| 92 | +# Read back fault count setting |
77 | 93 | print(
|
78 | 94 | f"Fault count setting value: {sensor.fault_count} "
|
79 | 95 | + f"name: {FaultCount.get_name(sensor.fault_count)}"
|
80 | 96 | )
|
81 | 97 |
|
| 98 | +# Configure threshold channel |
82 | 99 | print("\nSetting threshold channel to Channel 1 (Y)...")
|
83 | 100 | sensor.threshold_channel = 1
|
84 | 101 | channels = {
|
|
87 | 104 | 2: "(Z)",
|
88 | 105 | 3: "(W)",
|
89 | 106 | }
|
| 107 | +# Read back threshold channel setting |
90 | 108 | print(f"Threshold channel setting: Channel {channels[sensor.threshold_channel]}")
|
91 | 109 |
|
| 110 | +# Configure interrupt configuration |
92 | 111 | print("\nSetting interrupt configuration to data ready for all channels...")
|
93 | 112 | sensor.interrupt_config = IntConfig.DATA_READY_ALL
|
| 113 | + |
| 114 | +# Read back interrupt configuration setting |
94 | 115 | print(
|
95 | 116 | f"Interrupt configuration value: {sensor.interrupt_config} "
|
96 | 117 | + f"name: {IntConfig.get_name(sensor.interrupt_config)}"
|
|
135 | 156 | except RuntimeError:
|
136 | 157 | print("Error reading sensor data")
|
137 | 158 |
|
138 |
| - time.sleep(1) |
| 159 | + time.sleep(1) # read once per second |
0 commit comments