forked from kieranc/power
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio-test.c
79 lines (68 loc) · 2.26 KB
/
gpio-test.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
/*
* This file was adapted and simplified from the example isr.c
* distributed with wiringPi by Gordon Henderson
*
* It waits for an interrupt on GPIO 1 and prints 'Interrupt' to stdout
* It is used with a python script to monitor pulses from a power meter
* and report the usage to EmonCMS
*
* See here: http://github.com/kieranc/power/
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
#include <wiringPi.h>
#include <unistd.h>
#include <time.h>
//timestamp vars
time_t unixtime;
void myInterrupt() {
printf ("Interrupt\n") ;
fflush (stdout) ;
FILE * fptr;
fptr = fopen("/var/log/gpio-test.log", "a");
time(&unixtime);
fprintf(fptr,"%s: Interrupted\n", asctime(localtime(&unixtime)));
fclose(fptr);
}
/*
*********************************************************************************
* main
*********************************************************************************
*/
int main (void)
{
FILE * fptr2;
fptr2 = fopen("/var/log/gpio-test.log", "a");
time(&unixtime);
fprintf(fptr2,"%s: New Run!\n", asctime(localtime(&unixtime)));
fclose(fptr2);
wiringPiSetup () ;
wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt) ;
for (;;)
{
sleep(UINT_MAX);
}
return 0;
}