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.cpp@8:819934a27c2d, 2014-01-20 (annotated)
- Committer:
- ohneta
- Date:
- Mon Jan 20 00:07:35 2014 +0000
- Revision:
- 8:819934a27c2d
- Parent:
- 7:40010b4dc51d
update InterruptIn, Timer, Timeout functons
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.cpp |
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 | #include "TinyJS.h" |
ohneta | 3:f56c36ea8266 | 11 | #include "Mbed_Functions.h" |
ohneta | 3:f56c36ea8266 | 12 | |
ohneta | 3:f56c36ea8266 | 13 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 14 | |
ohneta | 3:f56c36ea8266 | 15 | void mbedDigitalIn(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 16 | { |
ohneta | 3:f56c36ea8266 | 17 | string pinNameString = c->getParameter("pinName")->getString(); |
ohneta | 3:f56c36ea8266 | 18 | PinName pinName = _mbedPinNameExchange(pinNameString); |
ohneta | 3:f56c36ea8266 | 19 | string modeString = c->getParameter("mode")->getString(); |
ohneta | 3:f56c36ea8266 | 20 | |
ohneta | 3:f56c36ea8266 | 21 | int mode = PullDown; |
ohneta | 3:f56c36ea8266 | 22 | if (modeString == "PullUp") { |
ohneta | 3:f56c36ea8266 | 23 | mode = PullUp; |
ohneta | 3:f56c36ea8266 | 24 | } else if (modeString == "PullNone"){ |
ohneta | 3:f56c36ea8266 | 25 | mode = PullNone; |
ohneta | 3:f56c36ea8266 | 26 | } else if (modeString == "PullDown") { |
ohneta | 3:f56c36ea8266 | 27 | mode = PullDown; |
ohneta | 3:f56c36ea8266 | 28 | } |
ohneta | 3:f56c36ea8266 | 29 | |
ohneta | 3:f56c36ea8266 | 30 | DigitalIn din = DigitalIn((PinName)pinName); |
ohneta | 3:f56c36ea8266 | 31 | din.mode((PinMode)mode); |
ohneta | 3:f56c36ea8266 | 32 | int val = din.read(); |
ohneta | 3:f56c36ea8266 | 33 | c->getReturnVar()->setInt(val); |
ohneta | 3:f56c36ea8266 | 34 | } |
ohneta | 3:f56c36ea8266 | 35 | |
ohneta | 3:f56c36ea8266 | 36 | void mbedDigitalOut(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 37 | { |
ohneta | 3:f56c36ea8266 | 38 | string pinNameString = c->getParameter("pinName")->getString(); |
ohneta | 3:f56c36ea8266 | 39 | PinName pinName = _mbedPinNameExchange(pinNameString); |
ohneta | 3:f56c36ea8266 | 40 | |
ohneta | 3:f56c36ea8266 | 41 | int val = c->getParameter("val")->getInt(); |
ohneta | 3:f56c36ea8266 | 42 | |
ohneta | 3:f56c36ea8266 | 43 | DigitalOut dout = DigitalOut((PinName)pinName); |
ohneta | 3:f56c36ea8266 | 44 | dout.write(val); |
ohneta | 3:f56c36ea8266 | 45 | } |
ohneta | 3:f56c36ea8266 | 46 | |
ohneta | 3:f56c36ea8266 | 47 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 48 | |
ohneta | 3:f56c36ea8266 | 49 | void mbedAnalogIn(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 50 | { |
ohneta | 3:f56c36ea8266 | 51 | string pinNameString = c->getParameter("pinName")->getString(); |
ohneta | 3:f56c36ea8266 | 52 | PinName pinName = _mbedPinNameExchange(pinNameString); |
ohneta | 3:f56c36ea8266 | 53 | |
ohneta | 3:f56c36ea8266 | 54 | AnalogIn ain = AnalogIn((PinName)pinName); |
ohneta | 3:f56c36ea8266 | 55 | float val = ain.read(); |
ohneta | 3:f56c36ea8266 | 56 | |
ohneta | 3:f56c36ea8266 | 57 | c->getReturnVar()->setDouble(val); |
ohneta | 3:f56c36ea8266 | 58 | } |
ohneta | 3:f56c36ea8266 | 59 | |
ohneta | 3:f56c36ea8266 | 60 | void mbedAnalogOut(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 61 | { |
ohneta | 3:f56c36ea8266 | 62 | string pinNameString = c->getParameter("pinName")->getString(); |
ohneta | 3:f56c36ea8266 | 63 | PinName pinName = _mbedPinNameExchange(pinNameString); |
ohneta | 5:b5e4fbe59746 | 64 | float val = c->getParameter("val")->getDouble(); |
ohneta | 3:f56c36ea8266 | 65 | |
ohneta | 3:f56c36ea8266 | 66 | AnalogOut aout = AnalogOut((PinName)pinName); |
ohneta | 3:f56c36ea8266 | 67 | aout.write(val); |
ohneta | 3:f56c36ea8266 | 68 | } |
ohneta | 3:f56c36ea8266 | 69 | |
ohneta | 3:f56c36ea8266 | 70 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 71 | #define INTERRUPT_NUM 4 |
ohneta | 6:30b4122b0ee2 | 72 | CTinyJS *_mbed_interruptIn_tinyJS; |
ohneta | 6:30b4122b0ee2 | 73 | int _mbed_interruptIn_Count = 0; |
ohneta | 6:30b4122b0ee2 | 74 | string _mbed_interruptIn_callbackStrings[INTERRUPT_NUM]; |
ohneta | 6:30b4122b0ee2 | 75 | InterruptIn* _mbed_interrupt_array[INTERRUPT_NUM] = {NULL, NULL, NULL, NULL}; |
ohneta | 3:f56c36ea8266 | 76 | |
ohneta | 6:30b4122b0ee2 | 77 | void _interruptVector0(){ |
ohneta | 6:30b4122b0ee2 | 78 | _mbed_interruptIn_tinyJS->execute(_mbed_interruptIn_callbackStrings[0]); |
ohneta | 3:f56c36ea8266 | 79 | } |
ohneta | 6:30b4122b0ee2 | 80 | void _interruptVector1(){ |
ohneta | 6:30b4122b0ee2 | 81 | _mbed_interruptIn_tinyJS->execute(_mbed_interruptIn_callbackStrings[1]); |
ohneta | 3:f56c36ea8266 | 82 | } |
ohneta | 6:30b4122b0ee2 | 83 | void _interruptVector2(){ |
ohneta | 6:30b4122b0ee2 | 84 | _mbed_interruptIn_tinyJS->execute(_mbed_interruptIn_callbackStrings[2]); |
ohneta | 3:f56c36ea8266 | 85 | } |
ohneta | 6:30b4122b0ee2 | 86 | void _interruptVector3(){ |
ohneta | 6:30b4122b0ee2 | 87 | _mbed_interruptIn_tinyJS->execute(_mbed_interruptIn_callbackStrings[3]); |
ohneta | 3:f56c36ea8266 | 88 | } |
ohneta | 3:f56c36ea8266 | 89 | |
ohneta | 7:40010b4dc51d | 90 | void mbedInterruptIn(CScriptVar *c, void *data) |
ohneta | 3:f56c36ea8266 | 91 | { |
ohneta | 3:f56c36ea8266 | 92 | string pinNameString = c->getParameter("pinName")->getString(); |
ohneta | 3:f56c36ea8266 | 93 | PinName pinName = _mbedPinNameExchange(pinNameString); |
ohneta | 3:f56c36ea8266 | 94 | |
ohneta | 3:f56c36ea8266 | 95 | string edgeString = c->getParameter("edge")->getString(); |
ohneta | 3:f56c36ea8266 | 96 | string modeString = c->getParameter("mode")->getString(); |
ohneta | 3:f56c36ea8266 | 97 | PinMode mode = PullDown; |
ohneta | 3:f56c36ea8266 | 98 | if (modeString == "PullUp") { |
ohneta | 3:f56c36ea8266 | 99 | mode = PullUp; |
ohneta | 3:f56c36ea8266 | 100 | } else if (modeString == "PullNone"){ |
ohneta | 3:f56c36ea8266 | 101 | mode = PullNone; |
ohneta | 3:f56c36ea8266 | 102 | } else if (modeString == "PullDown") { |
ohneta | 3:f56c36ea8266 | 103 | mode = PullDown; |
ohneta | 3:f56c36ea8266 | 104 | } |
ohneta | 3:f56c36ea8266 | 105 | |
ohneta | 6:30b4122b0ee2 | 106 | _mbed_interruptIn_tinyJS = (CTinyJS *)data; |
ohneta | 6:30b4122b0ee2 | 107 | _mbed_interruptIn_callbackStrings[_mbed_interruptIn_Count] = c->getParameter("callback")->getString(); |
ohneta | 3:f56c36ea8266 | 108 | |
ohneta | 6:30b4122b0ee2 | 109 | if (_mbed_interrupt_array[_mbed_interruptIn_Count] != NULL) { |
ohneta | 6:30b4122b0ee2 | 110 | delete _mbed_interrupt_array[_mbed_interruptIn_Count]; |
ohneta | 3:f56c36ea8266 | 111 | } |
ohneta | 3:f56c36ea8266 | 112 | |
ohneta | 6:30b4122b0ee2 | 113 | _mbed_interrupt_array[_mbed_interruptIn_Count] = new InterruptIn(pinName); |
ohneta | 6:30b4122b0ee2 | 114 | _mbed_interrupt_array[_mbed_interruptIn_Count]->mode(mode); |
ohneta | 3:f56c36ea8266 | 115 | |
ohneta | 6:30b4122b0ee2 | 116 | if (_mbed_interruptIn_Count == 0) { |
ohneta | 6:30b4122b0ee2 | 117 | if (edgeString == "fall") _mbed_interrupt_array[_mbed_interruptIn_Count]->fall(_interruptVector0); |
ohneta | 6:30b4122b0ee2 | 118 | else if (edgeString == "rise") _mbed_interrupt_array[_mbed_interruptIn_Count]->rise(_interruptVector0); |
ohneta | 6:30b4122b0ee2 | 119 | } else if (_mbed_interruptIn_Count == 1) { |
ohneta | 6:30b4122b0ee2 | 120 | if (edgeString == "fall") _mbed_interrupt_array[_mbed_interruptIn_Count]->fall(_interruptVector1); |
ohneta | 6:30b4122b0ee2 | 121 | else if (edgeString == "rise") _mbed_interrupt_array[_mbed_interruptIn_Count]->rise(_interruptVector1); |
ohneta | 6:30b4122b0ee2 | 122 | } else if (_mbed_interruptIn_Count == 2) { |
ohneta | 6:30b4122b0ee2 | 123 | if (edgeString == "fall") _mbed_interrupt_array[_mbed_interruptIn_Count]->fall(_interruptVector2); |
ohneta | 6:30b4122b0ee2 | 124 | else if (edgeString == "rise") _mbed_interrupt_array[_mbed_interruptIn_Count]->rise(_interruptVector2); |
ohneta | 6:30b4122b0ee2 | 125 | } else if (_mbed_interruptIn_Count == 3) { |
ohneta | 6:30b4122b0ee2 | 126 | if (edgeString == "fall") _mbed_interrupt_array[_mbed_interruptIn_Count]->fall(_interruptVector3); |
ohneta | 6:30b4122b0ee2 | 127 | else if (edgeString == "rise") _mbed_interrupt_array[_mbed_interruptIn_Count]->rise(_interruptVector3); |
ohneta | 3:f56c36ea8266 | 128 | } |
ohneta | 3:f56c36ea8266 | 129 | |
ohneta | 6:30b4122b0ee2 | 130 | c->getReturnVar()->setInt(_mbed_interruptIn_Count); |
ohneta | 3:f56c36ea8266 | 131 | |
ohneta | 6:30b4122b0ee2 | 132 | _mbed_interruptIn_Count++; |
ohneta | 6:30b4122b0ee2 | 133 | if (_mbed_interruptIn_Count >= INTERRUPT_NUM) { |
ohneta | 6:30b4122b0ee2 | 134 | _mbed_interruptIn_Count = 0; |
ohneta | 3:f56c36ea8266 | 135 | } |
ohneta | 3:f56c36ea8266 | 136 | } |
ohneta | 3:f56c36ea8266 | 137 | |
ohneta | 6:30b4122b0ee2 | 138 | /* |
ohneta | 3:f56c36ea8266 | 139 | void mbedRemoveInterrupt(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 140 | { |
ohneta | 6:30b4122b0ee2 | 141 | int intrno = c->getParameter("interrupt_no")->getInt(); |
ohneta | 6:30b4122b0ee2 | 142 | } |
ohneta | 6:30b4122b0ee2 | 143 | */ |
ohneta | 6:30b4122b0ee2 | 144 | |
ohneta | 6:30b4122b0ee2 | 145 | //--------------------------------------------- |
ohneta | 6:30b4122b0ee2 | 146 | |
ohneta | 6:30b4122b0ee2 | 147 | void mbedWait(CScriptVar *c, void *) |
ohneta | 6:30b4122b0ee2 | 148 | { |
ohneta | 6:30b4122b0ee2 | 149 | float val = c->getParameter("s")->getDouble(); |
ohneta | 6:30b4122b0ee2 | 150 | wait(val); |
ohneta | 3:f56c36ea8266 | 151 | } |
ohneta | 3:f56c36ea8266 | 152 | |
ohneta | 3:f56c36ea8266 | 153 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 154 | /** |
ohneta | 3:f56c36ea8266 | 155 | * |
ohneta | 3:f56c36ea8266 | 156 | */ |
ohneta | 3:f56c36ea8266 | 157 | void mbedMemfree(CScriptVar *c, void *) |
ohneta | 3:f56c36ea8266 | 158 | { |
ohneta | 3:f56c36ea8266 | 159 | int i = 0; |
ohneta | 3:f56c36ea8266 | 160 | while(1) { |
ohneta | 3:f56c36ea8266 | 161 | void *p = malloc(i); |
ohneta | 3:f56c36ea8266 | 162 | if (p == NULL) break; |
ohneta | 3:f56c36ea8266 | 163 | free(p); |
ohneta | 3:f56c36ea8266 | 164 | i++; |
ohneta | 3:f56c36ea8266 | 165 | } |
ohneta | 3:f56c36ea8266 | 166 | c->getReturnVar()->setInt(i); |
ohneta | 3:f56c36ea8266 | 167 | } |
ohneta | 3:f56c36ea8266 | 168 | |
ohneta | 3:f56c36ea8266 | 169 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 170 | PinName _mbedPinNameExchange(string pinNameString) |
ohneta | 3:f56c36ea8266 | 171 | { |
ohneta | 3:f56c36ea8266 | 172 | if (pinNameString == "p5") return p5; |
ohneta | 3:f56c36ea8266 | 173 | if (pinNameString == "p6") return p6; |
ohneta | 3:f56c36ea8266 | 174 | if (pinNameString == "p7") return p7; |
ohneta | 3:f56c36ea8266 | 175 | if (pinNameString == "p8") return p8; |
ohneta | 3:f56c36ea8266 | 176 | if (pinNameString == "p9") return p9; |
ohneta | 3:f56c36ea8266 | 177 | if (pinNameString == "p10") return p10; |
ohneta | 3:f56c36ea8266 | 178 | if (pinNameString == "p11") return p11; |
ohneta | 3:f56c36ea8266 | 179 | if (pinNameString == "p12") return p12; |
ohneta | 3:f56c36ea8266 | 180 | if (pinNameString == "p13") return p13; |
ohneta | 3:f56c36ea8266 | 181 | if (pinNameString == "p14") return p14; |
ohneta | 3:f56c36ea8266 | 182 | if (pinNameString == "p15") return p15; |
ohneta | 3:f56c36ea8266 | 183 | if (pinNameString == "p16") return p16; |
ohneta | 3:f56c36ea8266 | 184 | if (pinNameString == "p17") return p17; |
ohneta | 3:f56c36ea8266 | 185 | if (pinNameString == "p18") return p18; |
ohneta | 3:f56c36ea8266 | 186 | if (pinNameString == "p19") return p19; |
ohneta | 3:f56c36ea8266 | 187 | if (pinNameString == "p20") return p20; |
ohneta | 3:f56c36ea8266 | 188 | |
ohneta | 3:f56c36ea8266 | 189 | if (pinNameString == "p21") return p21; |
ohneta | 3:f56c36ea8266 | 190 | if (pinNameString == "p22") return p22; |
ohneta | 3:f56c36ea8266 | 191 | if (pinNameString == "p23") return p23; |
ohneta | 3:f56c36ea8266 | 192 | if (pinNameString == "p24") return p24; |
ohneta | 3:f56c36ea8266 | 193 | if (pinNameString == "p25") return p25; |
ohneta | 3:f56c36ea8266 | 194 | if (pinNameString == "p26") return p26; |
ohneta | 3:f56c36ea8266 | 195 | if (pinNameString == "p27") return p27; |
ohneta | 3:f56c36ea8266 | 196 | if (pinNameString == "p28") return p28; |
ohneta | 3:f56c36ea8266 | 197 | if (pinNameString == "p29") return p29; |
ohneta | 3:f56c36ea8266 | 198 | if (pinNameString == "p30") return p30; |
ohneta | 3:f56c36ea8266 | 199 | |
ohneta | 3:f56c36ea8266 | 200 | |
ohneta | 3:f56c36ea8266 | 201 | if (pinNameString == "LED1") return LED1; |
ohneta | 3:f56c36ea8266 | 202 | if (pinNameString == "LED2") return LED2; |
ohneta | 3:f56c36ea8266 | 203 | if (pinNameString == "LED3") return LED3; |
ohneta | 3:f56c36ea8266 | 204 | if (pinNameString == "LED4") return LED4; |
ohneta | 3:f56c36ea8266 | 205 | |
ohneta | 3:f56c36ea8266 | 206 | if (pinNameString == "USBTX") return USBTX; |
ohneta | 3:f56c36ea8266 | 207 | if (pinNameString == "USBRX") return USBRX; |
ohneta | 3:f56c36ea8266 | 208 | |
ohneta | 3:f56c36ea8266 | 209 | return NC; |
ohneta | 3:f56c36ea8266 | 210 | } |
ohneta | 3:f56c36ea8266 | 211 | |
ohneta | 6:30b4122b0ee2 | 212 | //--------------------------------------------- |
ohneta | 6:30b4122b0ee2 | 213 | Timer _mbedTimer; |
ohneta | 6:30b4122b0ee2 | 214 | |
ohneta | 6:30b4122b0ee2 | 215 | void mbedTimerStart(CScriptVar *c, void *) { |
ohneta | 6:30b4122b0ee2 | 216 | _mbedTimer.start(); |
ohneta | 6:30b4122b0ee2 | 217 | } |
ohneta | 6:30b4122b0ee2 | 218 | |
ohneta | 6:30b4122b0ee2 | 219 | void mbedTimerStop(CScriptVar *c, void *) { |
ohneta | 6:30b4122b0ee2 | 220 | _mbedTimer.stop(); |
ohneta | 6:30b4122b0ee2 | 221 | } |
ohneta | 6:30b4122b0ee2 | 222 | |
ohneta | 6:30b4122b0ee2 | 223 | void mbedTimerReset(CScriptVar *c, void *) { |
ohneta | 6:30b4122b0ee2 | 224 | _mbedTimer.reset(); |
ohneta | 6:30b4122b0ee2 | 225 | } |
ohneta | 3:f56c36ea8266 | 226 | |
ohneta | 6:30b4122b0ee2 | 227 | void mbedTimerRead(CScriptVar *c, void *) { |
ohneta | 6:30b4122b0ee2 | 228 | float tm = _mbedTimer.read(); |
ohneta | 6:30b4122b0ee2 | 229 | c->getReturnVar()->setDouble(tm); |
ohneta | 6:30b4122b0ee2 | 230 | } |
ohneta | 6:30b4122b0ee2 | 231 | |
ohneta | 6:30b4122b0ee2 | 232 | |
ohneta | 6:30b4122b0ee2 | 233 | //--------------------------------------------- |
ohneta | 6:30b4122b0ee2 | 234 | Timeout _mbedTimeout; |
ohneta | 6:30b4122b0ee2 | 235 | CTinyJS *_mbedTimeout_tinyJS; |
ohneta | 6:30b4122b0ee2 | 236 | string _mbedTimeout_callbackStrings; |
ohneta | 3:f56c36ea8266 | 237 | |
ohneta | 6:30b4122b0ee2 | 238 | void _mbedTimeoutVector() { |
ohneta | 6:30b4122b0ee2 | 239 | _mbedTimeout_tinyJS->execute(_mbedTimeout_callbackStrings); |
ohneta | 6:30b4122b0ee2 | 240 | } |
ohneta | 6:30b4122b0ee2 | 241 | |
ohneta | 6:30b4122b0ee2 | 242 | void mbedTimeout(CScriptVar *c, void *data) { |
ohneta | 6:30b4122b0ee2 | 243 | |
ohneta | 6:30b4122b0ee2 | 244 | _mbedTimeout_tinyJS = (CTinyJS *)data; |
ohneta | 6:30b4122b0ee2 | 245 | _mbedTimeout_callbackStrings = c->getParameter("callback")->getString(); |
ohneta | 6:30b4122b0ee2 | 246 | float t = c->getParameter("t")->getDouble(); |
ohneta | 6:30b4122b0ee2 | 247 | |
ohneta | 6:30b4122b0ee2 | 248 | _mbedTimeout.attach(&_mbedTimeoutVector, t); |
ohneta | 3:f56c36ea8266 | 249 | } |
ohneta | 3:f56c36ea8266 | 250 | |
ohneta | 3:f56c36ea8266 | 251 | |
ohneta | 3:f56c36ea8266 | 252 | //--------------------------------------------- |
ohneta | 3:f56c36ea8266 | 253 | // ----------------------------------------------- Register Functions |
ohneta | 3:f56c36ea8266 | 254 | void registerMbedFunctions(CTinyJS *tinyJS) { |
ohneta | 3:f56c36ea8266 | 255 | tinyJS->addNative("function mbed.DigitalIn(pinName, mode)", mbedDigitalIn, 0); |
ohneta | 3:f56c36ea8266 | 256 | tinyJS->addNative("function mbed.DigitalOut(pinName, val)", mbedDigitalOut, 0); |
ohneta | 3:f56c36ea8266 | 257 | |
ohneta | 3:f56c36ea8266 | 258 | tinyJS->addNative("function mbed.AnalogIn(pinName)", mbedAnalogIn, 0); |
ohneta | 3:f56c36ea8266 | 259 | tinyJS->addNative("function mbed.AnalogOut(pinName, val)", mbedAnalogOut, 0); |
ohneta | 3:f56c36ea8266 | 260 | |
ohneta | 8:819934a27c2d | 261 | tinyJS->addNative("function mbed.InterruptIn(pinName, edge, mode, callback)", mbedInterruptIn, tinyJS); |
ohneta | 6:30b4122b0ee2 | 262 | tinyJS->addNative("function mbed.wait(s)", mbedWait, 0); |
ohneta | 8:819934a27c2d | 263 | tinyJS->addNative("function mbed.TimerStart", mbedTimerStart, 0); |
ohneta | 8:819934a27c2d | 264 | tinyJS->addNative("function mbed.TimerStop", mbedTimerStop, 0); |
ohneta | 8:819934a27c2d | 265 | tinyJS->addNative("function mbed.TimerReset", mbedTimerReset, 0); |
ohneta | 8:819934a27c2d | 266 | tinyJS->addNative("function mbed.TimerRead", mbedTimerRead, 0); |
ohneta | 6:30b4122b0ee2 | 267 | tinyJS->addNative("function mbed.Timeout(callback, t)", mbedTimeout, tinyJS); |
ohneta | 6:30b4122b0ee2 | 268 | |
ohneta | 3:f56c36ea8266 | 269 | tinyJS->addNative("function mbed.memfree()", mbedMemfree, 0); |
ohneta | 3:f56c36ea8266 | 270 | } |
ohneta | 3:f56c36ea8266 | 271 | |
ohneta | 3:f56c36ea8266 | 272 |