-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
522 lines (476 loc) · 19.5 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
522 lines (476 loc) · 19.5 KB
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
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WindowsGamingInput = Windows.Gaming.Input;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.UI.Core;
using SharpDX.XInput;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace BluetoothPanel
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
// Serial bluetooth communication
public static readonly Guid SerialGuid = new Guid(0x00001101, 0x0000, 0x1000, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB);
private DispatcherQueue dispatcherQueue;
String SelectedDeviceId;
BluetoothDevice BTDevice;
RfcommDeviceService BTService;
StreamSocket BTSocket;
private DataWriter _writer;
private DataReader _reader;
ObservableCollection<KeyValueListItem> KeyValueListItems = new ObservableCollection<KeyValueListItem>();
ObservableCollection<KeyValueGridItem> KeyValueGridItems = new ObservableCollection<KeyValueGridItem>();
private Controller currentController;
bool listening = false;
DispatcherTimer dispatcherTimer;
public MainWindow()
{
this.InitializeComponent();
dispatcherQueue = DispatcherQueue.GetForCurrentThread();
LeftListView.ItemsSource = KeyValueListItems;
RightGridView.ItemsSource = KeyValueGridItems;
WindowsGamingInput.Gamepad.GamepadAdded += Gamepad_GamepadAdded;
}
private Gamepad gamepad;
private void Gamepad_GamepadAdded(object sender, WindowsGamingInput.Gamepad e)
{
Debug.WriteLine("ADDED" + e);
var controllers = new[] { new Controller(UserIndex.One), new Controller(UserIndex.Two), new Controller(UserIndex.Three), new Controller(UserIndex.Four) };
foreach (var controller in controllers)
{
if (controller.IsConnected)
{
currentController = controller;
}
}
if (currentController == null)
{
Debug.WriteLine("FAILED");
}
listening = true;
Gamepad_GamepadListener();
// Handle the connected gamepad.
// You can access information about the gamepad using the 'e' parameter.
}
private void Gamepad_GamepadRemoved(object sender, Gamepad e)
{
Debug.WriteLine("REMOVED" + e);
currentController = null;
listening = false;
// Handle the connected gamepad.
// You can access information about the gamepad using the 'e' parameter.
}
private void Gamepad_GamepadListener()
{
Task t = Task.Run(async () =>
{
while (listening)
{
if (currentController == null)
{
await Task.Delay(TimeSpan.FromSeconds(2));
continue;
}
var state = currentController.GetState();
var buttons = state.Gamepad.Buttons;
Dictionary<String, object> dataToSend = new Dictionary<String, object>()
{
{ "getLeftTrigger", state.Gamepad.LeftTrigger },
{ "getRightTrigger", state.Gamepad.RightTrigger },
{ "getLeftThumbX", state.Gamepad.LeftThumbX },
{ "getLeftThumbY", state.Gamepad.LeftThumbY },
{ "getRightThumbX", state.Gamepad.RightThumbX },
{ "getRightThumbY", state.Gamepad.RightThumbY },
{ "getAButton", buttons.HasFlag(GamepadButtonFlags.A) },
{ "getBButton", buttons.HasFlag(GamepadButtonFlags.B) },
{ "getXButton", buttons.HasFlag(GamepadButtonFlags.X) },
{ "getYButton", buttons.HasFlag(GamepadButtonFlags.Y) },
{ "getDPadUpButton", buttons.HasFlag(GamepadButtonFlags.DPadUp) },
{ "getDPadDownButton", buttons.HasFlag(GamepadButtonFlags.DPadDown) },
{ "getDPadLeftButton", buttons.HasFlag(GamepadButtonFlags.DPadLeft) },
{ "getDPadRightButton", buttons.HasFlag(GamepadButtonFlags.DPadRight) },
{ "getLeftShoulderButton", buttons.HasFlag(GamepadButtonFlags.LeftShoulder) },
{ "getRightShoulderButton", buttons.HasFlag(GamepadButtonFlags.RightShoulder) },
{ "getLeftThumbButton", buttons.HasFlag(GamepadButtonFlags.LeftThumb) },
{ "getRightThumbButton", buttons.HasFlag(GamepadButtonFlags.RightThumb) },
{ "getStartButton", buttons.HasFlag(GamepadButtonFlags.Start) },
{ "getBackButton", buttons.HasFlag(GamepadButtonFlags.Back) },
};
addKeyFromApp(dataToSend);
await Task.Delay(TimeSpan.FromMilliseconds(20));
}
});
}
private void addKeyFromApp(Dictionary<String, object> dataToSend)
{
Database.GetInstance().updateDBFromApp(dataToSend);
UpdateLeftListView();
UpdateRightGridView();
}
private void Window_Activated(object sender, Microsoft.UI.Xaml.WindowActivatedEventArgs e)
{
OffsetCanvasButtons(500, 500);
}
private void Window_SizeChanged(object sender, Microsoft.UI.Xaml.WindowSizeChangedEventArgs e)
{
double newWidth = e.Size.Width;
double newHeight = e.Size.Height;
OffsetCanvasButtons(newWidth, newHeight);
}
private void OffsetCanvasButtons(double width, double height)
{
Canvas.SetLeft(BottomRightButtonOne, width - 100);
Canvas.SetTop(BottomRightButtonOne, height - 50);
Canvas.SetLeft(BottomRightButtonTwo, width - 100);
Canvas.SetTop(BottomRightButtonTwo, height - 100);
Canvas.SetLeft(BottomRightButtonThree, width - 100);
Canvas.SetTop(BottomRightButtonThree, height - 150);
Canvas.SetLeft(BottomRightDropDown, width - 100);
Canvas.SetTop(BottomRightDropDown, height - 200);
}
public void LeftListViewItemClicked(object sender, RoutedEventArgs e)
{
var tappedStackPanel = sender as StackPanel;
var key = tappedStackPanel.DataContext as String;
// Check if item is already in Grid View
foreach (var item in KeyValueGridItems)
{
if (item.Key == key) return;
}
// Add Item to Grid View
KeyValueGridItems.Add(new KeyValueGridItem(key, Database.GetInstance().data[key].ToString()));
}
public void GridItemFocused(object sender, RoutedEventArgs e)
{
var focusedTextBox = sender as TextBox;
var key = focusedTextBox.DataContext as String;
List<String> currentKeys = KeyValueGridItems.Select(item => item.Key).ToList();
int index = currentKeys.IndexOf(key);
if (index != -1)
{
KeyValueGridItems[index].IsFocused = true;
}
}
public void GridItemUnfocused(object sender, RoutedEventArgs e)
{
// Remove item focus
var focusedTextBox = sender as TextBox;
var key = focusedTextBox.DataContext as String;
List<String> currentKeys = KeyValueGridItems.Select(item => item.Key).ToList();
int index = currentKeys.IndexOf(key);
if (index != -1)
{
KeyValueGridItems[index].IsFocused = false;
}
}
public void GridItemChanged(object sender, RoutedEventArgs e)
{
// Remove item focus
var focusedTextBox = sender as TextBox;
var key = focusedTextBox.DataContext as String;
List<String> currentKeys = KeyValueGridItems.Select(item => item.Key).ToList();
int index = currentKeys.IndexOf(key);
Database instance = Database.GetInstance();
Value currentValue = instance.data[key];
String newValue = focusedTextBox.Text;
KeyValueGridItems[index].Value = newValue;
// If item is a getter, update database
if (key.StartsWith("get"))
{
if (currentValue is BooleanValue)
{
if (bool.TryParse(newValue, out bool bRes))
{
instance.data[key] = new BooleanValue(bRes);
}
}
else if (currentValue is NumberValue)
{
if (double.TryParse(newValue, out double dRes))
{
instance.data[key] = new NumberValue(dRes);
}
else if (int.TryParse(newValue, out int iRes))
{
instance.data[key] = new NumberValue(iRes);
}
}
else if (currentValue is StringValue)
{
instance.data[key] = new StringValue(newValue);
}
}
UpdateLeftListView();
UpdateRightGridView();
}
public void GridStackPanelClicked(object sender, PointerRoutedEventArgs e)
{
// Check if it is a right click
if (e.GetCurrentPoint(sender as UIElement).Properties.IsRightButtonPressed)
{
var clickedStackPanel = sender as StackPanel;
var key = clickedStackPanel.DataContext as String;
List<String> currentKeys = KeyValueGridItems.Select(item => item.Key).ToList();
int index = currentKeys.IndexOf(key);
if (index != -1)
{
KeyValueGridItems.RemoveAt(index);
}
}
}
public async void FindBluetoothConnections(object sender, RoutedEventArgs e)
{
BottomRightButtonTwo.IsEnabled = false;
DeviceInformationCollection PairedBluetoothDevices =
await DeviceInformation.FindAllAsync(BluetoothDevice.GetDeviceSelectorFromPairingState(true));
BottomRightDropDownFlyout.Items.Clear();
Debug.WriteLine("Paired Devices: ");
foreach (DeviceInformation device in PairedBluetoothDevices)
{
Debug.WriteLine(device.Name.ToString());
MenuFlyoutItem temp = new MenuFlyoutItem { Text = device.Name };
temp.Click += (s, e) =>
{
DropDownSelected(device.Id);
};
BottomRightDropDownFlyout.Items.Add(temp);
}
BottomRightButtonTwo.IsEnabled = true;
}
public void ConnectToDevice(object sender, RoutedEventArgs e)
{
// await ConnectToDevice();
_reader = null;
_writer = null;
BTSocket.Dispose();
BTSocket = null;
BTService.Dispose();
BTService = null;
BTDevice.Dispose();
BTDevice = null;
Database.GetInstance().resetShouldSendData();
}
public void ResetShouldSendData(object sender, RoutedEventArgs e)
{
Database.GetInstance().resetShouldSendData();
}
public async Task<bool> ConnectToDevice()
{
bool retVal = false;
BottomRightButtonOne.IsEnabled = false;
try
{
BTDevice = await BluetoothDevice.FromIdAsync(SelectedDeviceId);
var result = await BTDevice.GetRfcommServicesForIdAsync(RfcommServiceId.FromUuid(SerialGuid));
if (result.Services.Count > 0)
{
BTService = result.Services[0];
BTSocket = new StreamSocket();
// Connect to thingy
await BTSocket.ConnectAsync(BTService.ConnectionHostName, BTService.ConnectionServiceName,
SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
_writer = new DataWriter(BTSocket.OutputStream);
_reader = new DataReader(BTSocket.InputStream);
retVal = true;
}
}
catch (Exception e)
{
Console.WriteLine("Could not connect to Serial Device");
Console.WriteLine($"Error: {e}");
}
BottomRightButtonOne.IsEnabled = true;
return retVal;
}
public async void DropDownSelected(String selected)
{
Debug.WriteLine(selected);
SelectedDeviceId = selected;
await ConnectToDevice();
ReadListener(_reader);
WriteSender(_writer);
}
public void ReadListener(DataReader reader)
{
Task t = Task.Run(async () =>
{
Debug.WriteLine("Started");
StringBuilder receiveBuffer = new StringBuilder();
while (true)
{
if (reader == null) { return; }
uint buf;
buf = await reader.LoadAsync(1);
if (reader.UnconsumedBufferLength > 0)
{
string s = reader.ReadString(1);
if (s.Equals("\n") || s.Equals("\r"))
{
try
{
OnMessageReceived(receiveBuffer.ToString());
receiveBuffer.Clear();
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
}
else
{
receiveBuffer.Append(s);
}
}
else
{
await Task.Delay(TimeSpan.FromSeconds(0));
}
}
});
}
public void OnMessageReceived(String message)
{
Debug.WriteLine(message);
try
{
Dictionary<String, object> temp = JsonSerializer.Deserialize<Dictionary<String, object>>(message);
Database.GetInstance().updateDB(temp);
UpdateLeftListView();
UpdateRightGridView();
}
catch (Exception e)
{
// TODO: FUTURE ME FIX THE ERROR BEING TRHROWN IN THS FUNCTION
// Debug.WriteLine(e);
}
}
public void WriteSender(DataWriter writer)
{
Task t = Task.Run(async () => {
while (true)
{
if (writer == null) return;
try
{
String db = Database.GetInstance().ToString();
writer.WriteString(db + "|");
await writer.StoreAsync();
await Task.Delay(50);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
return;
}
}
});
}
public void UpdateLeftListView()
{
// Update UI From non UI Thread
dispatcherQueue.TryEnqueue(() => {
List<String> currentKeys = KeyValueListItems.Select(item => item.Key).ToList();
foreach (var item in Database.GetInstance().data)
{
int index = currentKeys.IndexOf(item.Key);
// Is item in UI?
if (index != -1)
{
// ITEM ALREADY IN UI
// Check if value is different
if (KeyValueListItems[index].Value != item.Value.ToString())
{
// Value is different, update value
KeyValueListItems[index] = new KeyValueListItem(item.Key, item.Value.ToString());
}
}
else
{
// ITEM NOT IN UI
// Add item to UI
KeyValueListItems.Add(new KeyValueListItem(item.Key, item.Value.ToString()));
}
}
});
}
public void UpdateRightGridView()
{
dispatcherQueue.TryEnqueue(() =>
{
List<String> currentKeys = KeyValueGridItems.Select(item => item.Key).ToList();
foreach (var item in Database.GetInstance().data)
{
int index = currentKeys.IndexOf(item.Key);
// Is item in UI?
if (index != -1)
{
// ITEM ALREADY IN UI
// If item is being edited, do not update
if (KeyValueGridItems[index].IsFocused)
{
continue;
}
// Check if value is different
if (KeyValueGridItems[index].Value != item.Value.ToString())
{
// Value is different, update value
KeyValueGridItems[index] = new KeyValueGridItem(item.Key, item.Value.ToString());
}
}
}
});
}
}
public class KeyValueListItem
{
public String Key { get; set; }
public String Value { get; set; }
public KeyValueListItem(String Key, String Value)
{
this.Key = Key;
this.Value = Value;
}
}
public class KeyValueGridItem
{
public String Key { get; set; }
public String Value { get; set; }
public bool IsFocused { get; set; }
public KeyValueGridItem(String Key, String Value)
{
this.Key = Key;
this.Value = Value;
this.IsFocused = false;
}
}
}