can now save and read setting from EEPROM

This commit is contained in:
King Kévin 2013-10-17 16:35:58 +02:00
parent abcc33cae5
commit 2d2c25a6fe
3 changed files with 104 additions and 11 deletions

View File

@ -32,6 +32,8 @@
#include "ir_nec.h"
#include "settings.h"
#include <avr/eeprom.h> /* EEPROM handling */
/* global variables */
#define INPUT_MAX 255 /* max length for user input string */
char input[INPUT_MAX+2]; /* user input from USART */
@ -221,7 +223,23 @@ int main(void)
channel_flag = true; /* calculate above values later */
puts("LED dimmer up & running");
verify_settings();
uint8_t p = 0;
eeprom_update_byte((uint8_t*)&p,0x42);
printf("eeprom: %02x\n",eeprom_read_byte((uint8_t*)&p));
if (verify_settings()) {
puts("settings ok");
} else {
puts("settings corrupted");
puts("initializing settings");
initialize_settings();
puts("saving settings");
save_settings();
if (verify_settings()) {
puts("settings saved");
} else {
puts("settings not saved");
}
}
return 0;
while (true) {
/* calculated PWM values */

View File

@ -1,22 +1,94 @@
#include <stdint.h> /* Standard Integer Types */
#include <stdio.h> /* Standard IO facilities */
#include <stdlib.h> /* General utilities */
#include <stdbool.h> /* Boolean */
#include <avr/eeprom.h> /* EEPROM handling */
#include "settings.h"
#include <string.h> /* Strings */
#include "uart.h"
/* initialize variable */
volatile uint8_t* PORTS[CHANNELS_1+CHANNELS_2] = {&PORTC,&PORTC,&PORTC,&PORTC,&PORTC,&PORTD,&PORTD,&PORTD,&PORTD,&PORTD};
const uint8_t BITS[CHANNELS_1+CHANNELS_2] = {PC0,PC1,PC2,PC3,PC4};
uint8_t mode = 0;
uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2];
uint8_t mode;
uint8_t ir_keys[IR_ACTION_END][2];
void verify_settings(void)
const uint8_t MAGIC = 0x42; // magic header
bool verify_settings(void)
{
bool to_return;
uint8_t checksum = 0;
printf("brightness: %u\n",sizeof(brightness));
printf("checksum: %u\n",checksum);
uint8_t byte;
uint16_t settings_size = 1+sizeof(mode)+sizeof(brightness)+sizeof(ir_keys)+1; // the byte used for the checksum (magic header and checksum included)
for (uint16_t i=0; i<settings_size; i++) {
byte = eeprom_read_byte((const uint8_t*)i);
checksum ^= byte;
}
if (0==checksum) {
to_return = true;
} else {
to_return = false;
}
return to_return;
}
void initialize_settings(void)
{
mode = 0;
for (uint8_t i=0; i<MODES; i++) {
for (uint8_t j=0; j<CHANNELS_1+CHANNELS_2; j++) {
brightness[i][j] = 0;
}
}
for (uint8_t i=0; i<IR_ACTION_END; i++) {
ir_keys[i][0] = 0;
ir_keys[i][1] = 0;
}
}
void save_settings(void)
{
uint16_t addr = 0; // the address in the EEPROM
uint8_t checksum = 0;
eeprom_update_byte((uint8_t*)addr,MAGIC);
checksum ^= MAGIC;
addr++;
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)
{
uint16_t addr = 1; // the address in the EEPROM (skip magic header)
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++;
}
}

View File

@ -21,7 +21,10 @@ enum IR_ACTIONS { /* the actions for the infrared remote control */
CHANNEL_DOWN,
IR_ACTION_END
};
uint8_t ir_keys[IR_ACTION_END][2];
extern uint8_t ir_keys[IR_ACTION_END][2]; // the IR NEC values (address+command) for the actions
/* function to load/save the settings */
void verify_settings(void);
bool verify_settings(void);
void initialize_settings(void);
void save_settings(void);
void load_settings(void);