add power setting

This commit is contained in:
King Kévin 2013-10-19 14:36:03 +02:00
parent 2dba5b10d2
commit eeae409eae
3 changed files with 24 additions and 5 deletions

View File

@ -337,7 +337,7 @@ int main(void)
puts("LED dimmer up & running"); puts("LED dimmer up & running");
/* load settings */ // load (or initialize) settings
if (!verify_settings()) { if (!verify_settings()) {
initialize_settings(); initialize_settings();
save_settings(); save_settings();
@ -351,6 +351,13 @@ int main(void)
puts("settings loaded"); puts("settings loaded");
} }
// switch power supply as saved
if (power) {
PORTB &= ~(1<<nPS_ON);
} else {
PORTB |= (1<<nPS_ON);
}
while (true) { while (true) {
/* calculated PWM values */ /* calculated PWM values */
while (channel_flag) { while (channel_flag) {
@ -406,15 +413,19 @@ int main(void)
while (power_flag) { while (power_flag) {
if (pwr_ok) { if (pwr_ok) {
puts("power ok"); puts("power ok");
power = 1;
/* verify if FAN is present */ /* verify if FAN is present */
_delay_ms(500); _delay_ms(500);
if (0==tachometer) { if (0==tachometer) {
puts("FAN not on, switching power off"); puts("FAN not on, switching power off");
PORTB |= (1<<nPS_ON); PORTB |= (1<<nPS_ON);
power = 0;
} }
} else { } else {
puts("power off"); puts("power off");
power = 0;
} }
save_settings();
power_flag = false; power_flag = false;
} }
/* handle IR input */ /* handle IR input */
@ -436,13 +447,13 @@ int main(void)
ir_keys[to_learn][0] = ir_data.address; ir_keys[to_learn][0] = ir_data.address;
ir_keys[to_learn][1] = ir_data.command; ir_keys[to_learn][1] = ir_data.command;
} }
save_settings();
puts("IR code learned"); puts("IR code learned");
to_learn = IR_ACTION_END; to_learn = IR_ACTION_END;
learn_flag = false; learn_flag = false;
} else { } else {
ir_action(ir_data.address,ir_data.command); ir_action(ir_data.address,ir_data.command);
} }
save_settings();
} }
} }
pulse = 0; /* reset burst */ pulse = 0; /* reset burst */

View File

@ -7,8 +7,9 @@
#include "settings.h" #include "settings.h"
/* initialize variable */ /* initialize variable */
uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2]; uint8_t power;
uint8_t mode; uint8_t mode;
uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2];
uint8_t ir_keys[IR_ACTION_END][2]; uint8_t ir_keys[IR_ACTION_END][2];
const uint8_t MAGIC = 0x42; // magic header const uint8_t MAGIC = 0x42; // magic header
@ -18,7 +19,7 @@ bool verify_settings(void)
bool to_return; bool to_return;
uint8_t checksum = 0; uint8_t checksum = 0;
uint8_t byte; 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) uint16_t settings_size = sizeof(MAGIC)+sizeof(power)+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++) { for (uint16_t i=0; i<settings_size; i++) {
byte = eeprom_read_byte((const uint8_t*)i); byte = eeprom_read_byte((const uint8_t*)i);
checksum ^= byte; checksum ^= byte;
@ -33,6 +34,7 @@ bool verify_settings(void)
void initialize_settings(void) void initialize_settings(void)
{ {
power = 0;
mode = 0; mode = 0;
for (uint8_t i=0; i<MODES; i++) { for (uint8_t i=0; i<MODES; i++) {
for (uint8_t j=0; j<CHANNELS_1+CHANNELS_2; j++) { for (uint8_t j=0; j<CHANNELS_1+CHANNELS_2; j++) {
@ -52,6 +54,9 @@ void save_settings(void)
eeprom_update_byte((uint8_t*)addr,MAGIC); eeprom_update_byte((uint8_t*)addr,MAGIC);
checksum ^= MAGIC; checksum ^= MAGIC;
addr++; addr++;
eeprom_update_byte((uint8_t*)addr,power);
checksum ^= power;
addr++;
eeprom_update_byte((uint8_t*)addr,mode); eeprom_update_byte((uint8_t*)addr,mode);
checksum ^= mode; checksum ^= mode;
addr++; addr++;
@ -76,6 +81,8 @@ void save_settings(void)
void load_settings(void) void load_settings(void)
{ {
uint16_t addr = 1; // the address in the EEPROM (skip magic header) uint16_t addr = 1; // the address in the EEPROM (skip magic header)
power = eeprom_read_byte((uint8_t*)addr);
addr++;
mode = eeprom_read_byte((uint8_t*)addr); mode = eeprom_read_byte((uint8_t*)addr);
addr++; addr++;
for (uint8_t i=0; i<MODES; i++) { for (uint8_t i=0; i<MODES; i++) {

View File

@ -1,6 +1,7 @@
/* mode settings */ /* mode settings */
#define MODES 3 /* the number of modes */ #define MODES 3 /* the number of modes */
extern uint8_t mode; /* the current mode */ //TODO load the mode extern uint8_t power; /* power state */
extern uint8_t mode; /* the current mode */
extern uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2]; /* the mode brightness settings for the channels */ extern uint8_t brightness[MODES][CHANNELS_1+CHANNELS_2]; /* the mode brightness settings for the channels */
/* IR settings */ /* IR settings */