Example #1
0
static void wifiCallback(JsVar *data) {
  wice_msg_start(&wifiMessage); //start timer if not running
  if (wice_msg_isTimeout(&wifiMessage)){
    wice_msg_reset(&wifiMessage);
  } else {
    char *buffer[1024];
    jsvGetString(data, buffer, sizeof(buffer));
    int didAdd = wice_msg_add(&wifiMessage, buffer);
  }

  if (wice_msg_isComplete(&wifiMessage)){
    char *msg = wifiMessage.buffer;
    char *ledloc = strstr(msg, "led");
    if (ledloc != 0){
      switch (ledloc[6]){
        case 'R': blink(PIN_RED, 100); break;
        case 'G': blink(PIN_GRN, 100); break;
        case 'B': blink(PIN_BLUE, 100); break;
      }
    } 
    if (strstr(msg, MSG_WIFI_CONNECTED_YES) != 0) {
      connected = true; //start sending wifi messages
    }
    if (wice_msg_isFunction(&wifiMessage)){
      jspEvaluate(msg); //evalute in case server wants to say something
    }
    wice_msg_reset(&wifiMessage);
  }
}
Example #2
0
static void dash7Callback(JsVar *data){
  blink(PIN_BLUE, 20);
  hasConnection = true;
#ifdef GATEWAY
  wice_msg_start(&dash7Message); //start timer if not running
  if (wice_msg_isTimeout(&dash7Message)){
    wice_msg_reset(&dash7Message);
  } else {
    char buffer[1024];
    jsvGetString(data, buffer, sizeof(buffer));
    int didAdd = wice_msg_add(&dash7Message, buffer);
  }

  if (wice_msg_isComplete(&dash7Message)){
    char *msg = dash7Message.buffer;
    blink(PIN_GRN, 100);
    blink(PIN_GRN, 100);
    if (connected) {
      JsVar *str = jsvVarPrintf("%s", msg);
      JsVar *serial = jspGetNamedField(execInfo.root, SERIAL4_WIFI, false);
      jswrap_serial_print(serial, str);
      jsvUnLock(serial);
      jsvUnLock(str);
    }

    if (wice_msg_isFunction(&dash7Message)){
      jspEvaluate(msg); //evalute in case server wants to say something
    }
    wice_msg_reset(&dash7Message);
    sendReplyToListeningNodes("function(){digitalWrite(A14, 1);}();");
  }
#else
  WDEBUGSTRVAR(data);
#endif
}
Example #3
0
bool run_test(const char *filename) {
  printf("----------------------------------\r\n");
  printf("----------------------------- TEST %s \r\n", filename);
  char *buffer = read_file(filename);
  if (!buffer) exit(1);

  jshInit();
  jsvInit();
  jsiInit(false /* do not autoload!!! */);

  addNativeFunction("quit", nativeQuit);
  addNativeFunction("interrupt", nativeInterrupt);

  jsvUnLock(jspEvaluate(buffer, true));

  isRunning = true;
  bool isBusy = true;
  while (isRunning && (jsiHasTimers() || isBusy))
    isBusy = jsiLoop();

  JsVar *result = jsvObjectGetChild(execInfo.root, "result", 0/*no create*/);
  bool pass = jsvGetBool(result);
  jsvUnLock(result);

  if (pass)
    printf("----------------------------- PASS %s\r\n", filename);
  else {
    printf("----------------------------------\r\n");
    printf("----------------------------- FAIL %s <-------\r\n", filename);
    jsvTrace(execInfo.root, 0);
    printf("----------------------------- FAIL %s <-------\r\n", filename);
    printf("----------------------------------\r\n");
  }
  printf("BEFORE: %d Memory Records Used\r\n", jsvGetMemoryUsage());
 // jsvTrace(execInfo.root, 0);
  jsiKill();
  printf("AFTER: %d Memory Records Used\r\n", jsvGetMemoryUsage());
  jsvGarbageCollect();
  unsigned int unfreed = jsvGetMemoryUsage();
  printf("AFTER GC: %d Memory Records Used (should be 0!)\r\n", unfreed);
  jsvShowAllocated();
  jsvKill();
  jshKill();

  if (unfreed) {
    printf("FAIL because of unfreed memory.\r\n");
    pass = false;
  }

  //jsvDottyOutput();
  printf("\r\n");

  free(buffer);
  return pass;
}
Example #4
0
bool run_test(const char *filename) {
  printf("----------------------------------\r\n");
  printf("----------------------------- TEST %s \r\n", filename);
  char *buffer = (char *)read_file(filename);
  if (!buffer) return (false);

  jshInit();
  jsiInit(false /* do not autoload!!! */);

  jspAddNativeFunction(jsiGetParser(), "function quit()", nativeQuit);
  jspAddNativeFunction(jsiGetParser(), "function interrupt()", nativeInterrupt);

  jsvUnLock(jspEvaluate(jsiGetParser(), buffer ));

  isRunning = true;
  while (isRunning && jsiHasTimers()) jsiLoop();

  JsVar *result = jsvObjectGetChild(jsiGetParser()->root, "result", 0/*no create*/);
  bool pass = jsvGetBool(result);
  jsvUnLock(result);

  if (pass)
    printf("----------------------------- PASS %s\r\n", filename);
  else {
    printf("----------------------------------\r\n");
    printf("----------------------------- FAIL %s <-------\r\n", filename);
    jsvTrace(jsvGetRef(jsiGetParser()->root), 0);
    printf("----------------------------- FAIL %s <-------\r\n", filename);
    printf("----------------------------------\r\n");
  }
  printf("BEFORE: %d Memory Records Used\r\n", jsvGetMemoryUsage());
 // jsvTrace(jsiGetParser()->root, 0);
  jsiKill();
  printf("AFTER: %d Memory Records Used\r\n", jsvGetMemoryUsage());
  jsvGarbageCollect();
  printf("AFTER GC: %d Memory Records Used (should be 0!)\r\n", jsvGetMemoryUsage());
  jsvShowAllocated();
  jshKill();

  //jsvDottyOutput();
  printf("\r\n");

  free(buffer);
  return pass;
}
Example #5
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\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;
}
Example #6
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;
}
Example #7
0
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;
}
Example #8
0
/*JSON{
  "type" : "init",
  "generate" : "jswrap_wice_init"
}*/
void jswrap_wice_init() {
  jspCallNamedFunction(execInfo.root, "USB.setConsole", 0, 0);
  jspEvaluate("USB.setConsole();");
#ifdef WICE_DEBUG
  WDEBUGLN("USB is console");
#endif
  /*JsVar *mode = jsvNewFromString("output");
  jswrap_io_pinMode(jshGetPinFromString(PIN_DASH7_XTAL_EN), mode);
  jswrap_io_pinMode(jshGetPinFromString(PIN_DASH7_RST), mode);
  jsvUnLock(mode);*/

  enableDASH();

  // Set up the DASH7 USART how we want it
  JshUSARTInfo inf;
  jshUSARTInitInfo(&inf);
  inf.baudRate = 115200;
  inf.pinRX = jshGetPinFromString(PIN_DASH7_RX);
  inf.pinTX = jshGetPinFromString(PIN_DASH7_TX);
  jshUSARTSetup(EV_SERIAL1, &inf);

  JsVar *serial = jspGetNamedField(execInfo.root, SERIAL1_DASH7, false);
  jswrap_object_addEventListener(serial, "data", dash7Callback, JSWAT_VOID | (JSWAT_JSVAR<<(JSWAT_BITS)));
  jsvUnLock(serial);

  startTime = jshGetSystemTime();

  wice_msg_init(&wifiMessage, wifiMessageBuffer, 2048);
  wice_msg_init(&dash7Message, dash7MessageBuffer, 256);


  options = jspEvaluate("x = {\"repeat\": \"true\", \"edge\": \"rising\", \"debounce\":\"50\"}");
  Pin btn = jshGetPinFromString("B10");
  JsVar *btnmode = jsvNewFromString("input");
  jswrap_io_pinMode(btn, btnmode);
  jsvUnLock(btnmode);

#ifdef GATEWAY
  WDEBUGLN("GATEWAY");
  blink(PIN_GRN, 50);
  /// opendrain and digitalwrite 1 will 'opencircuit it'
  /// http://www.espruino.com/Reference#l__global_pinMode
  JsVar *opendrain = jsvNewFromString("opendrain");
  WDEBUGSTRVAR(opendrain);
  jswrap_io_pinMode(jshGetPinFromString(PIN_ESP_GPIO_0), opendrain);
  jswrap_io_pinMode(jshGetPinFromString(PIN_ESP_GPIO_2), opendrain);
  jshPinOutput(jshGetPinFromString(PIN_ESP_GPIO_0), 1);
  jshPinOutput(jshGetPinFromString(PIN_ESP_GPIO_2), 1);
  jsvUnLock(opendrain);
  /// must opendrain gpio0/2 because of boot modes
  /// https://github.com/esp8266/esp8266-wiki/wiki/Boot-Process
  enableWifi();

  // Set up the Wifi USART how we want it
  JshUSARTInfo inf4;
  jshUSARTInitInfo(&inf4);
  inf4.baudRate = 115200;
  inf4.pinRX = jshGetPinFromString(PIN_ESP_RX);
  inf4.pinTX = jshGetPinFromString(PIN_ESP_TX);
  jshUSARTSetup(EV_SERIAL4, &inf4);

  /// make button restart wifi for gateway
  JsVar *restartWifi_fn = jsvNewNativeFunction(restartWifi, JSWAT_VOID);
  btnEvent = jswrap_interface_setWatch(restartWifi_fn,
                            btn, 
                            options);
  jsvUnLock(restartWifi_fn);
  /// configure wifi usart callback
  JsVar *wifiSerial = jspGetNamedField(execInfo.root, SERIAL4_WIFI, false);
  jswrap_object_addEventListener(wifiSerial, "data", wifiCallback, JSWAT_VOID | (JSWAT_JSVAR<<(JSWAT_BITS)));
  jsvUnLock(wifiSerial);
  doSendSerial();
#else 
  WDEBUGLN("NODE");
  blink(PIN_BLUE, 50);
  /// make button a forced measurement for nodes
  JsVar *doMeasurementAndWaitForResponse_fn = jsvNewNativeFunction(doMeasurementAndWaitForResponse, JSWAT_VOID);
  btnEvent = jswrap_interface_setWatch(doMeasurementAndWaitForResponse_fn,
                            btn, 
                            options);
  jsvUnLock(doMeasurementAndWaitForResponse_fn);

  /// set up main interval callback for nodes
  JsVar *doMeasurement_fn = jsvNewNativeFunction(doMeasurement, JSWAT_VOID);
  currentInterval = jswrap_interface_setInterval(doMeasurement_fn, INTERVAL, 0);
  jsvUnLock(doMeasurement_fn);

  /// prepare the I2C bus for talking with the Si7050 temp sensor
  JsVar *s = jspEvaluate("I2C1.setup({scl:B8, sda:B9, bitrate:50000});"); jsvUnLock(s);
  disableDASH();
  jswrap_interface_setDeepSleep(true); //do deep sleep [TODO can we wake on press?]
#endif
}
Example #9
0
void getSerial(char *serialBuffer, int size){
  JsVar *serialNumberString = jspEvaluate("getSerial().toString();");
  jsvGetString(serialNumberString, serialBuffer, size);
  jsvUnLock(serialNumberString);
}