From: Philipp Hachtmann Date: Sun, 5 Feb 2017 03:29:40 +0000 (+0100) Subject: trennfix/sw: Some cleanup X-Git-Url: http://gitweb.hachti.de/?a=commitdiff_plain;h=af5fd9802f74d81d0aef8b45c200d16611b370ee;p=eisenbahn.git trennfix/sw: Some cleanup Signed-off-by: Philipp Hachtmann --- diff --git a/trennfix/sw/main.c b/trennfix/sw/main.c index 26a7ef0..d2111f7 100644 --- a/trennfix/sw/main.c +++ b/trennfix/sw/main.c @@ -32,83 +32,25 @@ #include #include "pin_magic.h" +#include "mm_lookup.h" -#define LED _PIN(PORTB, PORTB2) +#define LED _PIN(PORTB, PORTB2) #define SENSE _PIN(PORTB, PORTB4) #define DRIVE _PIN(PORTB, PORTB3) #define BTN _PIN(PORTB, PB1) #define SIGIN (!PINVAL(SENSE)) - -#define BTN_PRESSED (PINVAL(BTN)) +#define BTN_PRESSED (!PINVAL(BTN)) // #define DEBUG_TIMEOUT - #ifdef DEBUG_TIMEOUT #define MON_TIMEOUT(val) setpin(DRIVE, val) #else #define MON_TIMEOUT(xx) ({0;}) #endif - - -/* - * Lookup decoder number. - * - */ - -static uint8_t get_decoder(uint8_t mm_byte) -{ - switch(mm_byte) { - /* 0x00 is invalid */ - case 0xc0: return 1; - case 0x80: return 2; - case 0x30: return 3; - case 0xf0: return 4; - case 0xb0: return 5; - case 0x20: return 6; - case 0xe0: return 7; - case 0xa0: return 8; - - case 0x0c: return 9; - case 0xcc: return 10; - case 0x8c: return 11; - case 0x3c: return 12; - case 0xfc: return 13; - case 0xbc: return 14; - case 0x2c: return 15; - case 0xec: return 16; - case 0xac: return 17; - - case 0x08: return 18; - case 0xc8: return 19; - case 0x88: return 20; - case 0x38: return 21; - case 0xf8: return 22; - case 0xb8: return 23; - case 0x28: return 24; - case 0xe8: return 25; - - default: - return 0; - } -} - -static uint8_t get_command(uint8_t mm_byte) -{ - switch(mm_byte) { - case 0xc3: return 1; - case 0x03: return 2; - case 0xf3: return 3; - case 0x33: return 4; - case 0xcf: return 5; - case 0x0f: return 6; - case 0xff: return 7; - case 0x3f: return 8; - default: - return 0; - } -} +uint8_t EEMEM ee_our_decoder; +uint8_t EEMEM ee_our_key; static void setup_hw(void) { @@ -186,6 +128,7 @@ static void one(void) static void trigger(void) { + return; setpin(DRIVE, 1); _delay_us(20); setpin(DRIVE, 0); @@ -194,6 +137,7 @@ static void trigger(void) static volatile uint8_t our_decoder = 3; static volatile uint8_t our_key = 5; static volatile uint8_t learn_mode = 0; +static volatile uint8_t drive_on = 0; ISR(TIMER0_COMPA_vect) { @@ -218,8 +162,8 @@ ISR(TIMER0_COMPA_vect) { if ((shift_sub == shift_sub_first) && (shift_decoder == shift_decoder_first)) { - uint8_t decoder = get_decoder(shift_decoder); - uint8_t command = get_command(shift_sub); + uint8_t decoder = lookup_decoder(shift_decoder); + uint8_t command = lookup_command(shift_sub); if (decoder) { /* Congratulations, we have a valid command */ trigger(); @@ -229,14 +173,15 @@ ISR(TIMER0_COMPA_vect) { our_decoder = decoder; our_key = command; learn_mode = 0; + eeprom_write_byte(&ee_our_decoder, our_decoder); + eeprom_write_byte(&ee_our_key, our_key); } } else { - if (decoder == our_decoder) { if (command == our_key) - setpin(LED, 1); + drive_on = 1; if (command == 0) - setpin(LED, 0); + drive_on = 0; } } } @@ -292,12 +237,30 @@ ISR(PCINT0_vect) */ int main(void) { + uint8_t duty = 30; + uint8_t old_drive = 0; setup_hw(); + our_decoder = eeprom_read_byte(&ee_our_decoder); + our_key = eeprom_read_byte(&ee_our_key); + if (our_decoder == 0xff) + learn_mode = 1; sei(); - // while(PINVAL(BTN)); - + uint8_t i; while (1) { if (!learn_mode) { + if (old_drive == 0 && drive_on) { + setpin(DRIVE, 1); + _delay_ms(100); + } + old_drive = drive_on; + setpin(DRIVE, drive_on); + for (i = 0; i < duty; i++) + _delay_us(4); + for (i = 0; i < (255 - duty); i++) { + setpin(DRIVE, 0); + _delay_us(4); + } + if (!PINVAL(BTN)) learn_mode = 1; } else { diff --git a/trennfix/sw/mm_lookup.h b/trennfix/sw/mm_lookup.h new file mode 100644 index 0000000..be68dfb --- /dev/null +++ b/trennfix/sw/mm_lookup.h @@ -0,0 +1,62 @@ +#ifndef __MM_LOOKUP_H +#define __MM_LOOKUP_H + +/* + * Lookup decoder number. + * + */ +static uint8_t lookup_decoder(uint8_t mm_byte) +{ + switch(mm_byte) { + /* 0x00 is invalid */ + case 0xc0: return 1; + case 0x80: return 2; + case 0x30: return 3; + case 0xf0: return 4; + case 0xb0: return 5; + case 0x20: return 6; + case 0xe0: return 7; + case 0xa0: return 8; + + case 0x0c: return 9; + case 0xcc: return 10; + case 0x8c: return 11; + case 0x3c: return 12; + case 0xfc: return 13; + case 0xbc: return 14; + case 0x2c: return 15; + case 0xec: return 16; + case 0xac: return 17; + + case 0x08: return 18; + case 0xc8: return 19; + case 0x88: return 20; + case 0x38: return 21; + case 0xf8: return 22; + case 0xb8: return 23; + case 0x28: return 24; + case 0xe8: return 25; + + default: + return 0; + } +} + +static uint8_t lookup_command(uint8_t mm_byte) +{ + switch(mm_byte) { + case 0xc3: return 1; + case 0x03: return 2; + case 0xf3: return 3; + case 0x33: return 4; + case 0xcf: return 5; + case 0x0f: return 6; + case 0xff: return 7; + case 0x3f: return 8; + default: + return 0; + } +} + + +#endif