• 4992阅读
  • 0回复

谁能帮我翻译一下 下面这个键盘驱动是怎么实现的呢?!~ [复制链接]

上一主题 下一主题
离线ny257077
 
只看楼主 倒序阅读 楼主  发表于: 2010-04-29
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "kbd_types.h"

#define KEYBOARD "/dev/mcu/kbd"
#ifndef _KBD_H
#define _KBD_H

int  KBD_Open(void);
void KBD_Close(void);

void KBD_GetModifierInfo(MWKEYMOD *modifiers, MWKEYMOD *curmodifiers);

int  KBD_Read(char *kbuf, MWKEYMOD *modifiers, MWSCANCODE *scancode);


#endif

static int fd;

typedef struct
{
    unsigned short mwkey;
    int scancode;
}KeyMap;

static unsigned short scancodes[64];

#define __I2C_MEGA8__
#ifdef __ZLG7289__
static KeyMap keymap[] = {
      {MWKEY_KP0,  0x1d},
      {MWKEY_KP1,  0x21},
      {MWKEY_KP2,  0x25},
      {MWKEY_KP3,  0x23},
      {MWKEY_KP4,  0x29},
      {MWKEY_KP5,  0x2d},
      {MWKEY_KP6,  0x2b},
      {MWKEY_KP7,  0x31},
      {MWKEY_KP8,  0x35},
      {MWKEY_KP9,  0x33},
      {MWKEY_NUMLOCK,     0x11},
      {MWKEY_KP_DIVIDE,    0x15},
      {MWKEY_KP_MULTIPLY, 0x13},
      {MWKEY_KP_MINUS,  0x0b},
      {MWKEY_KP_PLUS,    0x34},
      {MWKEY_KP_ENTER,    0x24},
      {MWKEY_KP_DEL,    0x1b},
};
#else
#ifdef __I2C_MEGA8__
static KeyMap keymap[] = {        //update map policy
      {MWKEY_KP0,  0x0b},
      {MWKEY_KP1,  0x02},
      {MWKEY_KP2,  0x03},
      {MWKEY_KP3,  0x04},
      {MWKEY_KP4,  0x05},
      {MWKEY_KP5,  0x06},
      {MWKEY_KP6,  0x07},
      {MWKEY_KP7,  0x08},
      {MWKEY_KP8,  0x09},
      {MWKEY_KP9,  0x0a},
      
      {MWKEY_NUMLOCK,     0x2a},
      {MWKEY_KP_DIVIDE,    0x35},
      {MWKEY_KP_MULTIPLY, 0x37},
      {MWKEY_KP_MINUS,    0x4a},
      {MWKEY_KP_PLUS,    0x4e},
      {MWKEY_KP_ENTER,    0x1c},
      {MWKEY_KP_DEL,    0x53},
};

#endif //__I2C_MEGA8__
#endif //__ZLG7289__

int KBD_Open(void)
{
    int i;    
    fd = open(KEYBOARD, O_RDONLY | O_NONBLOCK);

    if (fd < 0)
    {
        printf("%s - Can't open keyboard!\n", __FUNCTION__);
        return -1;
    }

    for (i=0; i<sizeof(scancodes)/sizeof(scancodes[0]); i++)
        scancodes=MWKEY_UNKNOWN;

    for (i=0; i< sizeof(keymap)/sizeof(keymap[0]); i++)
        scancodes[keymap.scancode]=keymap.mwkey;

    return fd;
}


void KBD_Close(void)
{
    close(fd);
    fd = -1;
}

void KBD_GetModifierInfo(unsigned int * modifiers, unsigned int * curmodifiers)
{
    if (modifiers)
        *modifiers = 0;    /* no modifiers available */
    if (curmodifiers)
        *curmodifiers = 0;
}


int KBD_Read(char* kbuf, unsigned int * modifiers, unsigned short * scancode)
{
    int keydown = 0;
    int cc = 0;
    char buf,key;

    cc = read(fd, &buf, 1);

    if (cc < 0)
    {
        if ((errno != EINTR) && (errno != EAGAIN)
            && (errno != EINVAL))
            {
                perror("KBD KEY");
                return (-1);
            }
        else
            {
                return (0);
            }
    }
    if (cc == 0)
        return (0);

    /* If the code is big than 127, then we know that */
    /* we have a key down.  Figure out what we've got */

    *modifiers = 0;

    if (buf & 0x80)
    {
        keydown = 1;    /* Key pressed but not released */
    }
    else
    {
        keydown = 2;    /* key released */
    }

    buf &= (~0x80);
    *scancode = scancodes[(int) buf];
    *kbuf = *scancode ;
    return keydown;
    
}

char GetKey(void)
{
    int keydown=0;
     char  key=0;
    char  temp=0;
    unsigned int modifiers;
    unsigned short  scancode;
    keydown=KBD_Read(&key, &modifiers, &scancode);
    if(keydown==1)
      return key;
        else    
      return temp;
    
}

快速回复
限100 字节
 
上一个 下一个