Skip to content

Commit

Permalink
program ESP32s
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Sep 25, 2024
1 parent 0694366 commit 24d92bb
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 25 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"baudrate",
"BOOTSEL",
"buttonstatus",
"calib",
Expand Down
2 changes: 1 addition & 1 deletion docs/arduino-web-uploader.js

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions docs/cryptojs.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/esptool-js.js

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

<script src="./qrcode.min.js"></script>

<script type="module" src="./esptool-js.js"></script>
<script src="./cryptojs.js"></script>

<script src="./esptool-js.js"></script>

<script src="./script.js"></script> <!-- for the Upload Program section -->

Expand All @@ -36,10 +38,6 @@ <h2> <a target="_blank" rel="noopener noreferrer"
here</a>
or email <a href="mailto: [email protected]">[email protected]</a>.
</p>
<p>
This website can only change the settings of cars with the new code that this site uploads, <br>it won't
work with the original go baby go code.
</p>

<noscript> <mark> Your browser is not running a script that is required for this website to function.
Please
Expand Down Expand Up @@ -117,7 +115,7 @@ <h2>Connect to Car (to change settings)</h2>
<button id="serial-disconnect-button"
onclick='document.getElementById("serial-disconnect-button").style.border=""; closeSerial();'>disconnect
</button>
<label for="esp32-serial-baud" style='margin-left:25px'> esp32</label>
<label for="esp32-serial-baud" style='margin-left:25px; font-size: small;'> esp32</label>
<input type="checkbox" id="esp32-serial-baud">
<div id="serial-connected-short"></div>
<hr>
Expand Down Expand Up @@ -303,7 +301,6 @@ <h2>Calibrate and Configure Your Car</h2>
<br>
This website uses <a target="_blank" rel="noopener noreferrer"
href="https://github.com/dbuezas/arduino-web-uploader">arduino-web-uploader</a>
(generously shared under the MIT license)
by David Buezas for uploading code to cars that use Arduino Nano and Arduino Uno boards.
<br><br>
This website uses <a target="_blank" rel="noopener noreferrer"
Expand Down Expand Up @@ -455,10 +452,10 @@ <h2 id="help-upload">
&#x2022; The selector should be changed to say "new nano or uno" if you bought an
official Arduino Nano or Uno board.
<br><br>
&#x2022; The selector should be changed to say "rpi pico" if you
&#x2022; (Experimental!) The selector should be changed to say "rpi pico" if you
are using a Raspberry Pi Pico, or "rpi pico W" if you are using a Raspberry Pi Pico W.
<br><br>
&#x2022; The selector should be changed to say "ESP32" if you
&#x2022; (Experimental!) The selector should be changed to say "ESP32" if you
are using an ESP32. The "esp32" checkbox that you will later see next to the connect
button should automatically
check itself.
Expand Down
127 changes: 113 additions & 14 deletions docs/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var library_config_text = null; // variable holding the text for the currently l
var eepromAlertedEver = false; // has the user been alerted about EEPROM failure?
var picoUploadListenerFunction = null;
var esp32UploadListenerFunction = null;
// import { ESPLoader, Transport } from "./esptool-js.js"; // TODO: use file not url
document.addEventListener('DOMContentLoaded', async function () {
// runs on startup
// check if web serial is enabled
Expand Down Expand Up @@ -1345,10 +1344,36 @@ async function getCode() {
try {
if (board === "ESP") {
code = {};

code["boot_app"] = await getRequest("https://raw.githubusercontent.com/gobabygocarswithjoysticks/car-code/" + configurations_info[1] + "/hex/" + name + "/boot_app0.bin", true);
code["ino_bin"] = await getRequest("https://raw.githubusercontent.com/gobabygocarswithjoysticks/car-code/" + configurations_info[1] + "/hex/" + name + "/" + program + ".ino." + "bin", true);
code["bootloader"] = await getRequest("https://raw.githubusercontent.com/gobabygocarswithjoysticks/car-code/" + configurations_info[1] + "/hex/" + name + "/" + program + ".ino." + "bootloader.bin", true);
code["partitions"] = await getRequest("https://raw.githubusercontent.com/gobabygocarswithjoysticks/car-code/" + configurations_info[1] + "/hex/" + name + "/" + program + ".ino." + "partitions.bin", true);

const reader1 = new FileReader();
reader1.onload = (ev) => {
code["boot_app"] = ev.target.result;
}
reader1.readAsBinaryString(code["boot_app"]); // I know it's deprecated but I can't find anything else that works

const reader2 = new FileReader();
reader2.onload = (ev) => {
code["ino_bin"] = ev.target.result;
}
reader2.readAsBinaryString(code["ino_bin"]);

const reader3 = new FileReader();
reader3.onload = (ev) => {
code["bootloader"] = ev.target.result;
}
reader3.readAsBinaryString(code["bootloader"]);

const reader4 = new FileReader();
reader4.onload = (ev) => {
code["partitions"] = ev.target.result;
}
reader4.readAsBinaryString(code["partitions"]);

if (code["boot_app"] == null || code["ino_bin"] == null || code["bootloader"] == null || code["partitions"] == null) {
code = null; // there was a problem getting all 4 components of the code
}
Expand Down Expand Up @@ -1384,32 +1409,98 @@ async function getCode() {
if (esp32UploadListenerFunction != null) {
upload_button.removeEventListener("click", esp32UploadListenerFunction);
}
//TODO: MAKE SURE ESP32 UPLOADER CAN'T BE RUN TWICE AT THE SAME TIME OR ON TOP OF SERIAL CONNECTION
esp32UploadListenerFunction = async function () {
if (upload_button.hasAttribute("AWU")) { // if in normal uploader mode not pico mode
return;
}
if (!upload_button.hasAttribute("espUpload")) { // if not in esp32 uploader mode
return;
}
if (upload_button.disabled) {
return;
}
document.getElementById("upload-button").disabled = true;

upload_button.hidden = false;
upload_warning_span.innerHTML = "";
console.log("time to upload ESP32 code");
//https://github.com/espressif/esptool-js/blob/main/examples/typescript/src/index.ts

const device = await navigator.serial.requestPort({});
const transport = new Transport(device, true);
const flashOptions = {
transport,
baudrate: 115200,
terminal: espLoaderTerminal,
}
//using this example: https://github.com/espressif/esptool-js/blob/main/examples/typescript/src/index.ts
try {

var device = await navigator.serial.requestPort({});
var transport = new Transport(device, true);
let espLoaderTerminal = {
clean() {
},
writeLine(data) {
if (data === "Leaving...") {
document.getElementById("upload-progress").innerHTML = "Done!"
}
},
write(data) {
},
};

// files: python -m esptool --chip esp32 --port COM30 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x1000 blink.ino.bootloader.bin 0x8000 blink.ino.partitions.bin 0xe000 boot_app0.bin 0x10000 blink.ino.bin
let fileArray = [];

await fileArray.push({ data: await code["bootloader"], address: 0x1000 });
await fileArray.push({ data: await code["partitions"], address: 0x8000 });
await fileArray.push({ data: await code["boot_app"], address: 0xe000 });
await fileArray.push({ data: await code["ino_bin"], address: 0x10000 });

const flashOptionsMain = {
transport,
baudrate: 921600,
enableTracing: false,
debugLogging: false,
terminal: espLoaderTerminal
}

let esploader = new ESPLoader(flashOptionsMain);

alert("Hold the IO0 button on the ESP32 until the green progress bar appears. Press OK on this message when you have started to hold the button.")

let esploader = new ESP32Loader(flashOptions);
document.getElementById("upload-progress").innerHTML = "0%"

// esploader.writeFlash();
setTimeout(() => {
if (document.getElementById("upload-progress").innerHTML === "0%") {
alert("It's taking too long to connect to the ESP32. This can happen if you weren't holding the IO0 button. Try refreshing the website and trying again.");
}
}, 5000);


await esploader.main();

const flashOptions = {
fileArray: fileArray,
flashSize: "keep",
eraseAll: false,
compress: true,
baudrate: 921600,
reportProgress: (fileIndex, written, total) => {
espLoaderTerminal.writeLine("PROGRESS:" + fileIndex + "," + written + "," + total);
document.getElementById("upload-progress").innerHTML = Math.floor(1 + (fileIndex * 10) + ((fileIndex < 3) ? 9 * (written / total) : 68 * (written / total))) + "%";
}
, calculateMD5Hash: (image) => CryptoJS.MD5(CryptoJS.enc.Latin1.parse(image))
};

await esploader.writeFlash(flashOptions);

await esploader.hardReset();

} catch (e) {
console.log(e);
document.getElementById("upload-progress").innerHTML = "Error!"
} finally {
//
try {
await device.close();
} catch (e) {
console.log("caught error closing device (it probably never opened");
console.log(e);
}
document.getElementById("upload-button").disabled = false;
}
}
upload_button.addEventListener("click", esp32UploadListenerFunction);

Expand All @@ -1428,12 +1519,20 @@ async function getCode() {
if (upload_button.hasAttribute("espUpload")) { // if in esp32 uploader mode not pico mode
return;
}
if (upload_button.disabled) {
return;
}
document.getElementById("upload-button").disabled = true;


document.getElementById("upload-info-under-button").innerHTML = 'You have now downloaded the file containing the program for the Raspberry Pi Pico! To upload it, follow these steps: <ol><li>Unplug the USB cable from your computer.</li><li>Hold down the "BOOTSEL" button on the Pico and plug the USB cable back into your computer without letting go of the button.</li><li> The Pico should show up as a drive called "RPI-RP2" on your computer. </li><li> You can now stop holding the "BOOTSEL" button. </li><li> Drag and drop the file you just downloaded onto the Pico. </li><li>Wait for the Pico to restart (the RPI-RP2 drive should disappear).</li><li> You have uploaded the program. Now continue with customizing the settings. </li></ol>';

document.getElementById("uploading-step-4").innerHTML = 'You have now downloaded the file containing the program for the Raspberry Pi Pico! To upload it, follow these steps: <ol><li>Unplug the USB cable from your computer.</li><li>Hold down the "BOOTSEL" button on the Pico and plug the USB cable back into your computer without letting go of the button.</li><li> The Pico should show up as a drive called "RPI-RP2" on your computer. </li><li> You can now stop holding the "BOOTSEL" button. </li><li> Drag and drop the file you just downloaded onto the Pico. </li><li>Wait for the Pico to restart (the RPI-RP2 drive should disappear).</li><li> You have uploaded the program. Now continue with customizing the settings. </li></ol>';
downloadFile(code, program + ".ino.uf2");
cbdone("hcbp-uploading", "hcbp-upload-done");
document.getElementById("upload-button").style.outline = "0px";

document.getElementById("upload-button").disabled = false;
}
upload_button.addEventListener("click", picoUploadListenerFunction);
} else {
Expand Down

0 comments on commit 24d92bb

Please sign in to comment.