-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #432 from WildernessLabs/feature/v2-subdirs
Feature/v2 subdirs
- Loading branch information
Showing
5 changed files
with
116 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters