TinyJS on mbed. TinyJS is very simple JavaScript engine.

Dependencies:   mbed

TinyJS on mbed

what's this ?

TinyJS is an extremely simple (under than 2000 lines) JavaScript interpreter engine.
I ported on mbed. but it restrict any features.
TinyJS project is https://github.com/gfwilliams/tiny-js

TinyJSは2000行以下で書かれた非常に小さいJavaScriptインタプリタエンジンです。
これをmbedに移植してみました。(ただし、いろいろ制限があります)
本家はこちら。 https://github.com/gfwilliams/tiny-js

how to use

You must use on serial terminal application by mbed serial USB.
baud is 57600bps
USBシリアルとして、ターミナルソフトを接続するとコンソールが表示されます。
ボーレートは57600bpsになってます。

functions

functions for mbed.
mbed用関数

  • mbed.DigitalIn(pinName, mode)
  • mbed.DigitalOut(pinName, val)
  • mbed.AnalogIn(pinName)
  • mbed.AnalogOut(pinName, val)
  • mbed.InterruptIn(pinName, edge, mode, callback)
  • mbed.TimerStart()
  • mbed.TimerStop()
  • mbed.TimerReset()
  • mbed.TimerRead()
  • mbed.Timeout(callback, t)
  • mbed.wait(s)
  • mbed.memfree()

sample JavaScript codes

DigitalOut

mbed.DigitalOut('LED1', 1);
mbed.DigitalOut('LED2', 0);
mbed.DigitalOut('LED3', 1);
mbed.DigitalOut('LED4', 0);

LED1 = On, LED2=Off, LED3=On, ED4=Off
LED1 = 点灯、LED2=消灯、LED3=点灯、LED4=消灯

DigitalIn

print(mbed.DigitalIn('p5', 'PullUp'));

p5 is pull up, read, and print on console.
p5をPullUpして読みプリントする。

AnalogOut

mbed.AnalogOut('p18', 0.8);

p18 is analog output, value is 0.8.
p18を 値0.8でアナログ出力する。

AnalogIn

print(mbed.AnalogIn('p20'));

p20 is read analog voltage, and print on console.
p20をアナログ入力しプリントする。

InterruptIn

var led1 = 0;
mbed.InterruptIn('p5', 'fall', 'PullUp', function() {led1 = !led1; mbed.DigitalOut('LED1', led1);});

Interrupt on p5, and ON/OFF does LED1.
p5で割り込んでLED1をON/OFFする。

Timeout and wait sample code

mbed.Timeout(function() {mbed.DigitalOut('LED1', 1);mbed.wait(3);mbed.DigitalOut('LED1', 0);}, 4);

LED1=on when wait for 4 seconds. and LED1=off for 3 seconds later.
LED1を4秒待って点灯して3秒後に消灯する。

memfree

print(mbed.memfree());

This prints the number of bytes of the remainder memory on mbed where TinyJS is usable.
これはTinyJSが使えるmbed上での残りメモリのバイト数をプリントアウトする。

LED Blinker by Timeout

blinker = function() {var led = 0; mbed.Timeout(function() {led = !led; mbed.DigitalOut('LED1', led);blinker();}, 0.5);};
blinker();

LED Blinker by Timeout.
Timeoutを使ったLチカ。

restrictions

  • There is very little available memory. (Less than 9kbytes on LPC1768)
  • Registration of InterruptIn is 4 limit.
  • The loop to 8,192 times.
  • The built-in functions (general JavaScript functions) that TinyJS prepares for for securing of memory is not included.

more, more, more ....

制限事項

  • 利用できるメモリは非常に少ない。(LPC1768で9kbytes以下)
  • InterruptInで登録できる割り込みは4つまで。4つを超えると1つめから順番に削除される。
  • ループは8192回まで。
  • メモリ確保のためTinyJSが用意している組み込み関数(一般的なJavaScript関数)は含まれない。

他、多数....

sample movies

http://www.youtube.com/watch?v=ARp0DK70JGM
http://www.youtube.com/watch?v=UOZQ4eEC4xA

Committer:
ohneta
Date:
Mon Jan 20 00:07:35 2014 +0000
Revision:
8:819934a27c2d
Parent:
6:30b4122b0ee2
update InterruptIn, Timer, Timeout functons

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ohneta 0:aae260bdcdd9 1 /*
ohneta 0:aae260bdcdd9 2 * TinyJS for mbed.
ohneta 0:aae260bdcdd9 3 *
ohneta 0:aae260bdcdd9 4 * Authored by Takehisa Oneta (ohneta@gmail.com)
ohneta 0:aae260bdcdd9 5 * 10th Jan. 2013
ohneta 0:aae260bdcdd9 6 */
ohneta 0:aae260bdcdd9 7
ohneta 1:d793f113cfc0 8 /*
ohneta 1:d793f113cfc0 9 * JavaScript samples
ohneta 1:d793f113cfc0 10 * for (x = 0; x < 256; x++) {for (var i = 0; i <= 3; i++) {mbed.DigitalOut(led1, (1 << i) & 0x01);mbed.DigitalOut(led2, (1 << i) & 0x02);mbed.DigitalOut(led3, (1 << i) & 0x04);mbed.DigitalOut(led4, (1 << i) & 0x08);for (j = 0; j < 300; j++) dummy=0;}for (var i = 3; i >= 0; i--) {mbed.DigitalOut(led1, (1 << i) & 0x01);mbed.DigitalOut(led2, (1 << i) & 0x02);mbed.DigitalOut(led3, (1 << i) & 0x04);mbed.DigitalOut(led4, (1 << i) & 0x08);for (j = 0; j < 10; j++) dummy=0;}};
ohneta 1:d793f113cfc0 11 */
ohneta 1:d793f113cfc0 12
ohneta 0:aae260bdcdd9 13 #include "mbed.h"
ohneta 0:aae260bdcdd9 14 #include "TinyJS.h"
ohneta 3:f56c36ea8266 15 #include "Mbed_Functions.h"
ohneta 0:aae260bdcdd9 16
ohneta 0:aae260bdcdd9 17 Serial pc(USBTX, USBRX);
ohneta 0:aae260bdcdd9 18
ohneta 0:aae260bdcdd9 19 //unsigned char usbArea[1024] __attribute__((section("AHBSRAM0")));
ohneta 0:aae260bdcdd9 20 //unsigned char ethArea[1024] __attribute__((section("AHBSRAM1")));
ohneta 0:aae260bdcdd9 21
ohneta 0:aae260bdcdd9 22
ohneta 0:aae260bdcdd9 23 extern int tinyjs_main(int argc, char **argv);
ohneta 0:aae260bdcdd9 24
ohneta 0:aae260bdcdd9 25 //---------------------------------------------
ohneta 0:aae260bdcdd9 26
ohneta 0:aae260bdcdd9 27 int readOneLine(char *buffer, const int bufferSize)
ohneta 0:aae260bdcdd9 28 {
ohneta 0:aae260bdcdd9 29 int len = 0;
ohneta 0:aae260bdcdd9 30
ohneta 0:aae260bdcdd9 31 buffer[0] = '\0';
ohneta 0:aae260bdcdd9 32 while (true) {
ohneta 0:aae260bdcdd9 33 char c = pc.getc();
ohneta 0:aae260bdcdd9 34 pc.putc(c);
ohneta 0:aae260bdcdd9 35
ohneta 0:aae260bdcdd9 36 if ('\r' == c) {
ohneta 0:aae260bdcdd9 37 return len;
ohneta 0:aae260bdcdd9 38 } else if( '\n' == c ) {
ohneta 0:aae260bdcdd9 39 } else {
ohneta 0:aae260bdcdd9 40 buffer[len] = c;
ohneta 0:aae260bdcdd9 41 buffer[len + 1] = '\0';
ohneta 0:aae260bdcdd9 42 len++;
ohneta 0:aae260bdcdd9 43 if (len > bufferSize) {
ohneta 0:aae260bdcdd9 44 return len;
ohneta 0:aae260bdcdd9 45 }
ohneta 0:aae260bdcdd9 46 }
ohneta 0:aae260bdcdd9 47 }
ohneta 0:aae260bdcdd9 48
ohneta 0:aae260bdcdd9 49 return len;
ohneta 0:aae260bdcdd9 50 }
ohneta 0:aae260bdcdd9 51
ohneta 0:aae260bdcdd9 52 //---------------------------------------------
ohneta 0:aae260bdcdd9 53 //---------------------------------------------
ohneta 0:aae260bdcdd9 54
ohneta 0:aae260bdcdd9 55 int main() {
ohneta 0:aae260bdcdd9 56 pc.baud(57600);
ohneta 0:aae260bdcdd9 57
ohneta 0:aae260bdcdd9 58 while(1) {
ohneta 0:aae260bdcdd9 59 printf("\n");
ohneta 0:aae260bdcdd9 60 printf("--------------------------\n");
ohneta 6:30b4122b0ee2 61 printf("TinyJS on mbed\n");
ohneta 0:aae260bdcdd9 62
ohneta 0:aae260bdcdd9 63 tinyjs_main(NULL, NULL);
ohneta 0:aae260bdcdd9 64
ohneta 0:aae260bdcdd9 65 printf("--------------------------\n");
ohneta 0:aae260bdcdd9 66 printf("bye bye\n");
ohneta 0:aae260bdcdd9 67 }
ohneta 0:aae260bdcdd9 68 }