summaryrefslogtreecommitdiff
path: root/door.c
blob: 561289fac9f5768801503fe7e90611b7a01a5fd6 (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
#define F_CPU 8000000
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.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 B_EXITLIGHT	1
#define B_LIGHTBARRIER	0

#define B_IN		(1 << B_LIGHTBARRIER)

#define D_LED		4
#define D_DOORLOCK	5
#define D_DOORRIGHT	6
#define D_DOORLEFT	7

#define D_IN		((1 << D_DOORLOCK) | (1 << D_DOORRIGHT) | (1 << D_DOORLEFT))

#include "uart.c"
#include "can.c"

static uint16_t tick = 4;

static uint8_t pinstat;
static struct sendbuf {
	uint8_t door_lock;
	uint8_t door_left;
	uint8_t door_right;
	uint8_t lightbarrier;
} sendbuf;

ISR(TIMER1_OVF_vect)
{
	uint8_t newpin;

	tick++;
	if (tick == 0)
		tick = 16;

	if (tick < 4096 || ((tick & 16383) < 1024))
		PORTD |= (1 << D_LED);
	else
		PORTD &= ~(1 << D_LED);

	newpin = (PIND & D_IN) | (PINB & B_IN);
	pinstat ^= newpin;
#define pin_to_field(pin, field) \
	if (pinstat & (1 << pin)) { \
		sendbuf.field++; \
		if (newpin & (1 << pin)) \
			sendbuf.field |= 1; \
		else \
			sendbuf.field &= ~1; \
	}

	pin_to_field(D_DOORLOCK,  door_lock);
	pin_to_field(D_DOORRIGHT, door_right);
	pin_to_field(D_DOORLEFT,  door_left);
	pin_to_field(B_LIGHTBARRIER, lightbarrier);

	if (pinstat) {
		if (tick >= 128 && tick < 65408)
			tick = 4;
		else if (tick < 128)
			tick = 65408;
	}

	pinstat = newpin;

	if (tick == 8 || tick == 16)
		can_send(CANA_SENSOR_F(0x100), sizeof(sendbuf), (uint8_t *)&sendbuf);
}

int main(void)
{
	DDRB = (1 << B_SCK) | (1 << B_MOSI) | (1 << B_SS) | (1 << B_EXITLIGHT);
	PORTB = (1 << B_SS) | (1 << B_EXITLIGHT) | B_IN;
	DDRD = (1 << D_LED);
	PORTD = D_IN;

	uart_init();
	can_preinit();

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

	can_init();
	can_CANSTAT();

	TCNT1 = 0;
	OCR1A = 128;
	TIMSK1 = (1 << TOIE1);
	TIFR1  = (1 << TOV1);
	// 8 MHz / 8 / 256 = 3906.25 Hz
	// Fast PWM (8-bit) mode
	TCCR1A = (1 << COM1A1) | (1 << WGM10);
	TCCR1B = (1 << WGM12) | (1 << CS11);

	while (1) {
		_delay_ms(1);
	}
}