-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainController.cs
64 lines (48 loc) · 1.77 KB
/
MainController.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
using MagicEightMeadow.Controllers;
using MagicEightMeadow.Hardware;
using Meadow;
using Meadow.Foundation.Sensors.Motion;
using Meadow.Units;
using System;
using System.Threading.Tasks;
namespace MagicEightMeadow;
internal class MainController
{
private bool isAnswering;
private IMagicEightMeadowHardware hardware;
private DisplayController displayController;
public MainController(IMagicEightMeadowHardware hardware)
{
this.hardware = hardware;
}
public void Initialize()
{
hardware.Initialize();
displayController = new DisplayController(hardware.Display);
var consumer = Bmi270.CreateObserver(
handler: result => MotionSensorHandler(result),
filter: result => MotionSensorFilter(result));
hardware.MotionSensor.Subscribe(consumer);
}
private async void MotionSensorHandler(IChangeResult<(Acceleration3D? a3D, AngularVelocity3D? v3D, Temperature? t)> e)
{
if (isAnswering)
return;
isAnswering = true;
hardware.RgbPwmLed.SetColor(Color.Orange);
displayController.ShowAnswer();
await Task.Delay(TimeSpan.FromSeconds(5));
displayController.ShowQuestion();
hardware.RgbPwmLed.SetColor(Color.Green);
isAnswering = false;
}
private bool MotionSensorFilter(IChangeResult<(Acceleration3D? a3D, AngularVelocity3D? v3D, Temperature? t)> e)
{
return e.New.v3D.Value.X.DegreesPerSecond > 0.75 || e.New.v3D.Value.Y.DegreesPerSecond > 0.75 || e.New.v3D.Value.Z.DegreesPerSecond > 0.75;
}
public void Run()
{
displayController.ShowQuestion();
hardware.MotionSensor.StartUpdating(TimeSpan.FromSeconds(1));
}
}