/** * The event handler for ESP8266 tasks as created by system_os_post() on the TASK_APP_QUEUE. */ static void eventHandler( os_event_t *pEvent //!< ) { switch (pEvent->sig) { // Handle the main loop event. case TASK_APP_MAINLOOP: mainLoop(); break; // Handle the event to process received data. case TASK_APP_RX_DATA: { // Get the data from the UART RX buffer. If the size of the returned data is // not zero, then push it onto the Espruino processing queue for characters. char pBuffer[100]; int size = getRXBuffer(pBuffer, sizeof(pBuffer)); if (size > 0) { jshPushIOCharEvents(jsiGetConsoleDevice(), pBuffer, size); } } break; // Handle the unknown event type. default: os_printf("user_main: eventHandler: Unknown task type: %ld", pEvent->sig); break; } }
void jshInputThread() { while (isInitialised) { bool shortSleep = false; /* Handle the delayed Ctrl-C -> interrupt behaviour (see description by EXEC_CTRL_C's definition) */ if (execInfo.execute & EXEC_CTRL_C_WAIT) execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C_WAIT) | EXEC_INTERRUPTED; if (execInfo.execute & EXEC_CTRL_C) execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C) | EXEC_CTRL_C_WAIT; // Read from the console while (kbhit()) { int ch = getch(); if (ch<0) break; jshPushIOCharEvent(EV_USBSERIAL, (char)ch); } // Read from any open devices - if we have space if (jshGetEventsUsed() < IOBUFFERMASK/2) { int i; for (i=0;i<=EV_DEVICE_MAX;i++) { if (ioDevices[i]) { char buf[32]; // read can return -1 (EAGAIN) because O_NONBLOCK is set int bytes = (int)read(ioDevices[i], buf, sizeof(buf)); if (bytes>0) { //int j; for (j=0;j<bytes;j++) printf("]] '%c'\r\n", buf[j]); jshPushIOCharEvents(i, buf, (unsigned int)bytes); shortSleep = true; } } } } // Write any data we have IOEventFlags device = jshGetDeviceToTransmit(); while (device != EV_NONE) { char ch = (char)jshGetCharToTransmit(device); //printf("[[ '%c'\r\n", ch); if (ioDevices[device]) { write(ioDevices[device], &ch, 1); shortSleep = true; } device = jshGetDeviceToTransmit(); } #ifdef SYSFS_GPIO_DIR Pin pin; for (pin=0;pin<JSH_PIN_COUNT;pin++) if (gpioShouldWatch[pin]) { shortSleep = true; bool state = jshPinGetValue(pin); if (state != gpioLastState[pin]) { jshPushIOEvent(pinToEVEXTI(pin) | (state?EV_EXTI_IS_HIGH:0), jshGetSystemTime()); gpioLastState[pin] = state; } } #endif usleep(shortSleep ? 1000 : 50000); } }
/** * A callback function to be invoked when a line has been entered on the telnet client. * Here we want to pass that line to the JS parser for processing. */ static void telnetLineCB(char *line) { jsiConsolePrintf("LineCB: %s", line); // Pass the line to the interactive module ... jshPushIOCharEvents(jsiGetConsoleDevice(), line, strlen(line)); //jspEvaluate(line); //jsiDumpState(); telnet_send("JS> "); } // End of lineCB
// Attempt to receive on an established client connection, returns true if it received something bool telnetRecv(JsNetwork *net) { if (tnSrv.sock == 0 || tnSrv.cliSock == 0) return false; char buff[256]; int r = netRecv(net, tnSrv.cliSock, buff, 256); if (r > 0) { jshPushIOCharEvents(EV_TELNET, buff, r); } else if (r < 0) { telnetRelease(net); } if (r != 0) { //printf("tnSrv: recv sock=%d, %d bytes\n", tnSrv.sock, r); } return r != 0; }