Skip to content

Commit

Permalink
Xiao RP2040 devices can now be updated through the app!
Browse files Browse the repository at this point in the history
Thanks to the release of the Arduino CLI which powers the Arduino IDE 2.0. We can also use this to (hopefully) update Teensy powered devices too, as such, the old attempt to flash teensies has been stripped from the codebase
  • Loading branch information
DanForever committed Oct 4, 2022
1 parent 5c33438 commit e8b8fa8
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 120 deletions.
2 changes: 1 addition & 1 deletion Windows/Frontend/NotifyIcon/NotifyIconViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static void CreateTask(string assemblyPath)
td.Principal.RunLevel = TS.TaskRunLevel.Highest;

TS.LogonTrigger lt = new();
lt.UserId = Environment.UserName;
lt.UserId = System.Environment.UserName;
td.Triggers.Add(lt);

// Add an action that will launch Notepad whenever the trigger fires
Expand Down
2 changes: 0 additions & 2 deletions Windows/Testing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class Program
{
static async Task<int> Main(string[] args)
{
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Console.WriteLine("Dan's hardware monitor");

// Instanticate the class that will perform the main body of work for us
Expand Down
13 changes: 13 additions & 0 deletions Windows/Workhorse/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ namespace HardwareMonitor
{
public enum eMicrocontroller
{
// PJRC Teensy 3.2
Teensy32,

// PJRC Teensy 4.0
Teensy40,

// Seeeduino Xiao SAMD21
SeeediunoXiao,

// Seeeduino Xiao RP2040
Xiao2040,

Unknown
}

Expand Down Expand Up @@ -81,6 +89,8 @@ public class Device

private Version _version;

private System.Threading.SemaphoreSlim _semaphoreSlim = new(1, 1);

#endregion Private Fields

#region Public Properties
Expand All @@ -103,6 +113,7 @@ public Connection.ActiveConnection Connection

public Icon.Config Icons { get; set; }
public bool IsConnected => Connection is not null && Connection.IsOpen;
public bool DoNotRemove { get; set; }

public Layout.Config Layout => _layout;

Expand All @@ -113,6 +124,8 @@ public Connection.ActiveConnection Connection

public Version Version => _version;

public System.Threading.SemaphoreSlim Lock => _semaphoreSlim;

#endregion Public Properties

#region Events
Expand Down
32 changes: 32 additions & 0 deletions Windows/Workhorse/Environment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Arduino PC Health Monitor (PC Companion app)
* Polls the hardware sensors for data and forwards them on to the arduino device
* Copyright (C) 2022 Daniel Neve
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

namespace HardwareMonitor
{
public static class Environment
{
public static string UserfolderName => "DansHardwareMonitor";

public static string RoamingAppdata => System.IO.Path.Join(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), UserfolderName);
public static string LocalAppdata => System.IO.Path.Join(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), UserfolderName);

public static string ExecutablePath => System.Reflection.Assembly.GetEntryAssembly().Location;
public static string ExecutableFolder => System.IO.Path.GetDirectoryName(ExecutablePath);
}
}
2 changes: 1 addition & 1 deletion Windows/Workhorse/Layout/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Load()

Load(defaultConfigs);

string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string appdataPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);

// @todo: User layouts
}
Expand Down
39 changes: 28 additions & 11 deletions Windows/Workhorse/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,19 @@ public async Task<int> Run()

#region Private Methods

[Conditional("DEBUG")]
private void DebugPrintEnvironment()
{
Debug.WriteLine($"Roaming appdata folder: {Environment.RoamingAppdata}");
Debug.WriteLine($"Local appdata folder: {Environment.LocalAppdata}");
Debug.WriteLine($"Executable Path: {Environment.ExecutablePath}");
Debug.WriteLine($"Executable Folder: {Environment.ExecutableFolder}");
}

private async void Initialize()
{
DebugPrintEnvironment();

_sensorConfig = Monitor.Config.Computer.Load("Data/sensors.xml");
_pluginConfig = Plugin.Config.Config.Load("Data/plugins.xml");
_iconConfig = Icon.Config.Load("Data/icons.xml");
Expand All @@ -146,6 +157,7 @@ private async void Initialize()

_layoutManager.Load();

await Releases.DeviceUpdater.Initialise();
await Releases.Releases.UpdateLatestReleases();
}

Expand All @@ -162,7 +174,7 @@ private bool IsAlreadyConnected(Connection.AvailableConnection connection)

private bool ShouldRemoveDevice(Device device)
{
if (!device.IsConnected)
if (!device.IsConnected && !device.DoNotRemove)
{
Feedback?.Invoke($"Device disconnected: {device.Connection.Name}");
DeviceDisconnected?.Invoke(device);
Expand Down Expand Up @@ -224,6 +236,7 @@ private void PrintDeviceIdentity(Device device)
"Teensy 3.2",
"Teensy 4.0",
"Seeeduino Xiao",
"Seeeduino Xiao RP2040"
};

string[] ScreenStrings = new string[]
Expand Down Expand Up @@ -253,19 +266,23 @@ private async Task UpdateDevices()

foreach (Device device in _devices)
{
if (!device.IsConnected)
continue;

if(device.Layout == null)
await device.Lock.WaitAsync();
{
Layout.Config layout = _layoutManager.GetLayout(device.Resolution, device.Orientation);
if (layout != null)
await device.SetLayout(layout);
else
if (!device.IsConnected)
continue;
}

await device.Update(snapshot);
if (device.Layout == null)
{
Layout.Config layout = _layoutManager.GetLayout(device.Resolution, device.Orientation);
if (layout != null)
await device.SetLayout(layout);
else
continue;
}

await device.Update(snapshot);
}
device.Lock.Release();
}
}

Expand Down
171 changes: 171 additions & 0 deletions Windows/Workhorse/Releases/DeviceUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Arduino PC Health Monitor (PC Companion app)
* Polls the hardware sensors for data and forwards them on to the arduino device
* Copyright (C) 2022 Daniel Neve
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace HardwareMonitor.Releases
{
public static class DeviceUpdater
{
#region Public properties

public static bool Initialised { get; set; } = false;

public static string ArduinoCLIWorkspacePath => Path.Join(Environment.LocalAppdata, "arduino-cli");
public static string ArduinoCLIConfigPath => Path.Join(ArduinoCLIWorkspacePath, "config.yml");
public static string ArduinoCLIExecutableDevPath => Path.Join(Environment.ExecutableFolder, "..", "..", "External", "arduino-cli.exe");
public static string ArduinoCLIExecutableInstalledPath => Path.Join(Environment.ExecutableFolder, "Tools", "arduino-cli.exe");
public static string ArduinoCLIExecutablePath
{
get
{
if (File.Exists(ArduinoCLIExecutableInstalledPath)) return ArduinoCLIExecutableInstalledPath;
if (File.Exists(ArduinoCLIExecutableDevPath)) return ArduinoCLIExecutableDevPath;
return null;
}
}

public static bool ArduinoCLIConfigExists => File.Exists(ArduinoCLIConfigPath);
public static string ExtractedBinariesWorkspacePath => Path.Join(Environment.LocalAppdata, "downloaded-binaries");
public static string ExtractedBinaryPath => Path.Join(Environment.LocalAppdata, "downloaded-binaries", "device.bin");

#endregion Public properties

#region Public Methods

public static async Task Initialise()
{
if(!ArduinoCLIConfigExists)
{
CreateArduinoCLIConfig();
await DoArduinoCLIFirstTimeSetup();
}

Initialised = true;
}

public static async Task Update(Device device, string pathToZip)
{
if (!Initialised)
return;

PrepareBinaryWorkspace();
UnpackBinary(pathToZip);

string pathToBinary = ExtractedBinaryPath;

switch (device.Microcontroller)
{
case eMicrocontroller.Xiao2040:
if (Path.GetExtension(pathToBinary) != ".uf2")
{
string newPathToBinary = Path.ChangeExtension(pathToBinary, ".uf2");
File.Move(pathToBinary, newPathToBinary);
pathToBinary = newPathToBinary;
}

break;
}

// fully qualified board name
string fqbn = "";

// @todo: Move this to a config file
switch (device.Microcontroller)
{
case eMicrocontroller.Xiao2040:
fqbn = "rp2040:rp2040:seeed_xiao_rp2040";
break;
}

device.DoNotRemove = true;
await device.Lock.WaitAsync();
device.Connection.Disconnect();

await RunProcess(ArduinoCLIExecutablePath, $"--config-file \"{ArduinoCLIConfigPath}\" upload -t -v -p {device.Connection.Name} -i \"{pathToBinary}\" -b {fqbn}");

device.Lock.Release();
device.DoNotRemove = false;
}

#endregion Public Methods

#region Private Methods

private static void CreateArduinoCLIConfig()
{
Directory.CreateDirectory(ArduinoCLIWorkspacePath);
using (StreamWriter stream = new StreamWriter(ArduinoCLIConfigPath))
{
stream.WriteLine("directories:");
stream.WriteLine($" data: {Path.Join(ArduinoCLIWorkspacePath, "data")}");
stream.WriteLine($" downloads: {Path.Join(ArduinoCLIWorkspacePath, "downloads")}");
stream.WriteLine($" user: {Path.Join(ArduinoCLIWorkspacePath, "user")}");
stream.WriteLine("board_manager:");
stream.WriteLine(" additional_urls:");
stream.WriteLine(" - https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json");
stream.WriteLine(" - https://www.pjrc.com/teensy/package_teensy_index.json");
}
}

private static async Task DoArduinoCLIFirstTimeSetup()
{
await RunProcess(ArduinoCLIExecutablePath, $"--config-file \"{ArduinoCLIConfigPath}\" core update-index");
await RunProcess(ArduinoCLIExecutablePath, $"--config-file \"{ArduinoCLIConfigPath}\" core install rp2040:[email protected]");
await RunProcess(ArduinoCLIExecutablePath, $"--config-file \"{ArduinoCLIConfigPath}\" core install teensy:[email protected]");
}

private static async Task RunProcess(string exe, string arguments)
{
Process process = new();

process.StartInfo.FileName = exe;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
Debug.WriteLine($"DeviceUpdater: {e.Data}");
});

process.Start();
process.BeginOutputReadLine();

await process.WaitForExitAsync();
}

private static void PrepareBinaryWorkspace()
{
if(Directory.Exists(ExtractedBinariesWorkspacePath))
Directory.Delete(ExtractedBinariesWorkspacePath, true);
Directory.CreateDirectory(ExtractedBinariesWorkspacePath);
}

private static void UnpackBinary(string pathToZip)
{
// Unzip the binary
System.IO.Compression.ZipFile.ExtractToDirectory(pathToZip, ExtractedBinariesWorkspacePath);
}

#endregion Private Methods
}
}
Loading

0 comments on commit e8b8fa8

Please sign in to comment.