summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 3ee8fc62e3a86c47739077cd311ef323b53bd050 (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
/*
 * example_usage.c
 *
 */ 

#define bit_clr(a,b) ((a) &=~(1<<(b)))
#define bit_set(a,b) ((a) |= (1<<(b)))
#define bit_tst(a,b) ((a) & (1<<(b)))
#define bit_change(a,b) ((a) ^= (1<<(b)))

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define Soft_UART_TX_PORT PORTD
#define Soft_UART_TX_DDR DDRD
#define Soft_UART_TX_PIN 3
#define Soft_UART_Baud 9600
#include "Soft_UART_Timer1.h"

#include "fpm.h"

static inline void uart_write(const char *s)
{
	for (; *s; s++) {
		Soft_UART_send_byte(*s);
	}
	Soft_UART_send_byte('\r');
	Soft_UART_send_byte('\n');
}

static inline void print_config(void)
{
	const int SLEN = 25;

	char s[SLEN];
	struct fpm_cfg cfg;

	if (fpm_getcfg(&cfg)) {
		uart_write("FPM config:");
		snprintf(s, SLEN, "\tstatus: 0x%02X", cfg.status);
		uart_write(s);
		snprintf(s, SLEN, "\tsysid: 0x%02X", cfg.sysid);
		uart_write(s);
		snprintf(s, SLEN, "\tcap: %d", cfg.cap);
		uart_write(s);
		snprintf(s, SLEN, "\tsec: %d", cfg.sec_level);
		uart_write(s);
		snprintf(s, SLEN, "\taddr: 0x%02X%02X%02X%02X", cfg.addr[0], 
			cfg.addr[1], cfg.addr[2], cfg.addr[3]);
		uart_write(s);
		snprintf(s, SLEN, "\tpkt size: %d", cfg.pkt_size);
		uart_write(s);
	
		if (cfg.baud == 1)
			uart_write("\tbaud: 9600");
		else if (cfg.baud == 2)
			uart_write("\tbaud: 19200");
		else if (cfg.baud == 3)
			uart_write("\tbaud: 28800");
		else if (cfg.baud == 4)
			uart_write("\tbaud: 38400");
		else if (cfg.baud == 5)
			uart_write("\tbaud: 48000");
		else if (cfg.baud == 6)
			uart_write("\tbaud: 57600");
		else if (cfg.baud == 7)
			uart_write("\tbaud: 67200");
		else if (cfg.baud == 8)
			uart_write("\tbaud: 76800");
		else if (cfg.baud == 9)
			uart_write("\tbaud: 86400");
		else if (cfg.baud == 10)
			uart_write("\tbaud: 96000");
		else if (cfg.baud == 11)
			uart_write("\tbaud: 105600");
		else if (cfg.baud == 12)
			uart_write("\tbaud: 115200");
	} else {
		uart_write("Valid FPM config not found");
	}
}

int main(void)
{
	char s[30];
	uint8_t ismatch;
	uint16_t template_count;

	cli();
	Soft_UART_init();
	bit_set(DDRB,5);
	sei();

	if (fpm_init()) {
		print_config();
		template_count = fpm_getcount();
		snprintf(s, 30, "Template count: %d", template_count); 
		uart_write(s);

		if (template_count == 0) {
			uart_write("Enroll fingerprint");
			if (fpm_enroll()) {
				uart_write("Enrolled fingerprint"); 
			} else {
				uart_write("Enrollment error");
			}

			template_count = fpm_getcount();
			snprintf(s, 30, "New template count: %d", template_count); 
			uart_write(s);
		}

		uart_write("Authentcating...");
		if (fpm_match())
			uart_write("Fingerprint match");
		else
			uart_write("No match");
	}

    while (1) 
    {	
		_delay_ms(1000);
    }

	return 0;
}