void esp_sj_uart_dispatcher(void *arg) { int uart_no = (int) arg; struct esp_sj_uart_state *us = &sj_us[uart_no]; cs_rbuf_t *txb = esp_uart_tx_buf(uart_no); us->dispatch_pending = 0; esp_uart_dispatch_rx_top(uart_no); uint16_t tx_used_before = txb->used; /* ignore unused because of CS_DISABLE_JS below */ (void) tx_used_before; esp_uart_dispatch_tx_top(uart_no); #ifndef CS_DISABLE_JS cs_rbuf_t *rxb = esp_uart_rx_buf(uart_no); if (us->v7 != NULL) { /* Detect the edge of TX buffer becoming empty. */ if (tx_used_before > 0 && txb->used == 0) { v7_val_t txcb = v7_get(us->v7, us->obj, "_txcb", 5); if (v7_is_callable(us->v7, txcb)) { sj_invoke_cb(us->v7, txcb, us->obj, V7_UNDEFINED); } } if (rxb->used > 0) { /* Check for JS recv handler. */ v7_val_t rxcb = esp_sj_uart_get_recv_handler(uart_no); if (v7_is_callable(us->v7, rxcb)) { if (!us->recv_pending) { us->recv_pending = 1; sj_invoke_cb(us->v7, rxcb, us->obj, V7_UNDEFINED); /* Note: Callback has not run yet, it has been scheduled. */ } } else if (us->prompt) { uint8_t *cp; while (rxb != NULL && cs_rbuf_get(rxb, 1, &cp) == 1) { char c = (char) *cp; cs_rbuf_consume(rxb, 1); sj_prompt_process_char(c); /* UART could've been re-initialized by the command from prompt. */ rxb = esp_uart_rx_buf(uart_no); } } } } #endif esp_uart_dispatch_bottom(uart_no); }
void sj_invoke_cb0_this(struct v7 *v7, v7_val_t cb, v7_val_t this_obj) { v7_val_t args; v7_own(v7, &cb); args = v7_mk_array(v7); v7_own(v7, &args); sj_invoke_cb(v7, cb, this_obj, args); v7_disown(v7, &args); v7_disown(v7, &cb); }
void sj_invoke_cb2(struct v7 *v7, v7_val_t cb, v7_val_t arg1, v7_val_t arg2) { v7_val_t args; v7_own(v7, &cb); v7_own(v7, &arg1); v7_own(v7, &arg2); args = v7_create_array(v7); v7_own(v7, &args); v7_array_push(v7, args, arg1); v7_array_push(v7, args, arg2); sj_invoke_cb(v7, cb, v7_get_global(v7), args); v7_disown(v7, &args); v7_disown(v7, &arg2); v7_disown(v7, &arg1); v7_disown(v7, &cb); }
void sj_invoke_cb2_this(struct v7 *v7, v7_val_t cb, v7_val_t this_obj, v7_val_t arg1, v7_val_t arg2) { v7_val_t args; v7_own(v7, &cb); v7_own(v7, &arg1); v7_own(v7, &arg2); args = v7_mk_array(v7); v7_own(v7, &args); v7_array_push(v7, args, arg1); v7_array_push(v7, args, arg2); sj_invoke_cb(v7, cb, this_obj, args); v7_disown(v7, &args); v7_disown(v7, &arg2); v7_disown(v7, &arg1); v7_disown(v7, &cb); }