EReturn getVector(const tinyxml2::XMLElement & xml_vector, Eigen::VectorXd & eigen_vector) { //!< Temporaries double temp_entry; int i = 0; if (!xml_vector.GetText()) { INDICATE_FAILURE ; eigen_vector = Eigen::VectorXd(); //!< Null matrix again return PAR_ERR; } std::istringstream text_parser(xml_vector.GetText()); //!< Initialise looping text_parser >> temp_entry; while (!(text_parser.fail() || text_parser.bad())) //!< No commenting! { eigen_vector.conservativeResize(++i); //!< Allocate storage for this entry (by increasing i) eigen_vector(i - 1) = temp_entry; text_parser >> temp_entry; } return (i > 0) ? SUCCESS : PAR_ERR; }
EReturn getMatrix(const tinyxml2::XMLElement & xml_matrix, Eigen::MatrixXd & eigen_matrix) { int dimension = 0; if (xml_matrix.QueryIntAttribute("dim", &dimension) != tinyxml2::XML_NO_ERROR) { INDICATE_FAILURE ; eigen_matrix = Eigen::MatrixXd(); //!< Null matrix again return PAR_ERR; } if (dimension < 1) { INDICATE_FAILURE ; eigen_matrix = Eigen::MatrixXd(); //!< Null matrix again return PAR_ERR; } eigen_matrix.resize(dimension, dimension); if (!xml_matrix.GetText()) { INDICATE_FAILURE ; eigen_matrix = Eigen::MatrixXd(); //!< Null matrix again return PAR_ERR; } std::istringstream text_parser(xml_matrix.GetText()); double temp_entry; for (int i = 0; i < dimension; i++) //!< Note that this does not handle comments! { for (int j = 0; j < dimension; j++) { text_parser >> temp_entry; if (text_parser.fail() || text_parser.bad()) //!< If a failure other than end of file { INDICATE_FAILURE ; eigen_matrix.resize(0, 0); return PAR_ERR; } else { eigen_matrix(i, j) = temp_entry; } } } return SUCCESS; }
stat_t _command_dispatch() { #ifdef __AVR stat_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(cs.primary_src, cs.in_buf, sizeof(cs.in_buf))) == STAT_OK) { cs.bufp = cs.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); // not really an exception } tg_reset_source(); // reset to default source } return (status); // Note: STAT_EAGAIN, errors, etc. will drop through } #endif // __AVR #ifdef __ARM // detect USB connection and transition to disconnected state if it disconnected if (SerialUSB.isConnected() == false) cs.state = CONTROLLER_NOT_CONNECTED; // read input line and return if not a completed line if (cs.state == CONTROLLER_READY) { if (read_line(cs.in_buf, &cs.read_index, sizeof(cs.in_buf)) != STAT_OK) { cs.bufp = cs.in_buf; return (STAT_OK); // This is an exception: returns OK for anything NOT OK, so the idler always runs } } else if (cs.state == CONTROLLER_NOT_CONNECTED) { if (SerialUSB.isConnected() == false) return (STAT_OK); cm_request_queue_flush(); rpt_print_system_ready_message(); cs.state = CONTROLLER_STARTUP; } else if (cs.state == CONTROLLER_STARTUP) { // run startup code cs.state = CONTROLLER_READY; } else { return (STAT_OK); } cs.read_index = 0; #endif // __ARM #ifdef __RX stat_t status; parse_gcode_func_selection(CODE_PARSER); // 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(cs.primary_src, cs.in_buf, sizeof(cs.in_buf))) == STAT_OK) { cs.bufp = cs.in_buf; break; } // handle end-of-file from file devices if (status == STAT_EOF) { // EOF can come from file devices only //gfilerunning = false; xio_close(cs.primary_src); // macro_func_ptr = command_idle; if (cfg.comm_mode == TEXT_MODE) { fprintf_P(stderr, PSTR("End of command file\n")); } else { rpt_exception(STAT_EOF); // not really an exception } tg_reset_source(); // reset to default source } return (status); // Note: STAT_EAGAIN, errors, etc. will drop through } #endif // __AVR // set up the buffers cs.linelen = strlen(cs.in_buf)+1; // linelen only tracks primary input strncpy(cs.saved_buf, cs.bufp, SAVED_BUFFER_LEN-1); // save input buffer for reporting // dispatch the new text line switch (toupper(*cs.bufp)) { // first char case '!': { cm_request_feedhold(); break; } // include for AVR diagnostics and ARM serial 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) { text_response(STAT_OK, cs.saved_buf); } break; } case '$': case '?': case 'H': { // text mode input cfg.comm_mode = TEXT_MODE; text_response(text_parser(cs.bufp), cs.saved_buf); break; } case '{': { // JSON input cfg.comm_mode = JSON_MODE; json_parser(cs.bufp); break; } default: { // anything else must be Gcode if (cfg.comm_mode == JSON_MODE) { // run it as JSON... strncpy(cs.out_buf, cs.bufp, INPUT_BUFFER_LEN -8); // use out_buf as temp sprintf((char *)cs.bufp,"{\"gc\":\"%s\"}\n", (char *)cs.out_buf); // '-8' is used for JSON chars json_parser(cs.bufp); } else { //...or run it as text text_response(gc_gcode_parser(cs.bufp), cs.saved_buf); } } } return (STAT_OK); }