summaryrefslogtreecommitdiff
path: root/dim.c
diff options
context:
space:
mode:
Diffstat (limited to 'dim.c')
-rw-r--r--dim.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/dim.c b/dim.c
new file mode 100644
index 0000000..6414d5f
--- /dev/null
+++ b/dim.c
@@ -0,0 +1,74 @@
+static uint8_t dim_state;
+static int8_t dim_dir;
+
+#define DALI_ADDR 0xfe00
+
+static void tast_click(void)
+{
+ uart_puts("# click\n");
+ if (dim_state) {
+ dim_state = 0;
+ dali_send(0xff00);
+ PORTD |= (1 << D_LED1);
+ } else {
+ dim_state = 0xfe;
+ dali_send(DALI_ADDR | 0x108);
+ dali_send(DALI_ADDR | dim_state);
+ PORTD &= ~(1 << D_LED1);
+ }
+}
+
+static void do_tick(void)
+{
+ static uint8_t history;
+ static uint8_t downctr;
+
+ history <<= 1;
+ history |= (PIND >> D_TAST) & 1;
+
+ if (history & 0x3f) {
+ if (!downctr)
+ return;
+ if (downctr < 60) {
+ PORTD &= ~(1 << D_LED1);
+ tast_click();
+ } else
+ dim_dir = -dim_dir;
+
+ downctr = 0;
+ return;
+ }
+
+ 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 {
+ PORTD &= ~(1 << D_LED1);
+
+ uart_puts("\t# dim ");
+ uart_puthex(dim_state);
+ uart_puts(" ");
+ uart_puthex(dim_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 < 0x40)
+ dim_state = 0x40;
+ }
+ dali_send(DALI_ADDR | dim_state);
+ }
+}
+
+static void dim_init(void)
+{
+ dim_state = 0;
+ dim_dir = 1;
+}