From 10515a80e2ba88b54c7c8766a0c42c5082985c90 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sun, 10 Mar 2013 06:16:57 +0100 Subject: dim: use tracked state --- dim.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 79 insertions(+), 30 deletions(-) (limited to 'dim.c') diff --git a/dim.c b/dim.c index 8a40148..78ae5a3 100644 --- a/dim.c +++ b/dim.c @@ -1,39 +1,88 @@ -static uint8_t dim_state; -static int8_t dim_dir; +struct switchcfg { + uint8_t targets0[2]; + uint8_t targets1[2]; + uint8_t lastclick; + int8_t dir; + uint8_t tgt1active; +}; -#define DALI_ADDR 0xfe00 +#define T_DOUBLECLICK 75 +struct switchcfg sw; + +static uint8_t tast_curstate(void) +{ + uint16_t sum = 0; + sum += target[sw.targets0[0]]; + sum += target[sw.targets0[1]]; + if (sw.tgt1active) { + sum += target[sw.targets1[0]]; + sum += target[sw.targets1[1]]; + sum++; + sum >>= 1; + } + sum++; + sum >>= 1; + return sum; +} + +static void tast_applystate(uint8_t all, uint8_t target) +{ + target_set(sw.targets0[0], target); + target_set(sw.targets0[1], target); + if (all) { + target_set(sw.targets1[0], target); + target_set(sw.targets1[1], target); + } +} static void tast_click(void) { + uint8_t ltarget, all; + uart_puts("# click\n"); - if (dim_state) { - dim_state = 0; - dali_send(DALI_ADDR | 0x100); - PORTD |= (1 << D_LED1); + if (sw.lastclick < T_DOUBLECLICK) { + sw.lastclick = T_DOUBLECLICK; + sw.tgt1active = 1; + sw.dir = -1; + ltarget = 0xff; + all = 1; } else { - dim_state = 0xfe; - dali_send(DALI_ADDR | 0x108); - dali_send(DALI_ADDR | dim_state); - PORTD &= ~(1 << D_LED1); + sw.lastclick = 0; + ltarget = tast_curstate(); + if (ltarget) { + sw.tgt1active = 0; + sw.dir = +1; + ltarget = 0; + all = 1; + } else { + sw.dir = -1; + ltarget = 0xff; + all = 0; + } } + tast_applystate(all, ltarget); } static void do_tick(void) { static uint8_t history; static uint8_t downctr; + uint8_t state; history <<= 1; history |= (PIND >> D_TAST) & 1; + if (sw.lastclick < 0xff) + sw.lastclick++; + if (history & 0x3f) { if (!downctr) return; if (downctr < 60) { - PORTD &= ~(1 << D_LED1); + //PORTD &= ~(1 << D_LED1); tast_click(); } else - dim_dir = -dim_dir; + sw.dir = -sw.dir; downctr = 0; return; @@ -42,33 +91,33 @@ static void do_tick(void) if (downctr < 60) { PORTD |= (1 << D_LED1); downctr++; - } else if (downctr == 60) { - if (!dim_state) { - dali_send(DALI_ADDR | 0x108); - dim_dir = 1; - } - downctr++; } else { + state = tast_curstate(); PORTD &= ~(1 << D_LED1); uart_puts("\t# dim "); - uart_puthex(dim_state); + uart_puthex(state); uart_puts(" "); - uart_puthex(dim_dir); + uart_puthex(sw.dir); uart_puts("\n"); - for (int i = 0; i < 3; i++) { - dim_state += dim_dir; - if (dim_state == 0xff) - dim_state = 0xfe; - if (dim_state < 0x55) - dim_state = 0x55; + for (int i = 0; i < 6; i++) { + state += sw.dir; + if (state == 0xff) + state = 0xfe; + if (state < 0x55) + state = 0x55; } - dali_send(DALI_ADDR | dim_state); + tast_applystate(sw.tgt1active, state); } } static void dim_init(void) { - dim_state = 0; - dim_dir = 1; + sw.targets0[0] = 0x03; + sw.targets0[1] = 0x04; + sw.targets1[0] = 0x01; + sw.targets1[1] = 0x02; + sw.lastclick = 0xff; + sw.dir = 1; + sw.tgt1active = 0; } -- cgit v1.2.1