trennfix/sw: Some cleanup
authorPhilipp Hachtmann <hachti@hachti.de>
Sun, 5 Feb 2017 03:29:40 +0000 (04:29 +0100)
committerPhilipp Hachtmann <hachti@hachti.de>
Sun, 12 Feb 2017 17:03:58 +0000 (18:03 +0100)
Signed-off-by: Philipp Hachtmann <hachti@hachti.de>
trennfix/sw/main.c
trennfix/sw/mm_lookup.h [new file with mode: 0644]

index 26a7ef0abfe9b48cb572bb49d6f53695abdc2ac2..d2111f786f99b4b9e18bdec5ee0266f27aed7dc0 100644 (file)
 #include <stdint.h>
 
 #include "pin_magic.h"
 #include <stdint.h>
 
 #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 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
 
 // #define DEBUG_TIMEOUT
-
 #ifdef DEBUG_TIMEOUT
 #define MON_TIMEOUT(val) setpin(DRIVE, val)
 #else
 #define MON_TIMEOUT(xx) ({0;})
 #endif
 
 #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)
 {
 
 static void setup_hw(void)
 {
@@ -186,6 +128,7 @@ static void one(void)
 
 static void trigger(void)
 {
 
 static void trigger(void)
 {
+       return;
        setpin(DRIVE, 1);
        _delay_us(20);
        setpin(DRIVE, 0);
        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 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) {
        
 
 ISR(TIMER0_COMPA_vect) {
        
@@ -218,8 +162,8 @@ ISR(TIMER0_COMPA_vect) {
                
                if ((shift_sub == shift_sub_first) &&
                    (shift_decoder == shift_decoder_first)) {
                
                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();
                        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;
                                                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 {
                                        }
                                } else {
-                               
                                        if (decoder == our_decoder) {
                                                if (command == our_key)
                                        if (decoder == our_decoder) {
                                                if (command == our_key)
-                                                       setpin(LED, 1);
+                                                       drive_on = 1;
                                                if (command == 0)
                                                if (command == 0)
-                                                       setpin(LED, 0);
+                                                       drive_on = 0;
                                        }
                                }
                        }
                                        }
                                }
                        }
@@ -292,12 +237,30 @@ ISR(PCINT0_vect)
  */
 
 int main(void) {
  */
 
 int main(void) {
+       uint8_t duty = 30;
+       uint8_t old_drive = 0;
        setup_hw();
        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();
        sei();
-       //      while(PINVAL(BTN));
-       
+       uint8_t i;
        while (1) {
                if (!learn_mode) {
        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 {
                        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 (file)
index 0000000..be68dfb
--- /dev/null
@@ -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