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
Mbed_Functions.h@7:40010b4dc51d, 2014-01-19 (annotated)
- Committer:
- ohneta
- Date:
- Sun Jan 19 18:08:47 2014 +0000
- Revision:
- 7:40010b4dc51d
- Parent:
- 6:30b4122b0ee2
changed function name: SetInterrupt() to InterruptIn()
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ohneta | 3:f56c36ea8266 | 1 | /* |
ohneta | 3:f56c36ea8266 | 2 | * TinyJS for mbed. |
ohneta | 3:f56c36ea8266 | 3 | * Mbed_Functions.h |
ohneta | 3:f56c36ea8266 | 4 | * |
ohneta | 3:f56c36ea8266 | 5 | * Authored by Takehisa Oneta (ohneta@gmail.com) |
ohneta | 7:40010b4dc51d | 6 | * 17th Jan. 2014 - |
ohneta | 3:f56c36ea8266 | 7 | */ |
ohneta | 3:f56c36ea8266 | 8 | |
ohneta | 3:f56c36ea8266 | 9 | #include "mbed.h" |
ohneta | 3:f56c36ea8266 | 10 | |
ohneta | 3:f56c36ea8266 | 11 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 12 | void mbedDigitalIn(CScriptVar *c, void *); |
ohneta | 3:f56c36ea8266 | 13 | void mbedDigitalOut(CScriptVar *c, void *); |
ohneta | 3:f56c36ea8266 | 14 | void mbedAnalogIn(CScriptVar *c, void *); |
ohneta | 3:f56c36ea8266 | 15 | void mbedAnalogOut(CScriptVar *c, void *); |
ohneta | 7:40010b4dc51d | 16 | void mbedInterruptIn(CScriptVar *c, void *data); |
ohneta | 6:30b4122b0ee2 | 17 | void mbedWait(CScriptVar *c, void *); |
ohneta | 6:30b4122b0ee2 | 18 | void mbedTimerStart(CScriptVar *c, void *); |
ohneta | 6:30b4122b0ee2 | 19 | void mbedTimerStop(CScriptVar *c, void *); |
ohneta | 6:30b4122b0ee2 | 20 | void mbedTimerReset(CScriptVar *c, void *); |
ohneta | 6:30b4122b0ee2 | 21 | void mbedTimerRead(CScriptVar *c, void *); |
ohneta | 6:30b4122b0ee2 | 22 | |
ohneta | 3:f56c36ea8266 | 23 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 24 | void mbedMemfree(CScriptVar *c, void *); |
ohneta | 3:f56c36ea8266 | 25 | |
ohneta | 3:f56c36ea8266 | 26 | //--------------------------------------------- |
ohneta | 7:40010b4dc51d | 27 | void registerMbedFunctions(CTinyJS *tinyJS); |
ohneta | 3:f56c36ea8266 | 28 | PinName _mbedPinNameExchange(string pinNameString); |
ohneta | 7:40010b4dc51d | 29 | |
ohneta | 7:40010b4dc51d | 30 | //--------------------------------------------- |