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
Script.cpp@8:819934a27c2d, 2014-01-20 (annotated)
- Committer:
- ohneta
- Date:
- Mon Jan 20 00:07:35 2014 +0000
- Revision:
- 8:819934a27c2d
- Parent:
- 3:f56c36ea8266
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 | /* |
ohneta | 0:aae260bdcdd9 | 30 | * This is a simple program showing how to use TinyJS |
ohneta | 0:aae260bdcdd9 | 31 | */ |
ohneta | 0:aae260bdcdd9 | 32 | |
ohneta | 0:aae260bdcdd9 | 33 | /* |
ohneta | 0:aae260bdcdd9 | 34 | * TinyJS for mbed. |
ohneta | 0:aae260bdcdd9 | 35 | * |
ohneta | 0:aae260bdcdd9 | 36 | * Authored by Takehisa Oneta (ohneta@gmail.com) |
ohneta | 0:aae260bdcdd9 | 37 | * 10th Jan. 2013 |
ohneta | 0:aae260bdcdd9 | 38 | */ |
ohneta | 0:aae260bdcdd9 | 39 | #include "TinyJS.h" |
ohneta | 0:aae260bdcdd9 | 40 | #include "TinyJS_Functions.h" |
ohneta | 3:f56c36ea8266 | 41 | #ifdef MBED |
ohneta | 3:f56c36ea8266 | 42 | #include "Mbed_Functions.h" |
ohneta | 3:f56c36ea8266 | 43 | #endif |
ohneta | 3:f56c36ea8266 | 44 | |
ohneta | 0:aae260bdcdd9 | 45 | #include <assert.h> |
ohneta | 0:aae260bdcdd9 | 46 | #include <stdio.h> |
ohneta | 0:aae260bdcdd9 | 47 | |
ohneta | 0:aae260bdcdd9 | 48 | //const char *code = "var a = 5; if (a==5) a=4; else a=3;"; |
ohneta | 0:aae260bdcdd9 | 49 | //const char *code = "{ var a = 4; var b = 1; while (a>0) { b = b * 2; a = a - 1; } var c = 5; }"; |
ohneta | 0:aae260bdcdd9 | 50 | //const char *code = "{ var b = 1; for (var i=0;i<4;i=i+1) b = b * 2; }"; |
ohneta | 0:aae260bdcdd9 | 51 | const char *code = "function myfunc(x, y) { return x + y; } var a = myfunc(1,2); print(a);"; |
ohneta | 0:aae260bdcdd9 | 52 | |
ohneta | 0:aae260bdcdd9 | 53 | void js_print(CScriptVar *v, void *userdata) { |
ohneta | 0:aae260bdcdd9 | 54 | printf("> %s\n", v->getParameter("text")->getString().c_str()); |
ohneta | 0:aae260bdcdd9 | 55 | } |
ohneta | 0:aae260bdcdd9 | 56 | |
ohneta | 0:aae260bdcdd9 | 57 | void js_dump(CScriptVar *v, void *userdata) { |
ohneta | 0:aae260bdcdd9 | 58 | CTinyJS *js = (CTinyJS*)userdata; |
ohneta | 0:aae260bdcdd9 | 59 | js->root->trace("> "); |
ohneta | 0:aae260bdcdd9 | 60 | } |
ohneta | 0:aae260bdcdd9 | 61 | |
ohneta | 0:aae260bdcdd9 | 62 | #ifndef MBED |
ohneta | 0:aae260bdcdd9 | 63 | int main(int argc, char **argv) |
ohneta | 0:aae260bdcdd9 | 64 | { |
ohneta | 0:aae260bdcdd9 | 65 | CTinyJS *js = new CTinyJS(); |
ohneta | 0:aae260bdcdd9 | 66 | /* add the functions from TinyJS_Functions.cpp */ |
ohneta | 0:aae260bdcdd9 | 67 | registerFunctions(js); |
ohneta | 0:aae260bdcdd9 | 68 | /* Add a native function */ |
ohneta | 0:aae260bdcdd9 | 69 | js->addNative("function print(text)", &js_print, 0); |
ohneta | 0:aae260bdcdd9 | 70 | js->addNative("function dump()", &js_dump, js); |
ohneta | 0:aae260bdcdd9 | 71 | /* Execute out bit of code - we could call 'evaluate' here if |
ohneta | 0:aae260bdcdd9 | 72 | we wanted something returned */ |
ohneta | 0:aae260bdcdd9 | 73 | try { |
ohneta | 0:aae260bdcdd9 | 74 | js->execute("var lets_quit = 0; function quit() { lets_quit = 1; }"); |
ohneta | 0:aae260bdcdd9 | 75 | js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");"); |
ohneta | 0:aae260bdcdd9 | 76 | } catch (CScriptException *e) { |
ohneta | 0:aae260bdcdd9 | 77 | printf("ERROR: %s\n", e->text.c_str()); |
ohneta | 0:aae260bdcdd9 | 78 | } |
ohneta | 0:aae260bdcdd9 | 79 | |
ohneta | 0:aae260bdcdd9 | 80 | while (js->evaluate("lets_quit") == "0") { |
ohneta | 0:aae260bdcdd9 | 81 | char buffer[2048]; |
ohneta | 0:aae260bdcdd9 | 82 | fgets ( buffer, sizeof(buffer), stdin ); |
ohneta | 0:aae260bdcdd9 | 83 | try { |
ohneta | 0:aae260bdcdd9 | 84 | js->execute(buffer); |
ohneta | 0:aae260bdcdd9 | 85 | } catch (CScriptException *e) { |
ohneta | 0:aae260bdcdd9 | 86 | printf("ERROR: %s\n", e->text.c_str()); |
ohneta | 0:aae260bdcdd9 | 87 | } |
ohneta | 0:aae260bdcdd9 | 88 | } |
ohneta | 0:aae260bdcdd9 | 89 | delete js; |
ohneta | 0:aae260bdcdd9 | 90 | #ifdef _WIN32 |
ohneta | 0:aae260bdcdd9 | 91 | #ifdef _DEBUG |
ohneta | 0:aae260bdcdd9 | 92 | _CrtDumpMemoryLeaks(); |
ohneta | 0:aae260bdcdd9 | 93 | #endif |
ohneta | 0:aae260bdcdd9 | 94 | #endif |
ohneta | 0:aae260bdcdd9 | 95 | return 0; |
ohneta | 0:aae260bdcdd9 | 96 | } |
ohneta | 0:aae260bdcdd9 | 97 | #else |
ohneta | 0:aae260bdcdd9 | 98 | |
ohneta | 0:aae260bdcdd9 | 99 | int mbedErrorFlag; |
ohneta | 0:aae260bdcdd9 | 100 | std::string mbedErrorMessage; |
ohneta | 0:aae260bdcdd9 | 101 | |
ohneta | 0:aae260bdcdd9 | 102 | extern int readOneLine(char *buffer, const int bufferSize); |
ohneta | 0:aae260bdcdd9 | 103 | |
ohneta | 0:aae260bdcdd9 | 104 | int tinyjs_main(int argc, char **argv) |
ohneta | 0:aae260bdcdd9 | 105 | { |
ohneta | 0:aae260bdcdd9 | 106 | CTinyJS *js = new CTinyJS(); |
ohneta | 0:aae260bdcdd9 | 107 | //registerFunctions(js); |
ohneta | 0:aae260bdcdd9 | 108 | |
ohneta | 0:aae260bdcdd9 | 109 | js->addNative("function print(text)", &js_print, 0); |
ohneta | 0:aae260bdcdd9 | 110 | js->addNative("function dump()", &js_dump, js); |
ohneta | 0:aae260bdcdd9 | 111 | |
ohneta | 0:aae260bdcdd9 | 112 | js->execute("var lets_quit = 0; function quit() { lets_quit = 1; }"); |
ohneta | 0:aae260bdcdd9 | 113 | js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");"); |
ohneta | 0:aae260bdcdd9 | 114 | |
ohneta | 0:aae260bdcdd9 | 115 | // add mbed functions |
ohneta | 3:f56c36ea8266 | 116 | registerMbedFunctions(js); |
ohneta | 3:f56c36ea8266 | 117 | |
ohneta | 0:aae260bdcdd9 | 118 | |
ohneta | 0:aae260bdcdd9 | 119 | while (js->evaluate("lets_quit") == "0") { |
ohneta | 0:aae260bdcdd9 | 120 | char buffer[2048]; |
ohneta | 0:aae260bdcdd9 | 121 | int len = readOneLine(buffer, 2048); |
ohneta | 0:aae260bdcdd9 | 122 | { |
ohneta | 0:aae260bdcdd9 | 123 | mbedErrorFlag = 0; |
ohneta | 0:aae260bdcdd9 | 124 | js->execute(buffer); |
ohneta | 0:aae260bdcdd9 | 125 | if (mbedErrorFlag != 0) { |
ohneta | 0:aae260bdcdd9 | 126 | printf("ERROR: %s\n", mbedErrorMessage.c_str()); |
ohneta | 0:aae260bdcdd9 | 127 | } |
ohneta | 0:aae260bdcdd9 | 128 | } |
ohneta | 0:aae260bdcdd9 | 129 | } |
ohneta | 0:aae260bdcdd9 | 130 | delete js; |
ohneta | 0:aae260bdcdd9 | 131 | |
ohneta | 0:aae260bdcdd9 | 132 | #ifdef _WIN32 |
ohneta | 0:aae260bdcdd9 | 133 | #ifdef _DEBUG |
ohneta | 0:aae260bdcdd9 | 134 | _CrtDumpMemoryLeaks(); |
ohneta | 0:aae260bdcdd9 | 135 | #endif |
ohneta | 0:aae260bdcdd9 | 136 | #endif |
ohneta | 0:aae260bdcdd9 | 137 | return 0; |
ohneta | 0:aae260bdcdd9 | 138 | } |
ohneta | 0:aae260bdcdd9 | 139 | #endif |