static uint8_t _dispatch() { uint8_t status; // read input line or return if not a completed line // xio_gets() is a non-blocking workalike of fgets() if ((status = xio_gets(tg.src, tg.in_buf, sizeof(tg.in_buf))) != TG_OK) { if (status == TG_EOF) { // EOF can come from file devices only fprintf_P(stderr, PSTR("End of command file\n")); tg_reset_source(); // reset to default source } // Note that TG_EAGAIN, TG_NOOP etc. will just flow through return (status); } tg.linelen = strlen(tg.in_buf)+1; strncpy(tg.saved_buf, tg.in_buf, SAVED_BUFFER_LEN-1); // save input buffer for reporting // dispatch the new text line switch (toupper(tg.in_buf[0])) { case NUL: { // blank line (just a CR) if (cfg.comm_mode != JSON_MODE) { tg_text_response(TG_OK, tg.saved_buf); } break; } case 'H': { // intercept help screens cfg.comm_mode = TEXT_MODE; print_general_help(); tg_text_response(TG_OK, tg.in_buf); break; } case '$': case '?':{ // text-mode configs cfg.comm_mode = TEXT_MODE; tg_text_response(cfg_text_parser(tg.in_buf), tg.saved_buf); break; } case '{': { // JSON input cfg.comm_mode = JSON_MODE; js_json_parser(tg.in_buf); break; } default: { // anything else must be Gcode if (cfg.comm_mode == JSON_MODE) { strncpy(tg.out_buf, tg.in_buf, INPUT_BUFFER_LEN -8); // use out_buf as temp sprintf(tg.in_buf,"{\"gc\":\"%s\"}\n", tg.out_buf); // '-8' is used for JSON chars js_json_parser(tg.in_buf); } else { tg_text_response(gc_gcode_parser(tg.in_buf), tg.saved_buf); } } } return (TG_OK); }
static stat_t _dispatch() { uint8_t status; // read input line or return if not a completed line // xio_gets() is a non-blocking workalike of fgets() while (true) { if ((status = xio_gets(tg.primary_src, tg.in_buf, sizeof(tg.in_buf))) == STAT_OK) { tg.bufp = tg.in_buf; break; } // handle end-of-file from file devices if (status == STAT_EOF) { // EOF can come from file devices only if (cfg.comm_mode == TEXT_MODE) { fprintf_P(stderr, PSTR("End of command file\n")); } else { rpt_exception(STAT_EOF, 0); // not really an exception } tg_reset_source(); // reset to default source } return (status); // Note: STAT_EAGAIN, errors, etc. will drop through } tg.linelen = strlen(tg.in_buf)+1; // linelen only tracks primary input strncpy(tg.saved_buf, tg.bufp, SAVED_BUFFER_LEN-1); // save input buffer for reporting // dispatch the new text line switch (toupper(*tg.bufp)) { // first char // case '!': { cm_request_feedhold(); break; } // include for diagnostics // case '@': { cm_request_queue_flush(); break; } // case '~': { cm_request_cycle_start(); break; } case NUL: { // blank line (just a CR) if (cfg.comm_mode != JSON_MODE) { tg_text_response(STAT_OK, tg.saved_buf); } break; } case 'H': { // intercept help screens cfg.comm_mode = TEXT_MODE; print_general_help(); tg_text_response(STAT_OK, tg.bufp); break; } case '$': case '?':{ // text-mode configs cfg.comm_mode = TEXT_MODE; tg_text_response(cfg_text_parser(tg.bufp), tg.saved_buf); break; } case '{': { // JSON input cfg.comm_mode = JSON_MODE; js_json_parser(tg.bufp); break; } default: { // anything else must be Gcode if (cfg.comm_mode == JSON_MODE) { strncpy(tg.out_buf, tg.bufp, INPUT_BUFFER_LEN -8); // use out_buf as temp sprintf(tg.bufp,"{\"gc\":\"%s\"}\n", tg.out_buf); js_json_parser(tg.bufp); } else { tg_text_response(gc_gcode_parser(tg.bufp), tg.saved_buf); } } } return (STAT_OK); }