summaryrefslogtreecommitdiff
path: root/dim.c
blob: f10f0e7cd41508e239127971eb79de0de7999318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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(DALI_ADDR | 0x100);
		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;
}