summaryrefslogtreecommitdiff
path: root/lightctrl.c
blob: 79fdeee09db412f95251b860e0d79e87d04595b8 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#define F_CPU 8000000
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/delay.h>

const uint8_t __signature[3] __attribute__((section (".signature"), used)) =
        { SIGNATURE_2, SIGNATURE_1, SIGNATURE_0 };

#define B_SCK	5
#define B_MISO	4
#define B_MOSI	3
#define B_SS	2

#define D_TXD	1
#define D_DALII	3
#define D_DALIO	4
#define D_LED1	5
#define D_LED2	6
#define D_TAST	7

#include "uart.c"
#include "dali2.c"
#include "dali_ctl.c"
#include "dim.c"
#include "can.c"

static void can_rx_exec(void)
{
	if (can_rx_addr.b[0] == 0xcc
		&& (can_rx_addr.b[1] & 0xfb) == 0x08
		&& can_rx_addr.b[2] == 0x04
		&& can_rx_addr.b[3] == 0x7f
		&& (can_rx_dlc & 0x4f) == 1) {
		uart_puts("-- SET\n");

		if (!can_rx_data[0]) {
			dim_state = 0;
			dali_send(DALI_ADDR | 0x100);
		} else {
			if (!dim_state && can_rx_data[0])
				dali_send(DALI_ADDR | 0x108);
			dim_state = can_rx_data[0];
			dali_send(DALI_ADDR | dim_state);
		}
	}
}

int main(void)
{
	DDRD |= (1 << D_LED1) | (1 << D_LED2 ) | (1 << D_TXD) | (1 << D_DALIO);
	PORTD |= (1 << D_LED1) | (1 << D_TAST);
	PORTD &= ~(1 << D_LED1);

	uart_init();
	can_preinit();
	dali_init();
	dim_init();

	sei();
	uart_puts("\ninit done\n");

	can_init();
	can_CANSTAT();

	dali_search();

	uint16_t ctr = 0;

	while (1) {
		if (canint) {
			canint = false;
			can_int();

			if (can_rx_addr.b[0]) {
				can_rx_exec();
				can_rx_addr.u = 0;
			}
		}
		_delay_ms(5);
		do_tick();

		ctr++;
		switch (ctr) {
		case 2048:
			ctr = 0;
			break;
		case 512:
			dali_send(0xffa0);
			if (dali_rx_avail) {
				uart_puts("ll ");
				uart_puthex(dali_rx);
				uart_puts("\n");
				can_send(0xe608047f, 1, (uint8_t *)&dali_rx);
			} else
				uart_puts("ll noans\n");
			break;
		case 1024:
			can_send(0xe7000000, 8, (uint8_t *)&dalistat);

			uart_puts("dali stats: ");
			uart_puthex16(dalistat.rxok);
			uart_puts(" ok  ");
			uart_puthex16(dalistat.falsestart);
			uart_puts(" f-start  ");
			uart_puthex16(dalistat.noise);
			uart_puts(" noise  ");
			uart_puthex16(dalistat.manchester);
			uart_puts(" mch-err\n");
			break;
		case 1536:
			can_send(0xcc08047f, 1, (uint8_t *)&dim_state);
			break;
		}
	}
}

void __do_copy_data(void) __attribute__((naked, section (".init4"), used));
void __do_copy_data(void) { }