#include "./settings.h"
#include <EEPROM.h>


#define SettingsIdentifier 0x42
#define SettingsVersion1 1

SettingsContainer Settings;


struct SettingsHeader
{
  uint8_t identifier;
  uint8_t version;
};

#define SettingsHeightsAddress sizeof(SettingsHeader)

bool initialized = false;


// Forward declarations
void readHeights();


bool readSettings()
{
  memset(&Settings, 0, sizeof(SettingsContainer));

  SettingsHeader header;
  EEPROM.get(0, header);

  if (header.identifier == SettingsIdentifier)
  {
    if (header.version >= 1)
        readHeights();

    initialized = true;
    return true;
  }

  return false;
}


bool allowPartialWrite()
{
  if (initialized)
    return true;

  initialized = true;

  SettingsHeader header;
  header.identifier = SettingsIdentifier;
  header.version = SettingsVersion1;

  EEPROM.put(0, header);
  writeSettingsHeights();

  return false;
}


void readHeights()
{
  EEPROM.get(SettingsHeightsAddress, Settings.Height);
}


void writeSettingsHeights()
{
  if (allowPartialWrite())
    EEPROM.put(SettingsHeightsAddress, Settings.Height);
}
