-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxio_thread.c
67 lines (56 loc) · 1.11 KB
/
xio_thread.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "chip_xio.h"
#define BUTTON_ONE XIO_P4
#define BUTTON_TWO XIO_P5
static int button_one_state;
void *button_one_press( void *arg )
{
int *pin;
pin = (int*)arg;
while( 1 )
{
if( button_one_state != 0 )
button_one_state = strcmp( get_pin_value( *pin ), "0" );
}
}
void main( void )
{
if( !chip_xio_start() )
{
return;
}
if( !export_pin( BUTTON_ONE ) )
{
return;
}
else if( !export_pin( BUTTON_TWO ) )
{
return;
}
set_pin_input( BUTTON_ONE );
set_pin_input( BUTTON_TWO );
pthread_t pth;
int button_two_state;
button_one_state = button_two_state = 1;
int pin = BUTTON_ONE;
pthread_create( &pth, NULL, button_one_press, &pin );
while( button_two_state != 0)
{
if( button_one_state == 0 )
{
printf("Button one was pressed and the thread caught it!\n");
button_one_state = 1;
}
else
{
printf("Waiting for button one key press!\n");
}
sleep(2);
button_two_state = strcmp( get_pin_value(BUTTON_TWO), "0" );
}
pthread_cancel( pth );
unexport_pin(BUTTON_ONE);
unexport_pin(BUTTON_TWO);
}