forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenario1_ReadData.xaml.cs
More file actions
161 lines (137 loc) · 6.55 KB
/
Scenario1_ReadData.xaml.cs
File metadata and controls
161 lines (137 loc) · 6.55 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
//*********************************************************
//
// 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 System.Collections.Generic;
using System.Threading.Tasks;
using Windows.Devices.Spi;
using Windows.Devices.Enumeration;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace SDKTemplate
{
public sealed partial class Scenario1_ReadData : Page
{
private SpiDevice adxl345Sensor;
private DispatcherTimer timer;
public Scenario1_ReadData()
{
this.InitializeComponent();
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
StopScenario();
}
private async Task StartScenarioAsync()
{
String spiDeviceSelector = SpiDevice.GetDeviceSelector();
IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);
// 0 = Chip select line to use.
var ADXL345_Settings = new SpiConnectionSettings(0);
// 5MHz is the rated speed of the ADXL345 accelerometer.
ADXL345_Settings.ClockFrequency = 5000000;
// The accelerometer expects an idle-high clock polarity.
// We use Mode3 to set the clock polarity and phase to: CPOL = 1, CPHA = 1.
ADXL345_Settings.Mode = SpiMode.Mode3;
// If this next line crashes with an ArgumentOutOfRangeException,
// then the problem is that no SPI devices were found.
//
// If the next line crashes with Access Denied, then the problem is
// that access to the SPI device (ADXL345) is denied.
//
// The call to FromIdAsync will also crash if the settings are invalid.
//
// FromIdAsync produces null if there is a sharing violation on the device.
// This will result in a NullReferenceException a few lines later.
adxl345Sensor = await SpiDevice.FromIdAsync(devices[0].Id, ADXL345_Settings);
//
// Initialize the accelerometer:
//
// For this device, we create 2-byte write buffers:
// The first byte is the register address we want to write to.
// The second byte is the contents that we want to write to the register.
//
// 0x31 is address of data format register, 0x01 sets range to +- 4Gs.
byte[] WriteBuf_DataFormat = new byte[] { 0x31, 0x01 };
// 0x2D is address of power control register, 0x08 puts the accelerometer into measurement mode.
byte[] WriteBuf_PowerControl = new byte[] { 0x2D, 0x08 };
// Write the register settings.
//
// If this next line crashes with a NullReferenceException, then
// there was a sharing violation on the device.
// (See comment earlier in this function.)
//
// If this next line crashes for some other reason, then there was
// an error communicating with the device.
adxl345Sensor.Write(WriteBuf_DataFormat);
adxl345Sensor.Write(WriteBuf_PowerControl);
// Start the polling timer.
timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
timer.Tick += Timer_Tick;
timer.Start();
}
void StopScenario()
{
if (timer != null)
{
timer.Tick -= Timer_Tick;
timer.Stop();
timer = null;
}
// Release the SPI sensor.
if (adxl345Sensor != null)
{
adxl345Sensor.Dispose();
adxl345Sensor = null;
}
}
async void StartStopScenario_Click(object sender, RoutedEventArgs e)
{
if (timer != null)
{
StopScenario();
StartStopButton.Content = "Start";
ScenarioControls.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
else
{
StartStopButton.IsEnabled = false;
await StartScenarioAsync();
StartStopButton.IsEnabled = true;
StartStopButton.Content = "Stop";
ScenarioControls.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
void Timer_Tick(object sender, object e)
{
const byte ACCEL_SPI_RW_BIT = 0x80; // Bit used in SPI transactions to indicate read/write
const byte ACCEL_SPI_MB_BIT = 0x40; // Bit used to indicate multi-byte SPI transactions
const byte ACCEL_REG_X = 0x32; // Address of the X Axis data register
const int ACCEL_RES = 1024; // The ADXL345 has 10 bit resolution giving 1024 unique values
const int ACCEL_DYN_RANGE_G = 8; // The ADXL345 had a total dynamic range of 8G, since we're configuring it to +-4G
const int UNITS_PER_G = ACCEL_RES / ACCEL_DYN_RANGE_G; // Ratio of raw int values to G units
byte[] ReadBuf = new byte[6 + 1]; // Read buffer of size 6 bytes (2 bytes * 3 axes) + 1 byte padding
byte[] RegAddrBuf = new byte[1 + 6]; // Register address buffer of size 1 byte + 6 bytes padding
// Register address we want to read from with read and multi-byte bit set
RegAddrBuf[0] = ACCEL_REG_X | ACCEL_SPI_RW_BIT | ACCEL_SPI_MB_BIT;
// If this next line crashes, then there was an error communicating with the device.
adxl345Sensor.TransferFullDuplex(RegAddrBuf, ReadBuf);
// In order to get the raw 16-bit data values, we need to concatenate two 8-bit bytes for each axis
short AccelerationRawX = BitConverter.ToInt16(ReadBuf, 1);
short AccelerationRawY = BitConverter.ToInt16(ReadBuf, 3);
short AccelerationRawZ = BitConverter.ToInt16(ReadBuf, 5);
// Convert raw values to G's and display them.
X.Text = ((double)AccelerationRawX / UNITS_PER_G).ToString();
Y.Text = ((double)AccelerationRawY / UNITS_PER_G).ToString();
Z.Text = ((double)AccelerationRawZ / UNITS_PER_G).ToString();
}
}
}