summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 3af81c52b466be8477fce134d6802dcee3539458 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdint.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "fpm.h"

#define SERVO_PIN          PB1
#define SERVO_DDR          DDRB

#define PWM_MIN            500
#define PWM_MID            1600
#define PWM_MAX            2550
#define PWM_TOP            19999

#define LED_PIN            PB5
#define LED_DDR            DDRB
#define LED_PORT           PORTB

#define PWR_SERVO          PC4
#define PWR_FPM            PC5
#define PWR_DDR            DDRC
#define PWR_PORT           PORTC

#define FRONT_UNLOCK_PIN   PD2
#define FRONT_LOCK_PIN     PD3
#define ENROLL_PIN         PD4
#define BACK_LOCK_PIN      PD5
#define BACK_UNLOCK_PIN    PD6

#define INPUT_DDR          DDRD
#define INPUT_PORT         PORTD

#define FPM_UNLOCK_INT     INT0
#define FPM_INT_VEC        INT0_vect

#define FRONT_LOCK_INT     PCINT19
#define ENROLL_INT         PCINT20
#define BACK_LOCK_INT      PCINT21
#define BACK_UNLOCK_INT    PCINT22
#define BTN_INT_VEC        PCINT2_vect

enum CTRL { 
	NONE = 0, 
	LOCK_FRONT = 1, 
	LOCK_BACK = 2, 
	UNLOCK_FRONT = 3, 
	UNLOCK_BACK = 4, 
	ENROLL = 5 
}; 

static volatile enum CTRL cmd = NONE;

static inline void lock(void)
{
	OCR1A = PWM_MID;
	_delay_ms(500);
}

static inline void unlock(void)
{
	OCR1A = PWM_MAX;
	_delay_ms(500);
}

static inline void flash_led(void)
{
	for (int i = 0; i < 4; i++) {
		LED_PORT |= (1 << LED_PIN);
		_delay_ms(70);
		LED_PORT &= ~(1 << LED_PIN);
		_delay_ms(70);
	}
}

static inline void pwr_fpm_servo_on(void)
{
	PWR_PORT |= (1 << PWR_FPM) | (1 << PWR_SERVO);
}

static inline void pwr_fpm_servo_off(void)
{
	PWR_PORT &= ~((1 << PWR_FPM) | (1 << PWR_SERVO));
}

int main(void)
{
	uint16_t id;

	/* disable watchdog timer */
	cli();
	wdt_reset();
	MCUSR &= ~(1 << WDRF);
	WDTCSR |= (1 << WDCE) | (1 << WDE);
	WDTCSR = 0x00;

	/* power on peripherals */
	PWR_DDR |= (1 << PWR_FPM) | (1 << PWR_SERVO);
	pwr_fpm_servo_on();

	/* UART for FPM */
	UBRR0H = UBRRH_VALUE;
	UBRR0L = UBRRL_VALUE;
#if USE_2X
	UCSR0A |= (1 << U2X0);
#else
	UCSR0A &= ~(1 << U2X0);
#endif
	UCSR0B = (1 << TXEN0) | (1 << RXEN0);
	UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);

	/* servo */
	TCCR1A |= (1 << WGM11);
	TCCR1B |= (1 << WGM12) | (1 << WGM13);
	TCCR1B |= (1 << CS11);
	ICR1 = PWM_TOP;
	TCCR1A |= (1 << COM1A1);
	SERVO_DDR |= (1 << SERVO_PIN);

	/* battery check */
	LED_DDR |= (1 << LED_PIN);
	LED_PORT &= ~(1 << LED_PIN);

	/* input ports */
	INPUT_DDR &= ~((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | 
	               (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | 
	               (1 << ENROLL_PIN));

	INPUT_PORT |= ((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | 
	               (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | 
	               (1 << ENROLL_PIN));

	EICRA = 0b00000000;
	EIMSK = (1 << FPM_UNLOCK_INT);

	PCICR |= (1 << PCIE2);
	PCMSK2 |= ((1 << FRONT_LOCK_INT) | (1 << ENROLL_INT) | 
	           (1 << BACK_LOCK_INT) | (1 << BACK_UNLOCK_INT));

	flash_led();

	for (;;) {
		switch(cmd) {
		case LOCK_FRONT:
			lock();
			fpm_led(FLASH, RED, 1);
			break;
		case LOCK_BACK:
			lock();
			break;
		case UNLOCK_FRONT:
			if (fpm_match()) {
				fpm_led(BREATHE, BLUE, 1);
				unlock();
			} else {
				fpm_led(BREATHE, RED, 1);
			}
			break;
		case UNLOCK_BACK:
			unlock();
			fpm_led(FLASH, BLUE, 1);
			break;
		case ENROLL:
			id = fpm_match();
			if (id == 1 || id == 2) {
				fpm_led(BREATHE, BLUE, 1);
				_delay_ms(1000);
				if (fpm_enroll())
					fpm_led(BREATHE, BLUE, 1);
				else
					fpm_led(BREATHE, RED, 1);
			}
			break;
		default:
			break;
		}

		cmd = NONE;
		_delay_ms(500);

		pwr_fpm_servo_off();
		set_sleep_mode(SLEEP_MODE_PWR_DOWN);
		sleep_enable();
		sleep_bod_disable();
		sei();
		sleep_cpu();
		cli();
		sleep_disable();
		pwr_fpm_servo_on();
	}

	return 0;
}

static inline int is_pressed(uint8_t btn)
{
	if (!((PIND >> btn) & 0x01)) {
		_delay_ms(50);
		return !((PIND >> btn) & 0x01);
	}
	return 0;
}

ISR(FPM_INT_VEC)
{
	cmd = UNLOCK_FRONT;
}

ISR(BTN_INT_VEC)
{
	cli();

	if (is_pressed(FRONT_LOCK_PIN))
		cmd = LOCK_FRONT;
	else if (is_pressed(BACK_LOCK_PIN))
		cmd = LOCK_BACK;
	else if (is_pressed(BACK_UNLOCK_PIN))
		cmd = UNLOCK_BACK;
	else if (is_pressed(ENROLL_PIN))
		cmd = ENROLL;

	sei();
}