114 lines
2.9 KiB
C
Executable File
114 lines
2.9 KiB
C
Executable File
/* Demonstration C GPIO interrupt handling routine for Raspberry Pi
|
|
|
|
This is a modified code found at https://github.com/phil-lavin/raspberry-pi-gpio-interrupt
|
|
|
|
The program displays a notice whenever you:
|
|
-turn on the Raspberry Pi's pin 11 (apply 3.3V),
|
|
-turn the pin off.
|
|
|
|
This routine uses wiringPi library (follow the installation instructions at wiringpi.com),
|
|
and should be compiled with a command:
|
|
|
|
gcc source_filename -o executable_filename -lwiringPi
|
|
|
|
You must have root privileges to run it - I don't know any workaround yet:
|
|
|
|
sudo ./executable_filename
|
|
|
|
Then the program displays a notice whenever you turn the GPIO pin 11 on and off.
|
|
It runs as an infinite loop and you can cancel it by pressing ctrl-C.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <sys/time.h>
|
|
#include <wiringPi.h>
|
|
|
|
// Which GPIO pin we're using. For this program we'll use physical pin numbers.
|
|
#define PIN 27
|
|
|
|
// How much time a change must be since the last in order to count as a change
|
|
// (in microseconds); allows to avoid generating interrupts on contact bouncing etc.
|
|
#define IGNORE_CHANGE_BELOW_USEC 10000
|
|
|
|
// Current state of the pin
|
|
static volatile int state;
|
|
|
|
// Time of last change
|
|
struct timeval last_change;
|
|
|
|
|
|
// Handler for interrupt
|
|
void handle(void) {
|
|
struct timeval now;
|
|
static __uint64_t cnt;
|
|
unsigned long diff;
|
|
//gettimeofday(&now, NULL);
|
|
state = digitalRead(PIN);
|
|
printf("-->%d %d\n", ++cnt, state);
|
|
return;
|
|
|
|
// Time difference in microseconds
|
|
diff = (now.tv_sec * 1000000 + now.tv_usec) - (last_change.tv_sec * 1000000 + last_change.tv_usec);
|
|
|
|
// Filter any changes in intervals shorter than diff (like contact bouncing etc.):
|
|
/*
|
|
if (diff > IGNORE_CHANGE_BELOW_USEC) {
|
|
|
|
// Check whether the last state was on or off:
|
|
if (state) {
|
|
// Print info to console:
|
|
printf("Input goes off\n");
|
|
// You can add some code to do when input goes off here...
|
|
}
|
|
else {
|
|
// Print info to console:
|
|
printf("Input goes on\n");
|
|
// Add code to do when input goes on here...
|
|
}
|
|
|
|
// Change the "state" variable value:
|
|
state = !state;
|
|
}
|
|
*/
|
|
// Store the time for last state change:
|
|
last_change = now;
|
|
}
|
|
|
|
int main(void) {
|
|
// Init -- use the physical pin number on RPi P1 connector
|
|
if (wiringPiSetup() == -1)
|
|
{
|
|
printf("ошибка wiringPiSetup()\n");
|
|
exit(1);
|
|
}
|
|
|
|
// Set pin to input in case it's not
|
|
pinMode(PIN, INPUT);
|
|
pullUpDnControl(PIN, PUD_UP);
|
|
|
|
// Time now
|
|
gettimeofday(&last_change, NULL);
|
|
|
|
// Bind to interrupt
|
|
wiringPiISR(PIN, INT_EDGE_RISING, &handle);
|
|
|
|
// Get initial state of pin
|
|
state = digitalRead(PIN);
|
|
|
|
// Feedback for user that the program has started, depending on input state:
|
|
if (state) {
|
|
printf("Started! Initial state is on\n");
|
|
}
|
|
else {
|
|
printf("Started! Initial state is off\n");
|
|
}
|
|
|
|
// Waste time but not CPU
|
|
for (;;) {
|
|
sleep(1);
|
|
state = digitalRead(PIN);
|
|
}
|
|
}
|