diff --git a/pir.c b/pir.c new file mode 100644 index 0000000000000000000000000000000000000000..b1dd1d600f93875df2a93cc171b4d2b7a08cb775 --- /dev/null +++ b/pir.c @@ -0,0 +1,201 @@ +#include <arpa/inet.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include <pthread.h> +#include <dirent.h> +#include <errno.h> +#include <linux/spi/spidev.h> +#include <linux/types.h> +#include <stdint.h> + + +#define BUFFER_MAX 3 +#define DIRECTION_MAX 256 +#define VALUE_MAX 256 +#define BUFFER_SIZE 1024 + +#define IN 0 +#define OUT 1 +#define PWM 0 + +#define LOW 0 +#define HIGH 1 + +#define DISTANCE 50 + +#define MOTION_IN 0 //gpio17 + +volatile int eventCounter = 0; +unsigned char humandetect = 0; +int counter = 0; + +static const char *DEVICE = "/dev/spidev0.0"; +static uint8_t MODE = 0; +static uint8_t BITS = 8; +static uint32_t CLOCK = 1000000; +static uint16_t DELAY = 5; + + +void error_handling(char *message) { + fputs(message, stderr); + fputc('\n', stderr); + exit(1); +} + +static int prepare(int fd) { + if (ioctl(fd, SPI_IOC_WR_MODE, &MODE) == -1) { + perror("Can't set MODE"); + return -1; + } + + if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &BITS) == -1) { + perror("Can't set number of BITS"); + return -1; + } + + if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &CLOCK) == -1) { + perror("Can't set write CLOCK"); + return -1; + } + + if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &CLOCK) == -1) { + perror("Can't set read CLOCK"); + return -1; + } + + return 0; +} + +uint8_t control_bits_differential(uint8_t channel) { + return (channel & 7) << 4; +} + +uint8_t control_bits(uint8_t channel) { + return 0x8 | control_bits_differential(channel); +} + +int readadc(int fd, uint8_t channel) { + uint8_t tx[] = {1, control_bits(channel), 0}; + uint8_t rx[3]; + + struct spi_ioc_transfer tr = { + .tx_buf = (unsigned long)tx, + .rx_buf = (unsigned long)rx, + .len = ARRAY_SIZE(tx), + .delay_usecs = DELAY, + .speed_hz = CLOCK, + .bits_per_word = BITS, + }; + + if (ioctl(fd, SPI_IOC_MESSAGE(1), &tr) == 1) { + perror("IO Error"); + abort(); + } + + return ((rx[1] << 8) & 0x300) | (rx[2] & 0xFF); +} + +void myInterrupt(void) { + eventCounter++; + humandetect = 1; +} + +void* thread_pressure(void* arg){ + int sock = *((int*)arg); + int fd = open(DEVICE, O_RDWR); + if (fd <= 0) { + perror("Device open error"); + return -1; + } + if (prepare(fd) == -1) { + perror("Device prepare error"); + return -1; + } + + int v = readadc(fd,0); + char pressure[3] = "END" + while (1) { + + if (v>100){ + write(sock,pressure,strlen(pressure)); + } + printf("value: %d\n", readadc(fd, 0)); + usleep(10000); + } + +} + +void* thread_pir(void* arg){ + int sock = *((int*)arg); + char pir[3] = "NO"; + while ( 1 ) { + if(humandetect == 1) + { + printf("Detect %d\n", eventCounter ); + humandetect = 0; + while(digitalRead(MOTION_IN)) + { + printf("high %d \n", counter++); + delay(1000); + } + counter = 0; + } + else + { + printf(" No detect\n"); + write(sock,pir,strlen(pir)); + } + //eventCounter = 0; + delay( 500 ); // wait 1 second + } + +} + +int main(int argc, char *argv[]) { + int sock; + struct sockaddr_in serv_addr; + char on[2] = "1"; + int str_len; + int light = 0; + int status; + int thr_id; + + if (argc != 3) { + printf("Usage : %s <IP> <port>\n", argv[0]); + exit(1); + } + + sock = socket(PF_INET, SOCK_STREAM, 0); + if (sock == -1) error_handling("socket() error"); + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr(argv[1]); + serv_addr.sin_port = htons(atoi(argv[2])); + + if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1)//서버와 연결 + error_handling("connect() error"); + + pthread_t tid[2]; + thr_id = pthread_create(&tid[0], NULL, thread_pressure, (void *)&sock);//멀티스레드로 입력 함수 실행 + if (thr_id < 0) { + perror("thread create error : "); + exit(0); + } + thr_id = pthread_create(&tid[1], NULL, thread_pir, (void *)&sock);//멀티스레드로 출력 함수 실행 + if (thr_id < 0) { + perror("thread create error : "); + exit(0); + } + pthread_join(tid[0], (void **)&status); + pthread_join(tid[1], (void **)&status);//스레드가 종료하여 결합할 때까지 대기 + printf("[*] session closed\n"); + close(sock);//소켓종료 + return (0); +}