Skip to content

Commit

Permalink
Merge pull request #108 from robberwick/_data_to_message
Browse files Browse the repository at this point in the history
Add tests for string to bytes conversion and refactor
  • Loading branch information
robberwick authored Nov 17, 2024
2 parents 1799286 + ab1d58d commit 20be82b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
28 changes: 7 additions & 21 deletions src/blinkstick/blinkstick.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from blinkstick.constants import VENDOR_ID, PRODUCT_ID, BlinkStickVariant
from blinkstick.exceptions import BlinkStickException
from blinkstick.utilities import string_to_info_block_data

if sys.platform == "win32":
from blinkstick.backends.win32 import Win32Backend as USBBackend
Expand Down Expand Up @@ -454,25 +455,6 @@ def get_info_block2(self) -> str:
result += chr(i)
return result

def _data_to_message(self, data: str) -> bytes:
"""
Helper method to convert a string to byte array of 32 bytes.
@type data: str
@param data: The data to convert to byte array
@rtype: byte[32]
@return: It fills the rest of bytes with zeros.
"""
byte_array = bytearray([1])
for c in data:
byte_array.append(ord(c))

for i in range(32 - len(data)):
byte_array.append(0)

return bytes(byte_array)

def set_info_block1(self, data: str) -> None:
"""
Sets the infoblock1 with specified string.
Expand All @@ -482,7 +464,9 @@ def set_info_block1(self, data: str) -> None:
@type data: str
@param data: InfoBlock1 for the backend to set
"""
self.backend.control_transfer(0x20, 0x9, 0x0002, 0, self._data_to_message(data))
self.backend.control_transfer(
0x20, 0x9, 0x0002, 0, string_to_info_block_data(data)
)

def set_info_block2(self, data: str) -> None:
"""
Expand All @@ -493,7 +477,9 @@ def set_info_block2(self, data: str) -> None:
@type data: str
@param data: InfoBlock2 for the backend to set
"""
self.backend.control_transfer(0x20, 0x9, 0x0003, 0, self._data_to_message(data))
self.backend.control_transfer(
0x20, 0x9, 0x0003, 0, string_to_info_block_data(data)
)

def set_random_color(self) -> None:
"""
Expand Down
17 changes: 17 additions & 0 deletions src/blinkstick/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def string_to_info_block_data(data: str) -> bytes:
"""
Helper method to convert a string to byte array of 32 bytes.
@type data: str
@param data: The data to convert to byte array
@rtype: byte[32]
@return: It fills the rest of bytes with zeros.
"""
info_block_data = data[:31]
byte_array = bytearray([1] + [0] * 31)

for i, c in enumerate(info_block_data):
byte_array[i + 1] = ord(c)

return bytes(byte_array)
Empty file added tests/utilities/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions tests/utilities/test_string_to_info_block.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from blinkstick.utilities import string_to_info_block_data


def test_string_to_info_block_data_converts_string_to_byte_array():
block_string = "hello"
expected_padding_length = 31 - len(block_string)
result = string_to_info_block_data("hello")
expected = b"\x01hello" + b"\x00" * expected_padding_length
assert result == expected


def test_string_to_info_block_data_handles_empty_string():
result = string_to_info_block_data("")
expected = b"\x01" + b"\x00" * 31
assert result == expected


def test_string_to_info_block_data_truncates_long_string():
long_string = "a" * 40
result = string_to_info_block_data(long_string)
expected = b"\x01" + b"a" * 31
assert result == expected


def test_string_to_info_block_data_handles_exact_31_characters():
exact_string = "a" * 31
result = string_to_info_block_data(exact_string)
expected = b"\x01" + b"a" * 31
assert result == expected

0 comments on commit 20be82b

Please sign in to comment.