-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMeadowApp.cs
134 lines (117 loc) · 4.19 KB
/
MeadowApp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Leds;
using Meadow.Foundation.Motors;
using Meadow.Gateways.Bluetooth;
using Meadow.Peripherals.Leds;
using System.Threading.Tasks;
namespace MeadowBleRover;
// public class MeadowApp : App<F7FeatherV1> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7FeatherV2>
{
Definition bleTreeDefinition;
CharacteristicBool up, down, left, right;
RgbLed led;
Led ledUp, ledDown, ledLeft, ledRight;
CarController carController;
public override Task Initialize()
{
led = new RgbLed(
Device.Pins.OnboardLedRed,
Device.Pins.OnboardLedGreen,
Device.Pins.OnboardLedBlue);
led.SetColor(RgbLedColors.Red);
ledUp = new Led(Device.Pins.D13);
ledDown = new Led(Device.Pins.D10);
ledLeft = new Led(Device.Pins.D11);
ledRight = new Led(Device.Pins.D12);
ledUp.IsOn = ledDown.IsOn = ledLeft.IsOn = ledRight.IsOn = true;
var motorLeft = new HBridgeMotor
(
a1Pin: Device.Pins.D07,
a2Pin: Device.Pins.D08,
enablePin: Device.Pins.D09
);
var motorRight = new HBridgeMotor
(
a1Pin: Device.Pins.D02,
a2Pin: Device.Pins.D03,
enablePin: Device.Pins.D04
);
carController = new CarController(motorLeft, motorRight);
led.SetColor(RgbLedColors.Blue);
bleTreeDefinition = GetDefinition();
Device.BluetoothAdapter.StartBluetoothServer(bleTreeDefinition);
up.ValueSet += UpValueSet;
down.ValueSet += DownValueSet;
left.ValueSet += LeftValueSet;
right.ValueSet += RightValueSet;
led.SetColor(RgbLedColors.Green);
return base.Initialize();
}
void UpValueSet(ICharacteristic c, object data)
{
ledUp.IsOn = (bool)data;
if ((bool)data)
carController.MoveForward();
else
carController.Stop();
}
void DownValueSet(ICharacteristic c, object data)
{
ledDown.IsOn = (bool)data;
if ((bool)data)
carController.MoveBackward();
else
carController.Stop();
}
void LeftValueSet(ICharacteristic c, object data)
{
ledLeft.IsOn = (bool)data;
if ((bool)data)
carController.TurnLeft();
else
carController.Stop();
}
void RightValueSet(ICharacteristic c, object data)
{
ledRight.IsOn = (bool)data;
if ((bool)data)
carController.TurnRight();
else
carController.Stop();
}
Definition GetDefinition()
{
up = new CharacteristicBool(
"Up",
uuid: "017e99d6-8a61-11eb-8dcd-0242ac1300aa",
permissions: CharacteristicPermission.Write | CharacteristicPermission.Read,
properties: CharacteristicProperty.Write | CharacteristicProperty.Read
);
down = new CharacteristicBool(
"Down",
uuid: "017e99d6-8a61-11eb-8dcd-0242ac1300bb",
permissions: CharacteristicPermission.Write | CharacteristicPermission.Read,
properties: CharacteristicProperty.Write | CharacteristicProperty.Read
);
left = new CharacteristicBool(
"Left",
uuid: "017e99d6-8a61-11eb-8dcd-0242ac1300cc",
permissions: CharacteristicPermission.Write | CharacteristicPermission.Read,
properties: CharacteristicProperty.Write | CharacteristicProperty.Read
);
right = new CharacteristicBool(
"Right",
uuid: "017e99d6-8a61-11eb-8dcd-0242ac1300dd",
permissions: CharacteristicPermission.Write | CharacteristicPermission.Read,
properties: CharacteristicProperty.Write | CharacteristicProperty.Read
);
var service = new Service(
name: "ServiceA",
uuid: 253,
up, down, left, right
);
return new Definition("MeadowRover", service);
}
}