void CC3000_UsynchCallback(long lEventType, char *pcData, unsigned char ucLength) { if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE) { //ulSmartConfigFinished = 1; jsiConsolePrint("HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE\n"); } if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT) { jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_CONNECT\n"); //ulCC3000Connected = 1; /* Turn On LED */ SmartConfigLedOn(TRUE); } if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT) { jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_DISCONNECT\n"); //ulCC3000Connected = 0; /* Turn Off LED */ SmartConfigLedOn(FALSE); } if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP) { //ulCC3000DHCP = 1; jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_DHCP\n"); } if (lEventType == HCI_EVNT_WLAN_ASYNC_PING_REPORT) { jsiConsolePrint("HCI_EVNT_WLAN_ASYNC_PING_REPORT\n"); } }
/*JSON{ "type":"staticmethod", "class":"console", "name" : "log", "description" : "Print the supplied string(s)", "generate" : "jswrap_interface_print", "params" : [ [ "text", "JsVarArray", "One or more arguments to print"] ] }*/ void jswrap_interface_print(JsVar *v) { assert(jsvIsArray(v)); JsArrayIterator it; jsvArrayIteratorNew(&it, v); while (jsvArrayIteratorHasElement(&it)) { JsVar *v = jsvAsString(jsvArrayIteratorGetElement(&it), true); jsiConsoleRemoveInputLine(); jsiConsolePrintStringVar(v); jsvUnLock(v); jsvArrayIteratorNext(&it); if (jsvArrayIteratorHasElement(&it)) jsiConsolePrint(" "); } jsvArrayIteratorFree(&it); jsiConsolePrint("\n"); }
/*JSON{ "type":"staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "description" : "Initialise the WIZnet module and return an Ethernet object", "params" : [ ], "return" : ["JsVar", "An Ethernet Object"], "return_object":"Ethernet" }*/ JsVar *jswrap_wiznet_connect() { JsVar *ethObj = jspNewObject(0, "Ethernet"); // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); // CS Configuration jshSetPinStateIsManual(ETH_CS_PIN, false); jshPinOutput(ETH_CS_PIN, 1); // de-assert CS // Wiznet reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZCHIP Initialized fail.\r\n"); return 0; } /* PHY link status check */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); return 0; } } while (tmp == PHY_LINK_OFF); JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
/*JSON{ "type" : "staticmethod", "class" : "console", "name" : "log", "generate" : "jswrap_interface_print", "params" : [ ["text","JsVarArray","One or more arguments to print"] ] } Print the supplied string(s) to the console **Note:** If you're connected to a computer (not a wall adaptor) via USB but **you are not running a terminal app** then when you print data Espruino may pause execution and wait until the computer requests the data it is trying to print. */ void jswrap_interface_print(JsVar *v) { assert(jsvIsArray(v)); jsiConsoleRemoveInputLine(); JsvObjectIterator it; jsvObjectIteratorNew(&it, v); while (jsvObjectIteratorHasValue(&it)) { JsVar *v = jsvObjectIteratorGetValue(&it); if (jsvIsString(v)) jsiConsolePrintStringVar(v); else jsfPrintJSON(v, JSON_PRETTY | JSON_NEWLINES); jsvUnLock(v); jsvObjectIteratorNext(&it); if (jsvObjectIteratorHasValue(&it)) jsiConsolePrint(" "); } jsvObjectIteratorFree(&it); jsiConsolePrint("\n"); }
/*JSON{ "type":"method", "class": "Serial", "name" : "onData", "description" : ["When a character is received on this serial port, the function supplied to onData gets called.", "Only one function can ever be supplied, so calling onData(undefined) will stop any function being called"], "generate" : "jswrap_serial_onData", "params" : [ [ "function", "JsVarName", "A function to call when data arrives. It takes one argument, which is an object with a 'data' field"] ] }*/ void jswrap_serial_onData(JsVar *parent, JsVar *funcVar) { JsVar *skippedFunc = jsvSkipName(funcVar); if (!jsvIsFunction(skippedFunc) && !jsvIsString(skippedFunc)) { jsiConsolePrint("Function or String not supplied - removing onData handler.\n"); JsVar *handler = jsvFindChildFromString(parent, USART_CALLBACK_NAME, false); if (handler) { jsvRemoveChild(parent, handler); jsvUnLock(handler); } } else { jsvUnLock(jsvSetNamedChild(parent, funcVar, USART_CALLBACK_NAME)); } jsvUnLock(skippedFunc); }
int handleErrors() { int e = 0; JsVar *exception = jspGetException(); if (exception) { jsiConsolePrintf("Uncaught %v\n", exception); jsvUnLock(exception); e = 1; } if (jspIsInterrupted()) { jsiConsoleRemoveInputLine(); jsiConsolePrint("Execution Interrupted.\n"); jspSetInterrupted(false); e = 1; } return e; }
/// Call just before returning to idle loop. This checks for errors and tries to recover. Returns true if no errors. bool net_cc3000_checkError(JsNetwork *net) { bool hadErrors = false; while (jspIsInterrupted()) { hadErrors = true; jsiConsolePrint("CC3000 WiFi is not responding. Power cycling...\n"); jspSetInterrupted(false); // remove all existing connections networkState = NETWORKSTATE_OFFLINE; // ensure we don't try and send the CC3k anything socketKill(net); socketInit(); // power cycle JsVar *wlan = jsvObjectGetChild(execInfo.hiddenRoot, CC3000_OBJ_NAME, 0); if (wlan) { jswrap_wlan_reconnect(wlan); jsvUnLock(wlan); } else jsExceptionHere(JSET_INTERNALERROR, "No CC3000 object!\n"); // jswrap_wlan_reconnect could fail, which would mean we have to do this all over again } return hadErrors; }
/// Call just before returning to idle loop. This checks for errors and tries to recover. Returns true if no errors. bool net_cc3000_checkError(JsNetwork *net) { bool hadErrors = false; while (jspIsInterrupted()) { hadErrors = true; jsiConsolePrint("Looks like CC3000 has died again. Power cycling...\n"); jspSetInterrupted(false); // remove all existing connections networkState = NETWORKSTATE_OFFLINE; // ensure we don't try and send the CC3k anything httpKill(net); httpInit(); // power cycle JsVar *wlan = jsvObjectGetChild(execInfo.root, CC3000_OBJ_NAME, 0); if (wlan) { jswrap_wlan_reconnect(wlan); jsvUnLock(wlan); } else jsErrorInternal("No CC3000 object!\n"); // jswrap_wlan_reconnect could fail, which would mean we have to do this all over again } return hadErrors; }
void cc3000_usynch_callback(long lEventType, char *pcData, unsigned char ucLength) { if (lEventType == HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE) { //ulSmartConfigFinished = 1; //jsiConsolePrint("HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE\n"); } else if (lEventType == HCI_EVNT_WLAN_UNSOL_CONNECT) { //jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_CONNECT\n"); cc3000_state_change("connect"); } else if (lEventType == HCI_EVNT_WLAN_UNSOL_DISCONNECT) { //jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_DISCONNECT\n"); cc3000_state_change("disconnect"); } else if (lEventType == HCI_EVNT_WLAN_UNSOL_DHCP) { //jsiConsolePrint("HCI_EVNT_WLAN_UNSOL_DHCP\n"); cc3000_state_change("dhcp"); } else if (lEventType == HCI_EVNT_WLAN_ASYNC_PING_REPORT) { jsiConsolePrint("HCI_EVNT_WLAN_ASYNC_PING_REPORT\n"); } else if (lEventType == HCI_EVNT_BSD_TCP_CLOSE_WAIT) { uint8_t socketnum = pcData[0]; cc3000_socket_closed |= 1<<socketnum; //jsiConsolePrint("HCI_EVNT_BSD_TCP_CLOSE_WAIT\n"); } else { //jsiConsolePrintf("%x-usync", lEventType); }
int main(int argc, char **argv) { int i; for (i=1;i<argc;i++) { if (argv[i][0]=='-') { // option char *a = argv[i]; if (!strcmp(a,"-h") || !strcmp(a,"--help")) { show_help(); exit(1); } else if (!strcmp(a,"-e") || !strcmp(a,"--eval")) { if (i+1>=argc) die("Expecting an extra argument\n"); jshInit(); jsvInit(); jsiInit(true); addNativeFunction("quit", nativeQuit); jsvUnLock(jspEvaluate(argv[i+1], false)); int errCode = handleErrors(); isRunning = !errCode; bool isBusy = true; while (isRunning && (jsiHasTimers() || isBusy)) isBusy = jsiLoop(); jsiKill(); jsvKill(); jshKill(); exit(errCode); } else if (!strcmp(a,"--test")) { if (i+1>=argc) die("Expecting an extra argument\n"); bool ok = run_test(argv[i+1]); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-all")) { bool ok = run_all_tests(); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-all")) { bool ok = run_memory_tests(0); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem")) { if (i+1>=argc) die("Expecting an extra argument\n"); bool ok = run_memory_test(argv[i+1], 0); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-n")) { if (i+2>=argc) die("Expecting an extra 2 arguments\n"); bool ok = run_memory_test(argv[i+1], atoi(argv[i+2])); exit(ok ? 0 : 1); } else { printf("Unknown Argument %s\n", a); show_help(); exit(1); } } } if (argc==1) { printf("Interactive mode.\n"); } else if (argc==2) { // single file - just run it char *buffer = read_file(argv[1]); if (!buffer) exit(1); // check for '#' as the first char, and if so, skip the first line char *cmd = buffer; if (cmd[0]=='#') { while (cmd[0] && cmd[0]!='\n') cmd++; if (cmd[0]=='\n') cmd++; } jshInit(); jsvInit(); jsiInit(false /* do not autoload!!! */); addNativeFunction("quit", nativeQuit); jsvUnLock(jspEvaluate(cmd, true)); int errCode = handleErrors(); free(buffer); isRunning = !errCode; bool isBusy = true; while (isRunning && (jsiHasTimers() || isBusy)) isBusy = jsiLoop(); jsiKill(); jsvKill(); jshKill(); exit(errCode); } else { printf("Unknown arguments!\n"); show_help(); exit(1); } printf("Size of JsVar is now %d bytes\n", (int)sizeof(JsVar)); printf("Size of JsVarRef is now %d bytes\n", (int)sizeof(JsVarRef)); #ifndef __MINGW32__ struct sigaction sa; sa.sa_handler = sig_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) == -1) printf("Adding SIGINT hook failed\n"); else printf("Added SIGINT hook\n"); if (sigaction(SIGHUP, &sa, NULL) == -1) printf("Adding SIGHUP hook failed\n"); else printf("Added SIGHUP hook\n"); if (sigaction(SIGTERM, &sa, NULL) == -1) printf("Adding SIGTERM hook failed\n"); else printf("Added SIGTERM hook\n"); #endif//!__MINGW32__ jshInit(); jsvInit(); jsiInit(true); addNativeFunction("quit", nativeQuit); addNativeFunction("interrupt", nativeInterrupt); while (isRunning) { jsiLoop(); } jsiConsolePrint("\n"); jsiKill(); jsvGarbageCollect(); jsvShowAllocated(); jsvKill(); jshKill(); return 0; }
int main(int argc, char **argv) { int i; for (i=1;i<argc;i++) { if (argv[i][0]=='-') { // option char *a = argv[i]; if (!strcmp(a,"-h") || !strcmp(a,"--help")) { show_help(); exit(1); } else if (!strcmp(a,"-e") || !strcmp(a,"--eval")) { if (i+1>=argc) die("Expecting an extra argument"); jshInit(); jsiInit(true); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jsvUnLock(jspEvaluate(jsiGetParser(), argv[i+1])); isRunning = true; while (isRunning && jsiHasTimers()) jsiLoop(); jsiKill(); jshKill(); exit(0); } else if (!strcmp(a,"--test-all")) { bool ok = run_all_tests(); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-all")) { bool ok = run_memory_tests(0); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem")) { if (i+1>=argc) die("Expecting an extra argument"); bool ok = run_memory_test(argv[i+1], 0); exit(ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-n")) { if (i+2>=argc) die("Expecting an extra 2 arguments"); bool ok = run_memory_test(argv[i+1], atoi(argv[i+2])); exit(ok ? 0 : 1); } else { printf("Unknown Argument %s\n", a); show_help(); exit(1); } } } if (argc==1) { printf("Interactive mode.\n"); } else if (argc==2) { char *buffer = read_file(argv[1]); jshInit(); jsiInit(false /* do not autoload!!! */); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jsvUnLock(jspEvaluate(jsiGetParser(), buffer )); free(buffer); isRunning = true; while (isRunning && jsiHasTimers()) jsiLoop(); jsiKill(); jshKill(); exit(0); } else { printf("Unknown arguments!\n"); show_help(); exit(1); } printf("Size of JsVar is now %d bytes\n", (int)sizeof(JsVar)); printf("Size of JsVarRef is now %d bytes\n", (int)sizeof(JsVarRef)); struct sigaction sa; sa.sa_handler = sig_handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIGINT, &sa, NULL) == -1) printf("Adding SIGINT hook failed\n"); else printf("Added SIGINT hook\n"); if (sigaction(SIGHUP, &sa, NULL) == -1) printf("Adding SIGHUP hook failed\n"); else printf("Added SIGHUP hook\n"); if (sigaction(SIGTERM, &sa, NULL) == -1) printf("Adding SIGTERM hook failed\n"); else printf("Added SIGTERM hook\n"); jshInit(); jsiInit(true); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jspAddNativeFunction(jsiGetParser(), "function interrupt()", nativeInterrupt); while (isRunning) { jsiLoop(); } jsiConsolePrint("\n"); jsiKill(); jsvShowAllocated(); jshKill(); return 0; }
// --------------------------------------------------------------------------- // Error handlers... void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name) { jsiConsolePrintf("NRF ERROR 0x%x at %s:%d\n", error_code, p_file_name, line_num); jsiConsolePrint("REBOOTING.\n"); jshTransmitFlush(); NVIC_SystemReset(); }
/*JSON{ "type" : "staticmethod", "class" : "WIZnet", "name" : "connect", "generate" : "jswrap_wiznet_connect", "params" : [ ["spi", "JsVar", "Device to use for SPI (or undefined to use the default)"], ["cs", "pin", "The pin to use for Chip Select"] ], "return" : ["JsVar","An Ethernet Object"], "return_object" : "Ethernet" } Initialise the WIZnet module and return an Ethernet object */ JsVar *jswrap_wiznet_connect(JsVar *spi, Pin cs) { IOEventFlags spiDevice; if (spi) { spiDevice = jsiGetDeviceFromClass(spi); if (!DEVICE_IS_SPI(spiDevice)) { jsExceptionHere(JSET_ERROR, "Expecting SPI device, got %q", spi); return 0; } } else { // SPI config JshSPIInfo inf; jshSPIInitInfo(&inf); inf.pinSCK = ETH_CLK_PIN; inf.pinMISO = ETH_MISO_PIN; inf.pinMOSI = ETH_MOSI_PIN; inf.baudRate = 1000000; inf.spiMode = SPIF_SPI_MODE_0; jshSPISetup(ETH_SPI, &inf); spiDevice = ETH_SPI; } if (!jshIsPinValid(cs)) cs = ETH_CS_PIN; JsNetwork net; networkCreate(&net, JSNETWORKTYPE_W5500); net.data.device = spiDevice; net.data.pinCS = cs; networkSet(&net); JsVar *ethObj = jspNewObject(0, "Ethernet"); // CS Configuration jshSetPinStateIsManual(net.data.pinCS, false); jshPinOutput(net.data.pinCS, 1); // de-assert CS // Initialise WIZnet functions reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); reg_wizchip_spi_cbfunc(wizchip_read, wizchip_write); /* wizchip initialize*/ uint8_t tmp; uint8_t memsize[2][8] = { {2,2,2,2,2,2,2,2}, {2,2,2,2,2,2,2,2}}; if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1) { jsiConsolePrint("WIZnet Initialize failed.\r\n"); networkFree(&net); return 0; } #if _WIZCHIP_ == 5500 /* PHY link status check - W5100 doesn't have this */ do { if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1) { jsiConsolePrint("Unknown PHY Link status.\r\n"); networkFree(&net); return 0; } } while (tmp == PHY_LINK_OFF); #endif networkFree(&net); networkState = NETWORKSTATE_ONLINE; return ethObj; }
int espruino(int argc, char **argv) { int i; for (i=1;i<argc;i++) { if (argv[i][0]=='-') { // option char *a = argv[i]; if (!strcmp(a,"-h") || !strcmp(a,"--help")) { show_help(); return (1); } else if (!strcmp(a,"-e") || !strcmp(a,"--eval")) { if (i+1>=argc) die("Expecting an extra argument\n"); jshInit(); jsiInit(true); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jsvUnLock(jspEvaluate(jsiGetParser(), argv[i+1])); isRunning = true; //while (isRunning && jsiHasTimers()) jsiLoop(); jsiKill(); jshKill(); return (0); } else if (!strcmp(a,"--test")) { if (i+1>=argc) die("Expecting an extra argument\n"); bool ok = run_test(argv[i+1]); return (ok ? 0 : 1); } else if (!strcmp(a,"--test-all")) { bool ok = run_all_tests(); return (ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-all")) { bool ok = run_memory_tests(0); return (ok ? 0 : 1); } else if (!strcmp(a,"--test-mem")) { if (i+1>=argc) die("Expecting an extra argument\n"); bool ok = run_memory_test(argv[i+1], 0); return (ok ? 0 : 1); } else if (!strcmp(a,"--test-mem-n")) { if (i+2>=argc) die("Expecting an extra 2 arguments\n"); bool ok = run_memory_test(argv[i+1], atoi(argv[i+2])); return (ok ? 0 : 1); } else { printf("Unknown Argument %s\n", a); show_help(); return -1; } } } if (argc==1) { printf("Interactive mode.\n"); } else if (argc==2) { // single file - just run it char *buffer = (char *)read_file(argv[1]); if (!buffer) return (1); // check for '#' as the first char, and if so, skip the first line char *cmd = buffer; if (cmd[0]=='#') { while (cmd[0] && cmd[0]!='\n') cmd++; if (cmd[0]=='\n') cmd++; } jshInit(); jsiInit(false /* do not autoload!!! */); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jsvUnLock(jspEvaluate(jsiGetParser(), cmd )); free(buffer); //isRunning = true; //while (isRunning && jsiHasTimers()) jsiLoop(); jsiKill(); jshKill(); return -1; } else { printf("Unknown arguments!\n"); show_help(); return -1; } printf("Size of JsVar is now %d bytes\n", (int)sizeof(JsVar)); printf("Size of JsVarRef is now %d bytes\n", (int)sizeof(JsVarRef)); jshInit(); jsiInit(true); jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit); jspAddNativeFunction(jsiGetParser(), "function interrupt()", nativeInterrupt); while (isRunning) { jsiLoop(); } jsiConsolePrint("\n"); jsiKill(); jsvShowAllocated(); jshKill(); return 0; }