-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcds.c
More file actions
46 lines (36 loc) · 1.13 KB
/
cds.c
File metadata and controls
46 lines (36 loc) · 1.13 KB
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
#include <wiringPi.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define CDS 2
#define LED 0
int runCommandWithSocket(const char *cmd, int client_fd) {
if (strcmp(cmd, "CDS START") != 0) return -1;
pinMode(CDS, INPUT);
pullUpDnControl(CDS, PUD_DOWN);
pinMode(LED, OUTPUT);
int flags = fcntl(client_fd, F_GETFL, 0);
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
int prev = -1;
char sendbuf[64];
char recvbuf[2] = {0};
while (1) {
int current = digitalRead(CDS);
if (current != prev) {
digitalWrite(LED, current == LOW ? HIGH : LOW);
snprintf(sendbuf, sizeof(sendbuf), "CDS sensor %s -> LED %s\n종료하려면 q 입력\n",
current == LOW ? "LOW" : "HIGH",
current == LOW ? "ON" : "OFF");
write(client_fd, sendbuf, strlen(sendbuf));
prev = current;
}
if (read(client_fd, recvbuf, 1) > 0 && recvbuf[0] == 'q') {
write(client_fd, "CDS 종료\n", 11);
break;
}
delay(100);
}
digitalWrite(LED, LOW);
return 0;
}