-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio.c
112 lines (91 loc) · 2.73 KB
/
gpio.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
/* gpiokey - GPIO Setup and Scanning
Copyright (c) 2019 by Stefan Vorkoetter
This file is part of gpiokey.
gpiokey is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
gpiokey 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 General Public License for more
details.
You should have received a copy of the GNU General Public License along with
gpiokey. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include <unistd.h>
#include "x11.h"
#define NUM_PINS 32
static bool initialized = false;
static struct pinMap {
bool neg;
KeyCode keyCode;
bool pressed;
bool wasIdle;
} pinMap[NUM_PINS];
static KeyCode idleKey = 0;
bool ConfigureIdleKey( const char *key, const char **err )
{
return( (idleKey = StringToKeyCode(key,err)) != 0 );
}
bool ConfigureInputPin( int pin, bool neg, int pud, const char *key,
const char **err )
{
if( pin < 0 || pin > 31 || *key == '\0' ) {
*err = "pin number out of range";
return( false );
}
if( !initialized ) {
for( int i = 0; i < NUM_PINS; ++i ) {
pinMap[i].neg = false;
pinMap[i].keyCode = 0;
pinMap[i].pressed = false;
pinMap[i].wasIdle = false;
}
initialized = true;
}
if( pinMap[pin].keyCode )
fprintf(stderr,"gpiokey: warning, redefining GPIO pin %d\n",pin);
pinMap[pin].keyCode = StringToKeyCode(key,err);
if( pinMap[pin].keyCode == 0 )
return( false );
pinMap[pin].neg = neg;
char cmd[100];
sprintf(cmd,"gpio export %d in",pin);
system(cmd);
sprintf(cmd,"gpio -g mode %d %s",pin,
pud == 0 ? "tri" : pud > 0 ? "up" : "down");
system(cmd);
return( true );
}
void ScanInputPins( bool screenOn )
{
for( int pin = 0; pin < NUM_PINS; ++pin ) {
if( pinMap[pin].keyCode ) {
char path[100];
sprintf(path,"/sys/class/gpio/gpio%d/value",pin);
int fd = open(path,O_RDONLY);
if( fd >= 0 ) {
char value;
if( read(fd,&value,1) == 1 ) {
bool pressed = (value == '1') ^ pinMap[pin].neg;
if( pressed != pinMap[pin].pressed ) {
if( pressed && !screenOn && idleKey
|| !pressed && pinMap[pin].wasIdle )
{
SendKeyCode(idleKey,pressed);
pinMap[pin].wasIdle = pressed;
}
else
SendKeyCode(pinMap[pin].keyCode,pressed);
pinMap[pin].pressed = pressed;
}
}
close(fd);
}
}
}
}