-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoll_.c
81 lines (64 loc) · 1.29 KB
/
poll_.c
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <poll.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <wiringPi.h>
#include <stdlib.h>
#include <string.h>
volatile int flag=0;
char cstr[100];
int iret,ret,p[2];
void *thread_1(void *ptr);
char str[] = "start";
char str1[] = "end";
struct pollfd fds[1];
int main(){
fds->fd=p[0];
fds->events=POLLIN;
if (pipe(p)==-1)
{
printf("Pipe Failed" );
return 1;
}
if(wiringPiSetup()==-1){
exit(0);}
pinMode(15,OUTPUT);
char *led1="15";
pthread_t t1;
iret = pthread_create( &t1, NULL, thread_1,(void*)led1);
if(iret<0){
printf("Error starting thread 1");}
char c;
while(1){
printf("enter\n");
scanf("%c",&c);
switch(c){
case '1': write(p[1], str, strlen(str)+1);
break;
case '2':write(p[1], str1, strlen(str1)+1);
pthread_join( t1, NULL);
printf("Thread stopped");
delay(5000);
exit(0);
break;
default:break;
} }
return 0;}
void *thread_1( void *ptr)
{
int pin=atoi(ptr);
while(1){
ret=poll(fds, 1, 0);
if(fds->revents && POLLIN){
read(p[0], cstr, 6);
}
if(strncmp(cstr,str,6)==0){flag=1;}
else if (strncmp(cstr,str1,3)==0){pthread_exit(NULL);}
if(flag==1){
digitalWrite(pin,1);
delay(100);
digitalWrite(pin,0);
delay(100);
}
}
}