// -->> Writes the new data to a file <<-- //________________________________________ private async void OnNewDataEnqueuedFxn(object sender, EventArgs e) { int counter = 0; while (busy || counter < 200)//Waits for another writing operation to complete { counter++; } if (!busy) { //Writes to file busy = true; //Sets busy flag thetaClass dequeued = null; while (dataQueue.Count > 0) { if (dataQueue.TryDequeue(out dequeued))//Dqueues new data { await FileIO.AppendTextAsync(rootPage.dataFile, dequeued.StringTheta); } } busy = false;//Releases flag } else {//Failed to write to file rootPage.notifyFlyout("Reached max number of iterations", connectButton); } }
public void AddPoints(thetaClass newPoints) { xPlot.Points.Add(new Point(axisX, zeroY - newPoints.ThetaX * scale)); //Acc X point yPlot.Points.Add(new Point(axisX, zeroY - newPoints.ThetaY * scale)); //Acc Y point zPlot.Points.Add(new Point(axisX, zeroY - newPoints.ThetaZ * scale)); //Acc Z point //position.Points.Add(new Point(axisX, zeroY - newPoints.bodyPos * 100));//Acc Z point graphCanvas.UpdateLayout(); axisX += tickX; if (axisX > maxTickX) { axisX = 0; Clear(); } }
//New data is available at the sensor. private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { // BT_Code: An Indicate or Notify reported that the value has changed. byte[] newValue;//Stores the byte arraw //Reads the BLE buffer and stores its content into the newValue array CryptographicBuffer.CopyToByteArray(args.CharacteristicValue, out newValue); // Breathing monitoring notification if (sender.Uuid.Equals(BrService.BREATHING_UUID)) { // Notification from the IMU double[] newData = new double[3]; //Stores the IMU values //Converting bytes into Int8, and then to degrees // The angles are quantized as a signed 8 bit number that // represents -180 to 180 degrees sbyte tmp0; // Getting position position = unchecked ((byte)newValue[18]); int counter = 0; for (int i = 0; i < 18; i++) {//Getting angles tmp0 = unchecked ((sbyte)newValue[i]); newData[counter] = (tmp0 * 360) / 256; counter++; if (counter >= 3) { thetaClass newTetha = new thetaClass(); newTetha.ThetaData = newData; dataQueue.Enqueue(newTetha); plotQueue.Enqueue(newTetha); counter = 0; } } // Triggers queue event if (OnNewDataEnqueued != null) { OnNewDataEnqueued(this, EventArgs.Empty); } //Fires graph refresh event if (OnNewPointsAdded != null) { OnNewPointsAdded(this, EventArgs.Empty); } } //Check for other services sending data... }
// -->> Refreshes UI and Graph <<-- //_________________________________ private async void OnNewPointsAddedFxn(object sender, EventArgs e) {// Updates the UI and the graph await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { counter += 6; counterTextBlock.Text = counter.ToString(); thetaClass dequeued = null; while (dataQueue.Count > 0) { if (dataQueue.TryDequeue(out dequeued)) //Dqueues new data { dataTextBlock.Text = dequeued.StringTheta; graph.AddPoints(dequeued.ThetaData); } } timerTextBlock.Text = Convert.ToString(stopwatch.Elapsed); }); }