// Save the variable to EEPROM, if supported // bool AP_Param::save(void) { uint8_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; } // 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; }
// erase all EEPROM variables by re-writing the header and adding // a sentinal void AP_Param::erase_all(void) { struct EEPROM_header hdr; serialDebug("erase_all"); // write the header hdr.magic[0] = k_EEPROM_magic0; hdr.magic[1] = k_EEPROM_magic1; hdr.revision = k_EEPROM_revision; hdr.spare = 0; eeprom_write_check(&hdr, 0, sizeof(hdr)); // add a sentinal directly after the header write_sentinal(sizeof(struct EEPROM_header)); }
// 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; }
bool AS_Param::save(bool force_save) { uint8_t idx; const struct AS_Param::Info *info = find_var_info(); const AS_Param *ap; if (info == NULL) { // we don't have any info on how to store it return false; } struct Param_header phdr; phdr.type = info->type; phdr.key = info->key; ap = this; if (idx != 0) { ap = (const AS_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 as_var_type)phdr.type)); return true; } if (ofs == (uint16_t) ~0) { return false; } float v1 = cast_to_float((enum as_var_type)phdr.type); float v2; v2 = info->value; if (v1 == v2 && !force_save) { return true; } if (phdr.type != AS_PTYPE_INT32 && (fabs(v1-v2) < 0.0001f*fabs(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 as_var_type)phdr.type)+2*sizeof(phdr) >= _eeprom_size) { // we are out of room for saving variables Serial.print("EEPROM full"); return false; } // write a new sentinal, then the data, then the header write_sentinal(ofs + sizeof(phdr) + type_size((enum as_var_type)phdr.type)); eeprom_write_check(ap, ofs+sizeof(phdr), type_size((enum as_var_type)phdr.type)); eeprom_write_check(&phdr, ofs, sizeof(phdr)); Serial.print("Write param done\n"); return true; }