/
MainViewModel.cs
395 lines (346 loc) · 13.5 KB
/
MainViewModel.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
using EightRacer.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Windows.Networking.Proximity;
using Windows.Networking.Sockets;
using Windows.Phone.Speech.Synthesis;
using Windows.Storage.Streams;
namespace EightRacer.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
private readonly IDataService _dataService;
private PairedDeviceInfo pdi;
private DataWriter dw;
private StreamSocket socket;
private bool isConnected;
private Accelerometer accelerometer;
private DispatcherTimer timer;
private Vector3 acceleration;
private bool isDataValid;
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel(IDataService dataService)
{
ButtonConnect = new RelayCommand(DoButtonConnect);
ButtonDisconnect = new RelayCommand(DoButtonDisconnect);
PairedDeviceSelected = new RelayCommand<SelectionChangedEventArgs>(DoPairedDeviceSelected);
CheckBlueToothOn();
_pdis = new ObservableCollection<PairedDeviceInfo>();
PopulatePairedDevicesListBox();
InitializeSensors(); // wire up accelerometer
}
#region properties
public RelayCommand ButtonConnect { get; private set; }
public RelayCommand ButtonDisconnect { get; private set; }
public ICommand PairedDeviceSelected { get; private set; }
/// <summary>
/// The <see cref="DebugMessages" /> property's name.
/// </summary>
public const string DebugMessagesPropertyName = "DebugMessages";
private string _debugMessages = "";
/// <summary>
/// Sets and gets the DebugMessages property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string DebugMessages
{
get { return _debugMessages; }
set
{
if (_debugMessages == value)
{
return;
}
RaisePropertyChanging(DebugMessagesPropertyName);
_debugMessages = value;
RaisePropertyChanged(DebugMessagesPropertyName);
}
}
/// <summary>
/// The <see cref="Pdis" /> property's name.
/// Paired Device Info
/// </summary>
public const string PdiPropertyName = "Pdis";
private ObservableCollection<PairedDeviceInfo> _pdis;
/// <summary>
/// Sets and gets the Pdis property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<PairedDeviceInfo> Pdis
{
get { return _pdis; }
set
{
if (_pdis == value)
{
return;
}
RaisePropertyChanging(PdiPropertyName);
_pdis = value;
RaisePropertyChanged(PdiPropertyName);
}
}
#endregion
#region Methods
private IBuffer GetBufferFromByte(byte package)
{
using (DataWriter dw = new DataWriter())
{
dw.WriteByte(package);
return dw.DetachBuffer();
}
}
//private byte IntToByte(uint a, uint b)
//{
// uint x = a << 4;
// uint y = x | b;
// return (byte)y;
//}
private byte Shifty(int a, int b)
{
int x = a << 4;
return (byte)(x | b);
}
private async void DoButtonConnect()
{
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
await synth.SpeakTextAsync("Connecting to Selected");
}
try
{
socket = new StreamSocket();
var peer = pdi.PeerInfo;
//string serviceName = "MiRacer";
await socket.ConnectAsync(peer.HostName, "1");
isConnected = true;
DebugMessages += String.Format("AppResources.Msg_ConnectedTo {0}",
socket.Information.RemoteAddress.DisplayName);
}
catch (Exception e)
{
if (pdi == null)
{
MessageBox.Show("Please select Dagu car first"); // user may not have selected the paired Dagu car from list.
}
else
MessageBox.Show("Sorry, failed to connect");
Debug.WriteLine(e.ToString());
isConnected = false;
}
}
private async void DoButtonDisconnect()
{
try
{
var buff = GetBufferFromByte(Shifty(0, 0));
await socket.OutputStream.WriteAsync(buff);
socket.Dispose();
isConnected = false;
}
catch (Exception e)
{
MessageBox.Show("Error disposing");
}
}
private IBuffer GetBufferFromByteArray(byte[] package)
{
using (DataWriter dw = new DataWriter())
{
dw.WriteBytes(package);
return dw.DetachBuffer();
}
}
private async void PopulatePairedDevicesListBox()
{
// Configure PeerFinder to search for all paired devices.
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
Debug.WriteLine("No paired devices were found.");
}
else
{
foreach (var d in pairedDevices)
{
Pdis.Add(new PairedDeviceInfo(d));
}
}
}
private int x, y, dir, speed;
private int yLimit = 2;
private async void timer_Tick(object sender, EventArgs e)
{
//DebugMessages = string.Format("X: {0} Y: {1} Z: {2}",
// acceleration.X.ToString("0.00"),
// acceleration.Y.ToString("0.00"),
// acceleration.Z.ToString("0.00")
// );
DebugMessages = string.Format(
// "X: {0} Y: {1} ", acceleration.X.ToString("0.00"), acceleration.Y.ToString("0.00"));
"X: {0} Y: {1} ", acceleration.Y.ToString("0.00"), acceleration.X.ToString("0.00")); // x and y are reversed between phone and input to i-Racer
if (isConnected)
{
if (isDataValid)
{
x = (int)ofMap(acceleration.Y, -1.00f, 1.00f, -0xF, 0xF); // X = forward / backward *** Y = left/right on racer
// Although I get up to 1 (-1) from acceleration I want to make the control less drastic in the tilt action of the phone.
// 3x = (int)ofMap(acceleration.Y, -0.75f, 0.75f, -0xF, 0xF); // X = forward / backward *** Y = left/right on racer
y = (int)ofMap(acceleration.X, -1.00f, 1.00f, -10, 10);
if (x == 0 && y == 0)
{
Debug.WriteLine("Stop - x: {0} \ty: {1}", x, y);
dir = 0;
speed = 0;
goto TheEnd;
}
// straight
if (x > 0 & (y < yLimit & y > -yLimit))
{
dir = 1;
speed = Math.Abs(x);
goto TheEnd;
}
// backward
if (x < 0 & (y < yLimit & y > -yLimit))
{
dir = 2;
speed = Math.Abs(x);
goto TheEnd;
}
if (x < 0 && y < yLimit)
{
Debug.WriteLine("Left Backward \t\tx: {0} \ty: {1}", x, y);
dir = 7;
speed = Math.Abs(x);
goto TheEnd;
}
if (x < 0 && y > yLimit)
{
Debug.WriteLine("Right Backward - x < y > \tx: {0} \ty: {1}", x, y);
dir = 8;
speed = Math.Abs(x);
goto TheEnd;
}
if (x > 0 && y > yLimit)
{
Debug.WriteLine("Right Forward - x > y > \tx: {0} \ty: {1}", x, y);
dir = 6;
speed = Math.Abs(x);
goto TheEnd;
}
if (x > 0 && y < yLimit)
{
Debug.WriteLine("left forward - x > y < \tx: {0} \ty: {1}", x, y);
dir = 5;
speed = Math.Abs(x);
}
TheEnd:
var buff = GetBufferFromByte(Shifty(dir, speed));
await socket.OutputStream.WriteAsync(buff);
}
}
}
private async void DoPairedDeviceSelected(SelectionChangedEventArgs args)
{
pdi = args.AddedItems[0] as PairedDeviceInfo;
DebugMessages = pdi.DisplayName;
DebugMessages += "\n";
DebugMessages += pdi.HostName;
DebugMessages += "\n";
DebugMessages += pdi.ServiceName;
DebugMessages += "\n";
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
await synth.SpeakTextAsync(pdi.DisplayName);
}
}
private async void CheckBlueToothOn()
{
// Search for all paired devices
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
try
{
var peers = await PeerFinder.FindAllPeersAsync();
}
catch (Exception ex)
{
if ((uint)ex.HResult == 0x8007048F)
{
MessageBox.Show("Bluetooth is turned off");
}
}
}
// From modified from .cpp openframeworks.cc
float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax)
{
float outVal = ((value - inputMin) / (inputMax - inputMin) * (outputMax - outputMin) + outputMin);
return outVal;
}
private void InitializeSensors()
{
if (!Accelerometer.IsSupported)
{
// The device on which the application is running does not support
// the accelerometer sensor. Alert the user and hide the
// application bar.
DebugMessages = "device does not support Accelerometer";
// ApplicationBar.IsVisible = false;
}
else
{
// Initialize the timer and add Tick event handler, but don't start it yet.
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(30);
timer.Tick += new EventHandler(timer_Tick);
// Instantiate the accelerometer.
accelerometer = new Accelerometer();
// Specify the desired time between updates. The sensor accepts
// intervals in multiples of 20 ms.
accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
// The sensor may not support the requested time between updates.
// The TimeBetweenUpdates property reflects the actual rate.
// timeBetweenUpdatesTextBlock.Text = accelerometer.TimeBetweenUpdates.TotalMilliseconds + " ms";
accelerometer.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
}
try
{
DebugMessages = "starting accelerometer.";
accelerometer.Start();
timer.Start();
}
catch (InvalidOperationException)
{
DebugMessages = "unable to start accelerometer.";
}
}
void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
{
// Note that this event handler is called from a background thread
// and therefore does not have access to the UI thread. To update
// the UI from this handler, use Dispatcher.BeginInvoke() as shown.
// Dispatcher.BeginInvoke(() => { statusTextBlock.Text = "in CurrentValueChanged"; });
isDataValid = accelerometer.IsDataValid;
acceleration = e.SensorReading.Acceleration;
}
#endregion
}
}