forked from sharpbrick/powered-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExampleColorDistanceSensor.cs
60 lines (51 loc) · 2.45 KB
/
ExampleColorDistanceSensor.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpBrick.PoweredUp;
namespace Example;
public class ExampleColorDistanceSensor : BaseExample
{
public override async Task ExecuteAsync()
{
using (var twoPortHub = Host.FindByType<TwoPortHub>())
{
var colorDistSensor = twoPortHub.B.GetDevice<ColorDistanceSensor>();
var observers = new[] {
colorDistSensor.ColorObservable
.Subscribe(color => Log.LogInformation("Color: {0}", color)),
colorDistSensor.RgbObservable
.Subscribe(rgb => Log.LogInformation("RGB: R: {0}, G: {1}, B: {2}", rgb.red, rgb.green, rgb.blue)),
colorDistSensor.ReflectionObservable
.Subscribe(refl => Log.LogInformation("Reflection: {0}", refl)),
colorDistSensor.AmbientLightObservable
.Subscribe(aml => Log.LogInformation("Ambient Light: {0}", aml)),
colorDistSensor.CountObservable
.Subscribe(cnt => Log.LogInformation("Count: {0}", cnt)),
colorDistSensor.ProximityObservable
.Subscribe(dst => Log.LogInformation("Proximity: {0}", dst)),
};
await TestMode(colorDistSensor, colorDistSensor.ModeIndexColor);
await TestMode(colorDistSensor, colorDistSensor.ModeIndexRgb);
await TestMode(colorDistSensor, colorDistSensor.ModeIndexReflection);
await TestMode(colorDistSensor, colorDistSensor.ModeIndexAmbientLight);
await TestMode(colorDistSensor, colorDistSensor.ModeIndexCount);
await TestMode(colorDistSensor, colorDistSensor.ModeIndexProximity);
foreach (var observer in observers)
{
observer.Dispose();
}
await twoPortHub.SwitchOffAsync();
}
async Task TestMode(ColorDistanceSensor colorDistSensor, byte mode)
{
Log.LogInformation("Teseting mode {0} - START", mode);
await colorDistSensor.SetupNotificationAsync(mode, enabled: true);
await Task.Delay(TimeSpan.FromMinutes(1));
await colorDistSensor.SetupNotificationAsync(mode, enabled: false);
Log.LogInformation("Teseting mode {0} - END", mode);
}
}
}