Skip to content

Commit ce3e8a5

Browse files
MarekPietapdunaj
authored andcommitted
scripts: hid_configurator: Move LED stream to separate files
Change moves LED stream related code to separate files. Jira:DESK-593 Signed-off-by: Marek Pieta <[email protected]>
1 parent 545c7e6 commit ce3e8a5

File tree

4 files changed

+130
-116
lines changed

4 files changed

+130
-116
lines changed

scripts/hid_configurator/configurator_cli.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
from configurator_core import get_device_pid, open_device
1111
from configurator_core import change_config, fetch_config
1212
from modules.dfu import fwinfo, fwreboot, dfu_transfer, get_dfu_image_version
13-
from led_stream import send_continuous_led_stream, send_music_led_stream
13+
from modules.led_stream import send_continuous_led_stream
14+
from modules.music_led_stream import send_music_led_stream
1415

1516

1617
def progress_bar(permil):

scripts/hid_configurator/configurator_core.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import hid
88
import struct
99
import time
10-
import random
1110
import re
1211

1312
import logging
@@ -46,8 +45,6 @@
4645
BLE_BOND_PEER_ERASE = 0x0
4746
BLE_BOND_PEER_SEARCH = 0x1
4847

49-
LED_STREAM_DATA = 0x0
50-
5148
POLL_INTERVAL_DEFAULT = 0.02
5249
POLL_RETRY_COUNT = 200
5350

@@ -516,55 +513,5 @@ def fetch_config(dev, recipient, config_name, device_options, module_id):
516513
return success, ConfigParser(fetched_data, *format[opt_id]).config_get(config_name)
517514

518515

519-
class Step:
520-
def __init__(self, r, g, b, substep_count, substep_time):
521-
self.r = r
522-
self.g = g
523-
self.b = b
524-
self.substep_count = substep_count
525-
self.substep_time = substep_time
526-
527-
def generate_random_color(self):
528-
self.r = random.randint(0, 255)
529-
self.g = random.randint(0, 255)
530-
self.b = random.randint(0, 255)
531-
532-
533-
def led_send_single_step(dev, recipient, step, led_id):
534-
event_id = (EVENT_GROUP_LED_STREAM << GROUP_FIELD_POS) \
535-
| (LED_STREAM_DATA << TYPE_FIELD_POS)
536-
537-
# Chosen data layout for struct is defined using format string.
538-
event_data = struct.pack('<BBBHHB', step.r, step.g, step.b,
539-
step.substep_count, step.substep_time, led_id)
540-
541-
success = exchange_feature_report(dev, recipient, event_id,
542-
event_data, False,
543-
poll_interval=0.001)
544-
545-
return success
546-
547-
548-
def fetch_free_steps_buffer_info(dev, recipient, led_id):
549-
event_id = (EVENT_GROUP_LED_STREAM << GROUP_FIELD_POS) \
550-
| (led_id << MOD_FIELD_POS)
551-
552-
success, fetched_data = exchange_feature_report(dev, recipient,
553-
event_id, None, True,
554-
poll_interval=0.001)
555-
556-
if (not success) or (fetched_data is None):
557-
return False, (None, None)
558-
559-
# Chosen data layout for struct is defined using format string.
560-
fmt = '<B?'
561-
assert struct.calcsize(fmt) <= EVENT_DATA_LEN_MAX
562-
563-
if len(fetched_data) != struct.calcsize(fmt):
564-
return False, (None, None)
565-
566-
return success, struct.unpack(fmt, fetched_data)
567-
568-
569516
if __name__ == '__main__':
570517
print("Please run configurator_cli.py or gui.py to start application")
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#
2+
# Copyright (c) 2019 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
5+
6+
import struct
7+
import random
8+
9+
from configurator_core import TYPE_FIELD_POS, GROUP_FIELD_POS, MOD_FIELD_POS
10+
from configurator_core import EVENT_GROUP_LED_STREAM
11+
from configurator_core import EVENT_DATA_LEN_MAX
12+
13+
from configurator_core import exchange_feature_report
14+
15+
LED_STREAM_DATA = 0x0
16+
MS_PER_SEC = 1000
17+
18+
19+
class Step:
20+
def __init__(self, r, g, b, substep_count, substep_time):
21+
self.r = r
22+
self.g = g
23+
self.b = b
24+
self.substep_count = substep_count
25+
self.substep_time = substep_time
26+
27+
def generate_random_color(self):
28+
self.r = random.randint(0, 255)
29+
self.g = random.randint(0, 255)
30+
self.b = random.randint(0, 255)
31+
32+
33+
def validate_params(DEVICE_CONFIG, freq, led_id):
34+
valid_params = False
35+
36+
if freq <= 0:
37+
print('Frequency has to be greater than zero Hz')
38+
elif DEVICE_CONFIG['stream_led_cnt'] == 0:
39+
print('Device does not support LED stream')
40+
elif led_id < 0:
41+
print('LED ID cannot be less than zero')
42+
elif led_id >= DEVICE_CONFIG['stream_led_cnt']:
43+
print('LED with selected ID is not supported on selected device')
44+
else:
45+
valid_params = True
46+
47+
return valid_params
48+
49+
50+
def led_send_single_step(dev, recipient, step, led_id):
51+
event_id = (EVENT_GROUP_LED_STREAM << GROUP_FIELD_POS) \
52+
| (LED_STREAM_DATA << TYPE_FIELD_POS)
53+
54+
# Chosen data layout for struct is defined using format string.
55+
event_data = struct.pack('<BBBHHB', step.r, step.g, step.b,
56+
step.substep_count, step.substep_time, led_id)
57+
58+
success = exchange_feature_report(dev, recipient, event_id,
59+
event_data, False,
60+
poll_interval=0.001)
61+
62+
return success
63+
64+
65+
def fetch_free_steps_buffer_info(dev, recipient, led_id):
66+
event_id = (EVENT_GROUP_LED_STREAM << GROUP_FIELD_POS) \
67+
| (led_id << MOD_FIELD_POS)
68+
69+
success, fetched_data = exchange_feature_report(dev, recipient,
70+
event_id, None, True,
71+
poll_interval=0.001)
72+
73+
if (not success) or (fetched_data is None):
74+
return False, (None, None)
75+
76+
# Chosen data layout for struct is defined using format string.
77+
fmt = '<B?'
78+
assert struct.calcsize(fmt) <= EVENT_DATA_LEN_MAX
79+
80+
if len(fetched_data) != struct.calcsize(fmt):
81+
return False, (None, None)
82+
83+
return success, struct.unpack(fmt, fetched_data)
84+
85+
86+
def send_continuous_led_stream(dev, recipient, DEVICE_CONFIG, led_id, freq, substep_cnt = 10):
87+
if not validate_params(DEVICE_CONFIG, freq, led_id):
88+
return
89+
90+
try:
91+
# LED stream ends on user request (Ctrl+C) or when an error occurrs.
92+
step = Step(
93+
r = 0,
94+
g = 0,
95+
b = 0,
96+
substep_count = substep_cnt,
97+
substep_time = MS_PER_SEC // (freq * substep_cnt)
98+
)
99+
100+
print('LED stream started, press Ctrl+C to interrupt')
101+
while True:
102+
success, (free, ready) = fetch_free_steps_buffer_info(dev, recipient, led_id)
103+
104+
if not success:
105+
break
106+
107+
if not ready:
108+
print('LEDs are not ready')
109+
break
110+
111+
while free > 0:
112+
# Send steps with random color and predefined duration
113+
step.generate_random_color()
114+
115+
success = led_send_single_step(dev, recipient, step, led_id)
116+
117+
if not success:
118+
break
119+
120+
free -= 1
121+
except Exception as e:
122+
print(e)
123+
124+
print('LED stream ended')

scripts/hid_configurator/led_stream.py renamed to scripts/hid_configurator/modules/music_led_stream.py

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,68 +11,10 @@
1111
import threading
1212
import queue
1313

14-
from configurator_core import fetch_free_steps_buffer_info, led_send_single_step
15-
from configurator_core import Step
16-
17-
MS_PER_SEC = 1000
18-
19-
20-
def validate_params(DEVICE_CONFIG, freq, led_id):
21-
valid_params = False
22-
23-
if freq <= 0:
24-
print('Frequency has to be greater than zero Hz')
25-
elif DEVICE_CONFIG['stream_led_cnt'] == 0:
26-
print('Device does not support LED stream')
27-
elif led_id < 0:
28-
print('LED ID cannot be less than zero')
29-
elif led_id >= DEVICE_CONFIG['stream_led_cnt']:
30-
print('LED with selected ID is not supported on selected device')
31-
else:
32-
valid_params = True
33-
34-
return valid_params
35-
36-
37-
def send_continuous_led_stream(dev, recipient, DEVICE_CONFIG, led_id, freq, substep_cnt = 10):
38-
if not validate_params(DEVICE_CONFIG, freq, led_id):
39-
return
40-
41-
try:
42-
# LED stream ends on user request (Ctrl+C) or when an error occurrs.
43-
step = Step(
44-
r = 0,
45-
g = 0,
46-
b = 0,
47-
substep_count = substep_cnt,
48-
substep_time = MS_PER_SEC // (freq * substep_cnt)
49-
)
50-
51-
print('LED stream started, press Ctrl+C to interrupt')
52-
while True:
53-
success, (free, ready) = fetch_free_steps_buffer_info(dev, recipient, led_id)
54-
55-
if not success:
56-
break
57-
58-
if not ready:
59-
print('LEDs are not ready')
60-
break
61-
62-
while free > 0:
63-
# Send steps with random color and predefined duration
64-
step.generate_random_color()
65-
66-
success = led_send_single_step(dev, recipient, step, led_id)
67-
68-
if not success:
69-
break
70-
71-
free -= 1
72-
except:
73-
pass
74-
75-
print('LED stream ended')
14+
from .led_stream import MS_PER_SEC
15+
from .led_stream import Step
16+
from .led_stream import validate_params
17+
from .led_stream import fetch_free_steps_buffer_info, led_send_single_step
7618

7719

7820
class MusicLedStream():

0 commit comments

Comments
 (0)