172 lines
5.2 KiB
C#
172 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Windows.Input;
|
|
using AmebaPro3_ControlPanel.Services.Uart;
|
|
using AmebaPro3_ControlPanel.Utilities;
|
|
using Microsoft.Win32;
|
|
|
|
namespace AmebaPro3_ControlPanel.ViewModels;
|
|
|
|
public class MainPageViewModel : PageViewModelBase
|
|
{
|
|
private string _bootloaderPath = string.Empty;
|
|
private string _applicationPath = string.Empty;
|
|
private bool _isArduinoConnected;
|
|
private string? _selectedArduinoPort;
|
|
private int _arduinoBaudRate;
|
|
private string? _selectedArduinoTest;
|
|
|
|
public MainPageViewModel()
|
|
{
|
|
Title = "Panel";
|
|
|
|
AmebaConsole = new UartConsoleViewModel("AmebaPro3 UART", 1_500_000);
|
|
var amebaPorts = PortDiscovery.GetPorts();
|
|
AmebaConsole.SetPorts(amebaPorts);
|
|
if (amebaPorts.Length == 0)
|
|
{
|
|
AmebaConsole.LogLines.Add("[UART] No COM ports detected.");
|
|
}
|
|
else
|
|
{
|
|
AmebaConsole.LogLines.Add("[UART] Ready. Select a COM port and connect.");
|
|
}
|
|
|
|
ArduinoTests = new ObservableCollection<string>
|
|
{
|
|
"Test 1",
|
|
"Test 2",
|
|
"Test 3",
|
|
"Test 4",
|
|
"Test 5",
|
|
"Test 6",
|
|
"Test 7"
|
|
};
|
|
|
|
ArduinoPorts = new ObservableCollection<string> { "COM7", "COM8", "COM9" };
|
|
SelectedArduinoPort = ArduinoPorts.FirstOrDefault();
|
|
_arduinoBaudRate = 115200;
|
|
SelectedArduinoTest = ArduinoTests.FirstOrDefault();
|
|
|
|
BrowseBootloaderCommand = new RelayCommand(_ => PickFile(path => BootloaderPath = path));
|
|
BrowseAppCommand = new RelayCommand(_ => PickFile(path => ApplicationPath = path));
|
|
FlashImageCommand = new RelayCommand(_ => FlashImage(), _ => !string.IsNullOrWhiteSpace(BootloaderPath) && !string.IsNullOrWhiteSpace(ApplicationPath));
|
|
|
|
ArduinoConnectCommand = new RelayCommand(_ => ToggleArduinoConnection(), _ => !string.IsNullOrWhiteSpace(SelectedArduinoPort));
|
|
ArduinoResetCommand = new RelayCommand(_ => LogArduinoAction("Reset"));
|
|
ArduinoDownloadCommand = new RelayCommand(_ => LogArduinoAction("Download Mode"));
|
|
ArduinoNormalCommand = new RelayCommand(_ => LogArduinoAction("Normal Mode"));
|
|
ArduinoTestModeCommand = new RelayCommand(_ => LogArduinoAction($"Test Mode ({SelectedArduinoTest ?? "Not selected"})"));
|
|
}
|
|
|
|
public UartConsoleViewModel AmebaConsole { get; }
|
|
|
|
public ObservableCollection<string> ArduinoPorts { get; }
|
|
|
|
public ObservableCollection<string> ArduinoTests { get; }
|
|
|
|
public string BootloaderPath
|
|
{
|
|
get => _bootloaderPath;
|
|
set
|
|
{
|
|
if (SetProperty(ref _bootloaderPath, value))
|
|
{
|
|
(FlashImageCommand as RelayCommand)?.RaiseCanExecuteChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public string ApplicationPath
|
|
{
|
|
get => _applicationPath;
|
|
set
|
|
{
|
|
if (SetProperty(ref _applicationPath, value))
|
|
{
|
|
(FlashImageCommand as RelayCommand)?.RaiseCanExecuteChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsArduinoConnected
|
|
{
|
|
get => _isArduinoConnected;
|
|
private set => SetProperty(ref _isArduinoConnected, value);
|
|
}
|
|
|
|
public string? SelectedArduinoPort
|
|
{
|
|
get => _selectedArduinoPort;
|
|
set
|
|
{
|
|
if (SetProperty(ref _selectedArduinoPort, value))
|
|
{
|
|
(ArduinoConnectCommand as RelayCommand)?.RaiseCanExecuteChanged();
|
|
}
|
|
}
|
|
}
|
|
|
|
public int ArduinoBaudRate
|
|
{
|
|
get => _arduinoBaudRate;
|
|
set => SetProperty(ref _arduinoBaudRate, value);
|
|
}
|
|
|
|
public string? SelectedArduinoTest
|
|
{
|
|
get => _selectedArduinoTest;
|
|
set => SetProperty(ref _selectedArduinoTest, value);
|
|
}
|
|
|
|
public ICommand BrowseBootloaderCommand { get; }
|
|
|
|
public ICommand BrowseAppCommand { get; }
|
|
|
|
public ICommand FlashImageCommand { get; }
|
|
|
|
public ICommand ArduinoConnectCommand { get; }
|
|
|
|
public ICommand ArduinoResetCommand { get; }
|
|
|
|
public ICommand ArduinoDownloadCommand { get; }
|
|
|
|
public ICommand ArduinoNormalCommand { get; }
|
|
|
|
public ICommand ArduinoTestModeCommand { get; }
|
|
|
|
private void PickFile(Action<string> setPath)
|
|
{
|
|
var dialog = new OpenFileDialog
|
|
{
|
|
Filter = "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*",
|
|
Multiselect = false
|
|
};
|
|
|
|
if (dialog.ShowDialog() == true)
|
|
{
|
|
setPath(dialog.FileName);
|
|
}
|
|
}
|
|
|
|
private void FlashImage()
|
|
{
|
|
AmebaConsole.LogLines.Add($"[FLASH] Bootloader: {Path.GetFileName(BootloaderPath)}, App: {Path.GetFileName(ApplicationPath)}");
|
|
AmebaConsole.LogLines.Add("[FLASH] (UI-only) Flash operation queued.");
|
|
}
|
|
|
|
private void ToggleArduinoConnection()
|
|
{
|
|
IsArduinoConnected = !IsArduinoConnected;
|
|
var state = IsArduinoConnected ? "connected" : "disconnected";
|
|
AmebaConsole.LogLines.Add($"[ARDUINO] {state} on {SelectedArduinoPort} @ {ArduinoBaudRate} baud (UI placeholder).");
|
|
}
|
|
|
|
private void LogArduinoAction(string action)
|
|
{
|
|
AmebaConsole.LogLines.Add($"[ARDUINO] {action} triggered.");
|
|
}
|
|
}
|