Jeff Atwood, co-founder of Stack OverFlow, famously called it “Atwood’s Law,” which states:

Any application that can be written in JavaScript, will eventually be written in JavaScript.

Any application that can be implemented in JavaScript will eventually be implemented in JavaScript.

There is no doubt that JavaScript has become one of the most popular programming languages in the world. There is no need to dispute this point. Recently,hackereartJust published a blog post describing the use of major programming languages in 2016:

They therefore predict that this year’s most popular language will be:

  • JavaScript
  • Java
  • Python
  • PHP
  • R
  • Matlab
  • Arduino
  • Swift

JavaScript is one of the most popular programming languages in the world, and it has expanded its reach into many new and exciting ways of doing things. Here I try to explore some of JavaScript’s lesser-known ways:

Espruino

Espruino is a JavaScript interpreter designed specifically for microcontrollers (MCUS), with a minimum resource overhead of 128KB Flash & 8KB RAM, open source using the MPL-2.0 protocol.

Author Gordon Williams is such a generalist that he recently designed a piece of hardware called Puck.js to support Espruino.

On the shoulder of the giant, I made some modifications to make it compatible with my own developed hardware. I have submitted the modified code to GitHub, and you can clone it for fun.

Star

$git clone https://github.com/makerdiary/Espruino.git $CD Espruino # will YS - Beacon to connect to PC make $YS_BEACON = 1 RELEASE = 1 Flash # the terminal pops up with a bunch of characters, the blue light on the board flashes, and everything is easy... [====================] 100% DEBUG:root:reset stop on reset INFO:root:Programmed 446464 bytes (109 pages) at 14.56 kB/s DEBUG:root:uninit board <pyOCD.board.mbed_board.MbedBoard object at 0x1025e8a90> DEBUG:root:closing interfaceCopy the code

Espruino also has a companion development tool, the Espruino Web IDE, which can be used to edit code, download programs, and even for graphical programming. The following is a simple implementation of LED flashing code, there is a sense of deja vu:

var on = false; setInterval(function() { on = ! on; LED1.write(on); }, 500);Copy the code

Interestingly, Espruino hardware runs a JavaScript interpreter. JavaScript code uploaded to Espruino is only stored in RAM and lost after a power failure. This is exactly the same as the browser, which also shows the characteristics of dynamic parsing.

JerryScript

If Espruino is a bit toyish, JerryScript is more productized, and JerryScript has a low resource overhead of less than 64KB of RAM and less than 200KB of ROM.

You can’t talk about JerryScript without iot.JS and Samsung, and their “triangle” goes like this:

Iot.js is an Internet of Things application platform written in JavaScript; JerryScript is a small JavaScript engine for embedded devices; Samsung has open-source iot.js and JerryScript.

The entire internal architecture is as follows:

The following snippet of code shows the basic JerryScript workflow: initialize the engine → parse the JavaScript code → execute the code → end the run and free the memory.

{ jerry_init(JERRY_FLAG_ENABLE_LOG); char script[] = "print ('Hello, World! ');" ; jerry_parse(script, strlen(script)); jerry_run(); jerry_cleanup(); }Copy the code

Looking at the source code for JerryScript, you can see that it already runs on some RTOS (Zephyr, MBED OS, etc.). At the MBED Connect Asia 2016 conference in Shenzhen last year, Jan Jongboom said that he had brought JerryScript to MBED OS 5 and introduced a few simple examples.

Now that MBED OS 5 is supported, it is much easier to support your own hardware. As for setting up the development environment, you can refer to README, which is not difficult to implement.

$git clone https://github.com/YS-Beacon/mbed-js-example.git $CD mbed-js-example CNPM install $NPM install # gulp is used to obtain JerryScript source code . / build/jerryscript/the targets/mbedos5 / mbed - OS $git checkout master $git pull # from my warehouse back to the relevant target hardware configuration file $git remote set - the url Origin https://github.com/makerdiary/mbed-os.git $git pull # specify the target board, automatic compilation $gulp -- target = YS_BEACONCopy the code

Take a look at the source code of the application written by JavaScript, is there a kind of strange and familiar feeling:

// blink_leds.js var led = DigitalOut(LED1); var blink = function() { led.write(led.read() ? 0:1); print("blink! LED is now " + led.read()); }; module.exports = blink; // main.js var blink = require('./blink_leds'); setInterval(function() { blink(); }, 1000);Copy the code

Will compilembedos5.hexDownload the file to the target board and see if it is the result you want:

Blockly

If you dread programming languages or blinking cursors in command lines, Blockly may be your salvation and make you fun. BlocklyIs a JavaScript library developed by Google and open source, used to achieve graphical programming, just drag some variables, expressions, loops and other meanings of the graphical block, combined together to complete programming, isn’t it cool?

In fact, Blockly does only one thing: visual editing, generating code. Blockly does not care about the behavior behind the code, which leaves developers with a lot of imagination. For example, Ozobot developed an intelligent robot toy, and players can use Blockly to program the behavior of the robot:


Isn’t it fun? Unfortunately, there are no robots. However, you can experience Blockly first with Espruino and then build your own bots step by step (yes, the aforementioned Espruino is Blockly enabled) :

To be continued

Haven’t you had a good time yet? More interesting things, I am still exploring, I will learn to share, interested friends can pay a little attention to it.