-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzer.c
More file actions
67 lines (57 loc) · 1.94 KB
/
buzzer.c
File metadata and controls
67 lines (57 loc) · 1.94 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <wiringPi.h>
#include <softTone.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define SPKR 5
#define TOTAL_NOTE 32
int notes[] = {
391, 391, 440, 440, 391, 391, 330, 330,
391, 391, 330, 330, 294, 294, 294, 0,
391, 391, 440, 440, 391, 391, 330, 330,
391, 330, 294, 330, 262, 262, 262, 0
};
int runCommandWithSocket(const char *cmd, int client_fd) {
if (strcmp(cmd, "BUZZER PLAY") != 0) return -1;
softToneCreate(SPKR);
int flags = fcntl(client_fd, F_GETFL, 0);
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
int state = 1; // 1: playing, 0: paused
int current = 0;
char buf[2] = {0};
char menu[128];
snprintf(menu, sizeof(menu),
"===재생중===\n1. buzzer OFF\n2. 메뉴 화면으로\n");
write(client_fd, menu, strlen(menu));
while (current < TOTAL_NOTE) {
ssize_t len = read(client_fd, buf, 1);
if (len == 0) {
// 클라이언트가 연결을 끊음
break;
} else if (len > 0) {
if (buf[0] == '1') {
state = !state;
const char *resumed =
state ? "===재생중===\n1. buzzer OFF\n2. 메뉴 화면으로\n"
: "===일시정지됨===\n1. buzzer ON\n2. 메뉴 화면으로\n";
write(client_fd, resumed, strlen(resumed));
} else if (buf[0] == '2') {
softToneWrite(SPKR, 0);
write(client_fd, "BUZZER 종료\n", strlen("BUZZER 종료\n"));
return 0;
}
}
if (state == 1) {
softToneWrite(SPKR, notes[current]);
delay(280);
current++;
} else {
softToneWrite(SPKR, 0); // 일시정지 중에는 소리를 멈춘다
delay(100);
}
}
softToneWrite(SPKR, 0);
write(client_fd, "BUZZER 재생 완료\n", strlen("BUZZER 재생 완료\n"));
return 0;
}