basic action handling inplemented

This commit is contained in:
King Kévin 2013-10-14 20:45:08 +02:00
parent aa2b367773
commit 72182dc780
2 changed files with 44 additions and 13 deletions

View File

@ -78,6 +78,10 @@ ISR(TIMER2_OVF_vect) { /* timer 2 overflow interrupt vector */
timer2_ovf++; /* increase tachometer counter */ timer2_ovf++; /* increase tachometer counter */
} else { } else {
tachometer = 0; /* indicate no speed can be measured */ tachometer = 0; /* indicate no speed can be measured */
if (pwr_ok) { /* warn the fan is dead while the power in on */
power_flag = true;
}
timer2_ovf = 0;
} }
} }
@ -171,44 +175,56 @@ int main(void)
ioinit(); /* initialize IOs */ ioinit(); /* initialize IOs */
uint8_t command_i = 0; /* command index */ uint8_t command_i = 0; /* command index */
struct nec ir_data; /* last IR data */
ir_data.valid = false;
ir_data.repeat = false;
ir_data.address = 0;
ir_data.command = 0;
uint8_t ir_repeat = 0; /* number of times the IR data has been repeated */
puts("LED dimmer up & running"); puts("LED dimmer up & running");
while (true) { while (true) {
/* handle user input */ /* handle user input */
while (input_flag) { while (input_flag) {
input_flag = false;
/* echo back */ /* echo back */
char c = 0; char c = 0;
while (command_i<input_i) { while (command_i<input_i) {
c = input[command_i++]; c = input[command_i++];
putchar(c); putchar(c);
} }
action(c); uart_action(c);
input_flag = false;
} }
/* handle power state */ /* handle power state */
while (power_flag) { while (power_flag) {
power_flag = false;
if (pwr_ok) { if (pwr_ok) {
puts("power ok"); puts("power ok");
/* verify if FAN is present */ /* verify if FAN is present */
_delay_ms(1000); _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);
} }
} else { } else {
puts("power ko"); puts("power off");
} }
power_flag = false;
} }
/* handle IR input */ /* handle IR input */
while (ir_flag) { while (ir_flag) {
time2nec(burst,pulse-1); /* convert raw time burst in NEC format */ time2nec(burst,pulse-1); /* convert raw time burst in NEC format */
struct nec ir_data = nec2data(burst,pulse-1); /* decode NEC burst */ struct nec ir_tmp = nec2data(burst,pulse-1); /* decode NEC burst */
if (ir_data.valid) { if (ir_tmp.valid) {
if (ir_data.repeat) { if (ir_tmp.repeat) {
printf("IR signal repeated\n"); if (ir_repeat<0xff) {
ir_repeat++;
}
} else { } else {
printf("IR addr: %u, command: %u\n",ir_data.address,ir_data.command); ir_data = ir_tmp;
ir_repeat = 0;
}
if (ir_repeat==0 || ir_repeat>3) {
ir_action(ir_data.address,ir_data.command);
} }
} }
pulse = 0; /* reset burst */ pulse = 0; /* reset burst */
@ -218,8 +234,7 @@ int main(void)
return 0; return 0;
} }
static void uart_action(char c)
static void action(char c)
{ {
switch (c) { switch (c) {
case 'l': case 'l':
@ -369,3 +384,18 @@ static void action(char c)
break; break;
} }
} }
static void ir_action(uint8_t address, uint8_t command)
{
if (0==address && 72==command) {
printf("switching power supply ");
if (PINB&(1<<nPS_ON)) {
puts("on");
} else {
puts("off");
}
PINB |= (1<<nPS_ON);
} else {
printf("IR addr: %u, command: %u\n", address, command);
}
}

View File

@ -17,4 +17,5 @@
#define CH2_5 PD7 #define CH2_5 PD7
static void ioinit(void); static void ioinit(void);
static void action(char c); static void uart_action(char c);
static void ir_action(uint8_t address, uint8_t command);