forked from sharpbrick/powered-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleDuploTrainBase.cs
69 lines (56 loc) · 3.81 KB
/
ExampleDuploTrainBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;
namespace Example;
public class ExampleDuploTrainBase : BaseExample
{
public override async Task ExecuteAsync()
{
using var train = Host.FindByType<DuploTrainBaseHub>();
await train.VerifyDeploymentModelAsync(modelBuilder => modelBuilder
.AddHub<DuploTrainBaseHub>(hubBuilder => { })
);
using var d1 = train.Voltage.VoltageSObservable.Subscribe(x => Log.LogWarning($"Voltage: {x.Pct}% / {x.SI}"));
using var d2 = train.Motor.OnSecondsObservable.Subscribe(x => Log.LogWarning($"Seconds: {x}"));
using var d3 = train.Speedometer.SpeedObservable.Subscribe(x => Log.LogWarning($"Speed: {x.Pct}% / {x.SI}"));
using var d4 = train.Speedometer.CountObservable.Subscribe(x => Log.LogWarning($"Count: {x}"));
using var d5 = train.ColorSensor.ColorObservable.Subscribe(x => Log.LogWarning($"Color: {x}"));
using var d6 = train.ColorSensor.ColorTagObservable.Subscribe(x => Log.LogWarning($"Color Tag: {x}")); // does not work
using var d7 = train.ColorSensor.ReflectionObservable.Subscribe(x => Log.LogWarning($"Reflection: {x}"));
using var d8 = train.ColorSensor.RgbObservable.Subscribe(x => Log.LogWarning($"RGB {x.red}/{x.green}/{x.blue}"));
await train.Voltage.SetupNotificationAsync(train.Voltage.ModeIndexVoltageS, true);
// Motor: motor can either be queried for seconds active OR be instructed to run
//await train.Motor.SetupNotificationAsync(train.Motor.ModeIndexOnSec, false);
await train.Speedometer.TryLockDeviceForCombinedModeNotificationSetupAsync(train.Speedometer.ModeIndexSpeed, train.Speedometer.ModeIndexCount);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexSpeed, true, deltaInterval: 1);
await train.Speedometer.SetupNotificationAsync(train.Speedometer.ModeIndexCount, true, deltaInterval: 1);
await train.Speedometer.UnlockFromCombinedModeNotificationSetupAsync(true);
// ColorSensor: either this combination
// await train.ColorSensor.TryLockDeviceForCombinedModeNotificationSetupAsync(train.ColorSensor.ModeIndexColor, train.ColorSensor.ModeIndexReflection, train.ColorSensor.ModeIndexRgb);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColor, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexReflection, true, deltaInterval: 1);
// await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexRgb, true, deltaInterval: 1);
// await train.ColorSensor.UnlockFromCombinedModeNotificationSetupAsync(true);
// ColorSensor: or standalone
await train.ColorSensor.SetupNotificationAsync(train.ColorSensor.ModeIndexColorTag, true, deltaInterval: 1);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Horn);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Red);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Yellow);
await Task.Delay(1_000);
await train.RgbLight.SetRgbColorNoAsync(PoweredUpColor.Green);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.StationDeparture);
await train.Motor.StartPowerAsync(40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Steam);
await train.Motor.StartPowerAsync(-40);
await Task.Delay(3_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.Brake);
await train.Motor.StopByFloatAsync();
await Task.Delay(1_000);
await train.Speaker.PlaySoundAsync(DuploTrainBaseSound.WaterRefill);
await Task.Delay(1_000);
await train.SwitchOffAsync();
}
}