Skip to content

Commit

Permalink
Merge pull request #432 from WildernessLabs/feature/v2-subdirs
Browse files Browse the repository at this point in the history
Feature/v2 subdirs
  • Loading branch information
adrianstevens authored Jan 18, 2024
2 parents fe73568 + 3514ddf commit 11baa7f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 59 deletions.
99 changes: 48 additions & 51 deletions Source/v2/Meadow.CLI.Core/DFU/DfuContext.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
using System;
using System.Collections.Generic;
using DfuSharp;
using DfuSharp;

namespace MeadowCLI
namespace MeadowCLI;

public class DfuContext
{
public class DfuContext
private List<ushort> validVendorIDs = new List<ushort>
{
private List<ushort> validVendorIDs = new List<ushort>
{
0x22B1, // secret labs
0x1B9F, // ghi
0x05A, // who knows
0x0483 // bootloader
};

// --------------------------- INSTANCE
public static DfuContext? Current;

public static void Init()
{
Current = new DfuContext();
Current._context = new Context();
}

public static void Dispose()
{
Current?._context?.Dispose();
}
// --------------------------- INSTANCE

private Context? _context;

public List<DfuDevice>? GetDevices()
{
if (_context != null)
return _context.GetDfuDevices(validVendorIDs);
else
return null;
}

public bool HasCapability(Capabilities caps)
{
if (_context != null)
return _context.HasCapability(caps);
else
return false;
}

public void BeginListeningForHotplugEvents()
{
if (_context != null)
_context.BeginListeningForHotplugEvents();
}
0x22B1, // secret labs
0x1B9F, // ghi
0x05A, // who knows
0x0483 // bootloader
};

// --------------------------- INSTANCE
public static DfuContext? Current;

public static void Init()
{
Current = new DfuContext();
Current._context = new Context();
}

public static void Dispose()
{
Current?._context?.Dispose();
}
// --------------------------- INSTANCE

private Context? _context;

public List<DfuDevice>? GetDevices()
{
if (_context != null)
return _context.GetDfuDevices(validVendorIDs);
else
return null;
}

public bool HasCapability(Capabilities caps)
{
if (_context != null)
return _context.HasCapability(caps);
else
return false;
}

public void BeginListeningForHotplugEvents()
{
if (_context != null)
_context.BeginListeningForHotplugEvents();
}

}
1 change: 1 addition & 0 deletions Source/v2/Meadow.CLI.Core/DFU/DfuUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private static void FormatDfuOutput(string logLine, ILogger? logger, DfuFlashFor
}
else //Console out
{
Debug.WriteLine(logLine);
Console.Write(logLine);

Console.Write(logLine.Contains("%") ? "\r" : "\r\n");
Expand Down
59 changes: 59 additions & 0 deletions Source/v2/Meadow.CLI.Core/Firmware/FirmwareWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Meadow.CLI.Core.Internals.Dfu;
using Meadow.LibUsb;
using Microsoft.Extensions.Logging;
using System.Diagnostics;

namespace MeadowCLI;

public class FirmwareWriter
{
public IEnumerable<ILibUsbDevice> GetLibUsbDevices(bool useLegacyLibUsb = false)
{
ILibUsbProvider provider;

if (useLegacyLibUsb)
{
provider = new ClassicLibUsbProvider();
}
else
{
provider = new LibUsbProvider();
}

return provider.GetDevicesInBootloaderMode();
}

public bool IsDfuDeviceAvailable(bool useLegacyLibUsb = false)
{
try
{
return GetLibUsbDevices(useLegacyLibUsb).Count() > 0;
}
catch
{
return false;
}
}

public async Task WriteOsWithDfu(string osFile, ILogger? logger = null, bool useLegacyLibUsb = false)
{
var devices = GetLibUsbDevices(useLegacyLibUsb);

switch (devices.Count())
{
case 0: throw new Exception("No device found in bootloader mode");
case 1: break;
default: throw new Exception("Multiple devices found in bootloader mode - only connect one device");
}

var serialNumber = devices.First().GetDeviceSerialNumber();

Debug.WriteLine($"DFU Writing file {osFile}");

await DfuUtils.FlashFile(
osFile,
serialNumber,
logger: logger,
format: DfuUtils.DfuFlashFormat.ConsoleOut);
}
}
3 changes: 3 additions & 0 deletions Source/v2/Meadow.CLI.Core/Meadow.CLI.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<ProjectReference Include="..\Meadow.Cloud.Client\Meadow.Cloud.Client.csproj" />
<ProjectReference Include="..\Meadow.HCom\Meadow.HCom.csproj" />
<ProjectReference Include="..\Meadow.SoftwareManager\Meadow.SoftwareManager.csproj" />
<ProjectReference Include="..\Meadow.UsbLib.Core\Meadow.UsbLib.Core.csproj" />
<ProjectReference Include="..\Meadow.UsbLibClassic\Meadow.UsbLibClassic.csproj" />
<ProjectReference Include="..\Meadow.UsbLib\Meadow.UsbLib.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 5 additions & 8 deletions Source/v2/Meadow.UsbLib/LibUsbDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ static LibUsbProvider()

public List<ILibUsbDevice> GetDevicesInBootloaderMode()
{
if (_devices == null)
{
_devices = _context
.List()
.Where(d => d.Info.VendorId == UsbBootLoaderVendorID)
.Select(d => new LibUsbDevice(d))
.ToList<ILibUsbDevice>();
}
_devices = _context
.List()
.Where(d => d.Info.VendorId == UsbBootLoaderVendorID)
.Select(d => new LibUsbDevice(d))
.ToList<ILibUsbDevice>();

return _devices;
}
Expand Down

0 comments on commit 11baa7f

Please sign in to comment.