RGBController/RGBController2/ViewModels/Tabs/ArduinoTab.cs

85 lines
2.8 KiB
C#
Raw Normal View History

2020-10-25 19:52:14 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using RGBController2.Boards;
using System.ComponentModel;
2020-10-26 17:20:01 +00:00
using RGBController2.ViewModels.LightingModes;
2020-10-25 19:52:14 +00:00
namespace RGBController2.ViewModels.Tabs
{
2020-11-08 13:08:52 +00:00
/// <summary>
/// The view model for an ArduinoTab.
/// </summary>
2020-10-25 19:52:14 +00:00
public class ArduinoTab : Tab, INotifyPropertyChanged
{
2020-10-27 20:07:26 +00:00
2020-11-08 13:08:52 +00:00
/// <summary>
/// The array of available COM ports.
/// This is used to populate the items in the GUI.
/// </summary>
2020-10-25 19:52:14 +00:00
private string[] _availablePorts;
public string[] AvailablePorts
{
get { return _availablePorts; }
set { _availablePorts = value; }
}
2020-11-08 13:08:52 +00:00
/// <summary>
/// The selected port.
/// This is the selected item from the dropdown box in the GUI.
/// </summary>
2020-10-25 19:52:14 +00:00
private string _selectedPort;
public string SelectedPort
{
get { return _selectedPort; }
set
{
if (value != _selectedPort)
{
_selectedPort = value;
// Connect to the port here
2020-11-08 13:08:52 +00:00
_device = new ArduinoBoard(_selectedPort);
if (_device.Connected)
2020-10-26 17:20:01 +00:00
{
2020-10-25 19:52:14 +00:00
ConnectionStatus = "Device Connected";
2020-11-08 13:08:52 +00:00
EnableSelectLightingMode = true;
2020-10-26 17:20:01 +00:00
// Set the page to static lighting mode to allow the user to change the
// lighting mode now that we are connected to the arduino
2020-11-08 13:08:52 +00:00
SelectedLightingMode = new StaticViewModel(_device);
2020-10-26 17:20:01 +00:00
OnPropertyChanged(nameof(SelectedLightingMode));
}
2020-10-25 19:52:14 +00:00
else
2020-11-08 13:08:52 +00:00
{
2020-10-26 17:20:01 +00:00
ConnectionStatus = "Failed to Connect";
2020-11-08 13:08:52 +00:00
EnableSelectLightingMode = false;
}
2020-10-25 19:52:14 +00:00
OnPropertyChanged(nameof(ConnectionStatus));
2020-11-08 13:08:52 +00:00
OnPropertyChanged(nameof(EnableSelectLightingMode));
2020-10-25 19:52:14 +00:00
}
}
}
2020-11-08 13:08:52 +00:00
/// <summary>
/// Constructor for an Arduino tab.
/// </summary>
/// <param name="name">The tab header name.</param>
2020-10-25 19:52:14 +00:00
public ArduinoTab(string name)
{
2020-10-27 20:07:26 +00:00
TabType = tabType.Arduino;
2020-10-25 19:52:14 +00:00
Name = name;
ConnectionStatus = "Device Disconnected";
_selectedPort = "";
// Get a list of the available com ports
string[] ports = SerialPort.GetPortNames();
_availablePorts = ports;
2020-10-26 17:20:01 +00:00
// This is a temporary viewmodel that is used before the user has connected to a device
2020-10-27 20:07:26 +00:00
SelectedLightingMode = new InformationViewModel();
2020-10-25 19:52:14 +00:00
}
}
}