Skip to content

Commit 65ac034

Browse files
authored
Add TechnicDistanceSensor (#127)
- Add Distance and Light modes - Do not add extension port (to less information) - Fix MarioPants PropertyChanged notification. - Fix SystemType binary representation in source code - Add Example #112 non-breaking
1 parent 0c91ef3 commit 65ac034

6 files changed

Lines changed: 168 additions & 3 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
4+
using SharpBrick.PoweredUp;
5+
6+
namespace Example
7+
{
8+
public class ExampleTechnicDistanceSensor : BaseExample
9+
{
10+
public override async Task ExecuteAsync()
11+
{
12+
using (var hub = Host.FindByType<TechnicMediumHub>())
13+
{
14+
await hub.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
15+
.AddHub<TechnicMediumHub>(hubBuilder => hubBuilder
16+
.AddDevice<TechnicDistanceSensor>(hub.A))
17+
);
18+
19+
var technicDistanceSensor = hub.A.GetDevice<TechnicDistanceSensor>();
20+
21+
// measure distances (only single subscription)
22+
using var m1 = technicDistanceSensor.DistanceObservable.Subscribe(x => Log.LogWarning($"Distl: {x}"));
23+
using var m2 = technicDistanceSensor.DistsObservable.Subscribe(x => Log.LogWarning($"Dists: {x}"));
24+
using var m3 = technicDistanceSensor.SinglObservable.Subscribe(x => Log.LogWarning($"Singl: {x}"));
25+
26+
await technicDistanceSensor.SetupNotificationAsync(technicDistanceSensor.ModeIndexDistance, true);
27+
28+
// play with the eye lights (0-100)
29+
for (byte idx = 0; idx < 100; idx += 5)
30+
{
31+
await technicDistanceSensor.SetEyeLightAsync(0b0000_0000, idx, 0b0110_0100, idx);
32+
33+
Log.LogWarning($"Brightness: {idx}");
34+
await Task.Delay(2_000);
35+
}
36+
37+
await hub.SwitchOffAsync();
38+
}
39+
}
40+
}
41+
}

examples/SharpBrick.PoweredUp.Examples/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ static async Task Main(string[] args)
4242
//example = new Example.ExampleMarioPants();
4343
//example = new Example.ExampleMarioAccelerometer();
4444
//example = new Example.ExampleDuploTrainBase();
45-
example = new Example.ExampleTechnicColorSensor();
45+
//example = new Example.ExampleTechnicColorSensor();
46+
example = new Example.ExampleTechnicDistanceSensor();
4647

4748
// NOTE: Examples are programmed object oriented style. Base class implements methods Configure, DiscoverAsync and ExecuteAsync to be overwriten on demand.
4849
await example.InitHostAndDiscoverAsync(enableTrace);

src/SharpBrick.PoweredUp/Devices/DeviceFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public Type GetTypeFromDeviceType(DeviceType deviceType)
4242
DeviceType.RemoteControlButton => typeof(RemoteControlButton),
4343
DeviceType.RemoteControlRssi => typeof(RemoteControlRssi),
4444
DeviceType.TechnicColorSensor => typeof(TechnicColorSensor),
45+
DeviceType.TechnicDistanceSensor => typeof(TechnicDistanceSensor),
4546
DeviceType.TechnicMediumHubAccelerometer => typeof(TechnicMediumHubAccelerometer),
4647
DeviceType.TechnicMediumHubGyroSensor => typeof(TechnicMediumHubGyroSensor),
4748
DeviceType.TechnicMediumHubTiltSensor => typeof(TechnicMediumHubTiltSensor),
@@ -73,6 +74,7 @@ public static DeviceType GetDeviceTypeFromType(Type type)
7374
nameof(RemoteControlButton) => DeviceType.RemoteControlButton,
7475
nameof(RemoteControlRssi) => DeviceType.RemoteControlRssi,
7576
nameof(TechnicColorSensor) => DeviceType.TechnicColorSensor,
77+
nameof(TechnicDistanceSensor) => DeviceType.TechnicDistanceSensor,
7678
nameof(TechnicMediumHubAccelerometer) => DeviceType.TechnicMediumHubAccelerometer,
7779
nameof(TechnicMediumHubGyroSensor) => DeviceType.TechnicMediumHubGyroSensor,
7880
nameof(TechnicMediumHubTiltSensor) => DeviceType.TechnicMediumHubTiltSensor,

src/SharpBrick.PoweredUp/Devices/MarioHubPants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public MarioHubPants(ILegoWirelessProtocol protocol, byte hubId, byte portId)
2525
{
2626
_pantsMode = SingleValueMode<sbyte>(ModeIndexPants);
2727

28-
ObserveForPropertyChanged(_pantsMode.Observable, nameof(PantsObservable));
28+
ObserveForPropertyChanged(_pantsMode.Observable, nameof(Pants));
2929
}
3030

3131
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reactive.Linq;
5+
using System.Threading.Tasks;
6+
using SharpBrick.PoweredUp.Protocol;
7+
using SharpBrick.PoweredUp.Utils;
8+
9+
namespace SharpBrick.PoweredUp
10+
{
11+
public class TechnicDistanceSensor : Device, IPoweredUpDevice
12+
{
13+
protected SingleValueMode<short> _distlMode;
14+
protected SingleValueMode<short> _distsMode;
15+
protected SingleValueMode<short> _singlMode;
16+
protected MultiValueMode<sbyte> _lightMode;
17+
18+
public byte ModeIndexDistance { get; protected set; } = 0;
19+
public byte ModeIndexShortOnlyDistance { get; protected set; } = 1;
20+
public byte ModeIndexSingleMeasurement { get; protected set; } = 2;
21+
public byte ModeIndexLight { get; protected set; } = 5;
22+
23+
public short Distance => _distlMode.SI;
24+
public IObservable<short> DistanceObservable => _distlMode.Observable.Select(x => x.SI);
25+
public short ShortOnlyDistance => _distsMode.SI;
26+
public IObservable<short> DistsObservable => _distsMode.Observable.Select(x => x.SI);
27+
public short SingleDistanceMeasurementResult => _singlMode.SI;
28+
public IObservable<short> SinglObservable => _singlMode.Observable.Select(x => x.SI);
29+
30+
public TechnicDistanceSensor()
31+
{ }
32+
public TechnicDistanceSensor(ILegoWirelessProtocol protocol, byte hubId, byte portId)
33+
: base(protocol, hubId, portId)
34+
{
35+
_distlMode = SingleValueMode<short>(ModeIndexDistance);
36+
_distsMode = SingleValueMode<short>(ModeIndexShortOnlyDistance);
37+
_singlMode = SingleValueMode<short>(ModeIndexSingleMeasurement);
38+
_lightMode = MultiValueMode<sbyte>(ModeIndexLight);
39+
40+
ObserveForPropertyChanged(_distlMode.Observable, nameof(Distance));
41+
ObserveForPropertyChanged(_distsMode.Observable, nameof(ShortOnlyDistance));
42+
ObserveForPropertyChanged(_singlMode.Observable, nameof(SingleDistanceMeasurementResult));
43+
}
44+
45+
public Task SetEyeLightAsync(byte leftTop = 0x00, byte rightTop = 0x00, byte leftBottom = 0x00, byte rightBottom = 0x00)
46+
=> _lightMode.WriteDirectModeDataAsync(new byte[] { leftTop, rightTop, leftBottom, rightBottom });
47+
48+
public IEnumerable<byte[]> GetStaticPortInfoMessages(Version softwareVersion, Version hardwareVersion, SystemType systemType)
49+
=> ((softwareVersion, hardwareVersion, systemType) switch
50+
{
51+
(_, _, _) => @"
52+
0B-00-43-00-01-03-09-9F-00-60-00
53+
05-00-43-00-02
54+
11-00-44-00-00-00-44-49-53-54-4C-00-00-00-00-00-00
55+
0E-00-44-00-00-01-00-00-00-00-00-40-1C-45
56+
0E-00-44-00-00-02-00-00-00-00-00-00-C8-42
57+
0E-00-44-00-00-03-00-00-00-00-00-00-7A-43
58+
0A-00-44-00-00-04-43-4D-00-00
59+
08-00-44-00-00-05-91-00
60+
0A-00-44-00-00-80-01-01-05-01
61+
11-00-44-00-01-00-44-49-53-54-53-00-00-00-00-00-00
62+
0E-00-44-00-01-01-00-00-00-00-00-00-A0-43
63+
0E-00-44-00-01-02-00-00-00-00-00-00-C8-42
64+
0E-00-44-00-01-03-00-00-00-00-00-00-00-42
65+
0A-00-44-00-01-04-43-4D-00-00
66+
08-00-44-00-01-05-F1-00
67+
0A-00-44-00-01-80-01-01-04-01
68+
11-00-44-00-02-00-53-49-4E-47-4C-00-00-00-00-00-00
69+
0E-00-44-00-02-01-00-00-00-00-00-40-1C-45
70+
0E-00-44-00-02-02-00-00-00-00-00-00-C8-42
71+
0E-00-44-00-02-03-00-00-00-00-00-00-7A-43
72+
0A-00-44-00-02-04-43-4D-00-00
73+
08-00-44-00-02-05-90-00
74+
0A-00-44-00-02-80-01-01-05-01
75+
11-00-44-00-03-00-4C-49-53-54-4E-00-00-00-00-00-00
76+
0E-00-44-00-03-01-00-00-00-00-00-00-80-3F
77+
0E-00-44-00-03-02-00-00-00-00-00-00-C8-42
78+
0E-00-44-00-03-03-00-00-00-00-00-00-80-3F
79+
0A-00-44-00-03-04-53-54-00-00
80+
08-00-44-00-03-05-10-00
81+
0A-00-44-00-03-80-01-00-01-00
82+
11-00-44-00-04-00-54-52-41-57-00-00-00-00-00-00-00
83+
0E-00-44-00-04-01-00-00-00-00-00-C4-63-46
84+
0E-00-44-00-04-02-00-00-00-00-00-00-C8-42
85+
0E-00-44-00-04-03-00-00-00-00-00-C4-63-46
86+
0A-00-44-00-04-04-75-53-00-00
87+
08-00-44-00-04-05-90-00
88+
0A-00-44-00-04-80-01-02-05-00
89+
11-00-44-00-05-00-4C-49-47-48-54-00-00-00-00-00-00
90+
0E-00-44-00-05-01-00-00-00-00-00-00-C8-42
91+
0E-00-44-00-05-02-00-00-00-00-00-00-C8-42
92+
0E-00-44-00-05-03-00-00-00-00-00-00-C8-42
93+
0A-00-44-00-05-04-50-43-54-00
94+
08-00-44-00-05-05-00-10
95+
0A-00-44-00-05-80-04-00-03-00
96+
11-00-44-00-06-00-50-49-4E-47-00-00-00-00-00-00-00
97+
0E-00-44-00-06-01-00-00-00-00-00-00-80-3F
98+
0E-00-44-00-06-02-00-00-00-00-00-00-C8-42
99+
0E-00-44-00-06-03-00-00-00-00-00-00-80-3F
100+
0A-00-44-00-06-04-50-43-54-00
101+
08-00-44-00-06-05-00-90
102+
0A-00-44-00-06-80-01-00-01-00
103+
11-00-44-00-07-00-41-44-52-41-57-00-00-00-00-00-00
104+
0E-00-44-00-07-01-00-00-00-00-00-00-80-44
105+
0E-00-44-00-07-02-00-00-00-00-00-00-C8-42
106+
0E-00-44-00-07-03-00-00-00-00-00-00-80-44
107+
0A-00-44-00-07-04-50-43-54-00
108+
08-00-44-00-07-05-90-00
109+
0A-00-44-00-07-80-01-01-04-00
110+
11-00-44-00-08-00-43-41-4C-49-42-00-00-00-00-00-00
111+
0E-00-44-00-08-01-00-00-00-00-00-00-7F-43
112+
0E-00-44-00-08-02-00-00-00-00-00-00-C8-42
113+
0E-00-44-00-08-03-00-00-00-00-00-00-7F-43
114+
0A-00-44-00-08-04-50-43-54-00
115+
08-00-44-00-08-05-00-00
116+
0A-00-44-00-08-80-07-00-03-00
117+
"
118+
}).Trim().Split("\n").Select(s => BytesStringUtil.StringToData(s));
119+
}
120+
}

src/SharpBrick.PoweredUp/Enums/SystemType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace SharpBrick.PoweredUp
22
{
3+
// read binary interpretation careful: 3_5
34
public enum SystemType : byte
45
{
56
LegoWeDo20_WeDoHub = 0b000_00000,
@@ -9,7 +10,7 @@ public enum SystemType : byte
910
LegoSystem_BoostHub = 0b010_00000,
1011
LegoSystem_TwoPortHub = 0b010_00001,
1112
LegoSystem_TwoPortHandset = 0b010_00010,
12-
LegoSystem_Mario = 0b0100_0011, // UNSPECED, https://github.com/bricklife/LEGO-Mario-Reveng, 0x43,
13+
LegoSystem_Mario = 0b010_00011, // UNSPECED, https://github.com/bricklife/LEGO-Mario-Reveng, 0x43,
1314

1415
LegoTechnic_MediumHub = 0b100_00000, // UNSPECED
1516
}

0 commit comments

Comments
 (0)