TinyJS on mbed. TinyJS is very simple JavaScript engine.
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
TinyJS_Functions.h@8:819934a27c2d, 2014-01-20 (annotated)
- Committer:
- ohneta
- Date:
- Mon Jan 20 00:07:35 2014 +0000
- Revision:
- 8:819934a27c2d
- Parent:
- 0:aae260bdcdd9
update InterruptIn, Timer, Timeout functons
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ohneta | 0:aae260bdcdd9 | 1 | /* |
ohneta | 0:aae260bdcdd9 | 2 | * TinyJS |
ohneta | 0:aae260bdcdd9 | 3 | * |
ohneta | 0:aae260bdcdd9 | 4 | * A single-file Javascript-alike engine |
ohneta | 0:aae260bdcdd9 | 5 | * |
ohneta | 0:aae260bdcdd9 | 6 | * Authored By Gordon Williams <gw@pur3.co.uk> |
ohneta | 0:aae260bdcdd9 | 7 | * |
ohneta | 0:aae260bdcdd9 | 8 | * Copyright (C) 2009 Pur3 Ltd |
ohneta | 0:aae260bdcdd9 | 9 | * |
ohneta | 0:aae260bdcdd9 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
ohneta | 0:aae260bdcdd9 | 11 | * this software and associated documentation files (the "Software"), to deal in |
ohneta | 0:aae260bdcdd9 | 12 | * the Software without restriction, including without limitation the rights to |
ohneta | 0:aae260bdcdd9 | 13 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
ohneta | 0:aae260bdcdd9 | 14 | * of the Software, and to permit persons to whom the Software is furnished to do |
ohneta | 0:aae260bdcdd9 | 15 | * so, subject to the following conditions: |
ohneta | 0:aae260bdcdd9 | 16 | |
ohneta | 0:aae260bdcdd9 | 17 | * The above copyright notice and this permission notice shall be included in all |
ohneta | 0:aae260bdcdd9 | 18 | * copies or substantial portions of the Software. |
ohneta | 0:aae260bdcdd9 | 19 | |
ohneta | 0:aae260bdcdd9 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
ohneta | 0:aae260bdcdd9 | 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
ohneta | 0:aae260bdcdd9 | 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
ohneta | 0:aae260bdcdd9 | 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
ohneta | 0:aae260bdcdd9 | 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ohneta | 0:aae260bdcdd9 | 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
ohneta | 0:aae260bdcdd9 | 26 | * SOFTWARE. |
ohneta | 0:aae260bdcdd9 | 27 | */ |
ohneta | 0:aae260bdcdd9 | 28 | |
ohneta | 0:aae260bdcdd9 | 29 | #ifndef TINYJS_FUNCTIONS_H |
ohneta | 0:aae260bdcdd9 | 30 | #define TINYJS_FUNCTIONS_H |
ohneta | 0:aae260bdcdd9 | 31 | |
ohneta | 0:aae260bdcdd9 | 32 | #include "TinyJS.h" |
ohneta | 0:aae260bdcdd9 | 33 | |
ohneta | 0:aae260bdcdd9 | 34 | /// Register useful functions with the TinyJS interpreter |
ohneta | 0:aae260bdcdd9 | 35 | extern void registerFunctions(CTinyJS *tinyJS); |
ohneta | 0:aae260bdcdd9 | 36 | |
ohneta | 0:aae260bdcdd9 | 37 | #endif |
ohneta | 0:aae260bdcdd9 | 38 |