Skip to content
Snippets Groups Projects
Commit 775e14c8 authored by 김주영's avatar 김주영
Browse files

Merge branch 'Kimjuyoung-master-patch-83124' into 'main'

Kimjuyoung master patch

See merge request !2
parents fbc5d6fb 6b56ac17
No related branches found
No related tags found
1 merge request!2Kimjuyoung master patch
Rarp2.cpp 0 → 100644
/*
* See documentation at https://nRF24.github.io/RF24
* See License information at root directory of this library
* Author: Brendan Doherty (2bndy5)
*/
/**
* A simple example of sending data from 1 nRF24L01 transceiver to another.
*
* This example was written to be used on 2 devices acting as "nodes".
* Use `ctrl+c` to quit at any time.
*/
#include <ctime> // time()
#include <iostream> // cin, cout, endl
#include <string> // string, getline()
#include <time.h> // CLOCK_MONOTONIC_RAW, timespec, clock_gettime()
#include <RF24/RF24.h> // RF24, RF24_PA_LOW, delay()
#include<stdio.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#define IN 0
#define OUT 1
#define LOW 0
#define HIGH 1
#define VALUE_MAX 40
#define BUFFER_MAX 3
#define DIRECTION_MAX_P 45
#define VALUE_MAX_P 256
#define PIN 20
#define POUT2 21
#define POUT1 18
#define PWM 0
// 서보모터의 최소 각도와 최대 각도
#define MIN_ANGLE 500000
#define MAX_ANGLE 2500000
using namespace std;
/****************** Linux ***********************/
// Radio CE Pin, CSN Pin, SPI Speed
// CE Pin uses GPIO number with BCM and SPIDEV drivers, other platforms use their own pin numbering
// CS Pin addresses the SPI bus number at /dev/spidev<a>.<b>
// ie: RF24 radio(<ce_pin>, <a>*10+<b>); spidev1.0 is 10, spidev1.1 is 11 etc..
#define CSN_PIN 0
#ifdef MRAA
#define CE_PIN 15 // GPIO22
#else
#define CE_PIN 22
#endif
// Generic:
RF24 radio(17, CSN_PIN);
/****************** Linux (BBB,x86,etc) ***********************/
// See http://nRF24.github.io/RF24/pages.html for more information on usage
// See http://iotdk.intel.com/docs/master/mraa/ for more information on MRAA
// See https://www.kernel.org/doc/Documentation/spi/spidev for more information on SPIDEV
// For this example, we'll be using a payload containing
// a single float number that will be incremented
// on every successful transmission
float payload = 0;
void setRole(); // prototype to set the node's role
void master(); // prototype of the TX node's behavior
void slave(); // prototype of the RX node's behavior
// custom defined timer for evaluating transmission time in microseconds
struct timespec startTimer, endTimer;
uint32_t getMicros(); // prototype to get elapsed time in microseconds
static int PWMExport(int pwmnum)
{
char buffer[BUFFER_MAX];
int bytes_written;
int fd;
fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in unexport!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
fd = open("/sys/class/pwm/pwmchip0/export", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in export!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
return(0);
}
static int
PWMUnexport(int pwmnum) {
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in unexport!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pwmnum);
write(fd, buffer, bytes_written);
close(fd);
sleep(1);
return(0);
}
static int
PWMEnable(int pwmnum) {
static const char s_unenable_str[] = "0";
static const char s_enable_str[] = "1";
char path[DIRECTION_MAX_P];
int fd;
snprintf(path, DIRECTION_MAX_P, "/sys/class/pwm/pwmchip0/pwm%d/enable", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in enable!\n");
return -1;
}
write(fd, s_unenable_str, strlen(s_unenable_str));
close(fd);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in enable!\n");
return -1;
}
write(fd, s_enable_str, strlen(s_enable_str));
close(fd);
return(0);
}
static int
PWMUnable(int pwmnum)
{
static const char s_unable_str[] = "0";
char path[DIRECTION_MAX_P];
int fd;
snprintf(path, DIRECTION_MAX_P, "/sys/class/pwm/pwmchip0/pwm%d/enable", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in enable!\n");
return -1;
}
write(fd, s_unable_str, strlen(s_unable_str));
close(fd);
return(0);
}
static int
PWMWritePeriod(int pwmnum, int value)
{
char s_values_str[VALUE_MAX_P];
char path[VALUE_MAX_P];
int fd, byte;
snprintf(path, VALUE_MAX_P, "/sys/class/pwm/pwmchip0/pwm%d/period", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in period!\n");
return(-1);
}
byte = snprintf(s_values_str, 10, "%d", value);
if (-1 == write(fd, s_values_str, byte)) {
fprintf(stderr, "Failed to write value in period!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int
PWMWriteDutyCycle(int pwmnum, int value)
{
char path[VALUE_MAX_P];
char s_values_str[VALUE_MAX_P];
int fd, byte;
snprintf(path, VALUE_MAX_P, "/sys/class/pwm/pwmchip0/pwm%d/duty_cycle", pwmnum);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open in duty_cycle!\n");
return(-1);
}
byte = snprintf(s_values_str, 10, "%d", value);
if (-1 == write(fd, s_values_str, byte)) {
fprintf(stderr, "Failed to write value! in duty_cycle\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIOExport(int pin) {
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export",O_WRONLY);
if(-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int GPIOUnexport(int pin) {
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport",O_WRONLY);
if(-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}
static int GPIORead(int pin) {
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if (-1 == read(fd, value_str, 3)) {
fprintf(stderr, "Failed to read value! [ Pin Number : %d]\n", pin);
close(fd);
return(-1);
}
close(fd);
return(atoi(value_str));
}
static int GPIOWrite(int pin, int value) {
static const char s_values_str[] = "01";
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
fprintf(stderr, "Failed to open gpio value for writing!\n");
return(-1);
}
if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
fprintf(stderr, "Failed to write value!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
static int GPIODirection(int pin, int dir) {
static const char s_directions_str[] = "in\0out";
#define DIRECTION_MAX 35
char path[DIRECTION_MAX] = "/sys/class/gpio/gpio%d/direction";
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path,O_WRONLY);
if(-1 == fd) {
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}
if(-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)){
fprintf(stderr, "Failed to set direction!!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}
void error_handling(char *message){
fputs(message,stderr);
fputc('\n', stderr);
exit(1);
}
int main(int argc, char** argv)
{
int angle = 500000;
int direction = 1; // 1: 증가, -1: 감소
int clnt_sock;
struct sockaddr_in serv_addr;
char msg[2];
char on[2]="1";
int str_len;
int light = 0;
int repeat = 100000000;
// 이전 상태와 현재 상태를 추적하기 위한 변수
int state = 1;
int prev_state = 1;
// perform hardware check
if (!radio.begin()) {
cout << "radio hardware is not responding!!" << endl;
return 0; // quit now
}
// to use different addresses on a pair of radios, we need a variable to
// uniquely identify which address this radio will use to transmit
bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
// print example's name
cout << argv[0] << endl;
// Let these addresses be used for the pair
uint8_t address[2][6] = {"1Node", "LIFT1"};
// It is very helpful to think of an address as a path instead of as
// an identifying device destination
// Set the radioNumber via the terminal on startup
cout << "Which radio is this? Enter '0' or '1'. Defaults to '0' ";
string input;
getline(cin, input);
radioNumber = input.length() > 0 && (uint8_t)input[0] == 49;
// save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
// For debugging info
// radio.printDetails(); // (smaller) function that prints raw register values
// radio.printPrettyDetails(); // (larger) function that prints human readable data
// ready to execute program now
if(argc!=3){
printf("Usage : %s <IP> <port>\n",argv[0]);
exit(1);
}
clnt_sock = socket(PF_INET, SOCK_STREAM, 0);
if(clnt_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(clnt_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1)
error_handling("connect() error");
if(-1 == GPIOExport(POUT2) || -1 == GPIOExport(PIN))
{
return(1);
}
if(-1 == GPIODirection(POUT2, OUT) || -1 == GPIODirection(PIN, IN))
{
return(2);
}
PWMExport(PWM);
PWMWritePeriod(PWM, 20000000);
PWMWriteDutyCycle(PWM, 0);
PWMEnable(PWM);
do {
if (-1 == GPIOWrite(POUT2, 1)) {
return 3;
}
str_len = read(clnt_sock, msg, sizeof(msg));
if(str_len == -1)
error_handling("read() error");
printf("Receive message from Server : %s\n",msg);
/*if(strncmp(on,msg,1)==0)
light = 1;
else
light = 0;
GPIOWrite(POUT, light);*/
int pinValue = GPIORead(PIN);
printf("GPIORead: %d from pin %d\n", pinValue, PIN);
printf("angle: %d\n", angle);
// 상태 변화 감지
if (pinValue != prev_state) {
state = pinValue;
}
// 문자열을 정수로 변환
float a = atoi(msg);
if(a == 1){
// GPIORead(PIN)의 값이 0일 때에만 서보모터 제어
if (state == 0) {
PWMWriteDutyCycle(PWM, angle);
printf("angle: %d\n", angle);
angle += 50000*direction; // 각도 변경 방향에 따라 각도 증감
usleep(100000);
// 최대 각도 또는 최소 각도에 도달하면 방향 변경
if (angle >= MAX_ANGLE || angle <= MIN_ANGLE) {
direction *= -1;
usleep(5000000);
//c=최대각도 혹은 최소각도에 도달했을 때 잠깐 멈추기
}
}
}else{
PWMWriteDutyCycle(PWM, angle);
printf("angle: %d\n", angle);
if (angle > MIN_ANGLE) {
printf("angle: %d\n", angle);
angle -= 50000*direction; //서보모터 각도 감소(사람 없다고 판단시 리프트 자동 내려감)
usleep(100000);
}
}
prev_state = pinValue;
radio.stopListening(); // put radio in TX mode
unsigned int failure = 0; // keep track of failures
payload = a;
while (failure < 2) {
clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer); // start the timer
bool report = radio.write(&payload, sizeof(float)); // transmit & save the report
uint32_t timerElapsed = getMicros(); // end the timer
if (report) {
// payload was delivered
cout << "Transmission successful! Time to transmit = ";
cout << timerElapsed; // print the timer result
cout << " us. Sent: " << payload << endl; // print payload sent
}
else {
// payload was not delivered
cout << "Transmission failed or timed out" << endl;
}
failure++;
// to make this example readable in the terminal
delay(1000); // slow transmissions down by 1 second
}
cout << failure << " failures detected. Leaving TX role." << endl;
}while(repeat--);
if( -1 == GPIOUnexport(POUT2) || -1 == GPIOUnexport(PIN))
{
return(4);
}
close(clnt_sock);
//Disable GPIO pins
/*if (-1 == GPIOUnexport(POUT))
return(4);*/
return 0;
}
/**
* Calculate the elapsed time in microseconds
*/
uint32_t getMicros()
{
// this function assumes that the timer was started using
// `clock_gettime(CLOCK_MONOTONIC_RAW, &startTimer);`
clock_gettime(CLOCK_MONOTONIC_RAW, &endTimer);
uint32_t seconds = endTimer.tv_sec - startTimer.tv_sec;
uint32_t useconds = (endTimer.tv_nsec - startTimer.tv_nsec) / 1000;
return ((seconds)*1000 + useconds) + 0.5;
}
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUFFER_MAX 3
#define DIRECTION_MAX 45
#define IN 0
#define OUT 1
#define PWM 0
#define LOW 0
#define HIGH 1
#define VALUE_MAX 256
#define POUT 23
#define PIN 24
#define PINM 17
#define PINP 26 //Press sensor's GPIO PIN number
int mot_person = 0;
int pre_person = 0;
double distance = 0;
int personCheck[3] = {0,0,0};
pthread_t thread1, thread2, thread3, thread4;
//소켓연결
int serv_sock, clnt_sock = -1;
struct sockaddr_in serv_addr, clnt_addr;
socklen_t clnt_addr_size;
char msg[2];
void error_handling(char *message){
fputs(message, stderr);
fputc('\n',stderr);
exit(1);
}
static int GPIOExport(int pin){
#define BUFFER_MAX 3
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY); //경로를 열어서
if(-1 == fd){
fprintf(stderr, "Failed to open export for writing!\n");
return(-1);
}// 잘 못 열면(경로가 잘못됐거나 값이 없거나 .. etc) => 오류 msg 출력
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin); //파라미터로 입력받은 pin 넘버 buffer에 넘겨서 bytes_written 변수에 저장
write(fd, buffer, bytes_written); //fd에 buffer값 넣어서 fd(/sys/class/gpio/export/)에 write => 결과 : /sys/class/gpio/gpio(PIN_number)가 생성됨
close(fd);
return(0);
} // 해당 함수는 /sys/class/gpio/export에 GPIO Pin Number을 Write하는 함수입니다
static int GPIOUnexport(int pin){
char buffer[BUFFER_MAX];
ssize_t bytes_written;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if(-1 == fd){
fprintf(stderr, "Failed to open unexport for writing!\n");
return(-1);
}
bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return(0);
}// 해당 함수는 /sys/class/gpio/unexport에 GPIO Pin Number을 Write하는 함수입니다. //logic은 GPIOexport함수와 같습니다.
static int GPIODirection(int pin, int dir){
static const char s_directions_str[] = "in\0out";
//char path[DIRECTION_MAX] = "/sys/class/gpio/gpio24/direction";
char path[DIRECTION_MAX] = "/sys/class/gpio/gpio%d/direction"; //경로 지정
int fd;
snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin); // %d에 pin 넘버 넘겨받음
fd = open(path, O_WRONLY);
if(-1 == fd){
fprintf(stderr, "Failed to open gpio direction for writing!\n");
return(-1);
}
if(-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)){ // fd에 IN 또는 OUT 값 입력
fprintf(stderr, "Failed to set direction!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}// 해당 함수는 /sys/class/gpio/gpio(pin번호)/direction에 OUT인지, IN인지 write하는 함수
static int GPIORead(int pin){
char path[VALUE_MAX];
char value_str[3];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if(-1 == fd){
fprintf(stderr, "Failed to open gpio value for reading!\n");
return(-1);
}
if(-1 == read(fd, value_str, 3)){
fprintf(stderr, "Failed to read value!\n");
close(fd);
return(-1);
}
close(fd);
return(atoi(value_str));
}// 해당 함수는 /sys/class/gpio/gpio(pin번호)/direction에 OUT인지, IN인지 write하는 함수
static int GPIOWrite(int pin, int value){
static const char s_values_str[] = "01";//0또는 1로 입력받음
char path[VALUE_MAX];
int fd;
snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin); //pin 번호를 입력받아서 path에 저장
fd = open(path, O_WRONLY); //fd에 입력받은 path값을 이용해 파일 엶.
if(-1 == fd){
fprintf(stderr, "Failed to open gpio value for writing!\n"); // 접근 오류 -> 메세지 출력
return(-1);
}
if(1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)){ //0또는 1 값 입력, 그런데 만약 실패하면 오류메세지 출력
fprintf(stderr, "Failed to write value!\n");
close(fd);
return(-1);
}
close(fd);
return(0);
}// 해당 함수는 /sys/class/gpio/gpio(pin번호)/value에 (0또는 1) 값을 쓰는 함수) //ex. value/0 => led가 켜짐
void* motion() {
int repeat = 1000;
/*clock_t start_t, end_t;
clock_t n_per_st, n_per_en;
int cnt=0;
double time;*/
int flag_detect=0;
if(-1 == GPIOExport(PINM)){
printf("gpio export err\n");
}
usleep(100000);
if(-1 == GPIODirection(PINM, IN)){
printf("gpio direction err\n");
exit(2);
}
//GPIOWrite(PINM, 0);
usleep(10000);
do{
/*if(-1 == GPIOWrite(PINM,1)){
printf("gpio write/trigger erro\n");
exit(3);
}
usleep(100);*/
/*GPIOWrite(PINM,0);*/
printf("GPIORead's number : %d\n", GPIORead(PINM));
if(GPIORead(PINM)==0){
flag_detect++;
printf("flag_detection: %d\n", flag_detect);
if(flag_detect==10){
printf("Don't exist person!\n");
mot_person=0;
personCheck[0]=mot_person;
printf("personCheck[0]=%d!\n", personCheck[0]);
}
}else{
flag_detect=0;
printf("!!Exist person!\n");
mot_person=1;
personCheck[0]=mot_person;
printf("personCheck[0]=%d!\n", personCheck[0]);
}
usleep(2000000);
}
while(repeat--);
if(-1 == GPIOUnexport(PINM))
exit(4);
printf("complete\n");
exit(0);
}
void* microwave() {
int repeat = 100;
clock_t start_t, end_t;
double wave_time;
int cnt;
int wav_person;
if(-1 == GPIOExport(POUT) || -1 == GPIOExport(PIN)){
printf("gpio export err\n");
}
usleep(10000); //10000 -> 1000
if(-1 == GPIODirection(POUT, OUT) || -1 == GPIODirection(PIN, IN)){
printf("gpio direction err\n");
exit(2);
}
GPIOWrite(POUT, 0);
usleep(1000);
do{
if(-1 == GPIOWrite(POUT,1)){
printf("gpio write/trigger erro\n");
exit(3);
}
usleep(10);
GPIOWrite(POUT,0);
while(GPIORead(PIN)==0){
start_t = clock();
}
while(GPIORead(PIN)==1){
end_t = clock();
}
wave_time = (double)(end_t-start_t)/CLOCKS_PER_SEC;
double speedOfSound = 34000.0; // cm/s
double distanceInCm = (wave_time * speedOfSound) /2;
//double distanceInMm = distanceInCm * 10;
printf("time : %.4lf\n", wave_time);
printf("distance : %.4lfcm\n", distanceInCm);
//printf("distance : %.4lfmm\n", distanceInMm);
if(distanceInCm <= 10){
cnt++;
printf("cnt:%d\n",cnt);
if(cnt==10){
printf("person exists\n");
wav_person=1; //person exists
personCheck[1]=wav_person;
printf("personCheck[1]=%d!\n", personCheck[1]);
}
}else{
cnt=0;
printf("person not exists!\n");
wav_person=0;
personCheck[1]=wav_person;
printf("personCheck[1]=%d!\n", personCheck[1]);
}
usleep(2000000); //2000000 -> 2000
}
while(repeat--);
if(-1 == GPIOUnexport(POUT)||-1==GPIOUnexport(PIN))
exit(4);
printf("complete\n");
exit(0);
}
void* press()
{
int repeat = 1000;
if(-1 == GPIOExport(PINP)){
printf("gpio export err\n");
}
usleep(100000);
if(-1 == GPIODirection(PINP, IN)){
printf("gpio direction err\n");
exit(2);
}
//GPIOWrite(PIN, 0);
usleep(10000);
do{
/*if(-1 == GPIOWrite(PIN,1)){
printf("gpio write/trigger erro\n");
return(3);
}
usleep(100);*/
/*GPIOWrite(PIN,0);*/
if(GPIORead(PINP)==0){
printf("Pressure is not Detected!\n");
pre_person=0;
personCheck[2]=pre_person;
printf("personCheck[2]=%d!\n", personCheck[2]);
}else{
printf("Presure is detected!\n");
pre_person=1;
personCheck[2]=pre_person;
printf("personCheck[2]=%d!\n", personCheck[2]);
}
usleep(2000000); //
}
while(repeat--);
if(-1 == GPIOUnexport(PINP))
exit(4);
printf("complete\n");
exit(0);
}
void* superviser(void * arg)
{
while(1) {
sleep(1);
// for ( int i = 0; i < 3; i++ ) {
// printf("personCheck[%d] = %d\n",i, personCheck[i]);
// }
int existValue = 0;
int count = 0;
for ( int a = 0; a < 3; a++ ) {
if ( personCheck[a] == 1 ) {
count++;
}
}
if ( count >=2 ) {
existValue = 1;
}
if ( existValue == 1 ) {
printf("supervisor detect exists\n");
snprintf(msg, 2, "%d", existValue);
write(clnt_sock, msg, sizeof(msg));
printf("msg = %s\n", msg);
}else{
existValue = 0;
printf("supervisor detect person not exists!\n");
snprintf(msg, 2, "%d", existValue);
write(clnt_sock, msg, sizeof(msg));
printf("msg = %s\n", msg);
}
}
}
int main(int argc, char *argv[]) {
pthread_create(&thread1, NULL, motion, NULL);
pthread_create(&thread2, NULL, microwave, NULL);
pthread_create(&thread3, NULL, press, NULL);
pthread_create(&thread4, NULL, superviser, NULL);
printf("success");
if(argc!=2){
printf("Usage: %s <port>\n", argv[0]);
}
serv_sock = socket(PF_INET, SOCK_STREAM,0);
if(serv_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=htonl(INADDR_ANY);
serv_addr.sin_port=htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))== -1){
error_handling("bind() error");
}
if(listen(serv_sock, 5) == -1) error_handling("listen() error");
if(clnt_sock < 0){
clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
if(clnt_sock == -1) error_handling("accept() error");
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment