internal WpfAccelerometer3D(Windows7.Sensors.Sensors.Motion.Accelerometer3D sensor) : base(sensor) { _accelerometer = sensor as Accelerometer3D; if (_accelerometer == null) { throw new ArgumentException("sensor must be of Accelerometer3D type"); } }
/// <summary> /// Called when an accelerometer sensor is removed. /// </summary> /// <param name="sensorID"></param> public void OnLeave(Sensor sensor, Guid sensorID) { if (sensor.SensorID == _sensor.SensorID && sensor.SensorID != Guid.Empty) { _sensor = null; // attempt to find another sensor, if possible Initialize(); } }
/// <summary> /// Set the report interval of the sensor to desired values. /// </summary> /// <param name="sensor">The sensor on which to set the values.</param> private void SetupSensorProperties(Sensor sensor) { Accelerometer3D a3dSensor = (Accelerometer3D)sensor; a3dSensor.ReportInterval = DesiredReportInterval; if (a3dSensor.ReportInterval != DesiredReportInterval) { Debug.WriteLine("Error setting report interval. The desired value of " + DesiredReportInterval + " could not be set. The current report interval is: " + a3dSensor.ReportInterval); } }
void DataReport_Changed(Sensor sender, EventArgs e) { // The data report update comes in on a non-UI thread. // Whip up an anonymous delegate to handle the UI update. BeginInvoke(new MethodInvoker(delegate { Accelerometer3D accel = sender as Accelerometer3D; accelX.Acceleration = accel.CurrentAcceleration[AccelerationAxis.XAxis]; accelY.Acceleration = accel.CurrentAcceleration[AccelerationAxis.YAxis]; accelZ.Acceleration = accel.CurrentAcceleration[AccelerationAxis.ZAxis]; })); }
private void HookUpAccelerometer() { try { SensorList <Accelerometer3D> sl = SensorManager.GetSensorsByTypeId <Accelerometer3D>( ); if (sl.Count > 0) { Accelerometer3D accel = sl[0]; accel.AutoUpdateDataReport = true; accel.DataReportChanged += new DataReportChangedEventHandler(DataReport_Changed); } availabilityLabel.Text = "Accelerometers available = " + sl.Count; } catch (SensorPlatformException) { // This exception will also be hit in the Shown message handler. } }
/// <summary> /// Called when any new sensor is attached to the system. /// </summary> public void OnSensorEnter(Sensor pSensor, SensorState state) { // The sensor paramater should not be null System.Diagnostics.Debug.Assert(pSensor != null); // If we are currently using a sensor we don't want to stop using it and // use a new one so we only use a new sensor if we don't have one already. if (_sensor == null) { Guid sensorType = Guid.Empty; try { sensorType = pSensor.TypeID; } catch (COMException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } if (sensorType == SensorType) { _sensor = (Accelerometer3D)pSensor; try { pSensor.DataUpdated += new SensorDataUpdatedEventHandler(OnDataUpdated); pSensor.EventReceived += new SensorEventHandler(OnEvent); pSensor.SensorLeave += new SensorLeaveEventHandler(OnLeave); pSensor.StateChanged += new SensorStateChangedEventHandler(OnStateChanged); } catch (COMException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } state = pSensor.State; if (state == SensorState.Ready) { SetupSensorProperties(pSensor); SensorDataReport data = null; try { data = pSensor.GetDataReport(); } catch (COMException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } if (data != null) { OnDataUpdated(pSensor, data); } } else if (state == SensorState.AccessDenied) { if (RequestSensorAccess(pSensor)) { SetupSensorProperties(pSensor); } } } } }