/*************************************
Project:    OpAmp Tester
File:       EEPROM emulation
            Uses an area of STM32
            program memory as EEPROM
Version:    3.0 - May 2026
Written by: diy-electronic.ru
**************************************
Version history
  1.0  Mar 2025 Initial version
  2.1  Dec 2025 Minor cosmetic changes
  3.0  May 2026
**************************************/

#ifdef __DEBUG__
  #define FLASH_DEBUG 2 //Maximum verbosity level
#else
  #define FLASH_DEBUG 0 //No debugging messages
#endif

#define USING_FLASH_SECTOR_NUMBER (REGISTERED_NUMBER_FLASH_SECTORS - 2)

// Statement below includes FlashStorage_STM32F1 library 
// by Khoi Hoang v.1.1.0
// https://github.com/khoih-prog/FlashStorage_STM32F1
#include <FlashStorage_STM32F1.h>

const int WRITTEN_SIGNATURE = 0xBEEFDEED;
const int START_ADDRESS     = 0;

//------------ Global functions ---------------

/************************************/
/* Store current settings to EEPROM */
/************************************/
void eepromStoreSettings() {
  int eeAddress = START_ADDRESS + sizeof(WRITTEN_SIGNATURE); 
#ifdef __DEBUG__
  eepromDebugMessage("\nSaving settings to EEPROM on ", eeAddress);
#endif
  EEPROM.init();
  EEPROM.setCommitASAP(false);
  //--- Store ActiveSettings array
#ifdef __DEBUG__
  Serial.print("  ActiveSettings.array: ");
#endif
  for (int i=0; i<12; i++) {
    EEPROM.put(eeAddress, ActiveSettings.array[i]);
    eeAddress += sizeof(float); //Move address to the next byte after previous float.
#ifdef __DEBUG__
    Serial.print(ActiveSettings.array[i]);
    Serial.print(" ");
#endif
  }
  EEPROM.put(eeAddress, themeNum); //--- Store current color theme number
  eeAddress += sizeof(uint16_t);
  EEPROM.put(eeAddress, Voltage);  //--- Store current voltage mode
#ifdef __DEBUG__
  Serial.print("\n  Theme: ");
  Serial.println(themeNum);
  Serial.print("  Voltage: ");
  Serial.println((Voltage == voltageStandard) ? "12V" : "2.5V");
#endif
  if (!EEPROM.getCommitASAP()) {
    EEPROM.commit();
  }
}

/*************************************/
/* Read current settings from EEPROM */
/*************************************/
void eepromRetrieveSettings() {
  int eeAddress = START_ADDRESS + sizeof(WRITTEN_SIGNATURE); 
#ifdef __DEBUG__
  eepromDebugMessage("\nReading settings from EEPROM on ", eeAddress);
#endif
  EEPROM.init();
  //--- Retrive ActiveSettings array
#ifdef __DEBUG__
  Serial.print("  ActiveSettings.array: ");
#endif
  for (int i=0; i<12; i++) {
    EEPROM.get(eeAddress, ActiveSettings.array[i]);
    eeAddress += sizeof(float); //Move address to the next byte after previous float.
#ifdef __DEBUG__
    Serial.print(ActiveSettings.array[i]);
    Serial.print(" ");
#endif
  }
  EEPROM.get(eeAddress, themeNum); //--- Retrive current color theme number
  eeAddress += sizeof(uint16_t);
  EEPROM.get(eeAddress, Voltage);  //--- Retrive current voltage mode
#ifdef __DEBUG__
  Serial.print("\n  Theme: ");
  Serial.println(themeNum);
  Serial.print("  Voltage: ");
  Serial.println((Voltage == voltageStandard) ? "12V" : "2.5V");
#endif
}
 
//------------ Local functions ---------------

#ifdef __DEBUG__
// Display useful technical information about
// EEPROM emulated storage
void eepromDebugMessage(String firstLine, int address) {
  Serial.println(firstLine + BOARD_NAME);
  Serial.println(FLASH_STORAGE_STM32F1_VERSION);
  Serial.print("EEPROM length: "); 
  Serial.println(EEPROM.length());
  Serial.print("Begin from address: 0x");
  Serial.println(address, HEX);
}
#endif
