Skip to content

Commit 017633b

Browse files
committed
Add auto-detection for color LED deck variants in examples
Color LED examples now detect which deck variant is present (bottom or top-facing) and use the first one found. This keeps examples simple while supporting both colorLedBot and colorLedTop parameter groups.
1 parent 5865350 commit 017633b

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

examples/color_led_deck/color_led_cycle.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,40 @@ def pack_wrgb(input: wrgb) -> int:
9494
with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
9595
cf = scf.cf
9696

97+
# Detect which color LED deck is present (bottom or top)
98+
# We check for whichever deck is attached and use the first one found
99+
# Check for bottom-facing deck first
100+
if str(cf.param.get_value('deck.bcColorLedBot')) == '1':
101+
deck_params = {
102+
'color': 'colorLedBot.wrgb8888',
103+
'brightness': 'colorLedBot.brightCorr',
104+
'throttle': 'colorLedBot.throttlePct',
105+
'temp': 'colorLedBot.deckTemp'
106+
}
107+
print('Detected bottom-facing color LED deck')
108+
# Check for top-facing deck
109+
elif str(cf.param.get_value('deck.bcColorLedTop')) == '1':
110+
deck_params = {
111+
'color': 'colorLedTop.wrgb8888',
112+
'brightness': 'colorLedTop.brightCorr',
113+
'throttle': 'colorLedTop.throttlePct',
114+
'temp': 'colorLedTop.deckTemp'
115+
}
116+
print('Detected top-facing color LED deck')
117+
else:
118+
raise RuntimeError('No color LED deck detected!')
119+
97120
# Thermal status callback
98121
def thermal_status_callback(timestamp, data, logconf):
99-
throttle_pct = data['colorled.throttlePct']
122+
throttle_pct = data[deck_params['throttle']]
100123
if throttle_pct > 0:
101-
temp = data['colorled.deckTemp']
124+
temp = data[deck_params['temp']]
102125
print(f'WARNING: Thermal throttling active! Temp: {temp}°C, Throttle: {throttle_pct}%')
103126

104127
# Setup log configuration for thermal monitoring
105128
log_conf = LogConfig(name='ThermalStatus', period_in_ms=100)
106-
log_conf.add_variable('colorled.deckTemp', 'uint8_t')
107-
log_conf.add_variable('colorled.throttlePct', 'uint8_t')
129+
log_conf.add_variable(deck_params['temp'], 'uint8_t')
130+
log_conf.add_variable(deck_params['throttle'], 'uint8_t')
108131

109132
cf.log.add_config(log_conf)
110133
log_conf.data_received_cb.add_callback(thermal_status_callback)
@@ -113,7 +136,7 @@ def thermal_status_callback(timestamp, data, logconf):
113136
# Brightness correction: balances luminance across W/R/G/B channels
114137
# Set to 1 (enabled, default) for perceptually uniform colors
115138
# Set to 0 (disabled) for maximum brightness per channel
116-
cf.param.set_value('colorled.brightnessCorr', 1)
139+
cf.param.set_value(deck_params['brightness'], 1)
117140
time.sleep(0.1)
118141

119142
try:
@@ -123,9 +146,9 @@ def thermal_status_callback(timestamp, data, logconf):
123146
color: wrgb = cycle_colors(i)
124147
# print(color.r, color.g, color.b)
125148
color_uint32 = pack_wrgb(color)
126-
cf.param.set_value('colorled.wrgb8888', str(color_uint32))
149+
cf.param.set_value(deck_params['color'], str(color_uint32))
127150
time.sleep(0.01)
128151
except KeyboardInterrupt:
129152
print('\nStopping and turning off LED...')
130-
cf.param.set_value('colorled.wrgb8888', '0')
153+
cf.param.set_value(deck_params['color'], '0')
131154
time.sleep(0.1)

examples/color_led_deck/color_led_set.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,40 @@ def pack_wrgb(input: wrgb | rgb) -> int:
5959
with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
6060
cf = scf.cf
6161

62+
# Detect which color LED deck is present (bottom or top)
63+
# We check for whichever deck is attached and use the first one found
64+
# Check for bottom-facing deck first
65+
if str(cf.param.get_value('deck.bcColorLedBot')) == '1':
66+
deck_params = {
67+
'color': 'colorLedBot.wrgb8888',
68+
'brightness': 'colorLedBot.brightCorr',
69+
'throttle': 'colorLedBot.throttlePct',
70+
'temp': 'colorLedBot.deckTemp'
71+
}
72+
print('Detected bottom-facing color LED deck')
73+
# Check for top-facing deck
74+
elif str(cf.param.get_value('deck.bcColorLedTop')) == '1':
75+
deck_params = {
76+
'color': 'colorLedTop.wrgb8888',
77+
'brightness': 'colorLedTop.brightCorr',
78+
'throttle': 'colorLedTop.throttlePct',
79+
'temp': 'colorLedTop.deckTemp'
80+
}
81+
print('Detected top-facing color LED deck')
82+
else:
83+
raise RuntimeError('No color LED deck detected!')
84+
6285
# Thermal status callback
6386
def thermal_status_callback(timestamp, data, logconf):
64-
throttle_pct = data['colorled.throttlePct']
87+
throttle_pct = data[deck_params['throttle']]
6588
if throttle_pct > 0:
66-
temp = data['colorled.deckTemp']
89+
temp = data[deck_params['temp']]
6790
print(f'WARNING: Thermal throttling active! Temp: {temp}°C, Throttle: {throttle_pct}%')
6891

6992
# Setup log configuration for thermal monitoring
7093
log_conf = LogConfig(name='ThermalStatus', period_in_ms=100)
71-
log_conf.add_variable('colorled.deckTemp', 'uint8_t')
72-
log_conf.add_variable('colorled.throttlePct', 'uint8_t')
94+
log_conf.add_variable(deck_params['temp'], 'uint8_t')
95+
log_conf.add_variable(deck_params['throttle'], 'uint8_t')
7396

7497
cf.log.add_config(log_conf)
7598
log_conf.data_received_cb.add_callback(thermal_status_callback)
@@ -78,7 +101,7 @@ def thermal_status_callback(timestamp, data, logconf):
78101
# Brightness correction: balances luminance across R/G/B/W channels
79102
# Set to 1 (enabled, default) for perceptually uniform colors
80103
# Set to 0 (disabled) for maximum brightness per channel
81-
cf.param.set_value('colorled.brightnessCorr', 1)
104+
cf.param.set_value(deck_params['brightness'], 1)
82105
time.sleep(0.1)
83106

84107
try:
@@ -97,13 +120,13 @@ def thermal_status_callback(timestamp, data, logconf):
97120
# ========================================
98121

99122
color_uint32 = pack_wrgb(color)
100-
cf.param.set_value('colorled.wrgb8888', color_uint32)
123+
cf.param.set_value(deck_params['color'], color_uint32)
101124
time.sleep(0.01)
102125
print(f'Setting LED to R={color.r}, G={color.g}, B={color.b}, W={color.w}')
103126
print('Press Ctrl-C to turn off LED and exit.')
104127
while True:
105128
time.sleep(0.1)
106129
except KeyboardInterrupt:
107130
print('\nStopping and turning off LED...')
108-
cf.param.set_value('colorled.wrgb8888', 0)
131+
cf.param.set_value(deck_params['color'], 0)
109132
time.sleep(0.1)

0 commit comments

Comments
 (0)