Skip to content

Commit

Permalink
Add SetColorAsync method to ColorDistanceSensor (#204)
Browse files Browse the repository at this point in the history
#203 non-breaking
  • Loading branch information
tthiery committed Mar 21, 2024
1 parent d35a200 commit 352e2a7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ public override async Task ExecuteAsync()
{
var colorDistSensor = twoPortHub.B.GetDevice<ColorDistanceSensor>();

foreach (var color in Enum.GetValues<ColorDistanceSensorColor>())
{
await colorDistSensor.SetColorAsync(color);

await Task.Delay(TimeSpan.FromSeconds(1));
}

var observers = new[] {
colorDistSensor.ColorObservable
.Subscribe(color => Log.LogInformation("Color: {0}", color)),
Expand Down
23 changes: 22 additions & 1 deletion src/SharpBrick.PoweredUp/Devices/ColorDistanceSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using SharpBrick.PoweredUp.Protocol;
using SharpBrick.PoweredUp.Protocol.Messages;
using SharpBrick.PoweredUp.Utils;

namespace SharpBrick.PoweredUp;
Expand Down Expand Up @@ -61,6 +61,27 @@ protected override uint GetDefaultDeltaInterval(byte modeIndex)
_ => base.GetDefaultDeltaInterval(modeIndex),
};

public async Task<PortFeedback> SetColorAsync(ColorDistanceSensorColor colorLedMode)
{
AssertIsConnected();

await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage(_portId, ModeIndexLight, 10000, false)
{
HubId = _hubId,
});

var response = await _protocol.SendPortOutputCommandAsync(new GenericWriteDirectModeDataMessage(
_portId,
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
ModeIndexLight,
new byte[] { (byte)colorLedMode })
{
HubId = _hubId,
});

return response;
}

public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
=> ((softwareVersion, hardwareVersion, systemType) switch
{
Expand Down
10 changes: 10 additions & 0 deletions src/SharpBrick.PoweredUp/Enums/ColorDistanceSensorColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SharpBrick.PoweredUp;

public enum ColorDistanceSensorColor
{
None = 0,
Blue = 3,
Green = 5,
Red = 9,
White = 10,
}

0 comments on commit 352e2a7

Please sign in to comment.