+ int i;
+ unsigned char v;
+ byte *p;
+
+ v = EEPROM.read(0);
+ if ( v != EEPROM_VERSION )
+ {
+ // All models have been reset. Set the current model to 0
+ current_model = 0;
+ EEPROM.write(1, current_model);
+
+ calibrate();
+ model_defaults();
+ // The following does not yet work...
+ for ( i = 0; i < MAX_MODELS; i++)
+ write_model_settings(i);
+
+
+ // After saving calibration data and model defaults,
+ // update the saved version-identifier to the current ver.
+ EEPROM.write(0, EEPROM_VERSION);
+ }
+
+ // Read calibration values from EEPROM.
+ // This uses simple pointer-arithmetic and byte-by-byte
+ // to put bytes read from EEPROM to the data-struct.
+ p = (byte*)(void*)&input_cal;
+ for (i = 0; i < sizeof(input_cal_t); i++)
+ *p++ = EEPROM.read( EE_BASE_ADDR + i);
+
+ // Get the previously selected model from EEPROM.
+ current_model = EEPROM.read(1);
+ read_model_settings( current_model );
+}
+
+void read_model_settings(unsigned char mod_no)
+{
+ int model_address;
+ int i;
+ unsigned char v;
+ byte *p;
+
+ // Calculate the EEPROM start adress for the given model (mod_no)
+ model_address = EE_MDL_BASE_ADDR + (mod_no * sizeof(model_t));
+
+ // Do not try to write the model to EEPROM if it won't fit.
+ if ( INT_EEPROM_SIZE < (model_address + sizeof(model_t)) )
+ {
+ lcd.clear();
+ lcd.print("Aborting READ");
+ lcd.setCursor(0 , 1);
+ lcd.print("Invalid location");
+ delay(2000);
+ return;
+ }
+
+ lcd.clear();
+ lcd.print("Reading model ");
+ lcd.print( (int)mod_no );
+
+ // Pointer to the start of the model_t data struct,
+ // used for byte-by-byte reading of data...
+ p = (byte*)(void*)&model;
+ for (i = 0; i < sizeof(model_t); i++)
+ *p++ = EEPROM.read( model_address++ );
+
+#ifdef DEBUG
+ serial_dump_model();
+#endif
+
+ lcd.setCursor(0 , 1);
+ lcd.print("... Loaded.");
+ delay(1000);
+}
+
+void write_model_settings(unsigned char mod_no)
+{
+ int model_address;
+ int i;
+ unsigned char v;
+ byte *p;
+
+ // Calculate the EEPROM start adress for the given model (mod_no)
+ model_address = EE_MDL_BASE_ADDR + (mod_no * sizeof(model_t));
+
+ // Do not try to write the model to EEPROM if it won't fit.
+ if ( INT_EEPROM_SIZE < (model_address + sizeof(model_t)) )
+ {
+ lcd.clear();
+ lcd.print("Aborting SAVE");
+ lcd.setCursor(0 , 1);
+ lcd.print("No room for data");
+ delay(2000);
+ return;