-
Notifications
You must be signed in to change notification settings - Fork 0
/
geiger.c
82 lines (60 loc) · 1.21 KB
/
geiger.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
82
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <fcntl.h>
pthread_t geigerThread;
int geigerThreadID;
extern int geigerCount;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int count = 0;
int openGPIO(unsigned int gpio){
int fd, value;
char buf[64];
snprintf(buf, sizeof(buf), "echo %i >/sys/class/gpio/export", gpio);
system(buf);
snprintf(buf, sizeof(buf), "echo in > /sys/class/gpio/gpio%i/direction", gpio);
system(buf);
}
int GPIOValue(unsigned int gpio)
{
char buf[64];
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%i/value", gpio);
int fd = open(buf, O_RDONLY);
if (fd > 0){
char ch = 0;
read(fd, &ch, 1);
close(fd);
if (ch != '0') {
return 1;
} else {
return 0;
}
}else{
return -1;
}
}
void *geigerHandler(){
while(1){
if(GPIOValue(18) == 1){
pthread_mutex_lock( &mutex );
count++;
pthread_mutex_unlock( &mutex );
}
}
}
int readGeigerCount()
{
pthread_mutex_lock( &mutex );
geigerCount = count;
count = 0;
pthread_mutex_unlock( &mutex );
return 1;
}
int initialiseGeiger()
{
openGPIO(18);
if((geigerThreadID = pthread_create(&geigerThread, NULL, &geigerHandler, NULL))){
printf("failed\n");
}
return 1;
}