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

📄 ps2protocol.c

📁 基于Jennic公司Zigbee芯片JN5139做的无线键盘项目源码
💻 C
字号:
/****************************************************************************
 *
 * MODULE:             Wireless PS2 Bus, Protocol Engine
 *
 * COMPONENT:          $RCSfile$
 *
 * VERSION:            $Name$
 *
 * REVISION:           $Revision$
 *
 * DATED:              $Date$
 *
 * STATUS:             $State$
 *
 * AUTHOR:             APV Ward
 *
 * DESCRIPTION:
 * Converts PS2 three-key and extended-key sequences into printable ASCII codes.
 *
 * This file implements the PS2 protocol as a state machine, with states
 * represented using bool switches.
 *
 * Code conversion is required for a wireless keyboard demonstrator where key
 * presses are displayed on the coordinator's LCD - a wireless keyboard product
 * would echo the PS2 key sequences directly to a PS2 plug port.
 *
 * CHANGE HISTORY:
 *
 * $Log$
 *
 * LAST MODIFIED BY:   $Author$
 *                     $Modtime$
 *
 ****************************************************************************
 *
 * This software is owned by Jennic and/or its supplier and is protected
 * under applicable copyright laws. All rights are reserved. We grant You,
 * and any third parties, a license to use this software solely and
 * exclusively on Jennic products. You, and any third parties must reproduce
 * the copyright and warranty notice and any other legend of ownership on each
 * copy or partial copy of the software.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER
 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
 * ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES,
 * BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL,
 * INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.
 *
 * Copyright Jennic Ltd 2005, 2006. All rights reserved
 *
 ****************************************************************************/

#include "jendefs.h"
#include "ZKBconfig.h"
#include "PS2protocol.h"
#include "LedControl.h"
#include "PS2keyCodes.h"

/****************************************************************************/
/***        Local Defines                                                 ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/

/* pseudo state-machine variables */
bool_t   boBreakSeqStarted   = FALSE;
bool_t   boExtendSeqStarted  = FALSE;
bool_t   boShifted           = FALSE;
bool_t   boCapsLockOn        = FALSE;

/****************************************************************************/
/***        Local functions                                               ***/
/****************************************************************************/

/****************************************************************************/
/***        Exported functions                                            ***/
/****************************************************************************/

/****************************************************************************
 *
 * NAME: u8PS2protocol
 *
 * DESCRIPTION:
 * In keyboard demo mode all printable key presses are displayed on the
 * coordinator's LCD - this function maps PS2 keycode sequences (3 to 5 bytes)
 * to single ASCII printable characters.
 *
 * PARAMETERS:      key code from PS2 bus
 *
 * RETURNS:         Either ASCII code or 0
 *
 ****************************************************************************/

PUBLIC uint8 u8PS2protocol(uint8 u8Key)
{
    uint8  u8Temp = 0;

	switch (u8Key)
	{
		case 0xF0:

			/* hanldes    BREAK sequences ( F0, xx) and  */
			/*         EXTENDED sequences ( E0, F0, xx). */
			boBreakSeqStarted  = TRUE;
			boExtendSeqStarted = FALSE;
			break;

		case 0xE0:
			boExtendSeqStarted = TRUE;
			break;

		case 0xE1: 	/* special extended seq, such as PAUSE key */
			break;	/* ignore for this demo */

		default: /* ordinary keycode */
		{
			if (boBreakSeqStarted) /* last code was a BREAK code */
			{
				boBreakSeqStarted = FALSE;

				/* we only look for a release sequence on the SHIFT keys */
				if ( (u8Key == KBD_L_SHIFT) || (u8Key == KBD_R_SHIFT) )
				{
					boShifted = FALSE; /* shift key released */
				}
			}
			else if (boExtendSeqStarted)
			{
				boExtendSeqStarted = FALSE; /* ignore extended BREAK codes */
			}
			else
			{
				if ( (u8Key == KBD_L_SHIFT) || (u8Key == KBD_R_SHIFT) )
				{
					boShifted = TRUE; /* shift key is down ;-(  */
				}
				else if (u8Key == KBD_CAPS_LOCK)
				{
					boCapsLockOn = ! boCapsLockOn; /* toggles state */
					vLedControl(LED_COORD_CAPS, boCapsLockOn);
				}
				else
				{
					/* Found a character key, so translate into ASCII.		*/
					/* Lookup table translates non-printing keys to 0x00	*/
					if (boShifted)
					{
						/* lookup in shifted key table */
						u8Key = au8shiftedAlphabet[u8Key];
					}
					else
					{
						u8Temp = u8Key;
						u8Key  = au8alphabet[u8Key];

						/* if CAPS LOCK on, then re-lookup keycode */
						if (boCapsLockOn && u8Key >= 'a' && u8Key <= 'z' )
							u8Key = au8shiftedAlphabet[u8Temp];
					}

					u8Temp = u8Key; /* return the keycode */
				}
			}
		}
	}

	return u8Temp;
}

⌨️ 快捷键说明

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