there is a bug I cannot fix:
if I print the whole string to which the key is added then there is always a
\
at the 1st position.
e.g., if I enter
1234
it is shown
\1234
2nd problem:
in the line
(event.type != EV_KEY) ...
what is EV_KEY? it's no where been declared AFAIK...?
3rd problem:
at the first call the function appears not to wait for a keypress but runs through,
it just then waits for the 2nd, 3rd, 4th keypress and so on
(what maybe is the reason for the odd output).
what is wrong?
Code: Select all
// read letters from USB keyboard plugged to EV3
// readkeys.c
// ver. 1.02
// read USB kbd keys by: int getcharUSB()
//
// credits to Xander Soldaat
// reference:
// http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <linux/input.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
//#include "lms2012.h" // still shaky because of re-definitions
#include "ev3_button.h"
#include "ev3_lcd.h"
#define cls() LcdClean() // more C-like
#define mline 118 // line for status msg
//..............................................................................
// USB kbd device
//..............................................................................
#define EV_PRESSED 1
#define EV_RELEASED 0
#define EV_REPEAT 2
// QWERTZ = German kbd layout representation
// uppercase overhead = 128
char QWERTZ[]= " 1234567890-= qwertzuiopü+ asdfghjklöä# <yxcvbnm,.- * 789-456+1230, !'§$%&/()=?` QWERTZUIOPÜ* ASDFGHJKLÖÄ' >YXCVBNM;:_......";
#define KEY_UPPEROVHD 130 // uppercase overhead = 128
#define KEY_LEFTSHIFT 42 // kbd keycode
#define KEY_RIGHTSHIFT 54 // kbd keycode
#define KEY_CAPSLOCK 58 // kbd keycode
int _kbd_fd_; // kbd file dev handle
//..............................................................................
char init_USBkbd() {
char *device = "/dev/input/event0"; // event1: hangs up after cls
_kbd_fd_ = 0;
if ( (_kbd_fd_ = open(device, O_RDWR)) > 0 ) return 1;
else return 0;
}
//..............................................................................
int getcharUSB() {
struct input_event event;
int keycode;
char buf[10];
int retval;
static int UPCASE;
static char LEFTSHIFTTMP;
int num_bytes;
while (event.type != EV_KEY) {
num_bytes = read(_kbd_fd_, &event, sizeof(struct input_event));
}
if(event.value == EV_RELEASED)
{
keycode = event.code;
if(keycode==KEY_CAPSLOCK) {
UPCASE=(UPCASE==0)?KEY_UPPEROVHD:0;
return 0;
}
else
{
// SHIFT for uppercase
if ( (keycode==KEY_LEFTSHIFT)||(keycode==KEY_RIGHTSHIFT) ) {
UPCASE=(UPCASE==0)?KEY_UPPEROVHD:0; // (still shaky)
LEFTSHIFTTMP=1;
return 0;
}
if(keycode== 1) retval=27; // KEY_ESCAPE
else
if( (keycode==28)||(keycode==96) ) retval=10; // KEY_ENTER
else
if(keycode==14) retval=8; // KEY_BACKSPACE
else
retval= QWERTZ[keycode+UPCASE] ;
if (LEFTSHIFTTMP) { // SHIFT for uppercase
LEFTSHIFTTMP=0;
UPCASE=(UPCASE==0)?KEY_UPPEROVHD:0; // (still shaky)
}
// debug:
//sprintf(buf,"keycode:%3d ASCII:%3d", keycode,retval); LcdText(1, 0,20, buf);
}
}
return ( retval );
}
//..............................................................................
// task main()
// how to initialize and how to call
//..............................................................................
int main()
{
int key = 0, i=0;
char buf[120], text[120]; // text string
ButtonLedInit();
LcdInit();
cls();
if (!init_USBkbd()) {
LcdText(1, 0, mline, "keyboard device error! ");
Wait(2000);
goto quit;
}
LcdText(1, 0, mline, "press keys on keyboard! ");
text[0]='\0';
while (1) {
key=getcharUSB(); // read kbd kbdkeycode => ASCII code
// now what to do with the key ?
if (key==27) goto quit; // 27 = ESC => stop program
else
if(key>32) {
// add key to string
text[i+1]='\0';
text[i] =key;
sprintf(buf,"%c ", key);
LcdText(1, 0,10, buf); // display key
LcdText(1, 0,40, text); // display string
i++;
}
Wait(1);
}
quit:
LcdText(1, 0, 10, " ");
LcdText(1, 0, mline, "program terminated ");
Wait(500);
cls();
Wait(10);
LcdExit();
ButtonLedExit();
return 1;
}