Initial working version

master
Fabian Felix Selbach 2 years ago
commit 45a4873cc4
  1. 37
      Makefile
  2. 6
      compile_flags.txt
  3. 127
      kbdrv-main.c
  4. 120
      keyboard_mapping.h

@ -0,0 +1,37 @@
TARGET=kb-drv
ifeq (,$(TOOLCHAIN_PREFIX))
$(error TOOLCHAIN_PREFIX is not set)
endif
ifeq (,$(CFLAGS))
$(error CFLAGS is not set)
endif
ifeq (,$(LDFLAGS))
$(error LDFLAGS is not set)
endif
CC = $(TOOLCHAIN_PREFIX)gcc
CFLAGS += -I$(SYSROOT)/usr/include
LDFLAGS += -L$(SYSROOT)/lib
LDFLAGS += -L$(SYSROOT)/usr/lib
LDFLAGS += -lwiringx
SOURCE = $(wildcard *.c)
OBJS = $(patsubst %.c,%.o,$(SOURCE))
$(TARGET): $(OBJS)
$(CC) -o $@ $(OBJS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -include stdbool.h -o $@ -c $<
.PHONY: clean
clean:
@rm *.o -rf
@rm $(OBJS) -rf
@rm $(TARGET)

@ -0,0 +1,6 @@
-D_LARGEFILE_SOURCE
-D_LARGEFILE64_SOURCE
-D_FILE_OFFSET_BITS=64
-I/home/fabiclawz/duo-examples/duo-sdk/rootfs/usr/include
-include
stdbool.h

@ -0,0 +1,127 @@
#include <pthread.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>
#include "./keyboard_mapping.h"
#include <wiringx.h>
/*int columns[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
* int rows[] = {15, 16, 17, 18, 19, 20, 21, 22, 26, 27};
* unsigned char pressed_scancodes[6];
* char kmod = 0;
*/
bool state[sizeof(columns)/sizeof(int)][sizeof(rows)/sizeof(int)];
#define BUF_LEN 512
int main() {
if(wiringXSetup("duo", NULL) == -1){
wiringXGC();
return -1;
}
for(int i = 0; i < sizeof(columns)/sizeof(int); i++){
if(wiringXValidGPIO(columns[i]) != 0){
printf("Invalid GPIO %d\n", columns[i]);
return -1;
}
pinMode(columns[i], PINMODE_OUTPUT);
digitalWrite(columns[i], HIGH);
}
for(int i = 0; i < sizeof(rows)/sizeof(int); i++){
if(wiringXValidGPIO(rows[i]) != 0){
printf("Invalid GPIO %d\n", rows[i]);
return -1;
}
// digitalWrite(rows[i], LOW);
pinMode(rows[i], PINMODE_INPUT);
}
if(wiringXValidGPIO(num_lock) != 0){
printf("Invalid GPIO %d\n", num_lock);
return -1;
}
pinMode(num_lock, PINMODE_OUTPUT);
printf("Ready to start doing keyboard things :)\n");
const char *filename = "/dev/hidg0";
int fd = 0;
fd_set rfds;
char buf[BUF_LEN];
int retval, cmd_len;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 200;
if((fd = open(filename, O_RDWR, 0666)) == -1) {
perror(filename);
return -1;
}
while(true){
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 200;
retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1 && errno == EINTR)
continue;
if (retval < 0) {
perror("select()");
return 4;
}
if(FD_ISSET(fd, &rfds)) {
cmd_len = read(fd, buf, BUF_LEN - 1);
for(int i = 0; i < cmd_len; i++){
//CONTROL LEDS
digitalWrite(num_lock, (buf[i] % 2 == 1 ? HIGH : LOW));
}
}
char report[8];
memset(&report, 0x0, 8);
int curKey = 0;
for(int c = 0; c < sizeof(columns)/sizeof(int); c++){
digitalWrite(columns[c], LOW);
for(int r = 0; r < sizeof(rows)/sizeof(int); r++){
char curState = state[c][r];
state[c][r] = (digitalRead(rows[r]) == LOW);
if(state[c][r] == true && curKey < 6){
char *binding = mapping[c][r];
for(int i = 0; kmod[i].opt != NULL; i++){
if (strcmp(binding, kmod[i].opt) == 0) {
report[0] = report[0] | kmod[i].val;
break;
}
}
for(int i = 0; kval[i].opt != NULL; i++){
if(strcmp(binding, kval[i].opt) == 0) {
report[2 + curKey++] = kval[i].val;
break;
}
}
}
if(state[c][r] != curState) printf("statechange to %s column %d, row %d\n", (state[c][r] ? "HIGH" : "LOW"), c, r);
}
digitalWrite(columns[c], HIGH);
}
//printf("Report: ");
//for(int i = 0; i < sizeof(report)/sizeof(char); i++){
// printf("%x ", report[i]);
//}
//printf("\n");
write(fd, report, 8);//Send Report over USB
usleep(1000);
}
close(fd);
return 0;
}

@ -0,0 +1,120 @@
static int columns[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
static int rows[] = {14, 16, 17, 18, 19, 20, 21, 22, 26, 27};
//1,4,5,7
static int num_lock = 25;
static int caps_lock = 11;
static int scroll_lock = 13;
static char* mapping[sizeof(columns)/sizeof(int)][sizeof(rows)/sizeof(int)] = {
{"^", "--tab", "--caps-lock", "--left-shift", "--left-ctrl", "<", "a", "q", "1", "2"},//column 0
{"w", "s", "y", "--left-meta", "--left-alt", "x", "d", "e", "3", "4"},
{"r", "f", "c", "--spacebar", "v", "g", "t", "5", "6", "z"},
{"h", "b", "n", "j", "u", "7", "8", "i", "k", "m"},
{",", "l", "o", "9", "0", "p", "ö", ".", "--right-alt", "--right-meta"},
{"-", "ä", "ü", "ß", "´", "--bckspc", "--return", "+", "#", "--right-shift"},
{"--application", "--right-ctrl", "--f10", "--f11", "--f12", "0", "0", "0", "0", "0"},
{"--left", "--down", "--right", "--up", "--del", "--end", "--pagedown", "--pageup", "--home", "--insert"},
{"--kp-0", "--kp-1", "--kp-2", "--kp-3", "--kp-4", "--kp-5", "--kp-6", "--kp-7", "--kp-8", "--kp-9"},
{"--kp--enter", "--kp-plus", "--kp-minus", "--kp-mul", "--kp-div", "--num-lock", "0", "0", "0", "0"},
{"--esc", "--f1", "--f2", "--f3", "--f4", "--f5", "--f6", "--f7", "--f8", "--f9"}//column 10
};
struct options {
const char *opt;
unsigned char val;
};
static struct options kmod[] = {
{.opt = "--left-ctrl", .val = 0x01},
{.opt = "--right-ctrl", .val = 0x10},
{.opt = "--left-shift", .val = 0x02},
{.opt = "--right-shift", .val = 0x20},
{.opt = "--left-alt", .val = 0x04},
{.opt = "--right-alt", .val = 0x40},
{.opt = "--left-meta", .val = 0x08},
{.opt = "--right-meta", .val = 0x80},
{.opt = NULL}
};
static struct options kval[] = {
{.opt = "^", .val = 0x35},
{.opt = "1", .val = 0x1E},
{.opt = "2", .val = 0x1F},
{.opt = "3", .val = 0x20},
{.opt = "4", .val = 0x21},
{.opt = "5", .val = 0x22},
{.opt = "6", .val = 0x23},
{.opt = "7", .val = 0x24},
{.opt = "8", .val = 0x25},
{.opt = "9", .val = 0x26},
{.opt = "0", .val = 0x27},
{.opt = "ß", .val = 0x2D},
{.opt = "´", .val = 0x2E},
{.opt = "--bckspc", .val = 0x2A},
{.opt = "--tab", .val = 0x2B},
{.opt = "q", .val = 0x14},
{.opt = "w", .val = 0x1A},
{.opt = "e", .val = 0x08},
{.opt = "r", .val = 0x15},
{.opt = "t", .val = 0x17},
{.opt = "z", .val = 0x1C},
{.opt = "u", .val = 0xA0},
{.opt = "i", .val = 0x0C},
{.opt = "o", .val = 0x12},
{.opt = "p", .val = 0x13},
{.opt = "ü", .val = 0x2f},
{.opt = "+", .val = 0x30},
{.opt = "--caps-lock", .val = 0x39},
{.opt = "a", .val = 0x04},
{.opt = "s", .val = 0x16},
{.opt = "d", .val = 0x07},
{.opt = "f", .val = 0x09},
{.opt = "g", .val = 0x0A},
{.opt = "h", .val = 0x0B},
{.opt = "j", .val = 0x0D},
{.opt = "k", .val = 0x0E},
{.opt = "l", .val = 0x0F},
{.opt = "ö", .val = 0x33},
{.opt = "ä", .val = 0x34},
{.opt = "#", .val = 0x32},
{.opt = "--return", .val = 0x28},
{.opt = "<", .val = 0x64},
{.opt = "y", .val = 0x1D},
{.opt = "x", .val = 0x2B},
{.opt = "c", .val = 0x06},
{.opt = "v", .val = 0x19},
{.opt = "b", .val = 0x05},
{.opt = "n", .val = 0x11},
{.opt = "m", .val = 0x10},
{.opt = ",", .val = 0x36},
{.opt = ".", .val = 0x17},
{.opt = "-", .val = 0x38},
{.opt = "--spacebar", .val = 0x2C},
{.opt = "--esc", .val = 0x29},
{.opt = "--f1", .val = 0x3a},
{.opt = "--f2", .val = 0x3b},
{.opt = "--f3", .val = 0x3c},
{.opt = "--f4", .val = 0x3d},
{.opt = "--f5", .val = 0x3e},
{.opt = "--f6", .val = 0x3f},
{.opt = "--f7", .val = 0x40},
{.opt = "--f8", .val = 0x41},
{.opt = "--f9", .val = 0x42},
{.opt = "--f10", .val = 0x43},
{.opt = "--f11", .val = 0x44},
{.opt = "--f12", .val = 0x45},
{.opt = "--insert", .val = 0x49},
{.opt = "--home", .val = 0x4a},
{.opt = "--pageup", .val = 0x4b},
{.opt = "--del", .val = 0x4c},
{.opt = "--end", .val = 0x4d},
{.opt = "--pagedown", .val = 0x4e},
{.opt = "--right", .val = 0x4f},
{.opt = "--left", .val = 0x50},
{.opt = "--down", .val = 0x51},
{.opt = "--up", .val = 0x52},
{.opt = "--application", .val = 0x65},
{.opt = "--num-lock", .val = 0x53},
{.opt = "--kp-enter", .val = 0x58},
{.opt = NULL}
};
Loading…
Cancel
Save