// Load the variable from EEPROM, if supported // bool AP_Param::load(void) { uint32_t group_element = 0; const struct GroupInfo *ginfo; uint8_t idx; const struct AP_Param::Info *info = find_var_info(&group_element, &ginfo, &idx); if (info == NULL) { // we don't have any info on how to load it return false; } struct Param_header phdr; // create the header we will use to match the variable if (ginfo != NULL) { phdr.type = PGM_UINT8(&ginfo->type); } else { phdr.type = PGM_UINT8(&info->type); } phdr.key = PGM_UINT8(&info->key); phdr.group_element = group_element; // scan EEPROM to find the right location uint16_t ofs; if (!scan(&phdr, &ofs)) { // if the value isn't stored in EEPROM then set the default value if (ginfo != NULL) { uintptr_t base = PGM_POINTER(&info->ptr); set_value((enum ap_var_type)phdr.type, (void*)(base + PGM_UINT16(&ginfo->offset)), PGM_FLOAT(&ginfo->def_value)); } else { set_value((enum ap_var_type)phdr.type, (void*)PGM_POINTER(&info->ptr), PGM_FLOAT(&info->def_value)); } return false; } if (phdr.type != AP_PARAM_VECTOR3F && idx != 0) { // only vector3f can have non-zero idx for now return false; } AP_Param *ap; ap = this; if (idx != 0) { ap = (AP_Param *)((uintptr_t)ap) - (idx*sizeof(float)); } // found it _storage.read_block(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type)); return true; }
/* find a default value given a pointer to a default value in flash */ float AP_Param::get_default_value(const float *def_value_ptr) { for (uint16_t i=0; i<num_param_overrides; i++) { if (def_value_ptr == param_overrides[i].def_value_ptr) { return param_overrides[i].value; } } return PGM_FLOAT(def_value_ptr); }
// load default values for all scalars in a sketch. This does not // recurse into sub-objects void AP_Param::setup_sketch_defaults(void) { setup(); for (uint8_t i=0; i<_num_vars; i++) { uint8_t type = PGM_UINT8(&_var_info[i].type); if (type <= AP_PARAM_FLOAT) { void *ptr = (void*)PGM_POINTER(&_var_info[i].ptr); set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&_var_info[i].def_value)); } } }
// load default values for all scalars in a sketch. This does not // recurse into sub-objects void AP_Param::setup_sketch_defaults(void) { setup(); for (uint8_t i=0; i<_num_vars; i++) { uint8_t type = PGM_UINT8(&_var_info[i].type);//返回参数类型,强制转换为uint8,包括PARAM_NONE等 if (type <= AP_PARAM_FLOAT) {//按道理讲uint8_t一定会小于float,这里可能是保护的作用?? void *ptr = (void*)PGM_POINTER(&_var_info[i].ptr);//返回指向参数在内存中位置的指针 set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&_var_info[i].def_value));//把参数变量设为一个特定的值 } } }
// load default values for scalars in a group. This does not recurse // into other objects. This is a static function that should be called // in the objects constructor void AP_Param::setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info) { uintptr_t base = (uintptr_t)object_pointer; uint8_t type; for (uint8_t i=0; (type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE; i++) { if (type <= AP_PARAM_FLOAT) { void *ptr = (void *)(base + PGM_UINT16(&group_info[i].offset)); set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&group_info[i].def_value)); } } }
// Save the variable to EEPROM, if supported // bool AP_Param::save(void) { uint32_t group_element = 0; const struct GroupInfo *ginfo; uint8_t idx; const struct AP_Param::Info *info = find_var_info(&group_element, &ginfo, &idx); const AP_Param *ap; if (info == NULL) { // we don't have any info on how to store it return false; } struct Param_header phdr; // create the header we will use to store the variable if (ginfo != NULL) { phdr.type = PGM_UINT8(&ginfo->type); } else { phdr.type = PGM_UINT8(&info->type); } phdr.key = PGM_UINT8(&info->key); phdr.group_element = group_element; ap = this; if (phdr.type != AP_PARAM_VECTOR3F && idx != 0) { // only vector3f can have non-zero idx for now return false; } if (idx != 0) { ap = (const AP_Param *)((uintptr_t)ap) - (idx*sizeof(float)); } // scan EEPROM to find the right location uint16_t ofs; if (scan(&phdr, &ofs)) { // found an existing copy of the variable eeprom_write_check(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type)); return true; } if (ofs == (uint16_t) ~0) { return false; } // if the value is the default value then don't save if (phdr.type <= AP_PARAM_FLOAT) { float v1 = cast_to_float((enum ap_var_type)phdr.type); float v2; if (ginfo != NULL) { v2 = PGM_FLOAT(&ginfo->def_value); } else { v2 = PGM_FLOAT(&info->def_value); } if (v1 == v2) { return true; } if (phdr.type != AP_PARAM_INT32 && (fabsf(v1-v2) < 0.0001f*fabsf(v1))) { // for other than 32 bit integers, we accept values within // 0.01 percent of the current value as being the same return true; } } if (ofs+type_size((enum ap_var_type)phdr.type)+2*sizeof(phdr) >= _eeprom_size) { // we are out of room for saving variables return false; } // write a new sentinal, then the data, then the header write_sentinal(ofs + sizeof(phdr) + type_size((enum ap_var_type)phdr.type)); eeprom_write_check(ap, ofs+sizeof(phdr), type_size((enum ap_var_type)phdr.type)); eeprom_write_check(&phdr, ofs, sizeof(phdr)); return true; }