-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: basic support for hello fairy #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jr4
wants to merge
11
commits into
Bluetooth-Devices:main
Choose a base branch
from
jr4:fairy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c66f0fb
feat: basic support for hello fairy
b805a31
fix: rollback some unnecessary things
1e230ba
feat: fairy protocol updates
2b87b74
Merge branch 'main' into fairy
bdraco bfb1df8
chore: revert lock file changes
bdraco 6954b7b
chore: revert lock file changes
bdraco 755ae9c
chore: revert lock file changes
bdraco 599d208
chore: revert lock file changes
bdraco 2f13944
Merge remote-tracking branch 'origin/main' into fairy
bdraco fb768b0
Merge branch 'main' into fairy
bdraco 6c6391f
Merge branch 'main' into fairy
Ernst79 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| from __future__ import annotations | ||
| import colorsys | ||
| from math import floor | ||
| from flux_led.protocol import ProtocolBase | ||
| from led_ble.led_ble import LevelWriteMode | ||
|
|
||
|
|
||
| class ProtocolFairy(ProtocolBase): | ||
| """Protocol for Hello Fairy devices.""" | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """The name of the protocol.""" | ||
| return "Fairy" | ||
|
|
||
| def construct_state_query(self) -> bytearray: | ||
| """The bytes to send for a query request.""" | ||
| return self.construct_message(bytearray([0xAA, 0x01, 0x00])) | ||
|
|
||
| def construct_state_change(self, turn_on: int) -> bytearray: | ||
| """The bytes to send for a state change request.""" | ||
| return self.construct_message( | ||
| bytearray([0xAA, 0x02, 0x01, 1 if turn_on else 0]) | ||
| ) | ||
|
|
||
| def construct_pause(self, pause: bool) -> bytearray: | ||
| """The bytes to send for pausing or unpausing.""" | ||
| return self.construct_message(bytearray([0xAA, 0x11, 0x01, 1 if pause else 0])) | ||
|
|
||
| def construct_ir_state(self, turn_on: bool) -> bytearray: | ||
| """The bytes to send for enabling/disabling the IR remote.""" | ||
| return self.construct_message( | ||
| bytearray([0xAA, 0x0F, 0x01, 1 if turn_on else 0]) | ||
| ) | ||
|
|
||
| def construct_levels_change( | ||
| self, | ||
| persist: int, | ||
| red: int | None, | ||
| green: int | None, | ||
| blue: int | None, | ||
| warm_white: int | None, | ||
| cool_white: int | None, | ||
| write_mode: LevelWriteMode, | ||
| ) -> list[bytearray]: | ||
| """The bytes to send for a level change request.""" | ||
| h, s, v = colorsys.rgb_to_hsv( | ||
| (red or 0) / 255, (green or 0) / 255, (blue or 0) / 255 | ||
| ) | ||
| h_scaled = min(359, floor(h * 360)) | ||
| s_scaled = round(s * 1000) | ||
| v_scaled = round(v * 1000) | ||
| return [ | ||
| self.construct_message( | ||
| bytearray( | ||
| [ | ||
| 0xAA, | ||
| 0x03, | ||
| 0x07, | ||
| 0x01, | ||
| h_scaled >> 8, | ||
| h_scaled & 0xFF, | ||
| s_scaled >> 8, | ||
| s_scaled & 0xFF, | ||
| v_scaled >> 8, | ||
| v_scaled & 0xFF, | ||
| ] | ||
| ) | ||
| ) | ||
| ] | ||
|
|
||
| def construct_preset_pattern( | ||
| self, pattern: int, speed: int, brightness: int | ||
| ) -> list[bytearray]: | ||
| """The bytes to send for a preset pattern.""" | ||
| return [ | ||
| self.construct_message( | ||
| bytearray( | ||
| [ | ||
| 0xAA, | ||
| 0x03, | ||
| 0x04, | ||
| 0x02, | ||
| pattern & 0xFF, | ||
| (brightness >> 8) & 0xFF, | ||
| brightness & 0xFF, | ||
| ] | ||
| ) | ||
| ), | ||
| self.construct_message(bytearray([0xAA, 0x0C, 0x01, min(speed, 100)])), | ||
| ] | ||
|
|
||
| def construct_custom_effect( | ||
| self, rgb_list: list[tuple[int, int, int]], speed: int, transition_type: str | ||
| ) -> list[bytearray]: | ||
| """The bytes to send for a custom effect.""" | ||
| data_bytes = len(rgb_list) * 3 + 1 | ||
| hue_message = bytearray(data_bytes + 3) | ||
| hue_message[0:4] = [0xAA, 0xDA, data_bytes, 0x01] | ||
| for [i, [r, g, b]] in enumerate(rgb_list): | ||
| h, s, v = colorsys.rgb_to_hsv(r / 255, g / 255, b / 255) | ||
| if v < 0.25: | ||
| h = 0xFE # black | ||
| elif s < 0.25: | ||
| h = 0xFF # white | ||
| else: | ||
| h = floor(h * 0xAF) | ||
| # necessary to satisfy both flake and ruff: | ||
| a = i * 3 + 4 | ||
| b = a + 3 | ||
| hue_message[a:b] = [i >> 8, i & 0xFF, h] | ||
| return [ | ||
| *self.construct_motion(speed, 0), | ||
| self.construct_message(hue_message), | ||
| *self.construct_motion(speed, 2), | ||
| ] | ||
|
|
||
| def construct_motion(self, speed: int, transition: int) -> list[bytearray]: | ||
| """The bytes to send for motion speed and transition.""" | ||
| return [ | ||
| self.construct_message( | ||
| bytearray([0xAA, 0xD0, 0x04, transition, 0x64, speed, 0x01]) | ||
| ) | ||
| ] | ||
|
|
||
| def construct_message(self, raw_bytes: bytearray) -> bytearray: | ||
| """Calculate checksum of byte array and add to end.""" | ||
| csum = sum(raw_bytes) & 0xFF | ||
| raw_bytes.append(csum) | ||
| return raw_bytes |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a new method to the protocol to get the state command instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that method is implemented but the protocol is not resolved yet at this point