Skip to content

Commit 6d734ca

Browse files
committed
Merged branch segments_testing
1 parent fa9a799 commit 6d734ca

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

openrgb/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from openrgb import utils
77
from typing import Callable
88

9-
OPENRGB_PROTOCOL_VERSION = 3
9+
OPENRGB_PROTOCOL_VERSION = 4
1010

1111
if platform.system() == "Linux":
1212
NOSIGNAL: int = socket.MSG_NOSIGNAL

openrgb/orgb.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,43 @@ def set_color(self, color: utils.RGBColor, fast: bool = False):
4242
self.update()
4343

4444

45+
class Segment(utils.RGBContainer):
46+
'''
47+
A class to represent a segment
48+
'''
49+
50+
def __init__(self, data: utils.SegmentData, id: int, parent: Zone):
51+
self.leds = [None for _ in range(data.leds_count)]
52+
self.id = id
53+
self.parent_zone = parent
54+
self._update(data)
55+
56+
def _update(self, data: utils.SegmentData):
57+
self.name = data.name
58+
self.type = data.segment_type
59+
self.start_idx = data.start_idx
60+
self.leds_count = data.leds_count
61+
self.leds = self.parent_zone.leds[data.start_idx:data.start_idx + data.leds_count]
62+
63+
def set_color(self, color: utils.RGBColor, fast: bool = False):
64+
self.set_colors([color] * self.leds_count, fast)
65+
66+
def set_colors(self, colors: list[utils.RGBColor], fast: bool = False):
67+
new_colors = self.parent_zone.colors[:self.start_idx] + colors + self.parent_zone.colors[self.start_idx + self.leds_count:]
68+
self.parent_zone.set_colors(new_colors, fast)
69+
70+
4571
class Zone(utils.RGBContainer):
4672
'''
4773
A class to represent a zone
4874
'''
4975

5076
def __init__(self, data: utils.ZoneData, zone_id: int, device_id: int, network_client: NetworkClient):
5177
self.leds = [None for led in data.leds]
78+
try:
79+
self.segments = [None for _ in data.segments]
80+
except TypeError:
81+
self.segments = None
5282
self.device_id = device_id
5383
self.comms = network_client
5484
self.id = zone_id
@@ -65,6 +95,12 @@ def _update(self, data: utils.ZoneData):
6595
data.start_idx + x, self.device_id, self.comms)
6696
else:
6797
self.leds[x]._update(data.leds[x], data.colors[x])
98+
if self.segments:
99+
for x in range(len(data.segments)):
100+
if self.segments[x] is None:
101+
self.segments[x] = Segment(data.segments[x], x, self)
102+
else:
103+
self.segments[x]._update(data.segments[x])
68104
self.mat_width = data.mat_width
69105
self.mat_height = data.mat_height
70106
self.matrix_map = data.matrix_map

openrgb/utils.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ def unpack(cls, data: Iterator[int], version: int, *args) -> LEDData:
219219
Creates a new LEDData object from raw bytes
220220
221221
:param data: the raw bytes from the SDK
222-
:param start: what place in the data object to start
223222
'''
224223
name = parse_string(data)
225224
value = parse_var('I', data)
@@ -304,7 +303,6 @@ def unpack(cls, data: Iterator[int], version: int, index: int = 0) -> ModeData:
304303
Creates a new ModeData object from raw bytes
305304
306305
:param data: the raw bytes from the SDK
307-
:param start: what place in the data object to start
308306
:param index: which mode this is
309307
'''
310308
name = parse_string(data)
@@ -369,6 +367,42 @@ def unpack(cls, data: Iterator[int], version: int, index: int = 0) -> ModeData:
369367
)
370368

371369

370+
@dataclass
371+
class SegmentData:
372+
name: str
373+
segment_type: ZoneType
374+
start_idx: int
375+
leds_count: int
376+
377+
def pack(self, version: int) -> bytes:
378+
'''
379+
Packs itself into a bytes ready to be sent to the SDK or saved in a profile
380+
381+
:returns: raw data ready to be sent or saved
382+
'''
383+
data = pack_string(self.name)
384+
data += struct.pack("iII", self.segment_type, self.start_idx, self.leds_count)
385+
return data
386+
387+
@classmethod
388+
def unpack(cls, data: Iterator[int], version: int, *args) -> SegmentData:
389+
'''
390+
Unpacks the raw data into a SegmentData object
391+
392+
:param data: The raw byte data to unpack
393+
'''
394+
name = parse_string(data)
395+
segment_type = ZoneType(parse_var("i", data))
396+
start_idx = parse_var("I", data)
397+
leds_count = parse_var("I", data)
398+
return cls(
399+
name,
400+
segment_type,
401+
start_idx,
402+
leds_count
403+
)
404+
405+
372406
@dataclass
373407
class ZoneData:
374408
name: str
@@ -379,6 +413,7 @@ class ZoneData:
379413
mat_height: Optional[int]
380414
mat_width: Optional[int]
381415
matrix_map: Optional[list[list[Optional[int]]]] = None
416+
segments: Optional[list[SegmentData]] = None
382417
leds: list[LEDData] = field(default_factory=list)
383418
colors: list[RGBColor] = field(default_factory=list)
384419
start_idx: int = 0
@@ -411,6 +446,9 @@ def pack(self, version: int) -> bytes:
411446
)
412447
else:
413448
data += struct.pack("H", 0)
449+
450+
if version >= 4:
451+
data += pack_list(self.segments, version)
414452
return data
415453

416454
@classmethod
@@ -419,7 +457,6 @@ def unpack(cls, data: Iterator[int], version: int, *args) -> ZoneData:
419457
Unpacks the raw data into a ZoneData object
420458
421459
:param data: The raw byte data to unpack
422-
:param start: What place in the data object to start
423460
'''
424461
name = parse_string(data)
425462
zone_type = ZoneType(parse_var('i', data))
@@ -439,6 +476,11 @@ def unpack(cls, data: Iterator[int], version: int, *args) -> ZoneData:
439476
else:
440477
height, width = None, None
441478
matrix = None # type: ignore
479+
480+
if version >= 4:
481+
segments = parse_list(SegmentData, data, version)
482+
else:
483+
segments = None
442484
return cls(
443485
name,
444486
zone_type,
@@ -447,7 +489,8 @@ def unpack(cls, data: Iterator[int], version: int, *args) -> ZoneData:
447489
num_leds,
448490
height,
449491
width,
450-
matrix
492+
matrix,
493+
segments
451494
)
452495

453496

@@ -481,7 +524,6 @@ def unpack(cls, data: Iterator[int], version: int, *args) -> MetaData:
481524
Unpacks the raw data into a MetaData object
482525
483526
:param data: The raw byte data to unpack
484-
:param start: What place in the data object to start
485527
'''
486528
if version >= 1:
487529
vendor: Optional[str] = parse_string(data)

0 commit comments

Comments
 (0)