-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.c
489 lines (380 loc) · 10.9 KB
/
lock.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <signal.h>
#include <softPwm.h>
#include <pthread.h>
#include <sys/wait.h>
#include <spawn.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
extern char **environ;
//// servo ////
#define SERVO 19
#define BUTTON 21
//// servo ////
//// uart - bluetooth ////
#define BAUD_RATE 115200
#define BUZZER 26
#define BUF_SIZE 5
static const char* UART2_DEV = "/dev/ttyAMA1";
unsigned char buf[BUF_SIZE];
int fd_serial_bt;
//// uart - bluetooth ////
/// uart - rasp ///
#define R_BAUD_RATE 115200
#define R_BUF_SIZE 100
static const char* UART3_DEV = "/dev/ttyAMA2";
unsigned char r_buf[R_BUF_SIZE];
int fd_serial_rasp;
/// uart - rasp ///
//// fnd ////
#define GPIO_COUNT 12 //GPIO 포트 개수
#define DIGIT_COUNT 4 //FND 4개
#define SEG_COUNT 8 //7seg + dot
#define FONT_COUNT 20 //미리 정의된 폰트 개수
#define PRINT_TIME 2000 //출력 시간
//FND에 순서대로(1~12) 연결된 GPIO 포트들
const int gpio[GPIO_COUNT] = { 17, 27, 22, 10, 9, 11, 12, 7, 8, 25, 24, 23 };
//GPIO ports for the digit 0~3 pins(BCM)
int digits[DIGIT_COUNT];
//GPIO ports for the 7seg pins(BCM)
int segments[SEG_COUNT];
const int fndFont[FONT_COUNT][SEG_COUNT] = {
{1, 1, 1, 1, 1, 1, 0, 0}, //0
{0, 1, 1, 0, 0, 0, 0, 0}, //1
{1, 1, 0, 1, 1, 0, 1, 0}, //2
{1, 1, 1, 1, 0, 0, 1, 0}, //3
{0, 1, 1, 0, 0, 1, 1, 0}, //4
{1, 0, 1, 1, 0, 1, 1, 0}, //5
{1, 0, 1, 1, 1, 1, 1, 0}, //6
{1, 1, 1, 0, 0, 1, 0, 0}, //7
{1, 1, 1, 1, 1, 1, 1, 0}, //8
{1, 1, 1, 1, 0, 1, 1, 0}, //9
//
{1, 0, 0, 1, 1, 1, 0, 0}, //C //10
{0, 1, 1, 1, 1, 0, 1, 0}, //D //11
{1, 0, 0, 1, 1, 1, 1, 0}, //E //12
{0, 0, 0, 1, 1, 1, 0, 0}, //L //13
{0, 0, 1, 0, 1, 0, 1, 0}, //N //14
{1, 1, 1, 1, 1, 1, 0, 0}, //O //15
{1, 1, 0, 0, 1, 1, 1, 0}, //P //16
{1, 0, 1, 1, 0, 1, 1, 0}, //S //17
{0, 0, 0, 0, 1, 0, 1, 0}, //R //18
{0, 0, 0, 0, 0, 0, 0, 0}, //공백 //19
};
int str_clsd[DIGIT_COUNT] = { 10, 13, 17, 11,};
//// fnd ////
//// servo ////
void ServoInit() {
pinMode(SERVO, OUTPUT);
softPwmCreate(SERVO, 0, 200);
pinMode(BUTTON, INPUT);
pinMode(BUZZER, OUTPUT);
}
void ServoLock() {
printf("servo lock\n");
softPwmWrite(SERVO, 15);
delay(100);
}
void ServoUnlock() {
printf("servo unlock\n");
softPwmWrite(SERVO, 24);
//digitalWrite(BUZZER, HIGH);
delay(100);
}
//// servo ////
char* serialReadRasp(const int fd) {
// unsigned char buf[10];
char c;
size_t idx = 0;
while (idx < sizeof(r_buf) - 1) {
if (serialDataAvail(fd) > 0) {
c = serialGetchar(fd);
printf("serialReadRaspChar: %c\n", c);
if (c == '\0') {
break; // 문자열의 끝을 나타내는 NULL 문자를 받으면 종료
} else {
r_buf[idx++] = c;
}
}
}
serialFlush(fd);
r_buf[idx] = '\0'; // 문자열의 끝에 NULL 문자 추가
printf("serialReadRaspString: %s\n", r_buf);
fflush(stdout);
return r_buf;
}
unsigned char serialRead(const int fd) {
unsigned char x;
if(read(fd, &x, 1) != 1) {
return -1;
}
printf("serialRead: %c\n", x);
return x;
}
//// bluetooth ////
char* serialReadString(const int fd) {
//unsigned char buf[10];
char c;
size_t idx = 0;
while (idx < sizeof(buf) - 1) {
if (serialDataAvail(fd) > 0) {
c = serialGetchar(fd);
printf("serialRead: %c\n", c);
if (c == '\0') {
break; // 문자열의 끝을 나타내는 NULL 문자를 받으면 종료
} else {
buf[idx++] = c;
}
}
}
serialFlush(fd);
buf[idx] = '\0'; // 문자열의 끝에 NULL 문자 추가
printf("serialReadstring: %s\n", buf);
fflush(stdout);
return buf;
}
void serialWrite(const int fd, const unsigned char c) {
write(fd, &c, 1);
}
void serialWriteBytes(const int fd, const char *s) {
write(fd, s, strlen(s));
}
//// bluetooth ////
//// fnd ////
void ClearPinMap() {
printf("\ngood-bye\n");
for (int b = 0; b < SEG_COUNT; b++) {
digitalWrite(segments[b], LOW);
}
for (int b = 0; b < DIGIT_COUNT; b++) {
digitalWrite(digits[b], LOW);
}
pthread_exit(NULL);
exit(0);
}
void FndSelect(int position) {
for (int i = 0; i < DIGIT_COUNT; i++) {
if (i == position) {
digitalWrite(digits[i], LOW); //LOW가 on
}
else {
digitalWrite(digits[i], HIGH); //HIGH가 off
}
}
}
void FndPositionPrint(int str, int position) {
for(int j = 0; j < SEG_COUNT; j++) {
digitalWrite(segments[j], fndFont[str][j]);
}
FndSelect(position);
}
void FndFlush() {
for (int b = 0; b < SEG_COUNT; b++) {
digitalWrite(segments[b], LOW);
}
for (int b = 0; b < DIGIT_COUNT; b++) {
digitalWrite(digits[b], LOW);
}
}
void FndPrint(int *strArr) {
unsigned int time = millis();
while(1) {
for(int i = 0; i < DIGIT_COUNT; i++) {
FndPositionPrint(strArr[i], i);
delay(1);
}
//n초 동안 출력
if(millis() - time > PRINT_TIME) {
time = millis();
break;
}
}
FndFlush();
}
void FndInit() {
int gpioDigits[DIGIT_COUNT] = {11, 8, 7, 5};
int segmentDigits[SEG_COUNT] = {10, 6, 3, 1, 0, 9, 4, 2};
for (int i = 0; i < DIGIT_COUNT; i++) {
digits[i] = gpio[gpioDigits[i]];
}
for (int i = 0; i < SEG_COUNT; i++) {
segments[i] = gpio[segmentDigits[i]];
}
if(wiringPiSetupGpio()==-1){
printf("Wiringpi Error!");
exit(-1);
}
for (int i = 0; i < SEG_COUNT; i++)
pinMode(segments[i], OUTPUT);
for(int i = 0; i < DIGIT_COUNT; i++) {
pinMode(digits[i], OUTPUT);
digitalWrite(digits[i], HIGH); //LOW가 on
}
}
//// fnd ////
struct msg_buffer {
long msg_type;
int msg_int;
};
void *ThreadBuzzer(void *arg) {
printf("[Thread] buzzer thread start!\n");
while (1) {
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
struct msg_buffer message;
msgrcv(msgid, &message, sizeof(message), 1, 0);
printf("수신된 메시지: %d\n", message.msg_int);
msgctl(msgid, IPC_RMID, NULL);
if (message.msg_int == 1) {
printf("buzzer on\n");
digitalWrite(BUZZER, HIGH);
delay(1000);
digitalWrite(BUZZER, LOW);
}
else {
printf("buzzer off\n");
digitalWrite(BUZZER, LOW);
}
}
}
void *ThreadButton(void *arg) {
signal(SIGINT, ClearPinMap);
printf("[Thread] button thread start!\n");
while (1) {
if (digitalRead(BUTTON) == HIGH) {
printf("[Thread] button pressed!\n");
ServoLock();
serialWriteBytes(fd_serial_bt, "Locked!!\n");
// status = 0;
FndPrint(str_clsd);
serialWrite(fd_serial_rasp, 'c');
//message queue
key_t key;
int msgid;
key = ftok("progfile", 65);
msgid = msgget(key, 0666 | IPC_CREAT);
struct msg_buffer message;
message.msg_type = 1;
message.msg_int = 1;
// 메시지 큐에 메시지 전송
msgsnd(msgid, &message, sizeof(message), 0);
printf("메시지 전송 완료\n");
}
}
}
// mq - button, buzzer
// uart - rasp //
void *ThreadRasp(void *arg) {
printf("[Thread] raspberry pi uart start!\n");
char *buf;
char buf_c;
while (1) {
// rasp 읽기
//buf = serialReadRasp(fd_serial_rasp);
while(serialDataAvail(fd_serial_rasp)) {
buf_c = serialRead(fd_serial_rasp);
printf("[Thread] c: %c\n", buf_c);
if (buf_c == 'a') {
serialWriteBytes(fd_serial_bt, "Detected!!\n");
}
fflush(stdout);
}
delay(500);
}
}
// uart - rasp //
int main(void) {
pthread_t thread;
pthread_t raspThread;
//int fd_serial_bt;
char *buf;
int str_open[DIGIT_COUNT] = { 15, 16, 12, 14,};
int str_clsd[DIGIT_COUNT] = { 10, 13, 17, 11,};
int str_err[DIGIT_COUNT] = { 12, 18, 18, 19,};
char *password = "1234";
FndInit();
ServoInit();
signal(SIGINT, ClearPinMap);
ServoUnlock();
FndPrint(str_open);
ServoLock();
FndPrint(str_clsd);
if(wiringPiSetup() < 0)
return 1;
if(wiringPiSetupGpio() < 0)
return 1;
if((fd_serial_bt = serialOpen(UART2_DEV, BAUD_RATE)) < 0) {
printf("Unable to open serial device (BT).\n");
return 1;
}
if((fd_serial_rasp = serialOpen(UART3_DEV, R_BAUD_RATE)) < 0) {
printf("Unable to open serial device (RASP).\n");
return 1;
}
//button thread 생성
if(pthread_create(&thread, NULL, ThreadButton, NULL) < 0) {
printf("thread create error!\n");
exit(0);
}
//rasp uart thread 생성
if(pthread_create(&raspThread, NULL, ThreadRasp, NULL) < 0) {
printf("thread create error\n");
exit(0);
}
if(pthread_create(&thread, NULL, ThreadBuzzer, NULL) < 0) {
printf("thread create error!\n");
exit(0);
}
while(1) {
int status;
pid_t pid, r_pid;
char *argv[] = {"nfc-poll", NULL};
printf("nfc read start!\n");
posix_spawn(&pid, "/bin/nfc-poll", NULL, NULL, argv, environ);
if ((r_pid = waitpid(pid, &status, 0)) < 0) {
perror("waitpid() error\n");
}
printf("bluetooth read start!\n");
buf = serialReadString(fd_serial_bt);
printf("main: %s\n", buf);
fflush(stdout);
//읽은 값 모두 출력
for(int i = 0; i < BUF_SIZE; i++) {
if(buf[i] == '\0') {
break;
}
else if((buf[i] != '\0')) {
printf("buf[%d]: %c\n", i, buf[i]);
FndPositionPrint(buf[i] - '0', i);
delay(500);
fflush(stdout);
}
else {
break;
}
}
FndFlush();
if(strcmp(buf, password) == 0) {
printf("password correct!\n");
serialWriteBytes(fd_serial_bt, "\npassword correct!\n");
serialWrite(fd_serial_rasp, 'o');
ServoUnlock();
serialWriteBytes(fd_serial_bt, "Unlocked!!\n");
FndPrint(str_open);
}
else {
printf("password incorrect!\n");
serialWriteBytes(fd_serial_bt, "\npassword incorrect!\n");
FndPrint(str_err);
}
delay(10);
}
}