@@ -12,18 +12,18 @@ def demo_light_discovery():
12
12
"""Show what lights are available and their names."""
13
13
print ("🔍 Light Discovery" )
14
14
print ("=" * 50 )
15
-
15
+
16
16
with LightController () as controller :
17
17
print (f"Total lights detected: { len (controller )} " )
18
-
18
+
19
19
if controller :
20
20
print ("\n Available lights:" )
21
21
for i , name in enumerate (controller .list_lights ()):
22
22
print (f" { i } : { name } " )
23
23
else :
24
24
print ("No lights found - connect some USB lights to see the demo!" )
25
25
return False
26
-
26
+
27
27
print ()
28
28
return True
29
29
@@ -32,90 +32,90 @@ def demo_select_all():
32
32
"""Demonstrate selecting all lights."""
33
33
print ("🌟 Select All Lights" )
34
34
print ("=" * 50 )
35
-
35
+
36
36
with LightController () as controller :
37
37
selection = controller .all ()
38
38
print (f"Selected { len (selection )} lights: { selection .names ()} " )
39
-
39
+
40
40
if selection :
41
41
print ("Turning all lights GREEN for 2 seconds..." )
42
42
selection .turn_on ("green" )
43
43
time .sleep (2 )
44
44
selection .turn_off ()
45
-
45
+
46
46
print ()
47
47
48
48
49
49
def demo_select_by_index ():
50
50
"""Demonstrate selecting lights by index."""
51
51
print ("🎯 Select by Index" )
52
52
print ("=" * 50 )
53
-
53
+
54
54
with LightController () as controller :
55
55
# First light only
56
56
selection = controller .by_index (0 )
57
57
print (f"First light: { selection .names ()} " )
58
-
58
+
59
59
if selection :
60
60
print ("Blinking first light BLUE 3 times..." )
61
61
selection .blink ("blue" , count = 3 , speed = "fast" )
62
62
time .sleep (2 )
63
-
63
+
64
64
# Multiple specific indices
65
65
if len (controller ) >= 3 :
66
66
selection = controller .by_index (0 , 2 )
67
67
print (f"Lights 0 and 2: { selection .names ()} " )
68
-
68
+
69
69
print ("Turning lights 0 and 2 RED..." )
70
70
selection .turn_on ("red" )
71
71
time .sleep (1 )
72
72
selection .turn_off ()
73
-
73
+
74
74
print ()
75
75
76
76
77
77
def demo_select_by_name ():
78
78
"""Demonstrate selecting lights by exact name."""
79
- print ("📛 Select by Name" )
79
+ print ("📛 Select by Name" )
80
80
print ("=" * 50 )
81
-
81
+
82
82
with LightController () as controller :
83
83
available_names = controller .list_lights ()
84
-
84
+
85
85
if available_names :
86
86
# Select first available light by name
87
87
first_name = available_names [0 ]
88
88
selection = controller .by_name (first_name )
89
89
print (f"Selected '{ first_name } ': { len (selection )} light(s)" )
90
-
90
+
91
91
if selection :
92
92
print (f"Turning '{ first_name } ' YELLOW..." )
93
93
selection .turn_on ("yellow" )
94
94
time .sleep (1 )
95
95
selection .turn_off ()
96
-
96
+
97
97
# Try to select multiple by name (if available)
98
98
if len (available_names ) >= 2 :
99
99
selection = controller .by_name (available_names [0 ], available_names [1 ])
100
100
print (f"Selected multiple by name: { selection .names ()} " )
101
-
101
+
102
102
if selection :
103
103
print ("Blinking selected lights PURPLE..." )
104
104
selection .blink ("purple" , count = 2 )
105
105
time .sleep (2 )
106
-
106
+
107
107
print ()
108
108
109
109
110
110
def demo_select_by_pattern ():
111
111
"""Demonstrate selecting lights using regex patterns."""
112
112
print ("🔍 Select by Pattern" )
113
113
print ("=" * 50 )
114
-
114
+
115
115
with LightController () as controller :
116
116
available_names = controller .list_lights ()
117
117
print (f"Available lights: { available_names } " )
118
-
118
+
119
119
# Try common patterns
120
120
patterns_to_try = [
121
121
("Blyn.*" , "Blynclight devices" ),
@@ -124,108 +124,108 @@ def demo_select_by_pattern():
124
124
("Mute.*" , "MuteMe devices" ),
125
125
(r".*\(\d+\)" , "Devices with numbers in parentheses" ),
126
126
]
127
-
127
+
128
128
for pattern , description in patterns_to_try :
129
129
selection = controller .by_pattern (pattern )
130
130
if selection :
131
131
print (f"Pattern '{ pattern } ' ({ description } ): { selection .names ()} " )
132
-
132
+
133
133
print (f" Blinking matched lights CYAN..." )
134
134
selection .blink ("cyan" , count = 2 , speed = "medium" )
135
135
time .sleep (1.5 )
136
136
else :
137
137
print (f"Pattern '{ pattern } ' ({ description } ): No matches" )
138
-
138
+
139
139
print ()
140
140
141
141
142
142
def demo_practical_scenarios ():
143
143
"""Show practical real-world usage scenarios."""
144
144
print ("💡 Practical Usage Scenarios" )
145
145
print ("=" * 50 )
146
-
146
+
147
147
with LightController () as controller :
148
148
print ("Scenario 1: Status Light System" )
149
-
149
+
150
150
# Use first light as status indicator
151
151
status_light = controller .first ()
152
152
if status_light :
153
153
print (" System OK (GREEN)" )
154
154
status_light .turn_on ("green" )
155
155
time .sleep (1 )
156
-
156
+
157
157
print (" Warning (YELLOW blink)" )
158
158
status_light .blink ("yellow" , count = 3 )
159
159
time .sleep (2 )
160
-
160
+
161
161
print (" Error (RED blink)" )
162
162
status_light .blink ("red" , count = 5 )
163
163
time .sleep (3 )
164
-
164
+
165
165
status_light .turn_off ()
166
-
166
+
167
167
print ("\n Scenario 2: Work Lighting Control" )
168
-
168
+
169
169
# Turn on work lights (any lights available)
170
170
work_lights = controller .all ()
171
171
if work_lights :
172
172
print (" Setting work lighting (soft white)" )
173
173
work_lights .turn_on ("white" )
174
174
time .sleep (1 )
175
-
175
+
176
176
print (" Focus mode (bright blue)" )
177
177
work_lights .turn_on ("blue" )
178
178
time .sleep (1 )
179
-
179
+
180
180
print (" Break time (off)" )
181
181
work_lights .turn_off ()
182
-
182
+
183
183
print ("\n Scenario 3: Meeting Status" )
184
-
184
+
185
185
# Use pattern to control webcam-related lights
186
- meeting_lights = controller .by_pattern (".*" ) # All lights for demo
186
+ meeting_lights = controller .by_pattern (".*" ) # All lights for demo
187
187
if meeting_lights :
188
188
print (" Meeting started (steady red)" )
189
189
meeting_lights .turn_on ("red" )
190
190
time .sleep (1 )
191
-
191
+
192
192
print (" Do not disturb (blinking red)" )
193
193
meeting_lights .blink ("red" , count = 3 )
194
194
time .sleep (2 )
195
-
195
+
196
196
print (" Meeting ended (brief green)" )
197
197
meeting_lights .turn_on ("green" )
198
198
time .sleep (1 )
199
199
meeting_lights .turn_off ()
200
-
200
+
201
201
print ()
202
202
203
203
204
204
def demo_error_handling ():
205
205
"""Show how selection methods handle errors gracefully."""
206
206
print ("⚠️ Error Handling" )
207
207
print ("=" * 50 )
208
-
208
+
209
209
with LightController () as controller :
210
210
# These won't crash - they'll log warnings and return empty selections
211
-
211
+
212
212
print ("Trying invalid index (999)..." )
213
213
invalid_selection = controller .by_index (999 )
214
214
print (f" Result: { len (invalid_selection )} lights selected" )
215
215
invalid_selection .turn_on ("red" ) # No-op, no error
216
-
216
+
217
217
print ("Trying non-existent name..." )
218
218
missing_selection = controller .by_name ("NonExistentLight" )
219
219
print (f" Result: { len (missing_selection )} lights selected" )
220
220
missing_selection .blink ("blue" ) # No-op, no error
221
-
221
+
222
222
print ("Trying pattern with no matches..." )
223
223
empty_selection = controller .by_pattern ("ZZZZZ_NO_MATCH" )
224
224
print (f" Result: { len (empty_selection )} lights selected" )
225
225
empty_selection .turn_off () # No-op, no error
226
-
226
+
227
227
print ("All operations completed without errors!" )
228
-
228
+
229
229
print ()
230
230
231
231
@@ -235,26 +235,26 @@ def main():
235
235
print ("=" * 60 )
236
236
print ("This demo shows all the ways to select specific lights." )
237
237
print ("Connect some USB lights to see the full effect!\n " )
238
-
238
+
239
239
# Check if lights are available
240
240
has_lights = demo_light_discovery ()
241
-
241
+
242
242
if not has_lights :
243
243
print ("Demo requires USB lights to be connected." )
244
244
print ("The selection methods will work, but you won't see any visual effects." )
245
245
print ()
246
-
246
+
247
247
# Run all demos
248
248
demo_select_all ()
249
249
demo_select_by_index ()
250
250
demo_select_by_name ()
251
251
demo_select_by_pattern ()
252
252
demo_practical_scenarios ()
253
253
demo_error_handling ()
254
-
254
+
255
255
print ("✅ Demo complete! The new LightController provides flexible," )
256
256
print (" intuitive ways to select exactly the lights you want to control." )
257
257
258
258
259
259
if __name__ == "__main__" :
260
- main ()
260
+ main ()
0 commit comments