forked from kieranc/power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio-pigtest.c
156 lines (132 loc) · 3.6 KB
/
gpio-pigtest.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
*
* refactor using pigpio
*
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <pigpio.h>
#include <unistd.h>
#include <time.h>
int max_timeout = 15000; //15 sec max interrupt timeout
uint32_t previous_tick = 0; //used for preventing early retriggers
uint32_t pulse_duty_cycle = 30000; //trigger threshold (30ms, avg length of good pulse)
uint32_t min_interval;
_Bool logging = 0; //set this to 1 to log to file
/*
*********************************************************************************
* getTimeStamp (updates a byref buffer to a desired dateformat)
*********************************************************************************
*/
void getTimeStamp(char* buff, size_t buffersize) {
time_t t = time(NULL);
strftime(buff, buffersize, "%x %X %Z", localtime(&t));
}
/*
*********************************************************************************
* myInterrupt (triggered every interval once main loop proceeding)
*********************************************************************************
*/
void myInterrupt(int gpio, int level, uint32_t tick) {
FILE * fptr;
uint32_t interval_seen = tick - previous_tick;
if (interval_seen < min_interval)
// if (((tick < previous_tick + min_interval) && (previous_tick + min_interval <= 4294967295) && previous_tick > 0) || ((previous_tick + min_interval > 4294967295) && (tick < previous_tick + min_interval - 4294967296)))
{
level = 99;
}
previous_tick = tick;
if (logging)
{
// open up the file (if we're logging)
fptr = fopen("/var/log/gpio-pigtest.log", "a");
}
//prepare the timestamp (used if we're logging)
char tstampbuf[80];
getTimeStamp(tstampbuf, sizeof tstampbuf);
switch(level)
{
case 0:
case 1:
min_interval = pulse_duty_cycle;
printf ("Interrupt\n");
fflush (stdout);
if (logging)
{
fprintf(fptr,"%s: (Tick: %u) Interrupted (Gap: %i usec)\n", tstampbuf, tick, interval_seen);
}
break;
case 2:
min_interval = 0;
if (logging)
{
fprintf(fptr,"%s: (Tick: %u) Timeout (Gap: %i usec)\n", tstampbuf, tick, interval_seen);
}
break;
case 99:
min_interval = pulse_duty_cycle - interval_seen; //bit buggy if there are multiple false-triggers
if (logging)
{
fprintf(fptr,"%s: (Tick: %u) False Retrigger Skipped (Gap: %i usec)\n", tstampbuf, tick, interval_seen);
}
break;
default:
min_interval = 0;
if (logging)
{
fprintf(fptr,"%s: (Tick: %u) Bad Trigger (Gap: %i usec)\n", tstampbuf, tick, interval_seen);
}
}
if (logging)
{
fclose(fptr);
}
}
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
FILE * fptr2;
min_interval = pulse_duty_cycle; //start off with min_interval equal to a normal pulse duty cycle
char tstampbuf[80];
getTimeStamp(tstampbuf, sizeof tstampbuf);
puts(tstampbuf);
if (logging)
{
fptr2 = fopen("/var/log/gpio-pigtest.log", "a");
fprintf(fptr2,"%s: New Run!\n", tstampbuf);
}
if (gpioInitialise() < 0)
{
// pigpio initialisation failed.
if (logging)
{
fprintf(fptr2,"%s: Pigpio initialisation failed; Quitting!\n", tstampbuf);
fclose(fptr2);
return 0;
}
}
else
{
// pigpio initialised okay.
if (logging)
{
fprintf(fptr2,"%s: Pigpio initialisation OK\n", tstampbuf);
fclose(fptr2);
}
gpioSetISRFunc(18, FALLING_EDGE, max_timeout, myInterrupt);
}
for (;;)
{
sleep(UINT_MAX);
}
gpioTerminate();
return 0;
}