Lab_interaccio/2012/Edgard/Vdossier03/memory_manager.ino

225 lines
9.7 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(data);
Wire.endTransmission();
delay(4);
}
byte readEEPROM(int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
while (!Wire.available()); rdata = Wire.read();
//if (Wire.available()); rdata = Wire.read();
return rdata;
}
void saveEEPROM(unsigned long value, unsigned int address){
uint8_t val=0x00;
for (int i = 0; i < 4; i++)
{
val=(byte)(value>>(8*i));
writeEEPROM(eeprom, address+3-i, val);
}
writeEEPROM(eeprom, address+4, 0x0D);
}
void clear_model(unsigned int address){
for (int i = 0; i < 80; i++)
{
if (i<16) writeEEPROM(eeprom, address+i, ' ');
else writeEEPROM(eeprom, address+i, 0x00);
}
}
unsigned long copy_EEPROM(unsigned int address)
{
uint8_t temp=0;
unsigned long temp32 = 0;
int i=0;
temp = readEEPROM(eeprom, address + i);
while ((temp!=0x0D)&&(i<16)) {
temp32 = (temp32<<8) + temp;
i++;
temp = readEEPROM(eeprom, address + i);
}
return(temp32);
}
void move_model(byte currentOption){
unsigned int address = EEI2C_ADDR_MODELS + 80*currentOption;
unsigned int address_next = EEI2C_ADDR_MODELS + 80*(currentOption+1);
if (currentOption < (system_num_models - 1))
{
for (int i = 0; i < 80*(system_num_models - currentOption); i++)
{
writeEEPROM(eeprom, address+i, readEEPROM(eeprom, address_next + i ));
}
}
system_num_models--;
}
void erase_model(byte currentOption){
lcd.clear();
display_print(MSG_ERASE);
unsigned int address = EEI2C_ADDR_MODELS + 80*currentOption;
clear_model(address);
move_model(currentOption);
}
void config_init(){
// Check if the firmware version is the same of eeprom config
if (
EEPROM.read(EE_ADDR_SIGNATURE_CODE1) == SIGNATURE_CODE1 &&
EEPROM.read(EE_ADDR_SIGNATURE_CODE2) == SIGNATURE_CODE2 &&
EEPROM.read(EE_ADDR_SIGNATURE_CODE3) == SIGNATURE_CODE3 &&
EEPROM.read(EE_ADDR_SIGNATURE_CODE4) == SIGNATURE_CODE4 &&
EEPROM.read(EE_ADDR_CODE_MAYOR_VERSION) == CODE_MAYOR_VERSION &&
EEPROM.read(EE_ADDR_CODE_MINOR_VERSION) == CODE_MINOR_VERSION
) {
// loads in ram the eeprom config
config_loadBackup_system();
} else {
// clear the eeprom
for (int i = 0; i < EEPROM_SIZE; i++) EEPROM.write(i, 0xFF);
// writes sign codes
EEPROM.write(EE_ADDR_SIGNATURE_CODE1,SIGNATURE_CODE1);
EEPROM.write(EE_ADDR_SIGNATURE_CODE2,SIGNATURE_CODE2);
EEPROM.write(EE_ADDR_SIGNATURE_CODE3,SIGNATURE_CODE3);
EEPROM.write(EE_ADDR_SIGNATURE_CODE4,SIGNATURE_CODE4);
EEPROM.write(EE_ADDR_CODE_MAYOR_VERSION,CODE_MAYOR_VERSION);
EEPROM.write(EE_ADDR_CODE_MINOR_VERSION,CODE_MINOR_VERSION);
// inform of resetting action
display_printResetting();
// load defaults in ram and save it on eeprom
config_loadDefaults_system();
config_saveBackup_system();
}
}
// Load the system config from eeprom to ram
void config_loadBackup_system(){
for(int i=0; i<32; i++) ssid[i] = readEEPROM(eeprom, EEI2C_ADDR_SSID + i);
for(int i=0; i<32; i++) pass[i] = readEEPROM(eeprom, EEI2C_ADDR_PASS + i);
for(int i=0; i<16; i++) ip_host[i] = readEEPROM(eeprom, EEI2C_ADDR_HOST + i);
remote_Port = word(readEEPROM(eeprom, EEI2C_ADDR_REMOTE + 1),readEEPROM(eeprom, EEI2C_ADDR_REMOTE));
local_Port = word(readEEPROM(eeprom, EEI2C_ADDR_LOCAL + 1),readEEPROM(eeprom, EEI2C_ADDR_LOCAL));
for(int i=0; i<8; i++) timetotalvideo = timetotalvideo + (readEEPROM(eeprom, EEI2C_ADDR_timetotalvideo + i)<<(8*i));
for(int i=0; i<8; i++) timerecvideo = timerecvideo + (readEEPROM(eeprom, EEI2C_ADDR_timerecvideo + i)<<(8*i));
for(int i=0; i<8; i++) timepausevideo = timepausevideo + (readEEPROM(eeprom, EEI2C_ADDR_timepausevideo + i)<<(8*i));
for(int i=0; i<8; i++) timetotalphoto = timetotalphoto + (readEEPROM(eeprom, EEI2C_ADDR_timetotalphoto + i)<<(8*i));
for(int i=0; i<8; i++) timeflash = timeflash + (readEEPROM(eeprom, EEI2C_ADDR_timeflash + i)<<(8*i));
system_useBacklight = EEPROM.read(EE_ADDR_system_useBacklight);
system_useSpeaker = EEPROM.read(EE_ADDR_system_useSpeaker);
system_num_models = word(readEEPROM(eeprom, EEI2C_ADDR_system_num_models + 1),readEEPROM(eeprom, EEI2C_ADDR_system_num_models));
system_sel_model = word(readEEPROM(eeprom, EEI2C_ADDR_system_sel_model + 1),readEEPROM(eeprom, EEI2C_ADDR_system_sel_model));
system_baudrate = readEEPROM(eeprom, EEI2C_ADDR_system_baudrate);
system_id = readEEPROM(eeprom, EEI2C_ADDR_system_id);
system_mode = readEEPROM(eeprom, EEI2C_ADDR_system_mode);
unsigned int address_model = EEI2C_ADDR_MODELS + 80*system_sel_model;
remotestop=copy_EEPROM(address_model + 16);
remoterec=copy_EEPROM(address_model + 32);
lancstop=word(readEEPROM(eeprom, address_model + 48), readEEPROM(eeprom, address_model + 49));
lancrec=word(readEEPROM(eeprom, address_model + 64), readEEPROM(eeprom, address_model + 65));
}
// Load the default system config to ram
void config_loadDefaults_system() {
system_useBacklight = DEFAULT_system_useBacklight;
system_useSpeaker = DEFAULT_system_useSpeaker;
system_num_models = DEFAULT_system_num_models;
system_sel_model = DEFAULT_system_sel_model;
system_baudrate = DEFAULT_system_baudrate;
system_id = DEFAULT_system_id;
system_mode = DEFAULT_system_mode;
timetotalvideo = DEFAULT_timetotalvideo;
timerecvideo = DEFAULT_timerecvideo;
timepausevideo = DEFAULT_timepausevideo;
timetotalphoto = DEFAULT_timetotalphoto;
timeflash = DEFAULT_timeflash;
remote_Port = DEFAULT_remote_Port;
local_Port = DEFAULT_local_Port;
for(int i=0; i<32; i++) ssid[i]='\x00'; //Limpia memoria
for(int i=0; i<32; i++) pass[i]='\x00'; //Limpia memoria
for(int i=0; i<16; i++) ip_host[i]='\x00'; //Limpia memoria
for(int i=0; i<strlen(DEFAULT_SSID); i++) ssid[i] = DEFAULT_SSID[i];
for(int i=0; i<strlen(DEFAULT_PASS); i++) pass[i] = DEFAULT_PASS[i];
for(int i=0; i<strlen(DEFAULT_ip_host); i++) ip_host[i] = DEFAULT_ip_host[i];
for(int i=0; i<16; i++) writeEEPROM(eeprom, EEI2C_ADDR_HOST + i, '\x00'); //Limpia memoria
for(int i=0; i<32; i++) writeEEPROM(eeprom, EEI2C_ADDR_SSID + i, '\x00'); //Limpia memoria
for(int i=0; i<32; i++) writeEEPROM(eeprom, EEI2C_ADDR_PASS + i, '\x00'); //Limpia memoria
EEPROM.write(EE_ADDR_BATTERY_HIGH+1,highByte(DEFAULT_BATTERY_HIGH));
EEPROM.write(EE_ADDR_BATTERY_HIGH,lowByte(DEFAULT_BATTERY_HIGH));
EEPROM.write(EE_ADDR_BATTERY_LOW+1,highByte(DEFAULT_BATTERY_LOW));
EEPROM.write(EE_ADDR_BATTERY_LOW,lowByte(DEFAULT_BATTERY_LOW));
}
// Save the system config from ram to eeprom
void config_saveBackup_system(){
EEPROM.write(EE_ADDR_system_useBacklight, system_useBacklight);
EEPROM.write(EE_ADDR_system_useSpeaker, system_useSpeaker);
writeEEPROM(eeprom, EEI2C_ADDR_system_num_models + 1, highByte(system_num_models));
writeEEPROM(eeprom, EEI2C_ADDR_system_num_models, lowByte(system_num_models));
writeEEPROM(eeprom, EEI2C_ADDR_system_sel_model + 1, highByte(system_sel_model));
writeEEPROM(eeprom, EEI2C_ADDR_system_sel_model, lowByte(system_sel_model));
writeEEPROM(eeprom, EEI2C_ADDR_system_baudrate, system_baudrate);
writeEEPROM(eeprom, EEI2C_ADDR_system_id, system_id);
writeEEPROM(eeprom, EEI2C_ADDR_system_mode, system_mode);
writeEEPROM(eeprom, EEI2C_ADDR_REMOTE + 1, highByte(remote_Port));
writeEEPROM(eeprom, EEI2C_ADDR_REMOTE, lowByte(remote_Port));
writeEEPROM(eeprom, EEI2C_ADDR_LOCAL + 1, highByte(local_Port));
writeEEPROM(eeprom, EEI2C_ADDR_LOCAL, lowByte(local_Port));
for(int i=0; i<8; i++) writeEEPROM(eeprom, EEI2C_ADDR_timetotalvideo + i, timetotalvideo>>(8*i));
for(int i=0; i<8; i++) writeEEPROM(eeprom, EEI2C_ADDR_timerecvideo + i, timerecvideo >>(8*i));
for(int i=0; i<8; i++) writeEEPROM(eeprom, EEI2C_ADDR_timepausevideo + i, timepausevideo>>(8*i));
for(int i=0; i<8; i++) writeEEPROM(eeprom, EEI2C_ADDR_timetotalphoto + i, timetotalphoto >>(8*i));
for(int i=0; i<8; i++) writeEEPROM(eeprom, EEI2C_ADDR_timeflash + i, timeflash>>(8*i));
unsigned int address_model = EEI2C_ADDR_MODELS + 80*system_sel_model;
remotestop=copy_EEPROM(address_model + 16);
remoterec=copy_EEPROM(address_model + 32);
lancstop=word(readEEPROM(eeprom, address_model + 48), readEEPROM(eeprom, address_model + 49));
lancrec=word(readEEPROM(eeprom, address_model + 64), readEEPROM(eeprom, address_model + 65));
for(int i=0; i<strlen(ssid); i++) writeEEPROM(eeprom, EEI2C_ADDR_SSID + i, ssid[i]);
for(int i=0; i<strlen(pass); i++) writeEEPROM(eeprom, EEI2C_ADDR_PASS + i, pass[i]);
for(int i=0; i<strlen(ip_host); i++) writeEEPROM(eeprom, EEI2C_ADDR_HOST + i, ip_host[i]);
}
void calibrate_level_battery()
{
int t_bat=0;
for(int i=1; i<=10; i++) t_bat=t_bat+analogRead(bat);
t_bat=t_bat/10;
int t_eeprom;
t_eeprom=word(EEPROM.read(EE_ADDR_BATTERY_HIGH+1),EEPROM.read(EE_ADDR_BATTERY_HIGH));
if ((t_bat>t_eeprom)||(t_eeprom>=435))
{
EEPROM.write(EE_ADDR_BATTERY_HIGH+1,highByte(t_bat));
EEPROM.write(EE_ADDR_BATTERY_HIGH,lowByte(t_bat));
}
t_eeprom=word(EEPROM.read(EE_ADDR_BATTERY_LOW+1),EEPROM.read(EE_ADDR_BATTERY_LOW));
if ((t_bat<t_eeprom)||(t_eeprom<=335))
{
EEPROM.write(EE_ADDR_BATTERY_LOW+1,highByte(t_bat));
EEPROM.write(EE_ADDR_BATTERY_LOW,lowByte(t_bat));
}
batmax=word(EEPROM.read(EE_ADDR_BATTERY_HIGH+1),EEPROM.read(EE_ADDR_BATTERY_HIGH));
batmin=word(EEPROM.read(EE_ADDR_BATTERY_LOW+1),EEPROM.read(EE_ADDR_BATTERY_LOW));
}