-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
527 lines (441 loc) · 18.3 KB
/
MainPage.xaml.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// Copyright (c) Microsoft. All rights reserved.
using System;
using Windows.Devices.Gpio;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Blumenthalit.SocialUproar
{
public sealed partial class MainPage : Page
{
class WebResults
{
public WebResults() { }
public int RedVotes;
public int BlueVotes;
}
WebResults baselineResults = new WebResults();
WebResults votingResults = new WebResults();
private const int RED_PIN = 5;
private const int GREEN_PIN = 6;
private const int BLUE_PIN = 13;
private const int LEFT_MOTOR_PIN = 19;
private const int RIGHT_MOTOR_PIN = 20;
private GpioPin RedPin;
private GpioPin GreenPin;
private GpioPin BluePin;
private GpioPin BlueMotorPin;
private GpioPin RedMotorPin;
private SolidColorBrush VotingOpenBrush = new SolidColorBrush(Windows.UI.Colors.LimeGreen);
private SolidColorBrush VotingClosedBrush = new SolidColorBrush(Windows.UI.Colors.DarkGreen);
private SolidColorBrush RedOnBrush = new SolidColorBrush(Windows.UI.Colors.OrangeRed);
private SolidColorBrush RedOffBrush = new SolidColorBrush(Windows.UI.Colors.DarkRed);
private SolidColorBrush BlueOnBrush = new SolidColorBrush(Windows.UI.Colors.LightBlue);
private SolidColorBrush BlueOffBrush = new SolidColorBrush(Windows.UI.Colors.DarkBlue);
private DispatcherTimer timer;
private int SecondsRemaining;
private bool isInternetVote = false;
public MainPage()
{
//BlueMotorPin is the left motor pin and red motor pin is right motor pin
//We generally connect the yellow flappy bird to the right side of the case
//and the red blower to the left side of the case.
InitializeComponent();
RedPin = initPin(RED_PIN);
GreenPin = initPin(GREEN_PIN);
BluePin = initPin(BLUE_PIN);
BlueMotorPin = initPin(LEFT_MOTOR_PIN);
RedMotorPin = initPin(RIGHT_MOTOR_PIN);
SetState(RedPin, GpioPinValue.High);
SetState(GreenPin, GpioPinValue.High);
SetState(BluePin, GpioPinValue.High);
SetState(BlueMotorPin, GpioPinValue.Low);
SetState(RedMotorPin, GpioPinValue.High);
}
private GpioPin initPin(int PinNum)
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
// Show an error if there is no GPIO controller
GpioStatus.Text = "There is no GPIO controller on this device.";
throw new Exception(GpioStatus.Text);
}
return gpio.OpenPin(PinNum);
}
private void SetState(GpioPin pin, GpioPinValue HiOrLo)
{
try
{
pin.Write(HiOrLo);
pin.SetDriveMode(GpioPinDriveMode.Output);
GpioStatus.Text = "GPIO pin " + pin.PinNumber.ToString() + " set correctly.";
}
catch (Exception ex)
{
GpioStatus.Text = "Exception when setting pin #" + pin.PinNumber.ToString() + ". Error is " + ex.Message
+ " Sharing Mode is " + pin.SharingMode.ToString() + ".";
}
}
private void RedButton_Click(object sender, RoutedEventArgs e)
{
if (RedPin.Read() == GpioPinValue.High)
{
SetState(RedPin, GpioPinValue.Low);
RedButton.Background = RedOnBrush;
}
else
{
SetState(RedPin, GpioPinValue.High);
RedButton.Background = RedOffBrush;
}
}
private void GreenButton_Click(object sender, RoutedEventArgs e)
{
if (GreenPin.Read() == GpioPinValue.High)
{
SetState(GreenPin, GpioPinValue.Low);
GreenButton.Background = VotingOpenBrush;
}
else
{
SetState(GreenPin, GpioPinValue.High);
GreenButton.Background = VotingClosedBrush;
}
}
private void BlueButton_Click(object sender, RoutedEventArgs e)
{
if (BluePin.Read() == GpioPinValue.High)
{
SetState(BluePin, GpioPinValue.Low);
BlueButton.Background = BlueOnBrush;
}
else
{
SetState(BluePin, GpioPinValue.High);
BlueButton.Background = BlueOffBrush;
}
}
private void GoButton_Click(object sender, RoutedEventArgs e)
{
SetState(RedPin, GpioPinValue.High);
RedButton.Background = RedOffBrush;
SetState(BluePin, GpioPinValue.High);
BlueButton.Background = BlueOffBrush;
SetState(RedMotorPin, GpioPinValue.High);
SetState(BlueMotorPin, GpioPinValue.Low);
GoButton.Background = VotingOpenBrush;
SetState(GreenPin, GpioPinValue.Low);
votingResults.RedVotes = -1;
votingResults.BlueVotes = -1;
RedPrevCount.Text = "";
BluePrevCount.Text = "";
RedCurrentCount.Text = "";
BlueCurrentCount.Text = "";
RedVoteCount.Text = "";
BlueVoteCount.Text = "";
getBaselineVotes();
SecondsRemaining = int.Parse(VotingIntervalBox.Text);
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void getBaselineVotes()
{
if (isInternetVote)
{
GetCountsFromWeb(ref baselineResults);
RedPrevCount.Text = baselineResults.RedVotes.ToString();
BluePrevCount.Text = baselineResults.BlueVotes.ToString();
}
else
{
baselineResults.RedVotes = 0;
baselineResults.BlueVotes = 0;
RedPrevCount.Text = baselineResults.RedVotes.ToString();
BluePrevCount.Text = baselineResults.BlueVotes.ToString();
}
}
private void Timer_Tick(object sender, object e)
{
SecondsRemaining = SecondsRemaining - 1;
CountdownTimer.Text = (TimeSpan.FromSeconds(SecondsRemaining)).ToString("c");
if (SecondsRemaining == 0)
{
timer.Stop();
SetState(GreenPin, GpioPinValue.High);
GoButton.Background = VotingClosedBrush;
getNewVotes();
}
}
private void getNewVotes()
{
int RedVotes = 0;
int BlueVotes = 0;
if (isInternetVote)
{
GetCountsFromWeb(ref votingResults);//this sets VotingResults
if (votingResults.BlueVotes == -1)
{
debugText.Text += "Web call returned, but without data.";
}
RedCurrentCount.Text = votingResults.RedVotes.ToString();
BlueCurrentCount.Text = votingResults.BlueVotes.ToString();
if (votingResults.RedVotes == baselineResults.RedVotes && votingResults.BlueVotes == baselineResults.BlueVotes)
{
RedVotes = votingResults.RedVotes;
BlueVotes = votingResults.BlueVotes;
}
else
{
RedVotes = votingResults.RedVotes - baselineResults.RedVotes;
BlueVotes = votingResults.BlueVotes - baselineResults.BlueVotes;
}
RedVoteCount.Text = RedVotes.ToString();
BlueVoteCount.Text = BlueVotes.ToString();
}
else
{
RedVotes = (new Random(int.Parse(DateTime.Now.ToString("ss")))).Next(5,50);
BlueVotes = (new Random(int.Parse(DateTime.Now.ToString("ss")))).Next(50,100);
RedCurrentCount.Text = RedVotes.ToString();
BlueCurrentCount.Text = BlueVotes.ToString();
RedVoteCount.Text = RedCurrentCount.Text;
BlueVoteCount.Text = BlueCurrentCount.Text;
}
if (RedVotes > BlueVotes)
{
SetState(RedPin, GpioPinValue.Low);
SetState(RedMotorPin, GpioPinValue.Low);
SetState(BlueMotorPin, GpioPinValue.Low);
RedButton.Background = RedOnBrush;
BlueButton.Background = BlueOffBrush;
}
if (RedVotes < BlueVotes)
{
SetState(BluePin, GpioPinValue.Low);
SetState(BlueMotorPin, GpioPinValue.High);
SetState(RedMotorPin, GpioPinValue.High);
RedButton.Background = RedOffBrush;
BlueButton.Background = BlueOnBrush;
}
if (RedVotes == BlueVotes)
{
SetState(RedPin, GpioPinValue.Low);
SetState(RedMotorPin, GpioPinValue.Low);
SetState(BluePin, GpioPinValue.Low);
SetState(BlueMotorPin, GpioPinValue.High);
BlueButton.Background = BlueOnBrush;
RedButton.Background = RedOnBrush;
}
}
private void GetCountsFromWeb(ref WebResults VoteColorPair)
{
//Create an HTTP client object
// Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
System.Net.Http.HttpClient snhClient = new System.Net.Http.HttpClient();
//Add a user-agent header to the GET request.
var headers = snhClient.DefaultRequestHeaders;
//The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
//especially if the header value is coming from user input.
string header = "ie";
if (!headers.UserAgent.TryParseAdd(header))
{
throw new Exception("Invalid header value: " + header);
}
header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
if (!headers.UserAgent.TryParseAdd(header))
{
throw new Exception("Invalid header value: " + header);
}
Uri requestUri = new Uri(RestUrlBox.Text);
//Send the GET request asynchronously and retrieve the response as a string.
System.Net.Http.HttpResponseMessage snhResponse = new System.Net.Http.HttpResponseMessage();
// Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";
try
{
int Reds = -1;
int Blues = -1;
//Send the GET request
snhResponse = snhClient.GetAsync(requestUri).Result;
//httpResponse = await httpClient.GetAsync(requestUri,Windows.Web.Http.HttpCompletionOption.ResponseContentRead).Re;
snhResponse.EnsureSuccessStatusCode();
//httpResponse.EnsureSuccessStatusCode();
//httpResponseBody = httpResponse.Content.ToString();
httpResponseBody = snhResponse.Content.ReadAsStringAsync().Result;
debugText.Text += httpResponseBody;
int blueCloseCurlyLoc = httpResponseBody.IndexOf("}");
string bluestring = httpResponseBody.Substring(0, blueCloseCurlyLoc);
int lastColonLoc = bluestring.LastIndexOf(":");
bluestring = bluestring.Substring(lastColonLoc+1, blueCloseCurlyLoc - lastColonLoc-1);
Blues = int.Parse(bluestring);
int redCloseCurlyLoc = httpResponseBody.LastIndexOf("}");
string redstring = httpResponseBody.Substring(blueCloseCurlyLoc+1, redCloseCurlyLoc - blueCloseCurlyLoc);
lastColonLoc = redstring.LastIndexOf(":");
redCloseCurlyLoc = redstring.LastIndexOf("}");
redstring = redstring.Substring(lastColonLoc + 1, redCloseCurlyLoc - lastColonLoc - 1);
Reds = int.Parse(redstring);
/* JObject voteJson = JObject.Parse(httpResponseBody);
if (voteJson[0]["Color"].ToString() == "Blue")
{
Blues = int.Parse(voteJson[0]["Count"].ToString());
}
if (voteJson[1]["Color"].ToString() == "Red")
{
Reds = int.Parse(voteJson[1]["Count"].ToString());
}
*/
VoteColorPair.RedVotes = Reds;
VoteColorPair.BlueVotes = Blues;
}
catch (Exception ex)
{
httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
debugText.Text = httpResponseBody;
}
}
private void RedMotorButton_Click(object sender, RoutedEventArgs e)
{
if (RedMotorPin.Read() == GpioPinValue.High )
{
SetState(RedMotorPin, GpioPinValue.Low);
RedMotorButton.Background = RedOnBrush;
}
else
{
SetState(RedMotorPin, GpioPinValue.High);
RedMotorButton.Background = RedOffBrush;
}
}
private void BlueMotorButton_Click(object sender, RoutedEventArgs e)
{
if (BlueMotorPin.Read() == GpioPinValue.High)
{
SetState(BlueMotorPin, GpioPinValue.Low);
BlueMotorButton.Background = BlueOffBrush;
}
else
{
SetState(BlueMotorPin, GpioPinValue.High);
BlueMotorButton.Background = BlueOnBrush;
}
}
private void VoteCountMode_Toggled(object sender, RoutedEventArgs e)
{
if (VoteCountMode.IsOn)
{
isInternetVote = true;
}
else
{
isInternetVote = false;
}
}
private void rootPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
System.Diagnostics.Debug.WriteLine(rootPivot.SelectedIndex + ":" + ((PivotItem)(rootPivot.SelectedItem)).Name);
if (((PivotItem)(rootPivot.SelectedItem)).Name == "Vote")
{
if (isInternetVote)
{
VoteModeTextbox.Text = "Internet";
}
else
{
VoteModeTextbox.Text = "local";
}
SecondsRemaining = int.Parse(VotingIntervalBox.Text);
CountdownTimer.Text = (TimeSpan.FromSeconds(SecondsRemaining)).ToString("c");
}
if (((PivotItem)(rootPivot.SelectedItem)).Name == "Test")
{
if (BlueMotorPin.Read() == GpioPinValue.High)
{
BlueMotorButton.Background = BlueOnBrush;
}
else
{
BlueMotorButton.Background = BlueOffBrush;
}
if (RedMotorPin.Read() == GpioPinValue.Low)
{
RedMotorButton.Background = RedOnBrush;
}
else
{
RedMotorButton.Background = RedOffBrush;
}
if (RedPin.Read() == GpioPinValue.High)
{
RedButton.Background = RedOffBrush;
}
else
{
RedButton.Background = RedOnBrush;
}
if (BluePin.Read() == GpioPinValue.High)
{
BlueButton.Background = BlueOffBrush;
}
else
{
BlueButton.Background = BlueOnBrush;
}
if (GreenPin.Read() == GpioPinValue.High)
{
GreenButton.Background = VotingClosedBrush;
}
else
{
GreenButton.Background = VotingClosedBrush;
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message);
debugText.Text += ex.Message;
}
}
private void HurryUp_Click(object sender, RoutedEventArgs e)
{
if (SecondsRemaining > 5) { SecondsRemaining = 5; }
}
private void LeftMotorPinNumber_SelectionChanged(object sender, SelectionChangedEventArgs e)
//Not Tested yet
{
ChangeMotorPin(BlueMotorPin, (int)e.AddedItems[0],"Blue Motor Pin");
}
private void ChangeMotorPin(GpioPin MotorPin, int newPinNumber, string DescriptivePinName)
{
var gpio = GpioController.GetDefault();
if (gpio == null)
{
// Show an error if there is no GPIO controller
GpioStatus.Text = "There is no GPIO controller on this device.";
throw new Exception(GpioStatus.Text);
}
GpioOpenStatus status;
MotorPin.Dispose();
gpio.TryOpenPin(newPinNumber, GpioSharingMode.Exclusive, out MotorPin, out status);
if (status == GpioOpenStatus.PinOpened)
{
debugText.Text += DescriptivePinName + " set to " + newPinNumber.ToString();
}
else
{
debugText.Text += "Pin failed to open. Status is " + status.ToString() +".";
}
}
private void RightMotorPinNumber_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ChangeMotorPin(RedMotorPin, (int)e.AddedItems[0], "Red Motor Pin");
}
}
}