2013-10-17 13:29:01 +02:00
# include <stdint.h> /* Standard Integer Types */
# include <stdlib.h> /* General utilities */
# include <stdbool.h> /* Boolean */
2013-10-17 16:35:58 +02:00
# include <avr/eeprom.h> /* EEPROM handling */
2013-10-17 13:29:01 +02:00
2013-10-17 16:54:13 +02:00
# include "main.h"
2013-10-17 13:29:01 +02:00
# include "settings.h"
2013-10-26 17:31:04 +02:00
/* initialize variables */
2013-10-19 14:36:03 +02:00
uint8_t power ;
2013-10-17 16:35:58 +02:00
uint8_t mode ;
2013-10-19 14:36:03 +02:00
uint8_t brightness [ MODES ] [ CHANNELS_1 + CHANNELS_2 ] ;
2013-10-17 16:35:58 +02:00
uint8_t ir_keys [ IR_ACTION_END ] [ 2 ] ;
2013-10-26 17:31:04 +02:00
const uint8_t MAGIC = 0x42 ; /* magic header */
2013-10-17 16:35:58 +02:00
bool verify_settings ( void )
{
bool to_return ;
uint8_t checksum = 0 ;
uint8_t byte ;
2013-10-26 17:31:04 +02:00
uint16_t settings_size = sizeof ( MAGIC ) + sizeof ( power ) + sizeof ( mode ) + sizeof ( brightness ) + sizeof ( ir_keys ) + 1 ; /* the bytes used for the checksum (magic header and checksum included) */
for ( uint16_t i = 0 ; i < settings_size ; i + + ) { /* calculate checksum */
2013-10-17 16:35:58 +02:00
byte = eeprom_read_byte ( ( const uint8_t * ) i ) ;
2013-10-26 17:31:04 +02:00
if ( 0 = = i & & byte ! = MAGIC ) { /* verify magic header first */
2013-10-19 19:51:18 +02:00
return false ;
}
2013-10-17 16:35:58 +02:00
checksum ^ = byte ;
}
2013-10-26 17:31:04 +02:00
/* verify checkecum */
2013-10-17 16:35:58 +02:00
if ( 0 = = checksum ) {
to_return = true ;
} else {
to_return = false ;
}
return to_return ;
}
2013-10-17 13:29:01 +02:00
2013-10-17 16:35:58 +02:00
void initialize_settings ( void )
2013-10-17 13:29:01 +02:00
{
2013-10-26 17:31:04 +02:00
power = 0 ; /* power is off */
mode = 0 ; /* first mode used */
for ( uint8_t i = 0 ; i < MODES ; i + + ) { /* channel brightness set to 0 */
2013-10-17 16:35:58 +02:00
for ( uint8_t j = 0 ; j < CHANNELS_1 + CHANNELS_2 ; j + + ) {
brightness [ i ] [ j ] = 0 ;
}
}
2013-10-26 17:31:04 +02:00
for ( uint8_t i = 0 ; i < IR_ACTION_END ; i + + ) { /* codes for IR actions set to 0 */
2013-10-17 16:35:58 +02:00
ir_keys [ i ] [ 0 ] = 0 ;
ir_keys [ i ] [ 1 ] = 0 ;
}
}
void save_settings ( void )
{
2013-10-26 17:31:04 +02:00
uint16_t addr = 0 ; /* address in the EEPROM */
uint8_t checksum = 0 ; /* checksum of settings (including the magic header, else all 0 settings are calculated as valid) */
2013-10-17 16:35:58 +02:00
eeprom_update_byte ( ( uint8_t * ) addr , MAGIC ) ;
checksum ^ = MAGIC ;
addr + + ;
2013-10-19 14:36:03 +02:00
eeprom_update_byte ( ( uint8_t * ) addr , power ) ;
checksum ^ = power ;
addr + + ;
2013-10-17 16:35:58 +02:00
eeprom_update_byte ( ( uint8_t * ) addr , mode ) ;
checksum ^ = mode ;
addr + + ;
for ( uint8_t i = 0 ; i < MODES ; i + + ) {
for ( uint8_t j = 0 ; j < CHANNELS_1 + CHANNELS_2 ; j + + ) {
eeprom_update_byte ( ( uint8_t * ) addr , brightness [ i ] [ j ] ) ;
checksum ^ = brightness [ i ] [ j ] ;
addr + + ;
}
}
for ( uint8_t i = 0 ; i < IR_ACTION_END ; i + + ) {
eeprom_update_byte ( ( uint8_t * ) addr , ir_keys [ i ] [ 0 ] ) ;
checksum ^ = ir_keys [ i ] [ 0 ] ;
addr + + ;
eeprom_update_byte ( ( uint8_t * ) addr , ir_keys [ i ] [ 1 ] ) ;
checksum ^ = ir_keys [ i ] [ 1 ] ;
addr + + ;
}
eeprom_update_byte ( ( uint8_t * ) addr , checksum ) ;
}
void load_settings ( void )
{
2013-10-26 17:31:04 +02:00
uint16_t addr = 1 ; /* the address in the EEPROM (skip magic header) */
2013-10-19 14:36:03 +02:00
power = eeprom_read_byte ( ( uint8_t * ) addr ) ;
addr + + ;
2013-10-17 16:35:58 +02:00
mode = eeprom_read_byte ( ( uint8_t * ) addr ) ;
addr + + ;
for ( uint8_t i = 0 ; i < MODES ; i + + ) {
for ( uint8_t j = 0 ; j < CHANNELS_1 + CHANNELS_2 ; j + + ) {
brightness [ i ] [ j ] = eeprom_read_byte ( ( uint8_t * ) addr ) ;
addr + + ;
}
}
for ( uint8_t i = 0 ; i < IR_ACTION_END ; i + + ) {
ir_keys [ i ] [ 0 ] = eeprom_read_byte ( ( uint8_t * ) addr ) ;
addr + + ;
ir_keys [ i ] [ 1 ] = eeprom_read_byte ( ( uint8_t * ) addr ) ;
addr + + ;
}
2013-10-17 13:29:01 +02:00
}
2013-10-19 19:51:18 +02:00
void reset_settings ( void )
{
2013-10-26 17:31:04 +02:00
eeprom_update_byte ( ( uint8_t * ) 0 , 0 ) ; /* invalidate magic header so the verification fails */
2013-10-19 19:51:18 +02:00
}