forked from imurvai/brickcontroller2
-
Notifications
You must be signed in to change notification settings - Fork 3
[POC] SBrick Light - basic port support #194
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
Draft
vicocz
wants to merge
4
commits into
default
Choose a base branch
from
local/sbrick-light-poc
base: default
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,5 +23,6 @@ public enum DeviceType | |
| MK5, | ||
| MK3_8, | ||
| RemoteControl, | ||
| SBrickLight, | ||
| } | ||
| } | ||
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
164 changes: 164 additions & 0 deletions
164
BrickController2/BrickController2/DeviceManagement/Vengit/SBrickLightDevice.cs
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,164 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using BrickController2.DeviceManagement.IO; | ||
| using BrickController2.Helpers; | ||
| using BrickController2.PlatformServices.BluetoothLE; | ||
|
|
||
| using static BrickController2.DeviceManagement.Vengit.SBrickProtocol; | ||
|
|
||
| namespace BrickController2.DeviceManagement.Vengit; | ||
|
|
||
| internal class SBrickLightDevice : BluetoothDevice | ||
| { | ||
| private const int BANK_0_CHANNELS = 16; | ||
| private const int BANK_1_CHANNELS = 8; | ||
|
|
||
| private readonly OutputValuesGroup<byte> _bankOutputs0 = new(BANK_0_CHANNELS); | ||
| private readonly OutputValuesGroup<byte> _bankOutputs1 = new(BANK_1_CHANNELS); | ||
|
|
||
| private IGattCharacteristic? _firmwareRevisionCharacteristic; | ||
| private IGattCharacteristic? _hardwareRevisionCharacteristic; | ||
| private IGattCharacteristic? _remoteControlCharacteristic; | ||
|
|
||
| public SBrickLightDevice(string name, string address, byte[] deviceData, IDeviceRepository deviceRepository, IBluetoothLEService bleService) | ||
| : base(name, address, deviceRepository, bleService) | ||
| { | ||
| } | ||
|
|
||
| public override DeviceType DeviceType => DeviceType.SBrickLight; | ||
| public override string BatteryVoltageSign => "V"; | ||
| public override int NumberOfChannels => BANK_0_CHANNELS + BANK_1_CHANNELS; | ||
| protected override bool AutoConnectOnFirstConnect => false; | ||
|
|
||
| public override void SetOutput(int channel, float value) | ||
| { | ||
| CheckChannel(channel); | ||
| value = CutOutputValue(value); | ||
|
|
||
| // for lights use 0-255 range | ||
| var rawValue = (byte)(Math.Abs(value) * 255); | ||
|
|
||
| if (channel >= BANK_0_CHANNELS) | ||
| { | ||
| int lightChannel = channel - BANK_0_CHANNELS; | ||
| _bankOutputs1.SetOutput(lightChannel, rawValue); | ||
| } | ||
| else | ||
| { | ||
| _bankOutputs0.SetOutput(channel, rawValue); | ||
| } | ||
| } | ||
|
|
||
| protected override Task<bool> ValidateServicesAsync(IEnumerable<IGattService>? services, CancellationToken token) | ||
| { | ||
| var deviceInformationService = services?.FirstOrDefault(s => s.Uuid == GattProtocol.DeviceInformationServiceUuid); | ||
| _firmwareRevisionCharacteristic = deviceInformationService?.Characteristics?.FirstOrDefault(c => c.Uuid == GattProtocol.FirmwareRevisionCharacteristicUuid); | ||
| _hardwareRevisionCharacteristic = deviceInformationService?.Characteristics?.FirstOrDefault(c => c.Uuid == GattProtocol.HardwareRevisionCharacteristicUuid); | ||
|
|
||
| var remoteControlService = services?.FirstOrDefault(s => s.Uuid == SBrickProtocol.ServiceUuid); | ||
| _remoteControlCharacteristic = remoteControlService?.Characteristics?.FirstOrDefault(c => c.Uuid == RemoteControlCharacteristicUuid); | ||
|
|
||
| return Task.FromResult( | ||
| _firmwareRevisionCharacteristic is not null && | ||
| _hardwareRevisionCharacteristic is not null && | ||
| _remoteControlCharacteristic is not null); | ||
| } | ||
|
|
||
| protected override async Task<bool> AfterConnectSetupAsync(bool requestDeviceInformation, CancellationToken token) | ||
| { | ||
| try | ||
| { | ||
| if (requestDeviceInformation) | ||
| { | ||
| await ReadDeviceInfo(token).ConfigureAwait(false); | ||
| } | ||
| } | ||
| catch { } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| protected override async Task ProcessOutputsAsync(CancellationToken token) | ||
| { | ||
| try | ||
| { | ||
| // reset outputs | ||
| _bankOutputs0.Initialize(); | ||
| _bankOutputs1.Initialize(); | ||
|
|
||
| while (!token.IsCancellationRequested) | ||
| { | ||
| // process first bank 0 | ||
| bool changed = await TryProcessChanges(_bankOutputs0, LIGHTS_FLAGS_APPLY | LIGHTS_FLAGS_BANK_0, token); | ||
|
|
||
| // process additional bank 1 | ||
| if (await TryProcessChanges(_bankOutputs1, LIGHTS_FLAGS_APPLY | LIGHTS_FLAGS_BANK_1, token)) | ||
| { | ||
| changed = true; | ||
| } | ||
|
|
||
| if (!changed) | ||
| { | ||
| await Task.Delay(10, token).ConfigureAwait(false); | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| } | ||
| } | ||
|
|
||
| private async Task<bool> TryProcessChanges(OutputValuesGroup<byte> valueBank, byte flags, CancellationToken token) | ||
| { | ||
| try | ||
| { | ||
| if (valueBank.TryGetValues(out var values)) | ||
| { | ||
| var command = BuildSetAllLights(flags, values); | ||
| var success = await _bleDevice!.WriteAsync(_remoteControlCharacteristic!, command, token).ConfigureAwait(false); | ||
| if (success) | ||
| { | ||
| // confirm successful sending | ||
| valueBank.Commmit(); | ||
| await Task.Delay(5, token).ConfigureAwait(false); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private async Task ReadDeviceInfo(CancellationToken token) | ||
| { | ||
| var firmwareData = await _bleDevice!.ReadAsync(_firmwareRevisionCharacteristic!, token); | ||
| var firmwareVersion = firmwareData?.ToAsciiStringSafe(); | ||
| if (!string.IsNullOrEmpty(firmwareVersion)) | ||
| { | ||
| FirmwareVersion = firmwareVersion; | ||
| } | ||
|
|
||
| var hardwareData = await _bleDevice.ReadAsync(_hardwareRevisionCharacteristic!, token); | ||
| var hardwareVersion = hardwareData?.ToAsciiStringSafe(); | ||
| if (!string.IsNullOrEmpty(hardwareVersion)) | ||
| { | ||
| HardwareVersion = hardwareVersion; | ||
| } | ||
|
|
||
| // 0x0F Query ADC | voltage on 0x08 | ||
| await _bleDevice.WriteAsync(_remoteControlCharacteristic!, [0x0f, 0x08], token); | ||
| var voltageBuffer = await _bleDevice!.ReadAsync(_remoteControlCharacteristic!, token); | ||
| if (voltageBuffer is not null && voltageBuffer.Length >= 2) | ||
| { | ||
| var rawVoltage = voltageBuffer[0] + (voltageBuffer[1] << 8); | ||
| var voltage = (rawVoltage * 0.42567F) / 2047; | ||
| BatteryVoltage = voltage.ToString("F2"); | ||
| } | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
BrickController2/BrickController2/DeviceManagement/Vengit/SBrickProtocol.cs
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,43 @@ | ||
| using System; | ||
|
|
||
| namespace BrickController2.DeviceManagement.Vengit; | ||
|
|
||
| /// <summary> | ||
| /// Contains implementation of SBrick protocol <see href="https://social.sbrick.com/custom/The_SBrick_BLE_Protocol.pdf"/> | ||
| /// </summary> | ||
| internal static class SBrickProtocol | ||
| { | ||
| /// <summary> | ||
| /// SBrick - Remote control service UUID | ||
| /// </summary> | ||
| public static readonly Guid ServiceUuid = new("4dc591b0-857c-41de-b5f1-15abda665b0c"); | ||
| /// <summary> | ||
| /// Remote control service - Remote control commands characteristic UUID | ||
| /// </summary> | ||
| public static readonly Guid RemoteControlCharacteristicUuid = new("02b8cbcc-0e25-4bda-8790-a15f53e6010f"); | ||
|
|
||
| // Light flags | ||
| public const byte LIGHTS_FLAGS_BANK_0 = 0x00; | ||
| public const byte LIGHTS_FLAGS_BANK_1 = 0x01; | ||
| public const byte LIGHTS_FLAGS_APPLY = 0x80; | ||
|
|
||
| // data records | ||
| public const byte DATA_RECORD_PRODUCT_TYPE = 0x00; | ||
|
|
||
| public const byte PRODUCT_ID_SBRICK = 0x00; | ||
| public const byte PRODUCT_ID_SBRICK_LIGHT = 0x01; | ||
| public const byte PRODUCT_ID_UNKNOWN = 0xFF; | ||
|
|
||
| // message builders | ||
| public static byte[] BuildSetAllLights(byte flags, ReadOnlySpan<byte> values) | ||
| { | ||
| // 0x36 Set all lights | ||
| var buffer = new byte[2 + values.Length]; | ||
|
|
||
| buffer[0] = 0x36; | ||
| buffer[1] = flags; | ||
| values.CopyTo(buffer.AsSpan(2)); | ||
|
|
||
| return buffer; | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
BrickController2/BrickController2/DeviceManagement/Vengit/Vengit.cs
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,24 @@ | ||
| using BrickController2.DeviceManagement.DI; | ||
| using BrickController2.DeviceManagement.Vendors; | ||
| using BrickController2.Extensions; | ||
|
|
||
| namespace BrickController2.DeviceManagement.Vengit; | ||
|
|
||
| /// <summary> | ||
| /// Vendor: Vengit and all its device types: SBrick, SBrick Plus, SBrick Light and implementation of IBluetoothLEDeviceManager | ||
| /// </summary> | ||
| internal class Vengit : Vendor<Vengit> | ||
| { | ||
| public override string VendorName => "Vengit"; | ||
|
|
||
| protected override void Register(VendorBuilder<Vengit> builder) | ||
| { | ||
| // classic devices | ||
| builder.ContainerBuilder | ||
| .RegisterDevice<SBrickDevice>(DeviceType.SBrick) | ||
| .RegisterDevice<SBrickLightDevice>(DeviceType.SBrickLight); | ||
|
|
||
| // device manager | ||
| builder.RegisterDeviceManager<SBrickDeviceManager>(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Corrected spelling of 'Commmit' to 'Commit'.