2010-12-18 18:23:09 -04:00
|
|
|
using System;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using Timer=System.Windows.Forms.Timer;
|
|
|
|
|
|
|
|
namespace ArducopterConfigurator.PresentationModels
|
|
|
|
{
|
|
|
|
public class MainVm : NotifyProperyChangedBase, IPresentationModel
|
|
|
|
{
|
2010-12-23 11:59:00 -04:00
|
|
|
private readonly IComms _comms;
|
2010-12-18 18:23:09 -04:00
|
|
|
private bool _isConnected;
|
2011-02-13 05:25:54 -04:00
|
|
|
private IPresentationModel _selectedVm;
|
2010-12-18 18:23:09 -04:00
|
|
|
private string _selectedPort;
|
|
|
|
private string _apmVersion;
|
2010-12-20 18:14:39 -04:00
|
|
|
private Timer _connectionAttemptsTimer;
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
private SessionStates _connectionState;
|
|
|
|
|
|
|
|
|
2010-12-20 18:14:39 -04:00
|
|
|
public MainVm(IComms session)
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
2010-12-23 11:59:00 -04:00
|
|
|
_comms = session;
|
|
|
|
_comms.LineOfDataReceived += _session_LineOfDataReceived;
|
2010-12-18 18:23:09 -04:00
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
MonitorVms = new BindingList<IPresentationModel>
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
2011-02-14 10:48:47 -04:00
|
|
|
new SensorsVm(),
|
2011-02-13 05:25:54 -04:00
|
|
|
new TransmitterChannelsVm(),
|
|
|
|
new FlightControlPidsVm(),
|
|
|
|
new PositionAltitudePidsVm(),
|
|
|
|
//new MotorCommandsVm(session),
|
2011-02-13 14:14:28 -04:00
|
|
|
new SerialMonitorVm()
|
2010-12-18 18:23:09 -04:00
|
|
|
};
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
foreach (var vm in MonitorVms)
|
|
|
|
{
|
|
|
|
if (vm is ItalksToApm)
|
2011-02-13 14:14:28 -04:00
|
|
|
vm.sendTextToApm += MainVm_sendTextToApm;
|
2011-02-13 05:25:54 -04:00
|
|
|
}
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
ConnectCommand = new DelegateCommand(
|
|
|
|
_ => Connect(),
|
|
|
|
_ => _connectionState==SessionStates.Disconnected && AvailablePorts.Contains(SelectedPort));
|
|
|
|
|
|
|
|
DisconnectCommand = new DelegateCommand(_ => Disconnect(), _ => IsConnected);
|
|
|
|
|
|
|
|
RefreshPortListCommand = new DelegateCommand(_ => RefreshPorts());
|
|
|
|
|
2011-02-13 14:14:28 -04:00
|
|
|
RestoreDefaultsCommand = new DelegateCommand(_ => RestoreDefaults(), _ => IsConnected);
|
|
|
|
|
|
|
|
WriteToEepromCommand = new DelegateCommand(_ => WriteToEeprom(), _ => IsConnected);
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
ConnectionState = SessionStates.Disconnected;
|
|
|
|
|
|
|
|
AvailablePorts = new BindingList<string>();
|
2011-01-27 11:58:32 -04:00
|
|
|
|
|
|
|
RefreshPorts();
|
|
|
|
|
|
|
|
// Initially have selected the last discovered com port.
|
|
|
|
// I think this is more likely to be the correct one, as
|
|
|
|
// the built in comports come first, then the usb/serial
|
|
|
|
// converter ports
|
|
|
|
if (AvailablePorts.Count > 0)
|
|
|
|
SelectedPort = AvailablePorts[AvailablePorts.Count-1];
|
2010-12-18 18:23:09 -04:00
|
|
|
}
|
|
|
|
|
2011-02-13 14:14:28 -04:00
|
|
|
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
void MainVm_sendTextToApm(object sender, sendTextToApmEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender.Equals(_selectedVm)==false)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Non selected vm wants to send something to the APM");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_comms.IsConnected)
|
|
|
|
_comms.Send(e.textToSend);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
private void RefreshPorts()
|
|
|
|
{
|
|
|
|
AvailablePorts.Clear();
|
2010-12-23 11:59:00 -04:00
|
|
|
foreach (var c in _comms.ListCommPorts())
|
2010-12-18 18:23:09 -04:00
|
|
|
AvailablePorts.Add(c);
|
|
|
|
}
|
|
|
|
|
2011-02-13 14:14:28 -04:00
|
|
|
private void RestoreDefaults()
|
|
|
|
{
|
|
|
|
if (_comms.IsConnected)
|
|
|
|
_comms.Send("Y");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void WriteToEeprom()
|
|
|
|
{
|
|
|
|
if (_comms.IsConnected)
|
|
|
|
_comms.Send("W");
|
|
|
|
}
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
void _session_LineOfDataReceived(string strRx)
|
|
|
|
{
|
|
|
|
// If we are waiting for the version string
|
|
|
|
if (ConnectionState==SessionStates.Connecting)
|
|
|
|
{
|
2010-12-19 06:39:36 -04:00
|
|
|
if (strRx.Length > 6)
|
|
|
|
return; // too long for a version string - reject
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
// then assume that this is the version string received
|
|
|
|
ApmVersion = strRx;
|
|
|
|
ConnectionState = SessionStates.Connected;
|
|
|
|
_selectedVm.Activate();
|
|
|
|
}
|
2011-02-13 05:25:54 -04:00
|
|
|
else if (ConnectionState == SessionStates.Connected)
|
|
|
|
{
|
|
|
|
if (_selectedVm is ItalksToApm)
|
|
|
|
(_selectedVm as ItalksToApm).handleLineOfText(strRx);
|
|
|
|
}
|
2010-12-18 18:23:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public ICommand ConnectCommand { get; private set; }
|
|
|
|
|
|
|
|
public ICommand DisconnectCommand { get; private set; }
|
|
|
|
|
|
|
|
public ICommand RefreshPortListCommand { get; private set; }
|
2011-02-13 14:14:28 -04:00
|
|
|
|
|
|
|
public ICommand RestoreDefaultsCommand { get; private set; }
|
|
|
|
|
|
|
|
public ICommand WriteToEepromCommand { get; private set; }
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
public BindingList<string> AvailablePorts { get; private set; }
|
|
|
|
|
|
|
|
public enum SessionStates
|
|
|
|
{
|
|
|
|
Disconnected,
|
|
|
|
Connecting,
|
|
|
|
Connected,
|
|
|
|
}
|
|
|
|
|
|
|
|
public SessionStates ConnectionState
|
|
|
|
{
|
|
|
|
get { return _connectionState; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_connectionState != value)
|
|
|
|
{
|
|
|
|
_connectionState = value;
|
|
|
|
IsConnected = _connectionState == SessionStates.Connected;
|
|
|
|
FirePropertyChanged("ConnectionState");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// provided for convenience for binding set from the session state prop
|
|
|
|
public bool IsConnected
|
|
|
|
{
|
|
|
|
get { return _isConnected; }
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (_isConnected != value)
|
|
|
|
{
|
|
|
|
_isConnected = value;
|
|
|
|
FirePropertyChanged("IsConnected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string SelectedPort
|
|
|
|
{
|
|
|
|
get { return _selectedPort; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_selectedPort != value)
|
|
|
|
{
|
|
|
|
_selectedPort = value;
|
|
|
|
FirePropertyChanged("SelectedPort");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Connect()
|
|
|
|
{
|
2010-12-23 11:59:00 -04:00
|
|
|
_comms.CommPort = SelectedPort;
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
// Todo: check the status of this call success/failure
|
2011-02-27 04:09:16 -04:00
|
|
|
if (!_comms.Connect())
|
|
|
|
{
|
|
|
|
ConnectionState = SessionStates.Disconnected;
|
|
|
|
ApmVersion = "Error";
|
|
|
|
return;
|
|
|
|
}
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
ConnectionState = SessionStates.Connecting;
|
|
|
|
|
|
|
|
_connectionAttemptsTimer = new Timer();
|
|
|
|
_connectionAttemptsTimer.Tick += _connectionAttemptsTimer_Tick;
|
|
|
|
_connectionAttemptsTimer.Interval = 1000; //milliseconds
|
|
|
|
_connectionAttemptsTimer.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _connectionAttemptsTimer_Tick(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (_connectionState != SessionStates.Connecting)
|
|
|
|
{
|
|
|
|
_connectionAttemptsTimer.Stop();
|
|
|
|
return;
|
|
|
|
}
|
2010-12-23 11:59:00 -04:00
|
|
|
_comms.Send("X");
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
// once we connected, then get the version string
|
2010-12-23 11:59:00 -04:00
|
|
|
_comms.Send("!");
|
2010-12-18 18:23:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Disconnect()
|
|
|
|
{
|
2010-12-23 11:59:00 -04:00
|
|
|
_comms.Send("X");
|
|
|
|
_comms.DisConnect();
|
2010-12-18 18:23:09 -04:00
|
|
|
ConnectionState = SessionStates.Disconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string ApmVersion
|
|
|
|
{
|
|
|
|
get { return _apmVersion; }
|
|
|
|
private set
|
|
|
|
{
|
|
|
|
if (_apmVersion != value)
|
|
|
|
{
|
|
|
|
_apmVersion = value;
|
|
|
|
FirePropertyChanged("ApmVersion");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
public BindingList<IPresentationModel> MonitorVms { get; private set; }
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get { return "Arducopter Config"; }
|
|
|
|
}
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
public void Activate()
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeActivate()
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler updatedByApm;
|
|
|
|
|
|
|
|
public void Select(IPresentationModel vm)
|
2010-12-18 18:23:09 -04:00
|
|
|
{
|
|
|
|
if (_selectedVm==vm)
|
|
|
|
return;
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
if (vm == null)
|
|
|
|
throw new ArgumentNullException("vm");
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
if (_selectedVm!=null)
|
|
|
|
_selectedVm.DeActivate();
|
|
|
|
|
|
|
|
_selectedVm = vm;
|
|
|
|
_selectedVm.Activate();
|
|
|
|
}
|
2011-02-13 05:25:54 -04:00
|
|
|
|
|
|
|
public void handleLineOfText(string strRx)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler<sendTextToApmEventArgs> sendTextToApm;
|
2010-12-18 18:23:09 -04:00
|
|
|
}
|
|
|
|
}
|