Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TinyJS by
main2.cpp@9:f80cf055f03d, 2014-09-10 (annotated)
- Committer:
- va009039
- Date:
- Wed Sep 10 08:41:53 2014 +0000
- Revision:
- 9:f80cf055f03d
for GAE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 9:f80cf055f03d | 1 | #include "mbed.h" |
va009039 | 9:f80cf055f03d | 2 | #include "TinyJS.h" |
va009039 | 9:f80cf055f03d | 3 | #include "TinyJS_Functions.h" |
va009039 | 9:f80cf055f03d | 4 | #include "TinyJS_MathFunctions.h" |
va009039 | 9:f80cf055f03d | 5 | #include "Mbed_Functions.h" |
va009039 | 9:f80cf055f03d | 6 | #include <new> |
va009039 | 9:f80cf055f03d | 7 | |
va009039 | 9:f80cf055f03d | 8 | #define qq(...) #__VA_ARGS__ |
va009039 | 9:f80cf055f03d | 9 | |
va009039 | 9:f80cf055f03d | 10 | static const char* example = qq( |
va009039 | 9:f80cf055f03d | 11 | blinker = function() { |
va009039 | 9:f80cf055f03d | 12 | var led = 0; |
va009039 | 9:f80cf055f03d | 13 | mbed.Timeout(function() { |
va009039 | 9:f80cf055f03d | 14 | led = !led; |
va009039 | 9:f80cf055f03d | 15 | mbed.DigitalOut('LED1', led); |
va009039 | 9:f80cf055f03d | 16 | blinker(); |
va009039 | 9:f80cf055f03d | 17 | }, |
va009039 | 9:f80cf055f03d | 18 | 0.6); |
va009039 | 9:f80cf055f03d | 19 | }; |
va009039 | 9:f80cf055f03d | 20 | blinker(); |
va009039 | 9:f80cf055f03d | 21 | ); |
va009039 | 9:f80cf055f03d | 22 | |
va009039 | 9:f80cf055f03d | 23 | static const char* example2 = qq( |
va009039 | 9:f80cf055f03d | 24 | function myfunc(x, y) { |
va009039 | 9:f80cf055f03d | 25 | return x + y; |
va009039 | 9:f80cf055f03d | 26 | } |
va009039 | 9:f80cf055f03d | 27 | print("memfree"); |
va009039 | 9:f80cf055f03d | 28 | print(mbed.memfree()); |
va009039 | 9:f80cf055f03d | 29 | var a = myfunc(1,2); |
va009039 | 9:f80cf055f03d | 30 | print(a); |
va009039 | 9:f80cf055f03d | 31 | ); |
va009039 | 9:f80cf055f03d | 32 | |
va009039 | 9:f80cf055f03d | 33 | |
va009039 | 9:f80cf055f03d | 34 | #if 0 //defined(TARGET_NUCLEO_F401RE) |
va009039 | 9:f80cf055f03d | 35 | static const char* usr_pos = example2; |
va009039 | 9:f80cf055f03d | 36 | #elif defined(TARGET_LPC1768) |
va009039 | 9:f80cf055f03d | 37 | static const char* usr_pos = reinterpret_cast<const char*>(69632); |
va009039 | 9:f80cf055f03d | 38 | #elif defined(TARGET_LPC4088) |
va009039 | 9:f80cf055f03d | 39 | static const char* usr_pos = reinterpret_cast<const char*>(102400); |
va009039 | 9:f80cf055f03d | 40 | #elif defined(TARGET_K64F) |
va009039 | 9:f80cf055f03d | 41 | static const char* usr_pos = reinterpret_cast<const char*>(106496); |
va009039 | 9:f80cf055f03d | 42 | #else |
va009039 | 9:f80cf055f03d | 43 | #error "target error" |
va009039 | 9:f80cf055f03d | 44 | #endif |
va009039 | 9:f80cf055f03d | 45 | |
va009039 | 9:f80cf055f03d | 46 | void js_print(CScriptVar *v, void *userdata); // Script.cpp |
va009039 | 9:f80cf055f03d | 47 | void js_dump(CScriptVar *v, void *userdata); |
va009039 | 9:f80cf055f03d | 48 | extern int mbedErrorFlag; // Script.cpp |
va009039 | 9:f80cf055f03d | 49 | extern std::string mbedErrorMessage; // Script.cpp |
va009039 | 9:f80cf055f03d | 50 | |
va009039 | 9:f80cf055f03d | 51 | static void no_memory () { |
va009039 | 9:f80cf055f03d | 52 | std::printf("Failed to allocate memory!\n"); |
va009039 | 9:f80cf055f03d | 53 | std::exit (1); |
va009039 | 9:f80cf055f03d | 54 | } |
va009039 | 9:f80cf055f03d | 55 | |
va009039 | 9:f80cf055f03d | 56 | int main() { |
va009039 | 9:f80cf055f03d | 57 | std::set_new_handler(no_memory); |
va009039 | 9:f80cf055f03d | 58 | //printf("%s\n", __FILE__); |
va009039 | 9:f80cf055f03d | 59 | |
va009039 | 9:f80cf055f03d | 60 | CTinyJS *js = new CTinyJS(); |
va009039 | 9:f80cf055f03d | 61 | #if defined(TARGET_LPC4088)||defined(TARGET_K64F) |
va009039 | 9:f80cf055f03d | 62 | /* add the functions from TinyJS_Functions.cpp */ |
va009039 | 9:f80cf055f03d | 63 | registerFunctions(js); |
va009039 | 9:f80cf055f03d | 64 | /* add Math and Trigonometry functions from TinyJS_MathFunctions.cpp */ |
va009039 | 9:f80cf055f03d | 65 | registerMathFunctions(js); |
va009039 | 9:f80cf055f03d | 66 | #endif |
va009039 | 9:f80cf055f03d | 67 | /* Add a native function */ |
va009039 | 9:f80cf055f03d | 68 | js->addNative("function print(text)", &js_print, 0); |
va009039 | 9:f80cf055f03d | 69 | js->addNative("function dump()", &js_dump, js); |
va009039 | 9:f80cf055f03d | 70 | |
va009039 | 9:f80cf055f03d | 71 | // add mbed functions |
va009039 | 9:f80cf055f03d | 72 | registerMbedFunctions(js); |
va009039 | 9:f80cf055f03d | 73 | |
va009039 | 9:f80cf055f03d | 74 | mbedErrorFlag = 0; |
va009039 | 9:f80cf055f03d | 75 | js->execute(usr_pos); |
va009039 | 9:f80cf055f03d | 76 | if (mbedErrorFlag != 0) { |
va009039 | 9:f80cf055f03d | 77 | printf("ERROR: %s\n", mbedErrorMessage.c_str()); |
va009039 | 9:f80cf055f03d | 78 | } |
va009039 | 9:f80cf055f03d | 79 | } |
va009039 | 9:f80cf055f03d | 80 |