diff options
-rw-r--r-- | lightctrl_v2.c | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/lightctrl_v2.c b/lightctrl_v2.c index 4e5c472..5d90850 100644 --- a/lightctrl_v2.c +++ b/lightctrl_v2.c @@ -64,17 +64,21 @@ static uint16_t dali_user_idle(void); struct dali_dev { uint8_t set, actual; - __extension__ uint8_t cooldown : 4; - __extension__ uint8_t need_set : 1; - __extension__ uint8_t need_poweron : 1; - __extension__ uint8_t _unused0 : 1; __extension__ uint8_t present : 1; + __extension__ uint8_t _unused1 : 1; + __extension__ uint8_t need_set : 2; + __extension__ uint8_t need_poweron : 2; + __extension__ uint8_t need_poweroff : 2; __extension__ uint8_t tx_read : 1; __extension__ uint8_t tx_set : 3; - __extension__ uint8_t _unused1 : 4; + __extension__ uint8_t cooldown : 4; } __attribute__((packed)); +#define NEED_SET 2 +#define NEED_POWERON 2 +#define NEED_POWEROFF 2 + static struct dali_dev devices[DALI_NUMDEV]; static void target_set(uint8_t dst, uint8_t val); #define target_get(idx) devices[idx].set @@ -101,9 +105,11 @@ static void target_set(uint8_t dst, uint8_t val) uart_puthex(val); uart_puts("]"); - devices[dst].need_set = 1; + devices[dst].need_set = NEED_SET; if (!devices[dst].set && val) - devices[dst].need_poweron = 1; + devices[dst].need_poweron = NEED_POWERON; + else if (!val) + devices[dst].need_poweroff = NEED_POWEROFF; devices[dst].set = val; } @@ -114,9 +120,10 @@ static void systick_user_1hz(void) if (!devices[i].cooldown) continue; devices[i].cooldown--; - if (!devices[i].cooldown) - devices[i].need_set = devices[i].need_poweron = - (devices[i].set != 0); + if (!devices[i].cooldown && devices[i].set) { + devices[i].need_set = NEED_SET; + devices[i].need_poweron = NEED_POWERON; + } } } @@ -162,37 +169,37 @@ static uint16_t dali_user_idle(void) return ret; do { - if (devices[dali_exec_pos].need_set - && !devices[dali_exec_pos].cooldown) { - if (devices[dali_exec_pos].need_poweron) { - devices[dali_exec_pos].need_poweron = 0; - ret = (dali_exec_pos << 9) | 0x108; - break; - } - if (devices[dali_exec_pos].set) - ret = (dali_exec_pos << 9) | - (devices[dali_exec_pos].set == 0xff - ? 0xfe : devices[dali_exec_pos].set); - else { - ret = (dali_exec_pos << 9) | 0x100; - devices[dali_exec_pos].cooldown = - T_EVG_COOLDOWN; - } - devices[dali_exec_pos].need_set = 0; - devices[dali_exec_pos].tx_set = 7; + dali_exec_pos = (dali_exec_pos + 1) & (DALI_NUMDEV - 1); - dali_exec_pos = (dali_exec_pos + 1) & (DALI_NUMDEV - 1); - break; + if (devices[dali_exec_pos].need_poweroff) { + ret = (dali_exec_pos << 9) | 0x100; + devices[dali_exec_pos].need_poweroff--; + devices[dali_exec_pos].cooldown = + T_EVG_COOLDOWN; + + } else if (devices[dali_exec_pos].need_poweron) { + ret = (dali_exec_pos << 9) | 0x108; + devices[dali_exec_pos].need_poweron--; + + } else if (devices[dali_exec_pos].need_set) { + ret = (dali_exec_pos << 9) | + (devices[dali_exec_pos].set == 0xff + ? 0xfe : devices[dali_exec_pos].set); + devices[dali_exec_pos].need_set--; } + } while (dali_exec_pos != stop_at && ret == DALI_INVALID); + if (ret == DALI_INVALID) + return ret; - dali_exec_pos = (dali_exec_pos + 1) & (DALI_NUMDEV - 1); - } while (dali_exec_pos != stop_at); - if (ret != DALI_INVALID) { - uart_puttick(); - uart_puts("dali async "); - uart_puthex16(ret); - uart_puts("\n"); - } + uart_puttick(); + uart_puts("dali async "); + uart_puthex16(ret); + uart_puts("\n"); + + if (!devices[dali_exec_pos].need_set + && !devices[dali_exec_pos].need_poweron + && !devices[dali_exec_pos].need_poweroff) + devices[dali_exec_pos].tx_set = 7; return ret; } @@ -339,7 +346,7 @@ static void can_handle_disco(uint16_t sublab_addr) * MIN LEVEL * POWER ON LEVEL * SYSTEM FAILURE LEVEL - * FADE RATE, FADE TIME (2x 4 bit) + * FADE RATE (low nibble), FADE TIME (high nibble) * STATUS * SHORT ADDRESS (or 0xff if no device) */ |