forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scenario2_Polling.xaml.cpp
181 lines (160 loc) · 6.21 KB
/
Scenario2_Polling.xaml.cpp
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
//*********************************************************
//
// 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.
//
//*********************************************************
//
// Scenario2_Polling.xaml.cpp
// Implementation of the Scenario2_Polling class
//
#include "pch.h"
#include "Scenario2_Polling.xaml.h"
using namespace SDKTemplate;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Devices::Sensors;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Platform;
Scenario2_Polling::Scenario2_Polling() : rootPage(MainPage::Current), desiredReportInterval(0)
{
InitializeComponent();
compass = Compass::GetDefault();
if (compass != nullptr)
{
// 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.
uint32 minReportInterval = compass->MinimumReportInterval;
desiredReportInterval = minReportInterval > 16 ? minReportInterval : 16;
// Set up a DispatchTimer
TimeSpan span;
span.Duration = static_cast<int32>(desiredReportInterval) * 10000; // convert to 100ns ticks
dispatcherTimer = ref new DispatcherTimer();
dispatcherTimer->Interval = span;
dispatcherTimer->Tick += ref new Windows::Foundation::EventHandler<Object^>(this, &Scenario2_Polling::DisplayCurrentReading);
}
else
{
rootPage->NotifyUser("No compass 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>
void Scenario2_Polling::OnNavigatedTo(NavigationEventArgs^ e)
{
ScenarioEnableButton->IsEnabled = true;
ScenarioDisableButton->IsEnabled = false;
}
/// <summary>
/// Invoked when this page is no longer displayed.
/// </summary>
/// <param name="e"></param>
void Scenario2_Polling::OnNavigatedFrom(NavigationEventArgs^ e)
{
// If the navigation is external to the app do not clean up.
// This can occur on Phone when suspending the app.
if (e->NavigationMode == NavigationMode::Forward && e->Uri == nullptr)
{
return;
}
if (ScenarioDisableButton->IsEnabled)
{
Window::Current->VisibilityChanged::remove(visibilityToken);
dispatcherTimer->Stop();
// Restore the default report interval to release resources while the sensor is not in use
compass->ReportInterval = 0;
}
}
/// <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>
void Scenario2_Polling::VisibilityChanged(Object^ sender, VisibilityChangedEventArgs^ e)
{
// The app should watch for VisibilityChanged events to disable and re-enable sensor input as appropriate
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();
}
}
}
void Scenario2_Polling::DisplayCurrentReading(Object^ sender, Object^ e)
{
CompassReading^ reading = compass->GetCurrentReading();
if (reading != nullptr)
{
ScenarioOutput_MagneticNorth->Text = reading->HeadingMagneticNorth.ToString();
if (reading->HeadingTrueNorth != nullptr)
{
ScenarioOutput_TrueNorth->Text = reading->HeadingTrueNorth->Value.ToString();
}
else
{
ScenarioOutput_TrueNorth->Text = "No data";
}
switch (reading->HeadingAccuracy)
{
case MagnetometerAccuracy::Unknown:
ScenarioOutput_HeadingAccuracy->Text = "Unknown";
break;
case MagnetometerAccuracy::Unreliable:
ScenarioOutput_HeadingAccuracy->Text = "Unreliable";
break;
case MagnetometerAccuracy::Approximate:
ScenarioOutput_HeadingAccuracy->Text = "Approximate";
break;
case MagnetometerAccuracy::High:
ScenarioOutput_HeadingAccuracy->Text = "High";
break;
default:
ScenarioOutput_HeadingAccuracy->Text = "No data";
break;
}
}
}
void Scenario2_Polling::ScenarioEnable(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (compass != nullptr)
{
visibilityToken = Window::Current->VisibilityChanged::add(ref new WindowVisibilityChangedEventHandler(this, &Scenario2_Polling::VisibilityChanged));
// Set the report interval to enable the sensor for polling
compass->ReportInterval = desiredReportInterval;
dispatcherTimer->Start();
ScenarioEnableButton->IsEnabled = false;
ScenarioDisableButton->IsEnabled = true;
}
else
{
rootPage->NotifyUser("No compass found", NotifyType::ErrorMessage);
}
}
void Scenario2_Polling::ScenarioDisable(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Window::Current->VisibilityChanged::remove(visibilityToken);
dispatcherTimer->Stop();
// Restore the default report interval to release resources while the sensor is not in use
compass->ReportInterval = 0;
ScenarioEnableButton->IsEnabled = true;
ScenarioDisableButton->IsEnabled = false;
}