#include <EEPROM-Storage.h>



 #define V_memory_count 254

unsigned long V[V_memory_count]; 
int T_lv = 0;
byte T_n = 1;
byte DI = 0;
byte VI = 0;
byte T_SI = 0;
unsigned long timeStrored=0;
unsigned long val = 0;
int address;
int eei;
int ssidlen;
char hssid;

char ssid[40];          // The name of your WiFi network (SSID)
char password[40] ;             // WIFI network PASSWORD
char ssid_AP[40];             // Access point network SSID
char password_AP[40];         // Access point network PASSWORD
char wordhold[40];
//======================================temp

//======================================temp
// Variables =========================================A
int *p;       // declare a pointer to an int data type
int j = 6;
int i = 5;
int SaveOrLoad = 1; //reset to 0 when done
boolean SORS = false;
boolean SOLL = false;
boolean FactReset = true;
boolean SORS1 = true;
boolean SOLL1 = true;
boolean FactReset1 = true;
 
void FactoryReset()
{
  for(T_n = 1; T_n <= 7; T_n++)
    {
    T_lv = 90 + T_n;  
    V[T_lv] = 1;
    T_lv = 80 + T_n;  
    V[T_lv] = (9 * 10); // lazy way to pass .1
    T_lv = 70 + T_n;  
    V[T_lv] = (8 * 10); // lazy way to pass .1
    T_lv = 60 + T_n;  
    V[T_lv] = 28800;
    }
  }

void LoadEEProm ( int StartPt , int EndPt, int EEPromST)
{
            int iLoc;
            int EEPval;
            unsigned long Vval;
    for (iLoc = StartPt ; iLoc <= EndPt; iLoc++)
    {
              EEPval = (iLoc - StartPt)*4 + EEPromST;                    
                EEPROM.get(EEPval,Vval);
                     V[iLoc] = Vval;
   }
}

void StoreEEProm ( int StartPt , int EndPt, int EEPromST)
{
                int iLoc;
                int EEPval;
                unsigned long Vval;
                byte arr[4];
    for (iLoc = StartPt ; iLoc <= EndPt; iLoc++)
    {
            EEPval = (iLoc - StartPt)*4 + EEPromST;
             Vval = V[iLoc];

        arr[0] = Vval & 0xFF; // 0x78
             EEPROM.put(EEPval+3,arr[0]);
        arr[1] = (Vval >> 8) & 0xFF ;// 0x56
                    EEPROM.put((EEPval+2),arr[1]);
        arr[2] = (Vval >> 16) & 0xFF ;// 0x34
                    EEPROM.put((EEPval+1),arr[2]);
        arr[3] = (Vval >> 24) & 0xFF ;// 0x12
                    EEPROM.put((EEPval+0),arr[3]);
   }
}

void StringEEProm ( int Startpt , char SS[])
{
    int SSL = 40;
    int Loci = 0;
    char LocChar ;
    
//    SSL = sizeof(SS); // length of string
        Loci = 0;
        while (( 128 > SS[Loci])&&(SS[Loci] > 31)&&(Loci < 40))
            {  
              Loci++;
            };
        SSL = Loci;    
    
        EEPROM.put(Startpt,SSL); // place length of string at the begining
        for(Loci = 0 ;Loci <= SSL; Loci++)
            {
        LocChar = SS[Loci];
        EEPROM.put((Startpt + Loci + 2),LocChar);
            };
};

void EEPromString ( int Startpt , char *lc ) 
{
        int SSL = 40;
        byte Loci;
        char LocChar;
        char LocStr[40];

// StringEEProm(176,ssid);
    EEPROM.get(Startpt,SSL); // get length of string at the begining
        for(Loci = 0 ;Loci <= SSL; Loci++)
            {
        EEPROM.get((Startpt + Loci + 2),LocChar);
        ssid[Loci] = LocChar;
            }

 
}


// Procedures Pre Decliration=========================A

// Functions =========================================V

// Functions =========================================A

// SetUp ==============================================V
void setup() 
{

    pinMode(13, INPUT);
    pinMode(12, INPUT);
    pinMode(11, INPUT);
//------------------- USER SETTINGS ---------------------------------
    strcpy(ssid,"dd-wrt2");            // The name of your WiFi network (SSID)
    strcpy(password,"1118Vict");            // WIFI network PASSWORD
    strcpy(ssid_AP,"NodeMCU_2");            // Access point network SSID
    strcpy(password_AP,"1234567890");        // Access point network PASSWORD
    Serial.begin(115200);
    while (!(Serial)) {Serial.println("waiting");}
}

void loop() 
{
  SORS = digitalRead(12);
  SOLL = digitalRead(13);
  FactReset = digitalRead(11);

  if (FactReset1)
    {
      FactoryReset();
      FactReset1 = false;  
    }
// testing the eeprom write.
  if ((SORS1)) 
  {
        StoreEEProm ( 60 , 100, 0);
        StringEEProm(176,ssid);
        StringEEProm(208,password);
        StringEEProm(240,ssid_AP );
        StringEEProm(272,password_AP);
        Serial.println("StringEEProm");
        SORS1=false;
        strcpy(ssid,"no");            // The name of your WiFi network (SSID)
        strcpy(password,"no");            // WIFI network PASSWORD
        strcpy(ssid_AP,"no");            // Access point network SSID
        strcpy(password_AP,"no");
      Serial.println(ssid);
 };
if (SOLL1) 
{
        LoadEEProm( 60 , 100, 0);
        EEPromString(176,ssid);
        EEPromString(208,password); 
        EEPromString(240,ssid_AP); 
        EEPromString(272,password_AP);
        Serial.println("EEPromString"); 
        SOLL1=false;                               
 };
// Loop ===============================================A

// Functions and Procedures ===========================V



}