void MpuUpdated(object sender, AccelerationConditionChangeResult e) { up.IsOn = (0.20 < e.New.YAcceleration && e.New.YAcceleration < 0.80); down.IsOn = (-0.80 < e.New.YAcceleration && e.New.YAcceleration < -0.20); left.IsOn = (0.20 < e.New.XAcceleration && e.New.XAcceleration < 0.80); right.IsOn = (-0.80 < e.New.XAcceleration && e.New.XAcceleration < -0.20); }
///// <summary> ///// Starts continuously sampling the sensor. ///// ///// This method also starts raising `Changed` events and IObservable ///// subscribers getting notified. ///// </summary> public void StartUpdating(int standbyDuration = 1000) { // thread safety lock (_lock) { if (IsSampling) { return; } // state muh-cheen IsSampling = true; //SamplingTokenSource = new CancellationTokenSource(); //CancellationToken ct = SamplingTokenSource.Token; AccelerationConditions oldConditions; AccelerationConditionChangeResult result; var task1 = new Thread(new ThreadStart(() => { while (true) { if (!IsSampling) { // do task clean up here //_observers.ForEach(x => x.OnCompleted()); break; } // capture history oldConditions = AccelerationConditions.From(Conditions); // read Update(); // build a new result with the old and new conditions result = new AccelerationConditionChangeResult(oldConditions, Conditions); // let everyone know RaiseChangedAndNotify(result); // sleep for the appropriate interval Thread.Sleep(standbyDuration); } })); task1.Start(); } }
protected void RaiseChangedAndNotify(AccelerationConditionChangeResult changeResult) { Updated?.Invoke(this, changeResult); //base.NotifyObservers(changeResult); }