Signed-off-by: Philipp Hachtmann <hachti@hachti.de>
#include <stdint.h>
#include "pin_magic.h"
#include <stdint.h>
#include "pin_magic.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))
#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)
{
static void trigger(void)
{
static void trigger(void)
{
setpin(DRIVE, 1);
_delay_us(20);
setpin(DRIVE, 0);
setpin(DRIVE, 1);
_delay_us(20);
setpin(DRIVE, 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 our_decoder = 3;
static volatile uint8_t our_key = 5;
static volatile uint8_t learn_mode = 0;
+static volatile uint8_t drive_on = 0;
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();
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);
if (decoder == our_decoder) {
if (command == our_key)
if (decoder == our_decoder) {
if (command == our_key)
+ uint8_t duty = 30;
+ uint8_t old_drive = 0;
+ our_decoder = eeprom_read_byte(&ee_our_decoder);
+ our_key = eeprom_read_byte(&ee_our_key);
+ if (our_decoder == 0xff)
+ learn_mode = 1;
- // while(PINVAL(BTN));
-
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 {
--- /dev/null
+#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