; This code uses a user-defined class to ; store a set of persistent settings in an EEPROM. ; It uses a checksum to validate the stored settings. To init ;... Make eeprom SafeData(1,1,162) Make Settings SettingsClass SettingsChanged := False ; Set this flag whenever a setting has been changed by the user. ;... End To main LoadPersistentSettings ; Using the settings: Wait Settings.Delay Repeat Settings.Count result := rawReading * Settings.Calibration ;... End ;======================================== ; A Class to hold some persistent settings: Class SettingsClass Calibration Float Delay Int 8 Count Int 8 End ; Load the settings from EEPROM. Reset settings if the checksum is 'bad'. To LoadPersistentSettings If ReadSettings IsFalse SetDefaultSettings End ; Save the settings in the EEPROM if SettingsChanged flag is set. ; Call this at points in the code when you want the current settings to be saved. ; It only writes to the EEPROM when the settingsChanged flag has been set. To SaveSettings If SettingsChanged WriteSettings End #Define XSUM_EXTRA 42 ; Extra value added to checksum. ; Store settings in the EEPROM, and set a checksum. To WriteSettings Local xsum eeprom.Reset eeprom.Put(Settings) ; calc and write checksum... xsum := eeprom.Checksum(0, eeprom.Address) + XSUM_EXTRA eeprom.Put(xsum, Int 16) SettingsChanged := False End ; Read settings from the EEPROM and return true if checksum is good. To ReadSettings Local xsum, xsum2 eeprom.Reset eeprom.Get(Settings) xsum := eeprom.Checksum(0, eeprom.Address) + XSUM_EXTRA xsum2 := eeprom.Get(Int 16) SettingsChanged := False Return (xsum = xsum2) ; return true if xsums agree. End ; Set settings to their default values. To SetDefaultSettings Settings.Delay := 10 Settings.Calibration := 100.0 Settings.Count := 0 SettingsChanged := True End