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
|
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "functions.h"
static int fd_gpio;
static unsigned long old_state, new_state;
sigset_t saved_mask;
sigset_t sigio_mask;
gboolean signal_return=false;
static void sigio_handler_gpio(int sig, siginfo_t *info, void *context);
static void evaluate_gpio( void );
int gpio_taster_init( void ){
fd_gpio = open(GPIO_DEVICE, O_RDONLY);
if (fd_gpio < 0) {
fprintf(stderr, "failed to open %s: %s\n",
GPIO_DEVICE, strerror(errno));
return 1;
}
struct sigaction sigio_action = {
.sa_sigaction = sigio_handler_gpio,
.sa_flags = SA_SIGINFO,
};
int flags;
// Don't mask any signals when handling SIGIO (except
// SIGIO itself, which is masked by default.)
sigemptyset(&sigio_action.sa_mask);
if (sigaction(SIGIO, &sigio_action, NULL)) {
perror("failed to install SIGIO handler");
return 1;
}
// Tell the kernel that our process will take care of SIGIO
if (fcntl(fd_gpio, F_SETOWN, getpid())) {
perror("failed to set fd owner");
return 1;
}
// Enable asynchronous I/O
flags = fcntl(fd_gpio, F_GETFL);
if (fcntl(fd_gpio, F_SETFL, flags | O_ASYNC) < 0) {
perror("failed to set O_ASYNC");
return 1;
}
if (read(fd_gpio, &old_state, sizeof(old_state)) < 0) {
perror("failed to read initial pin state");
return 1;
}
new_state = old_state;
sigemptyset(&sigio_mask);
sigaddset(&sigio_mask, SIGIO);
return 0;
}
// This function will be called to evaluate pressed button
static void evaluate_gpio(){
if ( new_state != old_state && new_state < old_state ) {
if( ((new_state&(1<<BUTTON1_PIN))>>BUTTON1_PIN)==0 )
g_signal_emit_by_name( button_hoch_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON2_PIN))>>BUTTON2_PIN)==0 )
g_signal_emit_by_name( button_help_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON3_PIN))>>BUTTON3_PIN)==0 )
g_signal_emit_by_name( button4_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON4_PIN))>>BUTTON4_PIN)==0 )
g_signal_emit_by_name( button2_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON5_PIN))>>BUTTON5_PIN)==0 )
g_signal_emit_by_name( button_runter_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON6_PIN))>>BUTTON6_PIN)==0 )
g_signal_emit_by_name( button6_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON7_PIN))>>BUTTON7_PIN)==0 )
g_signal_emit_by_name( button8_event,"button_press_event", NULL, &signal_return);
else if( ((new_state&(1<<BUTTON8_PIN))>>BUTTON8_PIN)==0 )
g_signal_emit_by_name( button_info_event,"button_press_event", NULL, &signal_return);
printf("Signal send\n");
}
old_state = new_state;
usleep(10000);
}
// This handler will be called whenever, the pin state changes.
static void sigio_handler_gpio(int sig, siginfo_t *info, void *context){
unsigned long pin_state;
//sigprocmask(SIG_BLOCK, &sigio_mask, &saved_mask);
// This should never fail as we
// shouldn't be here if no new data is available.
if (read(fd_gpio, &pin_state, sizeof(pin_state)) < 0)
perror("read error in sigio_handler_gpio");
else
new_state = pin_state;
evaluate_gpio();
//sigprocmask(SIG_UNBLOCK, &sigio_mask, &saved_mask);
} |