-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storing usercode in repl-accessible file #38
Comments
The user code is stored directly in flash not in file system. Please see https://kalumajs.org/docs/boards/pico#flash |
Ok, so I found out how it's possible! Let's consider 2 files:
So // flash this file once to Kaluma. It in turn loads index.js stored in user-accessible /
let fs = require('fs')
function writeFile (fileName, string) {
return fs.writeFile(fileName, new TextEncoder().encode(string))
}
function readFile (fileName) {
return new TextDecoder().decode(fs.readFile(fileName))
}
function loadFile (fileName) {
try {
let code = readFile(fileName)
let func = new Function(code)
func()
} catch (e) {
console.log('No index.js to load or other error')
console.error(e)
}
}
if (fs.exists('/index.js')) loadFile('/index.js') The functions I've tried this with very short files and it works perfectly and ridiculously fast. With the writeFile('/index.js', `setInterval(() => console.log('hi'), 100)`)
.load ... this of course persists a reboot. When trying with larger files I'm currently getting a If this can work with larger files, then |
Ok so this works! Using this code means the code sent to the Pico runs pretty much instantly (vs several seconds using the ymodem implementation). // Flash this file once to Kaluma
// It in turn loads index.js, which users can update much quicker
let usercode = (() => {
let fs = require('fs')
let file = null
let code = ''
function transferStart (fileName = '/index.js') {
file = fs.open(fileName, 'w')
console.log('Transferring code...')
}
function transferChunk (codeChunk) {
fs.write(file, new TextEncoder().encode(codeChunk))
}
function transferEnd () {
fs.close(file)
console.log('... transfer done.')
}
function load (fileName = '/index.js') {
code = new TextDecoder().decode(fs.readFile(fileName))
try {
// let code = usercode.load('./index.js')
;(1, eval)(code) // need it like this so eval'd code is in global scope
} catch (e) {
console.log('Error with user code')
console.log(e)
}
return code
}
function show () {
console.log('\n----------- BEGIN CODE -----------\n')
console.log(code)
console.log('------------ END CODE ------------\n')
}
return { transferStart, transferChunk, transferEnd, load, show }
})();
usercode.load() One disadvantage is that if you use the |
I've implemented this in full here: https://www.mathsuniverse.com/pico Once connected to the Pico, the With that bootloader installed, the 'Run on Pico' button is ridiculously fast! Better would be if this kind of bootloader/flash.js is installed by default as part of the Kaluma .uf2, saving the 'install bootloader' step. Feel free to 'view source' on the link above - it's just a single ~650 line file with all JS inlined and no external resources. |
I just made a new video showing it in action. https://youtu.be/J1tOiVSxBqY?si=yCRjoMEp8UXaIZVN&t=69 (jumps to 1min9s to see how fast it is). Using ymodem: ~4s |
Running
.ls
shows no files, and.pwd
shows we're in root. Where is the usercode which is sent to the Pi Pico viakaluma flash -w
stored?If putting MicroPython on the Pi Pico, the usercode is stored in
main.py
in the root. It would be very useful if Kaluma did something similar (suggestindex.js
instead). The.ls
command etc would then make more sense.I'm asking because I'm thinking of flashing files to Kaluma in a similar way to how it works with MicroPython, which is much quicker!
Try https://www.mathsuniverse.com/pico for Kaluma JS and https://www.mathsuniverse.com/pico/python.html for Python. Try flashing a file with both and note how much quicker it is with Python.
With Kaluma JS, the file is sent using a ymodem implementation. With MicroPython, raw repl mode is entered and the code is just pasted in, then saved to
main.py
by MicroPython itself, then a command is then sent to soft-reset.So I'm thinking if the usercode is stored in the repl-accessible folder as
index.js
then we could use a similar method, usingfs = require('fs')
etc. The ymodem implementation could then be deprecated. My guess is that this would be much faster as well, like it is with MicroPython.The text was updated successfully, but these errors were encountered: