/** Write the pin name to a string. String must have at least 8 characters (to be safe) */ void jshGetPinString(char *result, Pin pin) { result[0] = 0; // just in case #ifdef PIN_NAMES_DIRECT if (jshIsPinValid(pin)) { result[0]='A'+pinInfo[pin].port-JSH_PORTA; itostr(pinInfo[pin].pin-JSH_PIN0,&result[1],10); #else if ( #if JSH_PORTA_OFFSET!=0 pin>=JSH_PORTA_OFFSET && #endif pin<JSH_PORTA_OFFSET+JSH_PORTA_COUNT) { result[0]='A'; itostr(pin-JSH_PORTA_OFFSET,&result[1],10); } else if (pin>=JSH_PORTB_OFFSET && pin<JSH_PORTB_OFFSET+JSH_PORTB_COUNT) { result[0]='B'; itostr(pin-JSH_PORTB_OFFSET,&result[1],10); } else if (pin>=JSH_PORTC_OFFSET && pin<JSH_PORTC_OFFSET+JSH_PORTC_COUNT) { result[0]='C'; itostr(pin-JSH_PORTC_OFFSET,&result[1],10); } else if (pin>=JSH_PORTD_OFFSET && pin<JSH_PORTD_OFFSET+JSH_PORTD_COUNT) { result[0]='D'; itostr(pin-JSH_PORTD_OFFSET,&result[1],10); #if JSH_PORTE_OFFSET!=-1 } else if (pin>=JSH_PORTE_OFFSET && pin<JSH_PORTE_OFFSET+JSH_PORTE_COUNT) { result[0]='E'; itostr(pin-JSH_PORTE_OFFSET,&result[1],10); #endif #if JSH_PORTF_OFFSET!=-1 } else if (pin>=JSH_PORTF_OFFSET && pin<JSH_PORTF_OFFSET+JSH_PORTF_COUNT) { result[0]='F'; itostr(pin-JSH_PORTF_OFFSET,&result[1],10); #endif #if JSH_PORTG_OFFSET!=-1 } else if (pin>=JSH_PORTG_OFFSET && pin<JSH_PORTG_OFFSET+JSH_PORTG_COUNT) { result[0]='G'; itostr(pin-JSH_PORTG_OFFSET,&result[1],10); #endif #if JSH_PORTH_OFFSET!=-1 } else if (pin>=JSH_PORTH_OFFSET && pin<JSH_PORTH_OFFSET+JSH_PORTH_COUNT) { result[0]='H'; itostr(pin-JSH_PORTH_OFFSET,&result[1],10); #endif #endif } else { strncpy(result, "UNKNOWN", 8); } } /// Given a var, convert it to a pin ID (or -1 if it doesn't exist). safe for undefined! Pin jshGetPinFromVar(JsVar *pinv) { if (jsvIsString(pinv) && pinv->varData.str[5]==0/*should never be more than 4 chars!*/) { return jshGetPinFromString(&pinv->varData.str[0]); } else if (jsvIsInt(pinv) /* This also tests for the Pin datatype */) { return (Pin)jsvGetInteger(pinv); } else return PIN_UNDEFINED; } Pin jshGetPinFromVarAndUnLock(JsVar *pinv) { Pin pin = jshGetPinFromVar(pinv); jsvUnLock(pinv); return pin; }
/*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 }
void disableWifi() { jshPinOutput(jshGetPinFromString(PIN_ESP_RST), 0); jshPinOutput(jshGetPinFromString(PIN_ESP_CP), 0); }
void disableDASH(){ /// TODO REINSTATE jshPinOutput(jshGetPinFromString(PIN_DASH7_XTAL_EN), 0); jshPinOutput(jshGetPinFromString(PIN_DASH7_RST), 0); }
void enableDASH(){ WDEBUGLN("enabling DASH"); jshPinOutput(jshGetPinFromString(PIN_DASH7_XTAL_EN), 1); jshPinOutput(jshGetPinFromString(PIN_DASH7_RST), 1); delay(80); //TODO: figure out why dash7 needs 80ms startup }
void blink(const char * pin, int msec){ jshPinOutput(jshGetPinFromString(pin), 1); delay(msec/2); jshPinOutput(jshGetPinFromString(pin), 0); delay(msec/2); }