From ff42c7923d556a83946af35db6c96d0de996950a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=A8=EB=8F=84=ED=98=81?= <mortglacia@ajou.ac.kr> Date: Wed, 18 Dec 2024 09:20:02 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20sysfs=EB=B0=A9=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B6=80=EC=A0=80=20=EC=A0=9C=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flame.c | 149 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 80 insertions(+), 69 deletions(-) diff --git a/flame.c b/flame.c index 4ecdffe..fba2c35 100644 --- a/flame.c +++ b/flame.c @@ -6,8 +6,6 @@ #include <arpa/inet.h> #include <fcntl.h> #include <time.h> -#include <wiringPi.h> -#include <softTone.h> #include <signal.h> #define BUFFER_MAX 3 @@ -20,63 +18,75 @@ #define HIGH 1 #define BUFFER_SIZE 1024 -#define POUT 23 // 초음파 트리거 핀 -#define PIN 24 // 초음파 에코 핀 +#define POUT 23 // 초음파 트리거 핀 +#define PIN 24 // 초음파 에코 핀 #define FLAME 21 // 불꽃 센서 핀 -#define GREEN 17 //초록색 LED 핀 -#define RED 22 //적색 LED 핀 -#define BUZZER 27 //부저 핀 +#define GREEN 17 // 초록색 LED 핀 +#define RED 22 // 적색 LED 핀 +#define BUZZER 27 // 부저 핀 int fireAlarmNotes[] = { 880, 880, 0, 880, 880, 0, 988, 988, 0, 880, 880, 0 }; struct sockaddr_in server_addr; int park = 0; // 주차 상태: 0 = 주차되지 않음, 1 = 주차됨 -int setFire = 1; // 화재 알림 상태: 1 = 화재 가능, 0 = 이미 화재 알림 보냄 -int sock; // 서버 소켓 -int redLed = 2; // 적색 LED의 초기 설정값 +int setFire = 1; +int sock; +int redLed = 2; int CLEAR = 1; + pthread_t ultra_thread, flame_thread, receive_thread; -// GPIO 제어 함수 선언 int GPIOExport(int pin); int GPIOUnexport(int pin); int GPIODirection(int pin, int dir); int GPIORead(int pin); int GPIOWrite(int pin, int value); -// 인터럽트 void signal_handler(int no); -// 초음파 센서 측정 스레드 +// PWM 기반 부저 소리 발생 함수 +void buzzerTone(int pin, int frequency, int duration) { + int period = 1000000 / frequency; // 주기 (마이크로초) + int halfPeriod = period / 2; + + for (int i = 0; i < (duration * 1000 / period); i++) { + GPIOWrite(pin, HIGH); + usleep(halfPeriod); + GPIOWrite(pin, LOW); + usleep(halfPeriod); + } +} + void *ultrasonic_sensor(void *arg) { clock_t start_t, end_t; double time; while (1) { - GPIOWrite(POUT, HIGH); // 초음파 트리거 핀 HIGH - usleep(10); // 10 마이크로초 신호 유지 - GPIOWrite(POUT, LOW); // 초음파 트리거 핀 LOW - - // 에코 핀 신호 대기 - while (GPIORead(PIN) == LOW) start_t = clock(); - while (GPIORead(PIN) == HIGH) end_t = clock(); - - time = (double)(end_t - start_t) / CLOCKS_PER_SEC; // 시간 계산 - double distance = time / 2 * 34000; // cm 단위 거리 계산 - - if(redLed == 2){ - GPIOWrite(RED, HIGH); - redLed = 1; + GPIOWrite(POUT, HIGH); + usleep(10); + GPIOWrite(POUT, LOW); + + while (GPIORead(PIN) == LOW) + start_t = clock(); + while (GPIORead(PIN) == HIGH) + end_t = clock(); + + time = (double)(end_t - start_t) / CLOCKS_PER_SEC; + double distance = time / 2 * 34000; + + if (redLed == 2) { + GPIOWrite(RED, HIGH); + redLed = 1; } - if (park == 0 && distance <= 10 && redLed == 1) { // 10cm 이하 -> 주차 + if (park == 0 && distance <= 10 && redLed == 1) { GPIOWrite(GREEN, HIGH); GPIOWrite(RED, LOW); park = 1; redLed = 0; printf("0-PARK\n"); send(sock, "0-PARK", strlen("0-PARK"), 0); - } else if (park == 1 && distance > 10 && redLed == 0) { // 10cm 초과 -> 주차 해제 + } else if (park == 1 && distance > 10 && redLed == 0) { GPIOWrite(GREEN, LOW); GPIOWrite(RED, HIGH); park = 0; @@ -85,47 +95,47 @@ void *ultrasonic_sensor(void *arg) { send(sock, "0-EXIT", strlen("0-EXIT"), 0); } - usleep(500000); // 0.5초 주기 + usleep(500000); } return NULL; } -// 불꽃 센서 감지 스레드 void *flame_sensor(void *arg) { - - printf("초기 FLAME 값: %d\n", GPIORead(FLAME)); - - wiringPiSetupGpio(); - softToneCreate(BUZZER); + printf("초기 FLAME 값은 %d\n", GPIORead(FLAME)); while (1) { - if (GPIORead(FLAME) == 0 && setFire == 1) { // 불꽃 감지 + if (GPIORead(FLAME) == 0 && setFire == 1) { printf("0-FIRE\n"); send(sock, "0-FIRE", strlen("0-FIRE"), 0); - setFire = 0; // 더 이상 화재 신호 전송하지 않음 + setFire = 0; CLEAR = 0; + printf("FLAME 값: %d\n", GPIORead(FLAME)); } - while(1){ - if (CLEAR == 0){ - for(int i = 0; i < sizeof(fireAlarmNotes)/sizeof(int); i++){ - softToneWrite(BUZZER, fireAlarmNotes[i]); - delay(200); - } - } - - else{ - softToneWrite(BUZZER,0); - break; - } + + while (CLEAR == 0) { + for (int i = 0; i < sizeof(fireAlarmNotes) / sizeof(int); i++) { + if (CLEAR != 0) { + break; + } + if (fireAlarmNotes[i] > 0) { + buzzerTone(BUZZER, fireAlarmNotes[i], 200); + } else { + usleep(200000); + } + } + if (CLEAR != 0) { + break; + } } - usleep(200000); // 0.2초 주기 + + GPIOWrite(BUZZER, LOW); + usleep(200000); } return NULL; } -// 서버 메시지 수신 스레드 void *receive_message(void *arg) { char buffer[BUFFER_SIZE]; int valread; @@ -140,29 +150,28 @@ void *receive_message(void *arg) { sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Socket creation error"); - sleep(5); // 5초 대기 후 재시도 + sleep(5); continue; } if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { perror("Connection failed. 재연결 시도 중..."); close(sock); - sleep(5); // 5초 대기 후 재시도 + sleep(5); continue; } printf("서버에 재연결되었습니다.\n"); - break; // 재연결 성공 시 루프 탈출 + break; } } else { - buffer[valread] = '\0'; // 수신된 메시지 null-terminate + buffer[valread] = '\0'; printf("서버로부터 받은 메시지: %s\n", buffer); - buffer[strcspn(buffer, "\r\n")] = '\0'; - if (strcmp(buffer, "0-CLEAR") == 0) { // 화재 해제 명령 - setFire = 1; // 화재 감지 준비 상태로 전환 - CLEAR = 1; // 0-CLEAR을 받았음 - printf("setFire가 1로 설정되었습니다.\n"); + if (strcmp(buffer, "CLEAR") == 0) { + setFire = 1; + CLEAR = 1; + printf("setFire 및 CLEAR가 %d, %d로 설정되었습니다.\n", setFire, CLEAR); } } } @@ -229,16 +238,18 @@ int main() { GPIOUnexport(BUZZER); return 0; } - // ctrl+c로 인해 프로세스 종료시 잔류 신호 차단 -void signal_handler(int no){ - printf("System Interrupt.\n"); - GPIOWrite(GREEN, LOW); - GPIOWrite(RED, LOW); - softToneWrite(BUZZER,0); - pinMode(BUZZER, INPUT); // 부저 입력모드 전환으로 인한 잔류 신호 차단 - exit(0); + + +void signal_handler(int no) { + printf("System Interrupt.\n"); + GPIOWrite(GREEN, LOW); + GPIOWrite(RED, LOW); + GPIOWrite(BUZZER, LOW); + close(sock); + exit(0); } + int GPIOExport(int pin) { char buffer[BUFFER_MAX]; ssize_t bytes_written; -- GitLab