#ifndef __EEPROM_METHOD__
#define __EEPROM_METHOD__


/////////////////////////////////////////////////////////////////////////////////
// I2C & TSM12M
#define pSDA			I2C_SDA
#define pSCL			I2C_SCL

#define I2C_ACK			0
#define I2C_NACK		1

#define SDA_IN()		I2C_SDA_PORT_OUTPUT()
#define SDA_OUT()		I2C_SDA_PORT_INPUT()
#define ACK_READ(x)		SDA_IN(); pSCL = HIGH; x = pSDA; pSCL = LOW; SDA_OUT()

#define I2C_start()		pSDA = HIGH; I2C_D; pSCL = HIGH; I2C_D;\
	                    pSDA = LOW;  I2C_D; pSCL = LOW;  I2C_D

#define I2C_stop()		pSCL = LOW;  I2C_D; pSDA = LOW;  I2C_D;\
	                    pSCL = HIGH; I2C_D; pSDA = HIGH; I2C_D;
/////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////
// M24M02
//#define M24M02_CHIP1_ADD	0xa0
//#define M24M02_CHIP2_ADD	0xa8

/////////////////////////////////////////////////////////////////////////////////



#define	MAX_CHANNEL			10
#define HOURS				24
#define MAX_MONTHS			24

#define NETWORK_ADDRESS		0x1000		// flash memory address
#define WIFI_IDPWD_ADDRESS		0x103C		// flash memory address + ?

#define HEAD_FLAG			0xA0		
#define POWER_FLAG			HEAD_FLAG

#define POWER_DATA_LENGTH	1024

enum SEARCH_KIND			{DAY_K, WEEK_K, MONTH_K};

typedef struct
{
	unsigned char	year;
	unsigned char	month;
	unsigned char   day;
	unsigned long	point;
}CHEAD_FIELD;

/*typedef struct
{
	unsigned char   flag;						// 1
	unsigned long   a;							// 4
	CHEAD_FIELD		cur;						// 7
	//CHEAD_FIELD		search;						// 7
	unsigned char   reserved[20];				// 20   total : 32
}CHEAD;*/

typedef struct
{
	unsigned short	idx;

}CHEAD_INFO;

typedef struct
{
	unsigned char	flag;
	CHEAD_FIELD		cur;
}CHEAD;

typedef struct
{
	unsigned char	flag;
	unsigned char	year;
	unsigned char	month;
	unsigned char	day;
	unsigned char	week;
	unsigned char   hour;						// 6	sum 6
				
	unsigned long sum_onehour[MAX_CHANNEL];  // 40       46          각 채널별 24시간의 합 
	unsigned long sum_ch;         				 //   4        50        채널의 합 ==> 즉 하루의 합 
	unsigned long week_grap_unit ;
	unsigned char	reserved[10];				// 14        64
	unsigned long	value[HOURS][MAX_CHANNEL];	// 960  total 1024
}CPOWERS;

typedef struct
{
	unsigned char	year;
	unsigned char	month;
	unsigned char	day;
	unsigned char	week;
	unsigned char	hour;
	unsigned char	channel_num;
	unsigned long   power;

}CPOWERS_AUG;

typedef struct
{
	unsigned char	year;
	unsigned char	month;
	unsigned char	day;
}CSEARCH_INFO;


//////////////////////////////////////////////////////////////////////////////////////////
//#define HEAD_READ()	M24M02_read_buff (HEAD_ADDRESS, (unsigned char *)&ph, sizeof(ph))
//#define HEAD_WRITE()	M24M02_write_buff(HEAD_ADDRESS, (unsigned char *)&ph, sizeof(ph))
//#define DATA_READ(x)	M24M02_read_buff(x, (unsigned char *)&power, POWER_DATA_LENGTH)
//#define DATA_WRITE(x)  Power_data_save(x)
#define DATA_READ(x)	sflash_fast_read(x, (unsigned char *)&power, POWER_DATA_LENGTH)
//#define DATA_WRITE(x)   Power_data_save(x)


int  get_head_info(void);
void put_head_info(void);
void Read_PowerInfomation(void); // 최초 실행시 한번 
unsigned long get_eeprom_address(CPOWERS_AUG *p, char kind);

int PutEnergyData_OneHour(CPOWERS_AUG *p);
int GetEnergyDataOneHour(CPOWERS_AUG *p);
int GetEnergyDataOneDay(CPOWERS_AUG *p);
int GetEnergyDataPeakOfWeek(CPOWERS_AUG *p); // thhan 0825
int GetEnergyDataOneWeek(CPOWERS_AUG *p);
//int GetEnergyDataOneWeek1(CPOWERS_AUG *p);
int GetEnergyDataOneMonth(CPOWERS_AUG *p);
int GetEnergyDataOneMonthFromToday(CPOWERS_AUG *p);
#endif // __EEPROM_METHOD__