Skip to content

Commit 352e2a7

Browse files
authored
Add SetColorAsync method to ColorDistanceSensor (#204)
#203 non-breaking
1 parent d35a200 commit 352e2a7

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

examples/SharpBrick.PoweredUp.Examples/ExampleColorDistanceSensor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ public override async Task ExecuteAsync()
1616
{
1717
var colorDistSensor = twoPortHub.B.GetDevice<ColorDistanceSensor>();
1818

19+
foreach (var color in Enum.GetValues<ColorDistanceSensorColor>())
20+
{
21+
await colorDistSensor.SetColorAsync(color);
22+
23+
await Task.Delay(TimeSpan.FromSeconds(1));
24+
}
25+
1926
var observers = new[] {
2027
colorDistSensor.ColorObservable
2128
.Subscribe(color => Log.LogInformation("Color: {0}", color)),

src/SharpBrick.PoweredUp/Devices/ColorDistanceSensor.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reactive.Linq;
5-
using System.Text;
65
using System.Threading.Tasks;
76
using SharpBrick.PoweredUp.Protocol;
7+
using SharpBrick.PoweredUp.Protocol.Messages;
88
using SharpBrick.PoweredUp.Utils;
99

1010
namespace SharpBrick.PoweredUp;
@@ -61,6 +61,27 @@ protected override uint GetDefaultDeltaInterval(byte modeIndex)
6161
_ => base.GetDefaultDeltaInterval(modeIndex),
6262
};
6363

64+
public async Task<PortFeedback> SetColorAsync(ColorDistanceSensorColor colorLedMode)
65+
{
66+
AssertIsConnected();
67+
68+
await _protocol.SendMessageAsync(new PortInputFormatSetupSingleMessage(_portId, ModeIndexLight, 10000, false)
69+
{
70+
HubId = _hubId,
71+
});
72+
73+
var response = await _protocol.SendPortOutputCommandAsync(new GenericWriteDirectModeDataMessage(
74+
_portId,
75+
PortOutputCommandStartupInformation.ExecuteImmediately, PortOutputCommandCompletionInformation.CommandFeedback,
76+
ModeIndexLight,
77+
new byte[] { (byte)colorLedMode })
78+
{
79+
HubId = _hubId,
80+
});
81+
82+
return response;
83+
}
84+
6485
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
6586
=> ((softwareVersion, hardwareVersion, systemType) switch
6687
{
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SharpBrick.PoweredUp;
2+
3+
public enum ColorDistanceSensorColor
4+
{
5+
None = 0,
6+
Blue = 3,
7+
Green = 5,
8+
Red = 9,
9+
White = 10,
10+
}

0 commit comments

Comments
 (0)