void nv_read_parameter_int(uint32_t* val) { nvObj_t *nv = nv_reset_nv_list(); for (uint16_t i = 0; i < nv_index_max(); i++) { if (cfgArray[i].target == val) { nv->index = i; break; } } strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); read_persistent_value(nv); nv_set(nv); }
static void _set_defa(nvObj_t *nv) { cm_set_units_mode(MILLIMETERS); // must do inits in MM mode for (nv->index=0; nv_index_is_single(nv->index); nv->index++) { if (GET_TABLE_BYTE(flags) & F_INITIALIZE) { nv->value = GET_TABLE_FLOAT(def_value); strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); nv_set(nv); nv_persist(nv); } } sr_init_status_report(); // reset status reports rpt_print_initializing_message(); // don't start TX until all the NVM persistence is done }
void nv_save_parameter_flt(float* val) { nvObj_t *nv = nv_reset_nv_list(); for (uint16_t i = 0; i < nv_index_max(); i++) { if (cfgArray[i].target == val) { nv->index = i; break; } } nv->value = *val; strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); nv_set(nv); nv_persist(nv); }
stat_t set_grp(nvObj_t *nv) { if (cfg.comm_mode == TEXT_MODE) return (STAT_INVALID_OR_MALFORMED_COMMAND); for (uint8_t i=0; i<NV_MAX_OBJECTS; i++) { if ((nv = nv->nx) == NULL) break; if (nv->valuetype == TYPE_EMPTY) break; else if (nv->valuetype == TYPE_NULL) // NULL means GET the value nv_get(nv); else { nv_set(nv); nv_persist(nv); } } return (STAT_OK); }
static stat_t _json_parser_kernal(char *str) { stat_t status; int8_t depth; nvObj_t *nv = nv_reset_nv_list(); // get a fresh nvObj list char group[GROUP_LEN+1] = {""}; // group identifier - starts as NUL int8_t i = NV_BODY_LEN; ritorno(_normalize_json_string(str, JSON_OUTPUT_STRING_MAX)); // return if error // parse the JSON command into the nv body do { if (--i == 0) { return (STAT_JSON_TOO_MANY_PAIRS); } // length error // if ((status = _get_nv_pair_strict(nv, &str, &depth)) > STAT_EAGAIN) { // erred out if ((status = _get_nv_pair(nv, &str, &depth)) > STAT_EAGAIN) { // erred out return (status); } // propagate the group from previous NV pair (if relevant) if (group[0] != NUL) { strncpy(nv->group, group, GROUP_LEN); // copy the parent's group to this child } // validate the token and get the index if ((nv->index = nv_get_index(nv->group, nv->token)) == NO_MATCH) { nv->valuetype = TYPE_NULL; return (STAT_UNRECOGNIZED_NAME); } if ((nv_index_is_group(nv->index)) && (nv_group_is_prefixed(nv->token))) { strncpy(group, nv->token, GROUP_LEN); // record the group ID } if ((nv = nv->nx) == NULL) return (STAT_JSON_TOO_MANY_PAIRS);// Not supposed to encounter a NULL } while (status != STAT_OK); // breaks when parsing is complete // execute the command nv = nv_body; if (nv->valuetype == TYPE_NULL){ // means GET the value ritorno(nv_get(nv)); // ritorno returns w/status on any errors } else { cm_parse_clear(*nv->stringp); // parse Gcode and clear alarms if M30 or M2 is found ritorno(cm_is_alarmed()); // return error status if in alarm, shutdown or panic ritorno(nv_set(nv)); // set value or call a function (e.g. gcode) nv_persist(nv); } return (STAT_OK); // only successful commands exit through this point }
/****************************************************************************** * text_parser() - update a config setting from a text block (text mode) * _text_parser_kernal() - helper for above * * Use cases handled: * - $xfr=1200 set a parameter (strict separators)) * - $xfr 1200 set a parameter (relaxed separators) * - $xfr display a parameter * - $x display a group * - ? generate a status report (multiline format) */ stat_t text_parser(char_t *str) { nvObj_t *nv = nv_reset_nv_list(); // returns first object in the body stat_t status = STAT_OK; // trap special displays if (str[0] == '?') { // handle status report case sr_run_text_status_report(); return (STAT_OK); } if (str[0] == 'H') { // print help screens help_general((nvObj_t *)NULL); return (STAT_OK); } // pre-process the command if ((str[0] == '$') && (str[1] == NUL)) { // treat a lone $ as a sys request strcat(str,"sys"); } // parse and execute the command (only processes 1 command per line) ritorno(_text_parser_kernal(str, nv)); // run the parser to decode the command if ((nv->valuetype == TYPE_NULL) || (nv->valuetype == TYPE_PARENT)) { if (nv_get(nv) == STAT_COMPLETE){ // populate value, group values, or run uber-group displays return (STAT_OK); // return for uber-group displays so they don't print twice } } else { // process SET and RUN commands if (cm.machine_state == MACHINE_ALARM) return (STAT_MACHINE_ALARMED); status = nv_set(nv); // set (or run) single value if (status == STAT_OK) { nv_persist(nv); // conditionally persist depending on flags in array } } nv_print_list(status, TEXT_MULTILINE_FORMATTED, JSON_RESPONSE_FORMAT); // print the results return (status); }
/************************************************************************************ * config_init() - called once on hard reset * * Performs one of 2 actions: * (1) if persistence is set up or out-of-rev load RAM and NVM with settings.h defaults * (2) if persistence is set up and at current config version use NVM data for config * * You can assume the cfg struct has been zeroed by a hard reset. * Do not clear it as the version and build numbers have already been set by tg_init() * * NOTE: Config assertions are handled from the controller */ void config_init() { nvObj_t *nv = nv_reset_nv_list(); char *P_str_axis[3] = {"x","y", "z"}; config_init_assertions(); #ifdef __ARM // ++++ The following code is offered until persistence is implemented. // ++++ Then you can use the AVR code (or something like it) cfg.comm_mode = JSON_MODE; // initial value until EEPROM is read _set_defa(nv); #endif #ifdef __AVR cm_set_units_mode(MILLIMETERS); // must do inits in millimeter mode nv->index = 0; // this will read the first record in NVM read_persistent_value(nv); if (nv->value != cs.fw_build) { // case (1) NVM is not setup or not in revision // if (fp_NE(nv->value, cs.fw_build)) { _set_defa(nv); } else { // case (2) NVM is setup and in revision rpt_print_loading_configs_message(); for (nv->index=0; nv_index_is_single(nv->index); nv->index++) { if (GET_TABLE_BYTE(flags) & F_INITIALIZE) { strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); // read the token from the array read_persistent_value(nv); nv_set(nv); } } sr_init_status_report(); } #endif #ifdef __RX // ++++ The following code is offered until persistence is implemented. // ++++ Then you can use the AVR code (or something like it) cm_set_units_mode(MILLIMETERS); // must do inits in millimeter mode nv->index = 0; // this will read the first record in NVM spiffs_DIR sf_dir; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; spiffs_file *fd = &uspiffs[0].f; spiffs *fs = &uspiffs[0].gSPIFFS; R_FlashDataAreaAccess(0xFFFF,0xFFFF); checkifParFlashed = (char *)(0x00100000); if (SPIFFS_opendir(fs, "/", &sf_dir) == NULL) { } pe = SPIFFS_readdir(&sf_dir, pe); *fd = SPIFFS_open(fs, "config.met", SPIFFS_RDWR | SPIFFS_DIRECT, 0); SPIFFS_close(fs, *fd); if (*fd == SPIFFS_ERR_NOT_FOUND && strcmp(checkifParFlashed,checkParPhrase)) { // case (1) NVM is not setup or not in revision *fd = SPIFFS_open(fs, "config.met", SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_DIRECT, 0); SPIFFS_close(fs, *fd); R_FlashEraseRange(0x00100000,0x20); R_FlashWrite(0x00100000,(uint32_t)checkParPhrase, 0x20); _set_defa(nv); } else { // case (2) NVM is setup and in revision rpt_print_loading_configs_message(); for (nv->index=0; nv_index_is_single(nv->index); nv->index++) { if (GET_TABLE_BYTE(flags) & F_INITIALIZE) { strncpy_P(nv->token, cfgArray[nv->index].token, TOKEN_LEN); // read the token from the array read_persistent_value(nv); nv_set(nv); } } sr_init_status_report(); } z_step_pulse = (M1_TRAVEL_PER_REV*M1_STEP_ANGLE)/(360*M1_MICROSTEPS); #endif }