void EEPROMWearLeveler::write( uint16_t address, uint8_t value )
{
  if ( _num_of_vars == AVR_EEPROM_SIZE )
  {
    // Revert back to EEPROM class if the nubmer of variables is
    // greater than AVR_EEPROM_SIZE/4
    EEPROM.write( address, value );
  }
  else
  {
    // bounds check
    if ( address >= _num_of_vars )
    {
      std::cout << " BAD address \n";           
      return;
    }
    uint16_t write_offset = findNextWriteAddress( address );
    std::cout << "write_offset: " << write_offset << std::endl;

    uint16_t pb_start_addr = parameterBufferAddress( address );
    std::cout << "pb_start_addr: " << pb_start_addr << std::endl;

    std::cout << "write address: " <<  pb_start_addr + write_offset << std::endl;

    // Write value to paramater buffer
    EEPROM.write( pb_start_addr + write_offset, value );
  
    // Update status buffer 
    uint16_t sb_start_addr = statusBufferAddress( address );
    uint16_t curr_index = sb_start_addr + write_offset;
    uint16_t prev_index;

    std::cout << "curr_index: " <<  (int)curr_index << std::endl;
    std::cout << "sb_start_addr: " <<  (int)sb_start_addr << std::endl;
    std::cout << "write_offset: " <<  (int)write_offset << std::endl;
    
    // Wrap around case
    if ( curr_index == sb_start_addr )
      prev_index = sb_start_addr + _buffer_len - 1;
    else
      prev_index = curr_index - 1;

    uint16_t sb_val = EEPROM.read(prev_index) + 1;
    std::cout << "write status buffer at: " <<  (int)curr_index << std::endl;
    std::cout << "\tstatus buffer value: " <<  (int)sb_val << std::endl;

    EEPROM.write( curr_index, sb_val );
  }
}
void EEPROMWearLeveler::clear()
{
  for (int i = 0; i < AVR_EEPROM_SIZE; i++)
  {
    EEPROM.write(i, 0);
  }  
}
Exemple #3
0
//------------------------------------------------------------------
bool StateMachine::saveToEEPROM() {
	Serial.println(" Save to EEPROMprom!");
	EEPROM.write(CHANELSADDRES    , c1.threshold);
	EEPROM.write(CHANELSADDRES + 1, c1.timeS);
	EEPROM.write(CHANELSADDRES + 2, c1.timeBan);

	EEPROM.write(CHANELSADDRES + 3, c2.threshold);
	EEPROM.write(CHANELSADDRES + 4, c2.timeS);
	EEPROM.write(CHANELSADDRES + 5, c2.timeBan);

	EEPROM.write(CHANELSADDRES + 6, c3.threshold);
	EEPROM.write(CHANELSADDRES + 7, c3.timeS);
	EEPROM.write(CHANELSADDRES + 8, c3.timeBan);	
}
//  save data to EEPROM
//  return: the size saved, 0 for error
static uint32_t EEPROM_Save(uint32_t addr, uint8_t * data, uint32_t size)
{
    //  the EEPROM size is 1K (0x000-0x3FF)
    if (addr + size < 0x400)
    {
        for (int i = 0; i < size; ++i)
        {
            EEPROM.write(addr + i, data[i]);
        }
        return size;
    }
    else
    {
        return 0;
    }
}