/// <summary> /// Stop Measurement ans Sequencer. /// </summary> public void Stop() { running = false; ConLogger.Log("Controller Stoped", LogLevel.DEBUG); SequencesTimer.Stop(); if (MeasurementTimer != null) { try { MeasurementTimer.Dispose(); lock (MeasurementCSVLogger) { MeasurementCSVLogger.Stop(); } //reset time to 0 so that there are no problems during the measurement // and the first value will be measured right away Configuration.AnalogPins.ForEach(o => o.LastValue = 0); // Configuration.AnalogPins.ForEach (o => o.Values = new List<DateTimeValue> ()); } catch (Exception) { } } KeeperOfTime.Stop(); if (OnControllerStoped != null) { OnControllerStoped.Invoke(this, null); } }
/// <summary> /// Raised by the <see cref="MeasurementTimer"/>. Collects data from board. /// </summary> private void OnMeasurementTimerTickWindows(object state) { try { if (running) { double time = KeeperOfTime.ElapsedMilliseconds; var analogPins = Configuration.AnalogPins.Where(o => o.LastValue + o.Interval < time).ToArray(); if (analogPins.Length > 0) { analogPins.ToList().ForEach(o => o.LastValue += o.Interval); var query = analogPins.Select(o => o.Number).ToArray(); var vals = ArduinoController.ReadAnalogPin(query); var now = DateTime.Now; for (int i = 0; i < analogPins.Length; i++) { lock (analogPins) { analogPins [i].Value = new DateTimeValue(vals [i], now); } } var analogPinValues = analogPins.Select(o => o.Value.Value).ToList <double> (); var analogPinValuesNames = analogPins.ToList().Select(o => o.DisplayName).ToList(); var MeComValues = Configuration.MeasurementCombinations .Where(o => !double.IsNaN(o.Value.Value)) .Select(o => o.Value.Value) .ToList <double> (); var MeComValuesNames = Configuration.MeasurementCombinations .Where(o => !double.IsNaN(o.Value.Value)) .Select(o => o.DisplayName) .ToList(); var names = analogPinValuesNames; names.AddRange(MeComValuesNames); var values = analogPinValues; values.AddRange(MeComValues); MeasurementCSVLogger.Log(names, values); } } else { System.Threading.Timer t = (System.Threading.Timer)state; t.Dispose(); } } catch (Exception e) { ConLogger.Log(e.ToString(), LogLevel.ERROR); } }
/// <summary> /// Stops controller and referenced timers. Writes the preferences. /// </summary> public void Quit() { running = false; ConLogger.Stop(); if (SequencesTimer != null) { SequencesTimer.Stop(); } KeeperOfTime.Stop(); WritePreferences(); }
/// <summary> /// Start this controller and measurements. /// </summary> public void Start() { //Save the port, so that next time the connection may be automaticly established Backend.Properties.Settings.Default.LastConnectedPort = ArduinoController.SerialPortName; Backend.Properties.Settings.Default.Save(); KeeperOfTime.Restart(); running = true; ArduinoController.SetPinModes(Configuration.AnalogPins.Select(o => o.RealNumber).ToArray <uint> (), Configuration.DigitalPins.Select(o => o.RealNumber).ToArray <uint> ()); StartTime = DateTime.Now; LastCondition = new ushort[] { 0, 0, 0, 0 }; SequencesTimer = new System.Timers.Timer(10); SequencesTimer.Elapsed += OnSequenceTimeElapsed; MeasurementPreProcessing(); if (System.Environment.OSVersion.Platform == PlatformID.Unix) { MeasurementTimer = new System.Threading.Timer(new TimerCallback(OnMeasurementTimerTick), null, 0, 10); } else { //because windows sux MeasurementTimer = new System.Threading.Timer(new TimerCallback(OnMeasurementTimerTickWindows), null, 0, 1); } SequencesTimer.Start(); ConLogger.Log("Controller Started", LogLevel.DEBUG); ConLogger.Log("Start took: " + KeeperOfTime.ElapsedMilliseconds + "ms", LogLevel.DEBUG); if (OnControllerStarted != null) { OnControllerStarted.Invoke(this, null); } }