JavaScript for Microcontrollers
github.com
github.com
Compiling expressive syntax through a powerful optimizing compiler to produce binaries run by microcontrollers is really sweet. Definitely the way to go, if you want to get the most functionality from limited hardware, without going all the way to assembly.
I guess JavaScript could be good for people who want to do very simple things, and then invest lots of time fighting limitations as their ambitions grow, rather than learning something useful. Like people do with Excel.
Which brings me to my next question: why not take it to the limit, and make microcontrollers programmable purely in Excel macros? That would be maximally accessible. Lots of people know Excel and can’t get enough pain.
function increment() {
var out = []
for (let i of [1,2,3,4,5]) {
out.push(i + 1)
}
return out
}
vs ES6: const increment = () => [1,2,3,4,5].map(i => i + 1)
etc. Most people's experience with JS/TS has simply been before ES6 (which had massive problems, like `var` and `this` scoping etc.), it's really nice nowadays. var increment = [1, 2, 3, 4, 5].map( function(num){ return num + 1 } )
The two big syntactic wins of modern JS are let/const over the filth that was var and the convenience of arrow functions over anonymous function literals. var increment = function() {
return [1,2,3,4,5].map(function(num) {
return num + 1;
});
};
Moreover, modern JS is not merely convenient syntactic sugar over old JS. Let/const have different scopes, arrow functions have no bindable context.You also now have classes, proxies, async/await, generators, destructuring, default/rest/spread parameters, ...
Add typing via jsdocs or TypeScript and - like the parent comment said - "it's really nice".
Often people mistake JS for a mere DOM manipulation language and think of
document.getElementById('myDiv').innerHTML = 'Hello World';
instead of what it has become today.I still hate it though for sorting arrays with only number types lexically:
[1,2,10,20].sort(); //[1,10,2,20]In fact, your example suffers from the same problem: It sorts in descending order (20,10,2,1) instead of ascending (1,2,10,20).
Telling JS to use a TypedArray [Int8,...,BigInt64] instead can solve this without remembering and providing your own comparator:
Float64Array.from([1,2,10,20]).sort(); //[1,2,10,20]I will concede that I failed to write a function definition, thanks for catching that. I guess I unintentionally refactored the outer function away because development brain took over? Sloppy on my part, regardless.
The point was that modern JS is more convenient to use: Const/let, arrows, classes, proxies, async/await, generators, destructuring, default/rest/spread parameters, etc.
Even if you discard const/let/arrows as merely syntactic improvements (which they are not), you should not ignore the others - unless of course they are all syntactic improvements with added features at which point "syntax" lost its "meaning".
I.e. due to the lack of typing this will fly just fine:
const calculateInterestingNumber = () <calculations goes here>;
let twiceAsInterestingNumber = 2 * calculateInterestingNumber; // (ooops, missing parenthesis)
... until runtime.
This was just the silliest contrived example.
In practice this - as you are fixing someone else's code and misplace/forget a set of parenthesis - ends up taking 30 minutes to figure out.
Sometimes reading comments about what microcontrollers aren't able to do in 2020 looks like cargo cult of people lacking the experience of how it was like to code with 640 KB.
I highlight that. In fact, Rust is another kid on the block that is very promising for embedding.
In the other hand, sorry but I simply don't get why JS for interactions on such highly constrained devices. Probably as you said, it's intended to do very simple things with less effort as possible.
If nothing else, it seems like a good idea to make microcontroller programming more accessible using one of the most used languages.
I'm not sure arguments regarding performance, or how readily available learning resources are for lower-level programming languages, are relevant here because those are likely not the concerns for anyone who wish to take advantage of frameworks like the one in question.
To many people the programming part is just a means to an end: for example, making LEDs blink for costumes. So why not celebrate when there are more accessible solutions available to them?
Anybody can take garageband and slap some autotuned vocals on a track and make a song. That does not make the average person a skilled musician. Musicians that have taken the years of practice to get that tone just right on their cello are not going to have a lot of respect for someone that achieves it pushing some buttons on their midi keyboard.
You are probably right though. I don't know if it really matters how the sausage is made.
I believe we owe the vitality of the software business to the fact that we never rationed, or maintained gatekeepers for, software development.
Disclosure: I play the cello. The lesson from the music business is that the definition of a skilled musician is no consolation when someone lowers the barrier to entry with technology (electric guitar & bass, scratch turntable, sampling, PC based recording, autotune).
Espruino: https://www.espruino.com/
JerryScript: https://jerryscript.net/
Running javascript on microcontrollers is surprisingly popular. Personally I keep returning to C, but these frameworks can be nice when they "just work".
Does this framework support general-purpose SPI communication, or only SPI displays?
Thats a pretty huge caveat.
Forty (40) years ago, children were writing assembly on home computers! No internet to run to for help, back then, but somehow, millions of kids learned how to do it.
Today, with all the resources available, assembly is "scary," and "too hard to teach."
I call BS on this notion. Complete BS.
JavaScript doesn't even really belong where it is on desktops and laptops. The very last place it belongs is on a constrained platform.
I doubt that there were millions in any single country, but over the entire world, I would be very surprised if "millions" is not accurate. (Precision is another discussion)
People learn much faster and better in groups than as individuals.
IIRC, that compares reasonably well with a 486 or Pentium computer running a single application, especially when you realize it's not running a GUI. A decent amount of software of that era was inteprreted - that's when Javascript and Python first came into existence.
Then tell me if you think any owner of a 486 would have tolerated that level of performance.
Worse, we even had interpreted Java applets, with the JIT a couple of years in the future to come.
Even today, JavaScript is 1/20th the speed of native code at best.
Imagine something dragging at 1/20th the speed of MS Office on a 486.
Naturally not, however not everyone was wealthy to be buying last generation Pentiums.
That’s not a knock against assembly, or to say that the same can’t be done in C with time and skill. But in many cases, compromising performance for developer efficiency is a good call.
>>> Bring your existing experience with JavaScript, but be prepared to think about performance, code size, and memory use in a different way.
I think the use case is if your application would actually benefit from the Javascript language, either because of what it can do, or to make something readable and maintainable in a way that might be hard with raw assembly.
In a similar vein, I played with CircuitPython for a while, and went back to C for my microcontroller projects. My programs tend to feed data up to a desktop computer, typically into the loving arms of a Python program.
I got into computers almost exactly 40 years ago, and the earliest exposure of most kids to programming was in BASIC. I read a book on the Z80 microprocessor, and felt that I could have at least taken a crack at assembly, but didn't have access to a Z80 machine.
The first programming I did was as a kid when I taught myself basic on an apple II+ after I saw a joystick and a manual sitting next to my dad’s computer. ...it looked a bit like my Atari console and I was curious. (I too eventually wrote assembly - you had to in those days - but it wasn’t the introduction to programming for anyone I knew.)
This led to other things, including the robotics company I started, where we are, as I write this, running JS on an esp32 (among other things), hoping to inspire someone else to discover programming as I did - through play and fun.
To me, JS is kinda like basic was when I was a kid. ...the first step on a path that could surely include assembly, ML, distributed systems, robotics, computer vision, etc.
But what should step 1 be for kids today? I have a 6 year old, and all I know is she’s growing up in a different world than I did. ;-)
Yours fondly. Oneupyou McGill
We aren't losing anything here.
Last time I checked, the majority of those kids were using BASIC. Specifically: Commodore BASIC or QBasic (DOS).
And Javascript is the modern version of BASIC: widely supported, well integrated into all modern OSes (phone, tablet, laptop, desktop), and capable of doing all of the graphics that a modern nascent programmer wishes.
Assembly wasn't that hard on those old things, so why is so much time spent today shoehorning JavaScript into places it shouldn't go? Spend that effort on something useful, or at least something sane.
> Assembly wasn't that hard on those old things
Assembly is harder than BASIC, and its harder than Javascript.
You can go to any store and pick out a beginner Javascript book these days. But how many Assembly tutorials or books are there?
Back in the 80s, it was more about Commodore BASIC books being distributed with the Commodore itself: ftp://www.zimmers.net/pub/cbm/c64/manuals/C64_Users_Guide.pdf
Pre-internet, it was books like these that allowed programmers to learn and grow.
Python turned into an abominable hot mess over the years due to lack of direction, and nobody really wants to program in JavaScipt willingly. It's something we have to put up with.
Sadly, there is no equivalent to BASIC in 2020.
Imagine dumping webpack, DOM and CSS on an unsuspecting kid today. Or even virtualenv and pip.
I know I'd "nope" out of there as fast as I could if that was me in 2020.
I'd love for you to back that up with some kind of logic.
What's the point in using a language which isn't quite sure what an integer is when you can just use C. It's not that hard.
The difficult part is all the crap in the toolchains not the code itself.
But C has way more "wat" in it than JS, in a sense. In JS when you make a wat you can generally debug it and fix it incredibly easily. In C when you wat, well, good luck.
A linter and Typescript and the fact that the "wat" stuff is actually almost always marginal and not commonly ran into in practice means you actually almost never are spending time on wat-ness at all in TS, where in C, a huge amount of time is spent there.
There's a vast gulf of productivity between TS and C (the former winning). TS is an incredibly smart choice for many things small and large. But yes, if you need a lot of low level hooks and incredible performance, don't use it.
Maybe interpreters are too much for µC, I think the most painful disadvantage of C is string manipulation and trends show a preference for human readable protocols for serial communication. For prototyping even JS might have use case though.
If the bar for entry into assembly programming in 1980 was so low that a child could do it, why are we thinking that we should lower the bar further today? Spend that effort on something which will actually help someone, rather than putting JavaScript on a microcontroller.
And yes, if you wanted to do anything really significant, you needed to learn assembly. But frankly most did not bother.
I'm not gatekeeping. I'm saying that we spend a lot of effort to lower the bar to software development when it is already very low. It is certainly already low enough that anyone can become a software developer. I would like to see that effort used for something more productive.