Project

Dependencies:   Hotboards_keypad TextLCD eeprom

i2ceeprom.cpp

Committer:
shivanandgowdakr
Date:
2018-10-23
Revision:
0:194ff03a2e6a
Child:
1:1894419d5def

File content as of revision 0:194ff03a2e6a:



// Example

#include <string>
#include "mbed.h"
#include "eeprom.h"

#define EEPROM_ADDR 0x02   // I2C EEPROM address is 0x00

#define SDA D14            // I2C SDA pin
#define SCL D15           // I2C SCL pin

#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))

extern float G_time;
extern float Y_time;
extern float R_time;


int32_t eeprom_size,max_size;
typedef struct _MyData {
                         int16_t sdata;
                         int32_t idata;
                         float fdata;
                       } MyData;

static void myerror(std::string msg)
{
  printf("Error %s\n",msg.c_str());
  exit(1);
}

void StoreCurrentMode(int32_t idata)
{
  EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10  
  eeprom_size = ep.getSize();
  max_size = MIN(eeprom_size,256);
  ep.write((uint32_t)(eeprom_size - 200),(int32_t)idata); // long write at address eeprom_size - 12
  if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
//    printf("\n");    
    ep.read((eeprom_size - 12),(int32_t&)idata);
   // printf("TransDC Read %d\r\n",idata);
}



int32_t readCurrentMode()
{
  int32_t idata;
  EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
    eeprom_size = ep.getSize();
  max_size = MIN(eeprom_size,256);
 ep.read((eeprom_size - 200),(int32_t&)idata);
 
 if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
//    printf("\n");
   // printf("Trans Down Count Read %d\r\n",idata);
   return idata;
}

bool WriteCorresspondingTimes(uint32_t currentMode, float gTime,float yTime,float rTime)
{
  EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
  eeprom_size = ep.getSize();
  max_size =  MIN(eeprom_size,256);
  
    uint32_t addr = eeprom_size - (currentMode*12);
  ep.write(addr,(float)gTime); // float write at address eeprom_size - 8
  if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
  
  // Test read short, long, float
  printf("\nGreen Time : (%f) :\n",gTime);  
 
  
  addr = eeprom_size - ((currentMode*12)-4);
  ep.write(addr,(float)yTime); // float write at address eeprom_size - 8
  if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
  
  // Test read short, long, float
  printf("\nYellow Time : (%f) :\n",yTime);  
  
     addr = eeprom_size - ((currentMode*12)-8);
  ep.write(addr,(float)rTime); // float write at address eeprom_size - 8
  if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
  
  // Test read short, long, float
  printf("\nRed Time : (%f) :\n",rTime);  


}


bool ReadCorresspondingTimes(uint32_t currentMode, float *gTime,float *yTime,float *rTime)
{
  float fdata=0.0;
  EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C128);  // 24C64 eeprom with sda = p9 and scl = p10
    eeprom_size = ep.getSize();
  max_size = MIN(eeprom_size,256);
 ep.read((eeprom_size - (currentMode*12)),(float&)fdata);

 if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
//    printf("\n");
   // printf("Trans Down Count Read %d\r\n",idata);
  *gTime=fdata;
  fdata=0.0;
  ep.read((eeprom_size - (currentMode*12)-4),(float&)fdata);
 
 if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
//    printf("\n");
   // printf("Trans Down Count Read %d\r\n",idata);
  *yTime=fdata;
  fdata=0.0;
    ep.read((eeprom_size - (currentMode*12)-8),(float&)fdata);
 
 if(ep.getError() != 0)
    myerror(ep.getErrorMessage());
//    printf("\n");
   // printf("Trans Down Count Read %d\r\n",idata);
  *rTime=fdata; 
}



void Last_Saved_Mode()
{
  //float Gtime=0,Ytime=0, Rtime=0;
  int32_t CurrentMode= readCurrentMode();  
  bool flag= ReadCorresspondingTimes( CurrentMode, &G_time,&Y_time, &R_time);   
}