-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
152 lines (133 loc) · 3.28 KB
/
main.c
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
#include "swipe_read.h"
#define TIMEOUT 2
#define IDLE 2
#define GRNT 3
#define DENY 4
#define MONY 5
void write_pins(int i, int g, int d, int m);
void GPIO_setup();
void set_lights(char mode);
void digital_write(int pin, int mode);
int swipe_loop();
/*
* main: creates a card object, and infinitely loops through, reading data
* to the card object, and then parsing that object.
*/
int main(int argc, char *argv[]) {
/* set up GPIO */
GPIO_setup();
system("amixer cset numid=1 -- 400");
/* get most recent files before running */
system("clear");
printf("Please wait\n");
printf("Retrieving most recent files from GitHub...\n");
fflush(stdout);
system("git pull");
sleep(TIMEOUT);
/* clear screen and begin loop */
system("clear");
return swipe_loop();
}
/*
* A loop which constantly gets the swipe data and executes commands
* according to what the swiped card should do.
*/
int swipe_loop() {
char *buffer = malloc(BUFFER_LEN + 1);
Card *card = malloc(sizeof(Card));
/* check for malloc failure */
if (!card) {
fprintf(stderr, "Error: memory allocation failed\n");
return -1;
}
/* infinite loop of reading, parsing, and checking */
for (;;) {
get_swipe_data(buffer);
if (!strcmp(buffer, "exit")) {
printf("Exit command received.\n");
free(card);
free(buffer);
printf("Copying log to rock (hitsuji down for maintenance)\n");
system("scp history.log [email protected]:~/Documents/");
return 0;
} else {
reset_card(card);
generate_card(card, buffer);
print_card(card);
switch (card->type) {
case ID:
if (check_whitelist(card)) {
printf("\n\nAccess granted.\n");
play_sound("success.mp3");
set_lights('g');
} else {
printf("\n\nAccess denied.\n");
play_sound("failure.mp3");
set_lights('d');
}
break;
case CREDIT:
printf("\n\nWe thank you for your donation.\n");
play_sound("donation.mp3");
set_lights('m');
break;
case UNKNOWN:
printf("\n\nSorry, card not recognized.\n");
play_sound("unrecognized.mp3");
set_lights('d');
break;
}
write_log(card);
}
/* wait a bit so we can see any output to console */
sleep(TIMEOUT);
set_lights('i');
system("clear");
}
return 0;
}
void GPIO_setup() {
char *buffer = malloc(BUFFER_LEN);
sprintf(buffer, "gpio mode %d OUTPUT", IDLE);
system(buffer);
sprintf(buffer, "gpio mode %d OUTPUT", GRNT);
system(buffer);
sprintf(buffer, "gpio mode %d OUTPUT", DENY);
system(buffer);
sprintf(buffer, "gpio mode %d OUTPUT", MONY);
system(buffer); free(buffer);
/* set lights to idle mode */
write_pins(1, 0, 0, 0);
}
void set_lights(char mode) {
switch (mode) {
case 'i':
write_pins(1, 0, 0, 0);
break;
case 'g':
write_pins(0, 1, 0, 0);
break;
case 'd':
write_pins(0, 0, 1, 0);
break;
case 'm':
write_pins(0, 0, 0, 1);
break;
}
}
void write_pins(int i, int g, int d, int m) {
if (i == 1) digital_write(IDLE, i);
if (g == 1) digital_write(GRNT, g);
if (d == 1) digital_write(DENY, d);
if (m == 1) digital_write(MONY, m);
digital_write(IDLE, i);
digital_write(GRNT, g);
digital_write(DENY, d);
digital_write(MONY, m);
return;
}
void digital_write(int pin, int mode) {
char *buffer = malloc(BUFFER_LEN);
sprintf(buffer, "gpio write %d %d", pin, mode);
system(buffer); free(buffer);
}