public ArduinoModel(TeamDataModel teamData1, TeamDataModel teamData2) { SetConnection(); if (_comOK) { _session = new ArduinoSession(_connection); _keepAliveTimer = new Timer(Constants.keepAliveInterval) { AutoReset = true }; _keepAliveTimer.Elapsed += _keepAliveTimer_Elapsed; _keepAliveTimer.Start(); //_session.SetSamplingInterval(400); _session.SetDigitalPinMode(Constants.rlimb, PinMode.InputPullup); _session.SetDigitalPinMode(Constants.rlimf, PinMode.InputPullup); _session.SetDigitalPinMode(Constants.blimb, PinMode.InputPullup); _session.SetDigitalPinMode(Constants.blimf, PinMode.InputPullup); _session.SetDigitalReportMode(Constants.limitSwitchPort, true); var tracker = _session.CreateDigitalStateMonitor(Constants.limitSwitchPort); teamData1.TeamControl.BackLimSwitch.Subscribe(tracker); teamData1.TeamControl.FrontLimSwitch.Subscribe(tracker); teamData2.TeamControl.BackLimSwitch.Subscribe(tracker); teamData2.TeamControl.FrontLimSwitch.Subscribe(tracker); //use code below to enable event managed pin updates //_session.DigitalStateReceived += DigitalStateReceivedHandler; } //Subscribe to controllable data changes //Planned: publish sensor updates using TeamSensor object in teamData _teamData1 = teamData1; _teamData1.TeamControl.HoverLed.PropertyChanged += OnLedChanged; _teamData1.TeamControl.Obstacle1Led.PropertyChanged += OnLedChanged; _teamData1.TeamControl.Obstacle2Led.PropertyChanged += OnLedChanged; _teamData1.TeamControl.Platform1Led.PropertyChanged += OnLedChanged; _teamData1.TeamControl.Platform2Led.PropertyChanged += OnLedChanged; _teamData1.TeamControl.StartLed.PropertyChanged += OnLedChanged; _teamData1.TeamControl.Motor.PropertyChanged += OnMotorChanged; _teamData2 = teamData2; _teamData2.TeamControl.HoverLed.PropertyChanged += OnLedChanged; _teamData2.TeamControl.Obstacle1Led.PropertyChanged += OnLedChanged; _teamData2.TeamControl.Obstacle2Led.PropertyChanged += OnLedChanged; _teamData2.TeamControl.Platform1Led.PropertyChanged += OnLedChanged; _teamData2.TeamControl.Platform2Led.PropertyChanged += OnLedChanged; _teamData2.TeamControl.StartLed.PropertyChanged += OnLedChanged; _teamData2.TeamControl.Motor.PropertyChanged += OnMotorChanged; }
public void SetDigitalReportMode(int portNumber, Boolean enable, int sleep = 0) { try { //when setting a port to be reported by Firmata on the arduino it's important that there aren't //other devices on pins on that port that may result in an inrush of reports and hence cause too //much traffic from the board to the host (which can result in buffers filling up and ulitmately a board reset) for (int i = 0; i < 8; i++) { int pin = GetPinForPort(portNumber, i); List <ArduinoDevice> devs = GetDevicesByPin(pin); if (devs == null) { continue; } foreach (ArduinoDevice dev in devs) { bool portAvailable = dev is Devices.SwitchSensor; if (!portAvailable) { String msg = String.Format("ArduinoDeviceManager::SetDigitalReportMode Cannot set digital port {0} as the device {1} on pin {2} is on that port", portNumber, dev.ID, pin); throw new Exception(msg); } } } //remove any record of the port state so we ensure we get a fresh one if (_portStates.ContainsKey(portNumber)) { _portStates.Remove(portNumber); } _session.SetDigitalReportMode(portNumber, enable); _sleep(sleep); } catch (Exception e) { String msg = String.Format("Error setting digital port {0} to report {1}, {2}: {3} ", portNumber, enable, e.GetType(), e.Message); Tracing?.TraceEvent(TraceEventType.Error, 0, msg); } }