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
Revision 3:f56c36ea8266, committed 2014-01-18
- Comitter:
- ohneta
- Date:
- Sat Jan 18 15:23:00 2014 +0000
- Parent:
- 2:f0b75885747c
- Child:
- 4:729cab0e95c6
- Commit message:
- mbed?f????????????; DigitalOut/DigitalIn/AnalogOut/AnalogIn/InterruptIn ???; pin_name???????
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Mbed_Functions.cpp Sat Jan 18 15:23:00 2014 +0000 @@ -0,0 +1,262 @@ +/* + * TinyJS for mbed. + * Mbed_Functions.cpp + * + * Authored by Takehisa Oneta (ohneta@gmail.com) + * 17th Jan. 2013 + */ + +#include "mbed.h" +#include "TinyJS.h" +#include "Mbed_Functions.h" + +//--------------------------------------------- + +void mbedDigitalIn(CScriptVar *c, void *) +{ + string pinNameString = c->getParameter("pinName")->getString(); + PinName pinName = _mbedPinNameExchange(pinNameString); + string modeString = c->getParameter("mode")->getString(); + + int mode = PullDown; + if (modeString == "PullUp") { + mode = PullUp; + } else if (modeString == "PullNone"){ + mode = PullNone; + } else if (modeString == "PullDown") { + mode = PullDown; + } + + DigitalIn din = DigitalIn((PinName)pinName); + din.mode((PinMode)mode); + int val = din.read(); + c->getReturnVar()->setInt(val); +} + +void mbedDigitalOut(CScriptVar *c, void *) +{ + string pinNameString = c->getParameter("pinName")->getString(); + PinName pinName = _mbedPinNameExchange(pinNameString); + + int val = c->getParameter("val")->getInt(); + + DigitalOut dout = DigitalOut((PinName)pinName); + dout.write(val); +} + +//--------------------------------------------- + +void mbedAnalogIn(CScriptVar *c, void *) +{ + string pinNameString = c->getParameter("pinName")->getString(); + PinName pinName = _mbedPinNameExchange(pinNameString); + + AnalogIn ain = AnalogIn((PinName)pinName); + float val = ain.read(); + + c->getReturnVar()->setDouble(val); +} + +void mbedAnalogOut(CScriptVar *c, void *) +{ + string pinNameString = c->getParameter("pinName")->getString(); + PinName pinName = _mbedPinNameExchange(pinNameString); + int val = c->getParameter("val")->getInt(); + + AnalogOut aout = AnalogOut((PinName)pinName); + aout.write(val); +} + +//--------------------------------------------- +#define INTERRUPT_NUM 4 +CTinyJS *interrupt_tinyJS; +int interruptCount = 0; +string callbackStrings[INTERRUPT_NUM]; +InterruptIn* intrInArray[INTERRUPT_NUM] = {NULL, NULL, NULL, NULL}; + +void _interruptVector_0(){ + interrupt_tinyJS->execute(callbackStrings[0]); +} +void _interruptVector_1(){ + interrupt_tinyJS->execute(callbackStrings[1]); +} +void _interruptVector_2(){ + interrupt_tinyJS->execute(callbackStrings[2]); +} +void _interruptVector_3(){ + interrupt_tinyJS->execute(callbackStrings[3]); +} + +void mbedSetInterrupt(CScriptVar *c, void *data) +{ + string pinNameString = c->getParameter("pinName")->getString(); + PinName pinName = _mbedPinNameExchange(pinNameString); + + string edgeString = c->getParameter("edge")->getString(); + string modeString = c->getParameter("mode")->getString(); + PinMode mode = PullDown; + if (modeString == "PullUp") { + mode = PullUp; + } else if (modeString == "PullNone"){ + mode = PullNone; + } else if (modeString == "PullDown") { + mode = PullDown; + } + + interrupt_tinyJS = (CTinyJS *)data; + callbackStrings[interruptCount] = c->getParameter("callback")->getString(); + + + if (intrInArray[interruptCount] != NULL) { + delete intrInArray[interruptCount]; + } + + intrInArray[interruptCount] = new InterruptIn(pinName); + intrInArray[interruptCount]->mode(mode); + + if (interruptCount == 0) { + if (edgeString == "fall") intrInArray[interruptCount]->fall(_interruptVector_0); else if (edgeString == "rise") intrInArray[interruptCount]->rise(_interruptVector_0); + } else if (interruptCount == 1) { + if (edgeString == "fall") intrInArray[interruptCount]->fall(_interruptVector_1); else if (edgeString == "rise") intrInArray[interruptCount]->rise(_interruptVector_1); + } else if (interruptCount == 2) { + if (edgeString == "fall") intrInArray[interruptCount]->fall(_interruptVector_2); else if (edgeString == "rise") intrInArray[interruptCount]->rise(_interruptVector_2); + } else if (interruptCount == 3) { + if (edgeString == "fall") intrInArray[interruptCount]->fall(_interruptVector_3); else if (edgeString == "rise") intrInArray[interruptCount]->rise(_interruptVector_3); + } + + + interruptCount++; + if (interruptCount >= INTERRUPT_NUM) { + interruptCount = 0; + } +} + + +void mbedRemoveInterrupt(CScriptVar *c, void *) +{ +} + +//--------------------------------------------- +/** + * + */ +void mbedMemfree(CScriptVar *c, void *) +{ + int i = 0; + while(1) { + void *p = malloc(i); + if (p == NULL) break; + free(p); + i++; + } + c->getReturnVar()->setInt(i); +} + + +//--------------------------------------------- +PinName _mbedPinNameExchange(string pinNameString) +{ + if (pinNameString == "p5") return p5; + if (pinNameString == "p6") return p6; + if (pinNameString == "p7") return p7; + if (pinNameString == "p8") return p8; + if (pinNameString == "p9") return p9; + if (pinNameString == "p10") return p10; + if (pinNameString == "p11") return p11; + if (pinNameString == "p12") return p12; + if (pinNameString == "p13") return p13; + if (pinNameString == "p14") return p14; + if (pinNameString == "p15") return p15; + if (pinNameString == "p16") return p16; + if (pinNameString == "p17") return p17; + if (pinNameString == "p18") return p18; + if (pinNameString == "p19") return p19; + if (pinNameString == "p20") return p20; + + if (pinNameString == "p21") return p21; + if (pinNameString == "p22") return p22; + if (pinNameString == "p23") return p23; + if (pinNameString == "p24") return p24; + if (pinNameString == "p25") return p25; + if (pinNameString == "p26") return p26; + if (pinNameString == "p27") return p27; + if (pinNameString == "p28") return p28; + if (pinNameString == "p29") return p29; + if (pinNameString == "p30") return p30; + + + if (pinNameString == "LED1") return LED1; + if (pinNameString == "LED2") return LED2; + if (pinNameString == "LED3") return LED3; + if (pinNameString == "LED4") return LED4; + + if (pinNameString == "USBTX") return USBTX; + if (pinNameString == "USBRX") return USBRX; + + return NC; +} + +/** + * + */ +void mbedPinNames(CScriptVar *c, void *) +{ + printf("mbed DIP Pin Names\n"); + printf("p5 : %d\n", p5); + printf("p6 : %d\n", p6); + printf("p7 : %d\n", p7); + printf("p8 : %d\n", p8); + printf("p9 : %d\n", p9); + printf("p10 : %d\n", p10); + printf("p11 : %d\n", p11); + printf("p12 : %d\n", p12); + printf("p13 : %d\n", p13); + printf("p14 : %d\n", p14); + printf("p15 : %d\n", p15); + printf("p16 : %d\n", p16); + printf("p17 : %d\n", p17); + printf("p18 : %d\n", p18); + printf("p19 : %d\n", p19); + printf("p20 : %d\n", p20); + printf("p21 : %d\n", p21); + printf("p22 : %d\n", p22); + printf("p23 : %d\n", p23); + printf("p24 : %d\n", p24); + printf("p25 : %d\n", p25); + printf("p26 : %d\n", p26); + printf("p27 : %d\n", p27); + printf("p28 : %d\n", p28); + printf("p29 : %d\n", p29); + printf("p30 : %d\n", p30); + + printf("\n"); + printf("LED mbed Pin Names\n"); + printf("LED1 : %d\n", LED1); + printf("LED2 : %d\n", LED2); + printf("LED3 : %d\n", LED3); + printf("LED4 : %d\n", LED4); + + printf("Other mbed Pin Names\n"); + printf("USBTX : %d\n", USBTX); + printf("USBRX : %d\n", USBRX); +} + + +//--------------------------------------------- +// ----------------------------------------------- Register Functions +void registerMbedFunctions(CTinyJS *tinyJS) { + tinyJS->addNative("function mbed.DigitalIn(pinName, mode)", mbedDigitalIn, 0); + tinyJS->addNative("function mbed.DigitalOut(pinName, val)", mbedDigitalOut, 0); + + tinyJS->addNative("function mbed.AnalogIn(pinName)", mbedAnalogIn, 0); + tinyJS->addNative("function mbed.AnalogOut(pinName, val)", mbedAnalogOut, 0); + + tinyJS->addNative("function mbed.SetInterrupt(pinName, edge, mode, callback)", mbedSetInterrupt, tinyJS); + tinyJS->addNative("function mbed.RemoveInterrupt(pinName)", mbedRemoveInterrupt, tinyJS); + + tinyJS->addNative("function mbed.PinNames()", mbedPinNames, 0); + + tinyJS->addNative("function mbed.memfree()", mbedMemfree, 0); +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Mbed_Functions.h Sat Jan 18 15:23:00 2014 +0000 @@ -0,0 +1,29 @@ +/* + * TinyJS for mbed. + * Mbed_Functions.h + * + * Authored by Takehisa Oneta (ohneta@gmail.com) + * 17th Jan. 2013 + */ + +#include "mbed.h" + +//--------------------------------------------- +void mbedDigitalIn(CScriptVar *c, void *); +void mbedDigitalOut(CScriptVar *c, void *); + +void mbedAnalogIn(CScriptVar *c, void *); +void mbedAnalogOut(CScriptVar *c, void *); + +void mbedSetInterrupt(CScriptVar *c, void *data); +void mbedRemoveInterrupt(CScriptVar *c, void *); + +//--------------------------------------------- +void mbedMemfree(CScriptVar *c, void *); +void mbedPinNames(CScriptVar *c, void *); + +void registerMbedFunctions(CTinyJS *tinyJS); + + +//--------------------------------------------- +PinName _mbedPinNameExchange(string pinNameString);
--- a/Script.cpp Sat Jan 11 22:10:19 2014 +0000 +++ b/Script.cpp Sat Jan 18 15:23:00 2014 +0000 @@ -38,6 +38,10 @@ */ #include "TinyJS.h" #include "TinyJS_Functions.h" +#ifdef MBED +#include "Mbed_Functions.h" +#endif + #include <assert.h> #include <stdio.h> @@ -97,12 +101,6 @@ extern int readOneLine(char *buffer, const int bufferSize); -// mbed function(s) -extern void mbedDigitalOut(CScriptVar *c, void *); -extern char *mbedLedsVarString(int ledNo); -extern void mbedMemfree(CScriptVar *c, void *); - - int tinyjs_main(int argc, char **argv) { CTinyJS *js = new CTinyJS(); @@ -115,12 +113,8 @@ js->execute("print(\"Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!\");"); // add mbed functions - js->addNative("function mbed.memfree()", &mbedMemfree, 0); - js->addNative("function mbed.DigitalOut(pinName, val)", &mbedDigitalOut, 0); - js->execute(mbedLedsVarString(1)); - js->execute(mbedLedsVarString(2)); - js->execute(mbedLedsVarString(3)); - js->execute(mbedLedsVarString(4)); + registerMbedFunctions(js); + while (js->evaluate("lets_quit") == "0") { char buffer[2048];
--- a/main.cpp Sat Jan 11 22:10:19 2014 +0000 +++ b/main.cpp Sat Jan 18 15:23:00 2014 +0000 @@ -12,12 +12,9 @@ #include "mbed.h" #include "TinyJS.h" +#include "Mbed_Functions.h" Serial pc(USBTX, USBRX); -DigitalOut led1(LED1); -DigitalOut led2(LED2); -DigitalOut led3(LED3); -DigitalOut led4(LED4); //unsigned char usbArea[1024] __attribute__((section("AHBSRAM0"))); //unsigned char ethArea[1024] __attribute__((section("AHBSRAM1"))); @@ -27,65 +24,6 @@ //--------------------------------------------- -void mbedDigitalOut(CScriptVar *c, void *) -{ - int pinName = c->getParameter("pinName")->getInt(); - int val = c->getParameter("val")->getInt(); - - switch (pinName) { - case LED1: - led1 = val; - break; - case LED2: - led2 = val; - break; - case LED3: - led3 = val; - break; - case LED4: - led4 = val; - break; - } -} - -char ledVarStr[32]; -char *mbedLedsVarString(int ledNo) -{ - switch (ledNo) { - case 1: - sprintf(ledVarStr, "var led1 = %d;", LED1); - break; - case 2: - sprintf(ledVarStr, "var led2 = %d;", LED2); - break; - case 3: - sprintf(ledVarStr, "var led3 = %d;", LED3); - break; - case 4: - sprintf(ledVarStr, "var led4 = %d;", LED4); - break; - default: - sprintf(ledVarStr, ""); - break; - } - return ledVarStr; -} - -//--------------------------------------------- -void mbedMemfree(CScriptVar *c, void *) -{ - int i = 0; - while(1) { - void *p = malloc(i); - if (p == NULL) break; - free(p); - i++; - } - c->getReturnVar()->setInt(i); -} - -//--------------------------------------------- - int readOneLine(char *buffer, const int bufferSize) { int len = 0;