⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.c

📁 连接到SPI总线的键盘驱动程序
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : keyboard.c
//* Object              :  
//* Creation            : HII   11/19/2002
//*
//*----------------------------------------------------------------------------
#include "keyboard.h"


// prototypes of functions defined in this module
void AT91F_FillKeybBuffer(AT91PS_KEYBOARD pSkeyboard, unsigned char *pPs2Fifo);
int AT91F_ReadOneKey(AT91PS_KEYBOARD pSkeyboard, char *car);
int AT91F_IsKeyPressed(AT91PS_KEYBOARD pSkeyboard);
void AT91F_UpdateMouse(AT91PS_MOUSE pSMouse, unsigned char *pPs2Fifo);
int AT91F_IsMouseMove(AT91PS_MOUSE pSMouse);
int AT91F_ReadMouse(AT91PS_MOUSE pSMouse, int *val);

//* Buffer to store keyboard events
unsigned char KeyboardFifo[BUFF_SIZE];
//* Buffer to store mouse events
unsigned int MousseFifo[BUFF_SIZE];

//* External variables defined in main.c
extern AT91S_KEYBOARD SKeyBoard;
extern AT91S_MOUSE    SMouse;

//* Partial scan code table : Please refer to the "SEMTECH PS2 adapter UR6HCPS2-SP40 datasheet"
const unsigned char AT91C_SCANCODE[][2] = {
	{0, 0},
	{0, 0},
	{0, 0},
	{0, 0},
	{'q', 'Q'},
	{'b', 'B'},
	{'c', 'C'},
	{'d', 'D'},
	{'e', 'E'},
	{'f', 'F'},
	{'g', 'G'},
	{'h', 'H'},
	{'i', 'I'},
	{'j', 'J'},
	{'k', 'K'},
	{'l', 'L'},
	{'m', 'M'},
	{'n', 'N'},
	{'o', 'O'},
	{'p', 'P'},
	{'a', 'A'},
	{'r', 'R'},
	{'s', 'S'},
	{'t', 'T'},
	{'u', 'U'},
	{'v', 'V'},
	{'z', 'Z'},
	{'x', 'X'},
	{'y', 'Y'},
	{'w', 'W'},
	{'1', '!'},
	{'2', '@'},
	{'3', '#'},
	{'4', '$'},
	{'5', '%'},
	{'6', '^'},
	{'7', '&'},
	{'8', '*'},
	{'9', '('},
	{'0', ')'},	
};


//*----------------------------------------------------------------------------
//* \fn    AT91F_OpenKeyBoard
//* \brief This function initialize the keyboard structure
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_OpenKeyBoard(AT91PS_KEYBOARD pSkeyboard)
{
	pSkeyboard->pStartBuffer = (char *)KeyboardFifo;
	pSkeyboard->pInBuffer    = (char *)KeyboardFifo;
	pSkeyboard->pOutBuffer   = (char *)KeyboardFifo;
	pSkeyboard-> NbKey       = 0;

	pSkeyboard->Handler = AT91F_FillKeybBuffer;
	pSkeyboard->IsKeyPressed = AT91F_IsKeyPressed;
	pSkeyboard->ReadKey = AT91F_ReadOneKey;
	
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_InitKeyBoards
//* \brief This function initialize the mousse structure
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_OpenMouse(AT91PS_MOUSE pSMouse)
{
	pSMouse->pStartMousseBuffer = (int *)MousseFifo;
	pSMouse->pInMousseBuffer    = (int *)MousseFifo;
	pSMouse->pOutMousseBuffer   = (int *)MousseFifo;
	pSMouse->NbKey              = 0;	

	pSMouse->Handler = AT91F_UpdateMouse;
	pSMouse->IsMouseMove = AT91F_IsMouseMove;
	pSMouse->ReadMouse = AT91F_ReadMouse;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_FillKeybBuffer
//* \brief This function is invoked by the PS2 interrupt handler
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_FillKeybBuffer(AT91PS_KEYBOARD pSkeyboard, unsigned char *pPs2Fifo)
{
unsigned char c;	
	c = pPs2Fifo[1];
	if(pPs2Fifo[0] & AT91C_CAPSLOCK)
		 c = AT91C_SCANCODE[c][1];
	else	
		c = AT91C_SCANCODE[c][0];
    if (pSkeyboard->NbKey < BUFF_SIZE)	// if the keyboard buffer is not full
    {
       	*(pSkeyboard->pInBuffer++) = c;        	
        pSkeyboard->NbKey++;

        if (pSkeyboard->pInBuffer >= (pSkeyboard->pStartBuffer + BUFF_SIZE)) // wrapp buffer
            pSkeyboard->pInBuffer = pSkeyboard->pStartBuffer;
    }
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_ReadOneKey
//* \brief This function allows to read a key in the keyboard buffer
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
int AT91F_ReadOneKey(AT91PS_KEYBOARD pSkeyboard, char *car)
{
	if(pSkeyboard->NbKey == 0)
		return 0;
	*car = *pSkeyboard->pOutBuffer++;
   	if (pSkeyboard->pOutBuffer >= (pSkeyboard->pStartBuffer + BUFF_SIZE)) // wrapp buffer
            pSkeyboard->pOutBuffer = pSkeyboard->pStartBuffer;
    pSkeyboard->NbKey --;
	return pSkeyboard->NbKey;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_IsKeyPressed
//* \brief This function return the number of stroke keys
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
int AT91F_IsKeyPressed(AT91PS_KEYBOARD pSkeyboard)
{
	return pSkeyboard->NbKey;
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_UpdateMouse
//* \brief This function is invoked by the PS2 interrupt handler
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_UpdateMouse(AT91PS_MOUSE pSMouse, unsigned char *pPs2Fifo)
{
    if (pSMouse->NbKey < BUFF_SIZE)	// if the keyboard buffer is not full
    {
       	*(pSMouse->pInMousseBuffer++) = *(int *)pPs2Fifo;        	
        pSMouse->NbKey++;

        if (pSMouse->pInMousseBuffer  >= (pSMouse->pStartMousseBuffer + BUFF_SIZE)) // wrapp buffer
            pSMouse->pInMousseBuffer = pSMouse->pStartMousseBuffer;
    }
}


//*----------------------------------------------------------------------------
//* \fn    AT91F_IsMouseMove
//* \brief This function return the number of mouse events
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
int AT91F_IsMouseMove(AT91PS_MOUSE pSMouse)
{
	return(pSMouse->NbKey);
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_ReadMouse
//* \brief This function is invoked by 
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
int AT91F_ReadMouse(AT91PS_MOUSE pSMouse, int *val)
{
	if(pSMouse->NbKey == 0)
		return 0;
	*val = *pSMouse->pOutMousseBuffer++;
   	if (pSMouse->pOutMousseBuffer >= (pSMouse->pStartMousseBuffer + BUFF_SIZE)) // wrapp buffer
            pSMouse->pOutMousseBuffer = pSMouse->pStartMousseBuffer;
    pSMouse->NbKey --;
	return pSMouse->NbKey;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -