Skip to content

Commit

Permalink
Merge pull request #881 from intechstudio/danim1130/rework-bootload-d…
Browse files Browse the repository at this point in the history
…etection

Rework Bootload detection
  • Loading branch information
danim1130 authored Jan 8, 2025
2 parents 8128f49 + ae249e4 commit 9967695
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 140 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/alpha-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV
echo "WORKFLOW_NAME=alpha" >> $GITHUB_ENV
- name: Install python setup-tools dependency
if: runner.os == 'macOS'
run: brew install python-setuptools

- name: Install dependencies
run: npm i

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/nightly-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV
echo "WORKFLOW_NAME=nightly" >> $GITHUB_ENV
- name: Install python setup-tools dependency
if: runner.os == 'macOS'
run: brew install python-setuptools

- name: Install dependencies
run: npm i

Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/stable-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
with:
node-version: "18.16.1"

- name: Install python setup-tools dependency
if: runner.os == 'macOS'
run: brew install python-setuptools

- name: Install dependencies
run: npm i

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"adm-zip": "^0.5.10",
"chokidar": "^3.5.3",
"discord.js": "^13.6.0",
"drivelist": "^12.0.2",
"electron-dl": "^3.4.1",
"electron-log": "^4.4.8",
"electron-store": "^8.1.0",
Expand All @@ -69,6 +70,7 @@
"svelte-watch-resize": "^1.0.3",
"ts-jest": "^29.1.2",
"typescript": "^5.0.0",
"usb": "^2.14.0",
"uuid": "^9.0.0",
"vite": "^4.5.2",
"ws": "^7.5.8"
Expand Down
16 changes: 15 additions & 1 deletion src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ import {
} from "./src/profiles";
import { fetchUrlJSON } from "./src/fetch";
import { getLatestVideo } from "./src/youtube";
import { SerialPort } from "serialport";
import { usb } from "usb";

log.info("App starting...");

usb.on("attach", () => {
let delay = 500;
async function retryFind() {
let result = await findBootloaderPath();
if (result) return;

delay += 500;
if (delay > 1500) return;
setTimeout(retryFind, delay);
}
setTimeout(retryFind, delay);
});
setTimeout(findBootloaderPath, 10000); //Initial check

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
Expand Down
88 changes: 34 additions & 54 deletions src/electron/src/firmware.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import nodeDiskInfo from "node-disk-info";
import Drive from "node-disk-info/dist/classes/drive";

import drivelist from "drivelist";

import log from "electron-log";
import fs from "fs-extra";

import { extractArchiveToTemp, downloadInMainProcess } from "./library";

import configuration from "../../../configuration.json";

export const firmware = {
mainWindow: undefined,
};
Expand All @@ -19,10 +18,10 @@ function delay(time) {
}

export async function findBootloaderPath() {
let diskInfo: Drive[] = [];
let diskInfo: drivelist.Drive[] = [];

try {
diskInfo = nodeDiskInfo.getDiskInfoSync();
diskInfo = await drivelist.list();
} catch (error) {
log.warn(error);
}
Expand All @@ -31,72 +30,51 @@ export async function findBootloaderPath() {
return;
}

// log.info(diskInfo)
// 7929 MAC || 15867 new
// 3965 for Linux and 4059648 for Windows (old bootloader)
// 7934 for Linux and 8123904 for Windows (new bootloader)

let gridDrive = diskInfo.find(
(a) =>
// old bootloader Linux Mac Win
a.blocks === 3965 ||
a.blocks === 7929 ||
a.blocks === 4059648 ||
// new bootloader Linux, Mac, M1Mac, Win
a.blocks === 7934 ||
a.blocks === 15867 ||
a.blocks === 15868 ||
a.blocks === 8123904 ||
// add esp32 bootloader block size here LINUX & M1 Mac, M1 Mac & WINDOWS
a.blocks === 32640 ||
a.blocks === 65281 ||
a.blocks === 65280 ||
a.blocks === 33423360
let gridDrives = diskInfo.filter(
(a) => a.size < 64 * 1024 * 1024 && a.isUSB && !a.isSystem && !a.isReadOnly
);
if (gridDrives.length === 0) return;

//console.log("DiskInfo", diskInfo)

let data;

if (gridDrive !== undefined) {
for (const gridDrive of gridDrives) {
let mountPath = gridDrive.mountpoints[0].path;
let data: string;
try {
data = fs.readFileSync(gridDrive.mounted + "/INFO_UF2.TXT", {
data = fs.readFileSync(mountPath + "/INFO_UF2.TXT", {
encoding: "utf8",
flag: "r",
});
} catch (error) {
console.warn(error);
}
}
if (data === undefined) continue;

if (data !== undefined && gridDrive !== undefined) {
// is it grid
if (data.indexOf("SAMD51N20A-GRID") !== -1) {
firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Grid D51 bootloader is detected!",
code: 3,
path: gridDrive.mounted,
path: mountPath,
});
return { path: gridDrive.mounted, architecture: "d51", product: "grid" };
return { path: mountPath, architecture: "d51", product: "grid" };
} else if (data.indexOf("ESP32S3") !== -1 && data.indexOf("Grid") !== -1) {
firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Grid ESP32 bootloader is detected!",
code: 3,
path: gridDrive.mounted,
path: mountPath,
});
return {
path: gridDrive.mounted,
path: mountPath,
architecture: "esp32",
product: "grid",
};
} else if (data.indexOf("ESP32S3") !== -1 && data.indexOf("Knot") !== -1) {
firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Knot ESP32 bootloader is detected!",
code: 3,
path: gridDrive.mounted,
path: mountPath,
});
return {
path: gridDrive.mounted,
path: mountPath,
architecture: "esp32",
product: "knot",
};
Expand All @@ -107,7 +85,7 @@ export async function findBootloaderPath() {
export async function firmwareDownload(targetFolder, product, arch, url) {
const { path } = await findBootloaderPath();

if (typeof path === "undefined") {
if (path === undefined) {
//bootloader not found
firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Error: No device connected.",
Expand Down Expand Up @@ -186,21 +164,23 @@ export async function firmwareDownload(targetFolder, product, arch, url) {

await delay(1500);

if (path !== undefined) {
try {
fs.copySync(
targetFolder + "/temp/" + firmwareFileName,
path + "/" + firmwareFileName
);
} catch (error) {
console.log("COPY ERROR UNBOUNT", error);
}
try {
fs.copySync(
targetFolder + "/temp/" + firmwareFileName,
path + "/" + firmwareFileName
);
} catch (error) {
console.log("COPY ERROR UNBOUND", error);

firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Update completed successfully!",
code: 5,
message: "Bootloader connection lost!",
code: 6,
});
} else {
log.warn("GRID_NOT_FOUND");
return;
}

firmware.mainWindow.webContents.send("onFirmwareUpdate", {
message: "Update completed successfully!",
code: 5,
});
}
102 changes: 17 additions & 85 deletions src/renderer/main/FirmwareCheck.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,8 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
let fwMismatch = false;
let dotdotdot = "";
let flagBootloaderCheck = 0;
let booloaderConnectionCheck = undefined;
let bootloader_path = undefined;
const startBootloaderCheck = () => {
if (flagBootloaderCheck === 1) {
return;
}
booloaderConnectionCheck = setInterval(() => find_bootloader_path(), 750);
flagBootloaderCheck = 1;
};
const stopBootloaderCheck = () => {
////console.log("Stop Trying")
clearInterval(booloaderConnectionCheck);
flagBootloaderCheck = 0;
};
// check for parsed modules
let runtime: GridRuntime;
Expand All @@ -54,15 +34,6 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
$: {
let firmwareMismatchFound = false;
if ($runtime.modules.length === 0) {
startBootloaderCheck();
if ($appSettings.firmwareNotificationState == 1) {
$appSettings.firmwareNotificationState = 2;
}
} else {
stopBootloaderCheck();
}
// check modules for firmware mismatch
$runtime.modules.forEach((device) => {
if ($appSettings.firmwareNotificationState == 6) {
Expand Down Expand Up @@ -95,11 +66,17 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
fwMismatch = true;
}
} else {
if (fwMismatch) {
//All mismatched module have been removed, progress state
appSettings.update((s) => {
s.firmwareNotificationState = 2;
return s;
});
}
fwMismatch = false;
}
}
let text = "";
let uploadProgressText = "";
window.electron.firmware.onFirmwareUpdate((_event, value) => {
Expand All @@ -114,6 +91,7 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
//console.log("Set state from ", $appSettings.firmwareNotificationState, " to ", value.code)
$appSettings.firmwareNotificationState = value.code;
bootloader_path = value.path;
if (value.message !== undefined) {
uploadProgressText = value.message;
Expand All @@ -128,62 +106,16 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
}
});
onMount(() => {
startBootloaderCheck();
if (ctxProcess.platform() == "darwin") {
text = "Command + Shift + R";
} else {
text = "Ctrl + Shift + R";
}
});
async function find_bootloader_path() {
//console.log("Try Detect Bootloader")
const value = await window.electron.firmware.findBootloaderPath();
if (value !== undefined) {
if (
$appSettings.firmwareNotificationState == 1 ||
$appSettings.firmwareNotificationState == 2 ||
$appSettings.firmwareNotificationState == 6
) {
//console.log("Successfuly detection", value.path)
bootloader_path = value.path;
} else {
//console.log("Dont care detection", value.path)
bootloader_path = value.path;
}
//stopBootloaderCheck();
} else {
if (
$appSettings.firmwareNotificationState == 4 ||
$appSettings.firmwareNotificationState == 5 ||
$appSettings.firmwareNotificationState == 6
) {
bootloader_path = undefined;
//console.log("Disconnect but no problem")
} else {
//console.log("Disconnect from state", $appSettings.firmwareNotificationState)
if (typeof bootloader_path !== "undefined") {
////console.log("Disconnect because lost", $appSettings.firmwareNotificationState)
bootloader_path = undefined;
uploadProgressText = "Bootloader connection lost!";
$appSettings.firmwareNotificationState = 6;
}
}
}
}
async function firmwareDownload(nightly) {
const folder = $appSettings.persistent.profileFolder;
const { product, architecture } =
await window.electron.firmware.findBootloaderPath();
let result = await window.electron.firmware.findBootloaderPath();
if (result === undefined) {
$appSettings.firmwareNotificationState = 6;
bootloader_path = undefined;
uploadProgressText = "Bootloader connection lost!";
return;
}
const { product, architecture } = result;
Analytics.track({
event: "FirmwareCheck",
Expand Down Expand Up @@ -285,7 +217,7 @@ STATE 6 | Error | Button -> STATE 0 (Close notification)
>
<div class="flex-col">
<div class="mx-2">
<b>Waiting for the bootloader to enumerate {dotdotdot} </b>
<b>Waiting for the bootloader to enumerate</b>
</div>
<div class="mx-2">Connect the module in bootloader mode!</div>
</div>
Expand Down

0 comments on commit 9967695

Please sign in to comment.