/// <summary> /// Raised by the <paramref name="SequencesTimer"/> elapsed event. /// </summary> /// <param name="sender">Sender.</param> /// <param name="args">Arguments.</param> private void OnSequenceTimeElapsed(object sender, System.Timers.ElapsedEventArgs args) { double time = KeeperOfTime.ElapsedMilliseconds; UInt16[] conditions = new UInt16[5]; conditions [0] = 0x0; conditions [1] = 0x0; conditions [2] = 0x0; conditions [3] = 0x0; conditions [4] = 0x0; foreach (Sequence seq in Configuration.Sequences) { if (seq.GetCurrentState(time) == DPinState.HIGH) { int arraypos = (int)seq.Pin.Number / 16; int shift = (int)seq.Pin.Number % 16; int pos = 0x1 << (int)shift; conditions [arraypos] = Convert.ToUInt16(conditions [arraypos] | pos); } } //only send new states if the is actual change if ( LastCondition [0] != conditions [0] || LastCondition [1] != conditions [1] || LastCondition [2] != conditions [2] || LastCondition [3] != conditions [3] || LastCondition [4] != conditions [4]) { ArduinoController.SetDigitalOutputPins(conditions); } LastCondition = conditions; }
/// <summary> /// Connects to last port. /// </summary> public void ConnectToLastPort() { if (!string.IsNullOrEmpty(Backend.Properties.Settings.Default.LastConnectedPort)) { ArduinoController.SerialPortName = Backend.Properties.Settings.Default.LastConnectedPort; ArduinoController.Setup(Configuration.Board.UseDTR); } }
/// <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> /// 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); } }
/// <summary> /// Initializes a new instance of the <see cref="Backend.Controller"/> class. /// </summary> /// <param name="ConfigurationPath">Configuration path.</param> public Controller(string ConfigurationPath = null) { Configuration = new BoardConfiguration(); using (MemoryStream memstream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.Boards))) { using (StreamReader str = new StreamReader(memstream)) { BoardConfigs = ConfigurationManager.ParseBoards(str); } } LastConfigurationLocations [0] = Backend.Properties.Settings.Default.Config1; LastConfigurationLocations [1] = Backend.Properties.Settings.Default.Config2; LastConfigurationLocations [2] = Backend.Properties.Settings.Default.Config3; LastConfigurationLocations [3] = Backend.Properties.Settings.Default.Config4; LastConfigurationLocations [4] = Backend.Properties.Settings.Default.Config5; #if DEBUG ConLogger = new InfoLogger(Resources.LogFileName, true, false, Settings.Default.LogLevel, Settings.Default.LogFilePath); #endif #if !DEBUG ConLogger = new InfoLogger(Resources.LogFileName, true, false, LogLevel.INFO, Settings.Default.LogFilePath); #endif ConLogger.LogToFile = Settings.Default.LogToFile; ConLogger.Start(); ArduinoController.AutoConnect = Settings.Default.AutoConnect; ArduinoController.Init(); ArduinoController.OnReceiveMessage += (sender, e) => ConLogger.Log("IN < " + e.Message, LogLevel.DEBUG); ArduinoController.OnSendMessage += (sender, e) => ConLogger.Log("OUT > " + e.Message, LogLevel.DEBUG); ArduinoController.OnConnectionChanged += ((o, e) => { if (e.Connected) { ConLogger.Log("Connected to: " + ArduinoController.Board.ToString(), LogLevel.INFO); } else { ConLogger.Log("Disconnected", LogLevel.INFO); } }); Configuration.OnPinsUpdated += (o, e) => { if (e.UpdateOperation == UpdateOperation.Change) { ConLogger.Log("Pin Update: [" + e.UpdateOperation + "] " + e.OldPin + " to " + e.NewPin); } else { ConLogger.Log("Pin Update: [" + e.UpdateOperation + "] " + e.OldPin); } }; Configuration.OnSequencesUpdated += (o, e) => { if (e.UpdateOperation == UpdateOperation.Change) { ConLogger.Log("Sequence Update: [" + e.UpdateOperation + "] " + e.OldSeq + " to " + e.OldSeq); } else { ConLogger.Log("Sequence Update: [" + e.UpdateOperation + "] " + e.OldSeq); } }; Configuration.OnSignalsUpdated += (o, e) => { if (e.UpdateOperation == UpdateOperation.Change) { ConLogger.Log("Sequence Update: [" + e.UpdateOperation + "] " + e.OldMeCom + " to " + e.NewMeCom); } else { ConLogger.Log("Sequence Update: [" + e.UpdateOperation + "] " + e.OldMeCom); } }; }