This repository has been archived by the owner on May 1, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from xamarin/feature/issue-348
GH-348: Add Barometer Sensor API
- Loading branch information
Showing
30 changed files
with
1,007 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Threading.Tasks; | ||
using Xamarin.Essentials; | ||
using Xunit; | ||
|
||
namespace DeviceTests | ||
{ | ||
public class Barometer_Tests | ||
{ | ||
[Fact] | ||
public void IsSupported() | ||
=> Assert.Equal(HardwareSupport.HasBarometer, Barometer.IsSupported); | ||
|
||
[Fact] | ||
[Trait(Traits.Hardware.Barometer, Traits.FeatureSupport.Supported)] | ||
public async Task Monitor() | ||
{ | ||
// TODO: the test runner app (UI version) should do this, until then... | ||
if (!HardwareSupport.HasBarometer) | ||
return; | ||
|
||
var tcs = new TaskCompletionSource<BarometerData>(); | ||
|
||
Barometer.ReadingChanged += Barometer_ReadingChanged; | ||
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e) | ||
{ | ||
tcs.TrySetResult(e.Reading); | ||
} | ||
Barometer.Start(SensorSpeed.UI); | ||
|
||
var d = await tcs.Task; | ||
|
||
Assert.True(d.Pressure >= 0); | ||
Barometer.Stop(); | ||
Barometer.ReadingChanged -= Barometer_ReadingChanged; | ||
} | ||
|
||
[Fact] | ||
[Trait(Traits.Hardware.Barometer, Traits.FeatureSupport.Supported)] | ||
public async Task IsMonitoring() | ||
{ | ||
// TODO: the test runner app (UI version) should do this, until then... | ||
if (!HardwareSupport.HasBarometer) | ||
return; | ||
|
||
var tcs = new TaskCompletionSource<BarometerData>(); | ||
Barometer.ReadingChanged += Barometer_ReadingChanged; | ||
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e) | ||
{ | ||
tcs.TrySetResult(e.Reading); | ||
} | ||
Barometer.Start(SensorSpeed.UI); | ||
|
||
var d = await tcs.Task; | ||
Assert.True(Barometer.IsMonitoring); | ||
|
||
Barometer.Stop(); | ||
Barometer.ReadingChanged -= Barometer_ReadingChanged; | ||
} | ||
|
||
[Fact] | ||
[Trait(Traits.Hardware.Barometer, Traits.FeatureSupport.Supported)] | ||
public async Task Stop_Monitor() | ||
{ | ||
// TODO: the test runner app (UI version) should do this, until then... | ||
if (!HardwareSupport.HasBarometer) | ||
return; | ||
|
||
var tcs = new TaskCompletionSource<BarometerData>(); | ||
Barometer.ReadingChanged += Barometer_ReadingChanged; | ||
void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e) | ||
{ | ||
tcs.TrySetResult(e.Reading); | ||
} | ||
Barometer.Start(SensorSpeed.UI); | ||
|
||
var d = await tcs.Task; | ||
|
||
Barometer.Stop(); | ||
Barometer.ReadingChanged -= Barometer_ReadingChanged; | ||
|
||
Assert.False(Barometer.IsMonitoring); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<views:BasePage xmlns="http://xamarin.com/schemas/2014/forms" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
xmlns:views="clr-namespace:Samples.View" | ||
xmlns:viewmodels="clr-namespace:Samples.ViewModel" | ||
x:Class="Samples.View.BarometerPage" | ||
Title="Barometer"> | ||
<views:BasePage.BindingContext> | ||
<viewmodels:BarometerViewModel /> | ||
</views:BasePage.BindingContext> | ||
|
||
<StackLayout> | ||
<Label Text="Monitor barometer for changes." FontAttributes="Bold" Margin="12" /> | ||
|
||
<ScrollView> | ||
<Grid Padding="12,0,12,12"> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition Width="*" /> | ||
<ColumnDefinition Width="*" /> | ||
</Grid.ColumnDefinitions> | ||
|
||
<Label Grid.Row="0" Grid.ColumnSpan="2" | ||
Text="{Binding Pressure, StringFormat='Barometer pressure (hPA): {0:N}'}" /> | ||
|
||
<Label Grid.Row="1" Grid.ColumnSpan="2" Text="Speed:" /> | ||
|
||
<Picker Grid.Row="2" Grid.ColumnSpan="2" HorizontalOptions="FillAndExpand" | ||
ItemsSource="{Binding Speeds}" | ||
SelectedIndex="{Binding Speed, Mode=TwoWay}" | ||
IsEnabled="{Binding IsActive, Converter={StaticResource NegativeConverter}}" | ||
Margin="0,0,0,10"/> | ||
|
||
|
||
<Button Grid.Row="3" Grid.Column="0" Text="Start" Command="{Binding StartCommand}" | ||
IsEnabled="{Binding IsActive, Converter={StaticResource NegativeConverter}}" /> | ||
|
||
<Button Grid.Row="3" Grid.Column="1" Text="Stop" Command="{Binding StopCommand}" | ||
IsEnabled="{Binding IsActive}" /> | ||
</Grid> | ||
</ScrollView> | ||
</StackLayout> | ||
|
||
</views:BasePage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Xamarin.Forms; | ||
using Xamarin.Forms.Xaml; | ||
|
||
namespace Samples.View | ||
{ | ||
public partial class BarometerPage : BasePage | ||
{ | ||
public BarometerPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Windows.Input; | ||
using Xamarin.Essentials; | ||
using Xamarin.Forms; | ||
|
||
namespace Samples.ViewModel | ||
{ | ||
public class BarometerViewModel : BaseViewModel | ||
{ | ||
bool isActive; | ||
double pressure; | ||
int speed = 2; | ||
|
||
public BarometerViewModel() | ||
{ | ||
StartCommand = new Command(OnStartBarometer); | ||
StopCommand = new Command(OnStop); | ||
} | ||
|
||
public ICommand StartCommand { get; } | ||
|
||
public ICommand StopCommand { get; } | ||
|
||
public bool IsActive | ||
{ | ||
get => isActive; | ||
set => SetProperty(ref isActive, value); | ||
} | ||
|
||
public double Pressure | ||
{ | ||
get => pressure; | ||
set => SetProperty(ref pressure, value); | ||
} | ||
|
||
public List<string> Speeds { get; } = | ||
new List<string> | ||
{ | ||
"Fastest", | ||
"Game", | ||
"Normal", | ||
"User Interface" | ||
}; | ||
|
||
public int Speed | ||
{ | ||
get => speed; | ||
set => SetProperty(ref speed, value); | ||
} | ||
|
||
public override void OnAppearing() | ||
{ | ||
Barometer.ReadingChanged += OnBarometerReadingChanged; | ||
base.OnAppearing(); | ||
} | ||
|
||
public override void OnDisappearing() | ||
{ | ||
OnStop(); | ||
Barometer.ReadingChanged -= OnBarometerReadingChanged; | ||
|
||
base.OnDisappearing(); | ||
} | ||
|
||
async void OnStartBarometer() | ||
{ | ||
try | ||
{ | ||
Barometer.Start((SensorSpeed)Speed); | ||
IsActive = true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
await DisplayAlertAsync($"Unable to start barometer: {ex.Message}"); | ||
} | ||
} | ||
|
||
void OnBarometerReadingChanged(object sender, BarometerChangedEventArgs e) | ||
{ | ||
Pressure = e.Reading.Pressure; | ||
} | ||
|
||
void OnStop() | ||
{ | ||
IsActive = false; | ||
Barometer.Stop(); | ||
Barometer.ReadingChanged -= OnBarometerReadingChanged; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.