-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeytest.c
More file actions
387 lines (342 loc) · 9.64 KB
/
keytest.c
File metadata and controls
387 lines (342 loc) · 9.64 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
MIT No Attribution
Copyright 2022 Thomas Dettbarn (dettus@dettus.net)
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Configuration starts here
//#define PATCHED_WIRING // comment in if you did the EXACT SAME wiring as I did ;)
//#define PINOUT_JETSONNANO // comment in if you connected the SH1106 directly to a Jetson Nano
//#define PINOUT_BANANAPI // comment in if you connected the SH1106 directly to a Banana Pi Zero
//#define PINOUT_RASPBERRYPI // comment in if you connected the SH1106 directly to a Raspberry Pi (Zero)
// Configuration ends here
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define RETVAL_OK 0
#define RETVAL_NOK -1
#define GPIO_INPUT 0
#define GPIO_OUTPUT 1
#define MAXBUFLEN 64
#define DELAY_US(us) usleep((us));
#define DELAY_MS(ms) usleep((ms)*1000);
#define SPI_MODE0 0
#define SPI_MODE1 1
#define SPI_MODE2 2
#define SPI_MODE3 3
#define SPI_LSBFIRST 0
#define SPI_MSBFIRST 1
#define SPI_DELAY //DELAY_US(1)
#define BITMAP_HEIGHT 64
#define BITMAP_WIDTH 128
//// depends on the development board
// the way I understand this is, that the physical pins are mapped into
// the system memory. to accces those, there is some sort of memory
// offset maybe
#if defined(PINOUT_JETSONNANO)
#warning "This mapping is for the Jetson Nano board."
// Jetson Nano
// source: https://maker.pro/nvidia-jetson/tutorial/how-to-use-gpio-pins-on-jetson-nano-developer-kit
const short physicalmapping[41]={
0, // placeholder. there is no PIN0
-1, -1,// PIN1 =3.3V DC, PIN2 =5V DC
-1, -1,// 3,4
-1, -1,// 5,6
216, -1, //PIN7 = AUDIO_MCLK
-1, -1, //9,10
50, 79, //PIN11=UART_2_RTS,PIN12=I2S_4_CLK
14, -1, //13,14
194,232, //15,16
-1, 15, //17,18
16, -1, //19,20
17, 13, //21,22
18, 19, //23,24
-1, 20, //25,26
-1, -1, //27,28
149, -1, //29,30
200,168, //31,32
38, -1, //33,34
76, 51, //35,36
12, 77, //37,38
-1, 78 //39,40
};
#elif defined(PINOUT_BANANAPI)
#warning "This mapping is for the Banana Pi board."
// source: https://wiki.fw-web.de/doku.php?id=en:bpi-r64:gpio
const short physicalmapping[41]={
0, // placeholder. there is no PIN0
-1, -1,// PIN1 =3.3V DC, PIN2 =5V DC
56, -1,// 3,4
55, -1,// 5,6
101, 59, //PIN7 = PWM7,8
-1, 60,// 9,10
51, -1, //PIN11=UART_2_RTS,PIN12=I2S_4_CLK
52, -1, //13,14
98, 61, //15,16
-1, 62, //17,18
68, -1, //19,20
69, 63, //21,22
67, 70, //23,24
-1, 66, //25,26
58, 57, //27,28
85, -1, //29,30
82, 64, //31,32
-1, -1, //33,34
-1, 65, //35,36
86, -1, //37,38
-1, -1 //39,40
};
#elif defined(PINOUT_RASPBERRYPI)
#warning "This mapping is for the Raspberry Pi (Zero) board"
// source: https://www.theengineeringprojects.com/wp-content/uploads/2021/03/raspberry-pi-zero-5.png
// NOTE: GPIO16 was actually missing in the diagram.
const short physicalmapping[41]={
0, // placeholder. there is no PIN0
-1, -1,// PIN1 =3.3V DC, PIN2 =5V DC
2, -1,// 3,4
3, -1,// 5,6
4, 14, //PIN7 = PWM7,8
-1, 15,// 9,10
17, 18, //PIN11=UART_2_RTS,PIN12=I2S_4_CLK
27, -1, //13,14
22, 23, //15,16
-1, 24, //17,18
10, -1, //19,20
9, 25, //21,22
11, 8, //23,24
-1, 7, //25,26
0, 1, //27,28
5, -1, //29,30
6, 12, //31,32
13, -1, //33,34
19, 16, //35,36
26, 20, //37,38
-1, 21 //39,40
};
#else
#error "The physical pins need to be mapped to the GPIO numbers. For some reason, those numbers are different for each board out there. So please have a look at the sourcecode, lines 21-26, and select the one that works best for you."
const int physicalmapping[41]={0};
#endif
// this is the GPIO pinout for the waveshare OLED 1.3 SH1106 hat
// source: https://www.waveshare.com/1.3inch-oled-hat.htm
#ifdef PATCHED_WIRING
// Apparently, not every Key was mapped to a working GPIO pin.
// So I did some rewiring. This is a setup that works
#define PIN_LEFT (physicalmapping[ 3]) // OLED pin 29
#define PIN_UP (physicalmapping[ 5]) // OLED pin 31
#define PIN_FIRE (physicalmapping[ 7]) // OLED pin 33
#define PIN_DOWN (physicalmapping[29]) // OLED pin 35
#define PIN_RIGHT (physicalmapping[31]) // OLED pin 37
#define PIN_KEY1 (physicalmapping[27]) // OLED pin 36
#define PIN_KEY2 (physicalmapping[24]) // OLED pin 38
#define PIN_KEY3 (physicalmapping[26]) // OLED pin 40
#define PIN_BL (physicalmapping[16]) // OLED pin 12
#define PIN_DC (physicalmapping[18])
#define PIN_RST (physicalmapping[22])
#define PIN_CS (physicalmapping[28]) // OLED pin 24
#define PIN_MOSI (physicalmapping[19])
#define PIN_MISO (physicalmapping[21])
#define PIN_SCLK (physicalmapping[23])
#else
#define PIN_LEFT (physicalmapping[29])
#define PIN_UP (physicalmapping[31])
#define PIN_FIRE (physicalmapping[33])
#define PIN_DOWN (physicalmapping[35])
#define PIN_RIGHT (physicalmapping[37])
#define PIN_KEY1 (physicalmapping[36])
#define PIN_KEY2 (physicalmapping[38])
#define PIN_KEY3 (physicalmapping[40])
#define PIN_BL (physicalmapping[12])
#define PIN_DC (physicalmapping[18])
#define PIN_RST (physicalmapping[22])
#define PIN_CS (physicalmapping[24])
#define PIN_MOSI (physicalmapping[19])
#define PIN_MISO (physicalmapping[21])
#define PIN_SCLK (physicalmapping[23])
#endif
int gpio_export(int pin)
{
int fd;
char buffer[MAXBUFLEN];
int len;
fd=open("/sys/class/gpio/export", O_WRONLY);
if (fd<0)
{
fprintf(stderr,"GPIO export for pin %d failed\n",pin);
return RETVAL_NOK;
}
len=snprintf(buffer,MAXBUFLEN,"%d",pin);
write(fd,buffer,len);
close(fd);
return RETVAL_OK;
}
int gpio_unexport(int pin)
{
int fd;
char buffer[MAXBUFLEN];
int len;
fd=open("/sys/class/gpio/unexport", O_WRONLY);
if (fd<0)
{
fprintf(stderr,"GPIO unexport for pin %d failed\n",pin);
return RETVAL_NOK;
}
len=snprintf(buffer,MAXBUFLEN,"%d",pin);
write(fd,buffer,len);
close(fd);
return RETVAL_OK;
}
int gpio_direction(int pin,int direction)
{
int fd;
char buffer[MAXBUFLEN];
snprintf(buffer,MAXBUFLEN,"/sys/class/gpio/gpio%d/direction",pin);
fd=open(buffer,O_WRONLY);
if (fd<0)
{
fprintf(stderr,"GPIO direction for pin %d cannot be set\n",pin);
return RETVAL_NOK;
}
if (direction==GPIO_INPUT)
{
write(fd,"in",2);
} else if (direction==GPIO_OUTPUT) {
write(fd,"out",3);
} else {
fprintf(stderr,"Type error for direction for pin %d (%d unknown)\n",pin,direction);
}
close(fd);
return RETVAL_OK;
}
int gpio_write(int pin,int value)
{
int fd;
char buffer[MAXBUFLEN];
snprintf(buffer,MAXBUFLEN,"/sys/class/gpio/gpio%d/value",pin);
fd=open(buffer,O_WRONLY);
if (fd<0)
{
fprintf(stderr,"Cannot access GPIO pin %d\n",pin);
return RETVAL_NOK;
}
write(fd,value?"1":"0",1);
close(fd);
return RETVAL_OK;
}
int gpio_read(int pin,int* value)
{
int fd;
char buffer[MAXBUFLEN];
int len;
snprintf(buffer,MAXBUFLEN,"/sys/class/gpio/gpio%d/value",pin);
fd=open(buffer,O_RDONLY);
if (fd<0)
{
fprintf(stderr,"Cannot access GPIO pin %d\n",pin);
return RETVAL_NOK;
}
len=read(fd,buffer,3);
if (len<0)
{
fprintf(stderr,"Unable to read from pin %d\n",pin);
close(fd);
return RETVAL_NOK;
}
*value=atoi(buffer);
close(fd);
return RETVAL_OK;
}
int gpio_pins_up()
{
int retval;
retval=RETVAL_OK;
// start with the GPIO configuration
retval|=gpio_export(PIN_LEFT);
retval|=gpio_export(PIN_UP);
retval|=gpio_export(PIN_FIRE);
retval|=gpio_export(PIN_DOWN);
retval|=gpio_export(PIN_RIGHT);
retval|=gpio_export(PIN_KEY1);
retval|=gpio_export(PIN_KEY2);
retval|=gpio_export(PIN_KEY3);
retval|=gpio_direction(PIN_LEFT, GPIO_INPUT);
retval|=gpio_direction(PIN_UP, GPIO_INPUT);
retval|=gpio_direction(PIN_FIRE, GPIO_INPUT);
retval|=gpio_direction(PIN_DOWN, GPIO_INPUT);
retval|=gpio_direction(PIN_RIGHT, GPIO_INPUT);
retval|=gpio_direction(PIN_KEY1, GPIO_INPUT);
retval|=gpio_direction(PIN_KEY2, GPIO_INPUT);
retval|=gpio_direction(PIN_KEY3, GPIO_INPUT);
return retval;
}
int gpio_pins_down()
{
int retval;
retval=RETVAL_OK;
retval|=gpio_unexport(PIN_LEFT);
retval|=gpio_unexport(PIN_UP);
retval|=gpio_unexport(PIN_FIRE);
retval|=gpio_unexport(PIN_DOWN);
retval|=gpio_unexport(PIN_RIGHT);
retval|=gpio_unexport(PIN_KEY1);
retval|=gpio_unexport(PIN_KEY2);
retval|=gpio_unexport(PIN_KEY3);
return retval;
}
int sh1106_up()
{
int retval;
retval=RETVAL_OK;
retval|=gpio_pins_up();
return retval;
}
int sh1106_down()
{
return gpio_pins_down();
}
void graceFulExit(int signal_number)
{
printf("shutting down ...\n");
sh1106_down();
exit(0);
}
int main(int argc,char** argv)
{
char *names[8]={"LEFT","UP","FIRE","DOWN","RIGHT","KEY1","KEY2","KEY3"};
int pins[8]={PIN_LEFT,PIN_UP,PIN_FIRE,PIN_DOWN,PIN_RIGHT,PIN_KEY1,PIN_KEY2,PIN_KEY3};
int lastval[8]={0};
signal(SIGINT, graceFulExit);
if (sh1106_up())
{
fprintf(stderr,"unable to start up pins. sorry.\n");
return 1;
}
while (1)
{
int i;
for (i=0;i<8;i++)
{
int val;
gpio_read(pins[i],&val);
if (val!=lastval[i])
{
printf("%8s %d->%d\n",names[i],lastval[i],val);
}
lastval[i]=val;
}
}
graceFulExit(0);
return 0;
}