-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.c
More file actions
73 lines (56 loc) · 1.35 KB
/
scan.c
File metadata and controls
73 lines (56 loc) · 1.35 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<wiringPi.h>
struct timeval sharedTimeval;
double current() {
gettimeofday(&sharedTimeval, NULL);
return sharedTimeval.tv_sec * 1e6 + sharedTimeval.tv_usec;
}
int main(int argc, char **argv) {
// wiring pi setup
if (wiringPiSetupGpio() < 0) {
fprintf(stderr, "Failed to setup wiring pi\n");
return 1;
}
// obtain read pin number
int pin = 20;
if (argc > 1) {
pin = atoi(argv[1]);
}
fprintf(stderr, "GPIO Pin Number : %d\n", pin);
// obtain wait interval (us)
double wait = 4e4;
if (argc > 2) {
wait = atoi(argv[2]);
}
// set gpio pin mode
pinMode(pin, INPUT);
double buffer[1024];
double currentTime;
int currentState, lastState;
int offset = 0;
lastState = digitalRead(pin);
fprintf(stderr, "Scanning begin\n");
while(1) {
currentTime = current();
if (offset && currentTime > buffer[offset - 1] + wait) {
break;
}
currentState = digitalRead(pin);
if (currentState != lastState) {
buffer[offset++] = currentTime;
lastState = currentState;
}
}
fprintf(stderr, "Scanning end\n");
//buffer[offset] = buffer[offset - 1];
//offset++;
fprintf(stderr, "Output begin\n");
int i;
for (i = 1; i < offset; i++) {
printf("%.0lf%s", buffer[i] - buffer[i - 1], (i & 1) ? "\t" : "\n");
}
fprintf(stderr, "Output end\n");
return 0;
}