-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main()
207 lines (179 loc) · 6.36 KB
/
Main()
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using Windows.Devices.Gpio;
using Windows.UI.Core;
namespace App3
{
public sealed partial class MainPage : Page
{
#region -----Variable Assignments-----
private const int leftE_PIN = 25;
private const int left1_PIN = 12;
private const int left2_PIN = 16;
private const int rightE_PIN = 18;
private const int right1_PIN = 24;
private const int right2_PIN = 23;
private GpioPin leftE;
private GpioPin left1;
private GpioPin left2;
private GpioPin rightE;
private GpioPin right1;
private GpioPin right2;
GpioPinValue pinValueHigh = GpioPinValue.High;
GpioPinValue pinValueLow = GpioPinValue.Low;
#endregion
public MainPage()
{
InitGPIO();
this.InitializeComponent();
CoreWindow.GetForCurrentThread().KeyDown += Window_KeyDown;
CoreWindow.GetForCurrentThread().KeyUp += Window_KeyUp;
}
private void InitGPIO()
{
//Gets the default GPIO if there is only one
GpioController gpio = GpioController.GetDefault();
// Show an error if there is no GPIO controller
if (gpio == null)
{
leftE = null;
left1 = null;
left2 = null;
rightE = null;
right1 = null;
right2 = null;
return;
}
leftE = gpio.OpenPin(leftE_PIN);
left1 = gpio.OpenPin(left1_PIN);
left2 = gpio.OpenPin(left2_PIN);
rightE = gpio.OpenPin(rightE_PIN);
right1 = gpio.OpenPin(right1_PIN);
right2 = gpio.OpenPin(right2_PIN);
leftE.Write(pinValueHigh);
left1.Write(pinValueHigh);
left2.Write(pinValueHigh);
rightE.Write(pinValueHigh);
right1.Write(pinValueHigh);
right2.Write(pinValueHigh);
leftE.SetDriveMode(GpioPinDriveMode.Output);
left1.SetDriveMode(GpioPinDriveMode.Output);
left2.SetDriveMode(GpioPinDriveMode.Output);
rightE.SetDriveMode(GpioPinDriveMode.Output);
right1.SetDriveMode(GpioPinDriveMode.Output);
right2.SetDriveMode(GpioPinDriveMode.Output);
}
private void defaultPins()
{
leftE.Write(pinValueHigh);
left1.Write(pinValueHigh);
left2.Write(pinValueHigh);
rightE.Write(pinValueHigh);
right1.Write(pinValueHigh);
right2.Write(pinValueHigh);
leftE.SetDriveMode(GpioPinDriveMode.Output);
left1.SetDriveMode(GpioPinDriveMode.Output);
left2.SetDriveMode(GpioPinDriveMode.Output);
rightE.SetDriveMode(GpioPinDriveMode.Output);
right1.SetDriveMode(GpioPinDriveMode.Output);
right2.SetDriveMode(GpioPinDriveMode.Output);
}
private void forwardButton_Click(object sender, RoutedEventArgs e)
{
carForward();
}
private void reverseButton_Click(object sender, RoutedEventArgs e)
{
carReverse();
}
private void stopButton_Click(object sender, RoutedEventArgs e)
{
carStop();
}
#region -----Car Movements-----
private void carForward()
{
defaultPins();
leftE.Write(pinValueHigh);
rightE.Write(pinValueHigh);
left1.Write(pinValueLow);
right1.Write(pinValueLow);
left2.Write(pinValueHigh);
right2.Write(pinValueHigh);
}
private void carReverse()
{
defaultPins();
leftE.Write(pinValueHigh);
rightE.Write(pinValueHigh);
left1.Write(pinValueHigh);
right1.Write(pinValueHigh);
left2.Write(pinValueLow);
right2.Write(pinValueLow);
}
private void carRight()
{
defaultPins();
leftE.Write(pinValueHigh);
rightE.Write(pinValueHigh);
left1.Write(pinValueLow);
right1.Write(pinValueHigh);
left2.Write(pinValueHigh);
right2.Write(pinValueLow);
}
private void carLeft()
{
defaultPins();
leftE.Write(pinValueHigh);
rightE.Write(pinValueHigh);
left1.Write(pinValueHigh);
right1.Write(pinValueLow);
left2.Write(pinValueLow);
right2.Write(pinValueHigh);
}
private void carStop()
{
defaultPins();
}
#endregion
private void Window_KeyDown(CoreWindow sender, KeyEventArgs e)
{
Debug.WriteLine(e.VirtualKey);
if (e.VirtualKey == Windows.System.VirtualKey.Up || e.VirtualKey == Windows.System.VirtualKey.W)
{
carForward();
}
else if (e.VirtualKey == Windows.System.VirtualKey.Down || e.VirtualKey == Windows.System.VirtualKey.S)
{
carReverse();
}
else if (e.VirtualKey == Windows.System.VirtualKey.X)
{
carStop();
}
else if (e.VirtualKey == Windows.System.VirtualKey.Left || e.VirtualKey == Windows.System.VirtualKey.A)
{
carLeft();
}
else if (e.VirtualKey == Windows.System.VirtualKey.Right || e.VirtualKey == Windows.System.VirtualKey.D)
{
carRight();
}
}
private void Window_KeyUp(CoreWindow sender, KeyEventArgs e)
{
Debug.WriteLine(e.VirtualKey);
if (
e.VirtualKey == Windows.System.VirtualKey.Up ||
e.VirtualKey == Windows.System.VirtualKey.W ||
e.VirtualKey == Windows.System.VirtualKey.Down ||
e.VirtualKey == Windows.System.VirtualKey.S ||
e.VirtualKey == Windows.System.VirtualKey.Left ||
e.VirtualKey == Windows.System.VirtualKey.A ||
e.VirtualKey == Windows.System.VirtualKey.Right ||
e.VirtualKey == Windows.System.VirtualKey.D ||
e.VirtualKey == Windows.System.VirtualKey.X
)
{
carStop();
}
}
}