forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenario2_Polling.xaml.cs
173 lines (153 loc) · 6.61 KB
/
Scenario2_Polling.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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using Windows.Devices.Sensors;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace SDKTemplate
{
public sealed partial class Scenario2_Polling : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
private Magnetometer _magnetometer;
private uint _desiredReportInterval;
private DispatcherTimer _dispatcherTimer;
public Scenario2_Polling()
{
this.InitializeComponent();
_magnetometer = Magnetometer.GetDefault();
if (_magnetometer != null)
{
// Select a report interval that is both suitable for the purposes of the app and supported by the sensor.
// This value will be used later to activate the sensor.
uint minReportInterval = _magnetometer.MinimumReportInterval;
_desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
// Set up a DispatchTimer
_dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += DisplayCurrentReading;
_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, (int)_desiredReportInterval);
}
else
{
rootPage.NotifyUser("No magnetometer found", NotifyType.ErrorMessage);
}
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ScenarioEnableButton.IsEnabled = true;
ScenarioDisableButton.IsEnabled = false;
}
/// <summary>
/// Invoked immediately before the Page is unloaded and is no longer the current source of a parent Frame.
/// </summary>
/// <param name="e">
/// Event data that can be examined by overriding code. The event data is representative
/// of the navigation that will unload the current Page unless canceled. The
/// navigation can potentially be canceled by setting Cancel.
/// </param>
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
if (ScenarioDisableButton.IsEnabled)
{
Window.Current.VisibilityChanged -= VisibilityChanged;
// Stop the dispatcher
_dispatcherTimer.Stop();
// Restore the default report interval to release resources while the sensor is not in use
_magnetometer.ReportInterval = 0;
}
base.OnNavigatingFrom(e);
}
/// <summary>
/// This is the event handler for VisibilityChanged events. You would register for these notifications
/// if handling sensor data when the app is not visible could cause unintended actions in the app.
/// </summary>
/// <param name="sender"></param>
/// <param name="e">
/// Event data that can be examined for the current visibility state.
/// </param>
private void VisibilityChanged(object sender, VisibilityChangedEventArgs e)
{
if (ScenarioDisableButton.IsEnabled)
{
if (e.Visible)
{
// Re-enable sensor input (no need to restore the desired reportInterval... it is restored for us upon app resume)
_dispatcherTimer.Start();
}
else
{
// Disable sensor input (no need to restore the default reportInterval... resources will be released upon app suspension)
_dispatcherTimer.Stop();
}
}
}
/// <summary>
/// This is the dispatcher callback.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void DisplayCurrentReading(object sender, object args)
{
MagnetometerReading reading = _magnetometer.GetCurrentReading();
if (reading != null)
{
ScenarioOutput_X.Text = String.Format("{0,5:0.00}", reading.MagneticFieldX);
ScenarioOutput_Y.Text = String.Format("{0,5:0.00}", reading.MagneticFieldY);
ScenarioOutput_Z.Text = String.Format("{0,5:0.00}", reading.MagneticFieldZ);
}
}
/// <summary>
/// This is the click handler for the 'Enable' button.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScenarioEnable(object sender, RoutedEventArgs e)
{
if (_magnetometer != null)
{
// Set the report interval to enable the sensor for polling
_magnetometer.ReportInterval = _desiredReportInterval;
Window.Current.VisibilityChanged += VisibilityChanged;
_dispatcherTimer.Start();
ScenarioEnableButton.IsEnabled = false;
ScenarioDisableButton.IsEnabled = true;
}
else
{
rootPage.NotifyUser("No magnetometer found", NotifyType.ErrorMessage);
}
}
/// <summary>
/// This is the click handler for the 'Disable' button.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ScenarioDisable(object sender, RoutedEventArgs e)
{
Window.Current.VisibilityChanged -= VisibilityChanged;
// Stop the dispatcher
_dispatcherTimer.Stop();
// Restore the default report interval to release resources while the sensor is not in use
_magnetometer.ReportInterval = 0;
ScenarioEnableButton.IsEnabled = true;
ScenarioDisableButton.IsEnabled = false;
}
}
}