📄 keyboard.c
字号:
/*****************************************************************************
* VAULT INFORMATION SERVICES *
* TEXAS INSTRUMENTS, INC. *
*----------------------------------------------------------------------------*
* "TUSB2136 Generic Keyboard Demo Program" *
* *
* Programmed by: Craig Steiner, VIS (csteiner@vaultbbs.com) *
* Develop Date: 30/Sep/2000 *
*----------------------------------------------------------------------------*
* Description: This software is a fully functional demonstration program that*
* illustrates the use of the TUSB2136 to implement a USB 101-key keyboard. *
* *
* This module is the "main" module. Also, pretty much everything that the *
* end-user will need to modify to create a standard keyboard is contained *
* in this module. *
* *
* Modified by Jim Chen 2001/01/03: *
* 1. total 102 keys *
* 2. can work in Mac *
* 3. use scanline8, scanline9 and return 0x40 as suspend key *
* 4. added usbSetIdle(void), void usbSetProtocol(void) for Mac *
* *
* Modified by Jim Chen 2001/01/19: *
* 1. fixed bug in high speed Mac *
* *
* Modified by Jim Chen 2001/03/02: *
* 1. merge 4 configurations into one binary code *
* 2. solve phantom key issue by Mike's solution *
* 3. version number started with Rev1.00 *
* *
* Modified by Jim Chen 2001/06/01: Rev1.01 *
* 1. changed version 4 from NewMotion one-key to flat keypad *
* *
******************************************************************************/
#include <io51.h>
#include "types.h"
#include "delay.h"
#include "usb.h"
#include "tusb2136.h"
// Maximum size of the Keypress buffer is 6 bytes, because that's what fits in
// a single HID standard keyboard response
#define KEYPRESS_BUFFER_SIZE 0x06
// The following definitions indicate which bit of the data byte sent from
// the host to the keyboard correspond to each of the given LEDs.
#define BIT_NUMLOCK 0x01
#define BIT_CAPSLOCK 0x02
#define BIT_SCROLLLOCK 0x04
// The following definitions indicate which GPIO pin corresponds to each
// of the keyboard LEDs.
#define PIN_NUMLOCK P3.4
#define PIN_CAPSLOCK P3.3
#define PIN_SCROLLLOCK P3.2
extern unsigned char s0123;
extern BYTE deviceReady;
extern BYTE bSuspended;
extern void gpioInitialization(void);
extern void setLEDs(BYTE bData);
extern int readSwitches(void);
extern BYTE strlen(char *string);
void sendUartByte(BYTE bValue);void sendUartString(char *string, BYTE bCount);
void sendUartHex(BYTE bValue);
extern BYTE memcmp(char *string1, char *string2, BYTE count);
extern void memcpy(char *string1, char *string2, BYTE count);
extern void InitializeUsbData(void);
extern void InitializeUsbHub(void);
extern void InitializeUsbFunction(void);
extern void GetVidPidSetting(void);
unsigned char intFlags = 0x00;
unsigned char otherFlags = 0x00;
void ClearKeyBoardBuffer(void);
void sendKeyData(int x);
void UpdateIEP1WithKeypress(void);
BYTE bLED = 0;
BYTE modifierByte = 0x00;
BYTE keypressBuffer[KEYPRESS_BUFFER_SIZE + 1];
char previousIEP1packet[8];
char *iep1Buffer = 0x00;
#pragma memory = dataseg(TUSB2136_DEBOUNCE_SEG)
BYTE keypressDebounce[256];
#pragma memory = default
/***************************************************************************
* Structure: KEYDEFS Structure *
* Programmer: Craig Steiner (csteiner@vaultbbs.com) *
* Description: The keyDefs[] structure contains a list of all the keys *
* that are supported by the keyboard and information related to each *
* key. Additional keys may be added to the program by adding *
* additional entries in this table. The structure is defined in *
* types.h. *
* *
* vidPidMask: The first column is a bit-mapped mask that indicates *
* whether the given key is supported by each VID/PID version. *
* For example, 0x0001 means that the given key is only supported *
* by VIDSTA configuration #1. 0x0003 means it is supported only *
* by configuration #1 and #2. 0xFFFF means it is supported by *
* all 16 possible configurations of VIDSTA. This allows a *
* single firmware which includes specialized key definitions *
* that only apply to certain products. *
* scanLine: The second column indicates which keyboard scan line must *
* be selected in order to detect the keypress. The current *
* software supports 18 scan lines. More scan lines may be *
* supported by modifying the "scan line selection" code in *
* the main() module. *
* keyPressMask: The third column is a bitmask which indicates which *
* bit will be brought low when the given key is pressed. *
* hidCode: The fourth column indicates the HID usage code for the *
* given keypress. The constants are delcared in usb.h. A *
* value of 0 in the hidCode coumn will cause the keypress to *
* be ignored. *
* modBit: The fifth (optional) column indicates which bit, if any, *
* should be set in the keyboard modifier byte. This is for *
* ALT, SHIFT, CTRL, and GUI keys. This is implemented as a *
* field in the array so that additional keys can easily be *
* added that mimic the functionality of the keys above. If the *
* value is omitted, it will be compiled to 0x00 which means the *
* key will NOT set any bit in the modifier byte of the report. *
***************************************************************************/
/* for reference only
struct KEYMAP_STRUCT
{
unsigned int vidPidMask; // Each bit indicates whether this key is supported in given VID/PID setting
unsigned long scanLine; // Indicates the column to select
BYTE keyPressMask; // Indicates the value returned if the given key is pressed
BYTE hidCode; // The value that is returned via USB to computer for this keypress
BYTE modBit; // Holds the bit which should be turned on in the modifier byte if this key is pressed
};
*/
// 4 kinds of keyboard matrix mapping
struct KEYMAP_STRUCT code keyDefs[] = {
//VID, Col, Ret, USB, ModByteBit
{0x0001, 18, 0x02, usbUsageLeftGUI, 0x08},
{0x0002, 18, 0x80, usbUsage1},
{0x0006, 18, 0x40, usbUsageA}, // for S[3:0]=2 and S[3:0]=4, 6=2+4
{0x0006, 18, 0x20, usbUsageJ},
{0x0006, 18, 0x10, usbUsageS},
{0x0004, 18, 0x80, usbUsageKeypadNumlock},
{0x0008, 18, 0x80, usbUsageQ}, // for David's keypad
{0x0008, 18, 0x40, usbUsageW}, // for David's keypad
{0x0008, 18, 0x20, usbUsageE}, // for David's keypad
{0x0008, 18, 0x10, usbUsageR}, // for David's keypad
{0x0008, 18, 0x08, usbUsageT}, // for David's keypad
{0x0008, 18, 0x04, usbUsageY}, // for David's keypad
{0x0008, 18, 0x02, usbUsageU}, // for David's keypad
{0x0001, 17, 0x80, usbUsageRightAlt, 0x40},
{0x0001, 17, 0x20, usbUsageLeftAlt, 0x04 },
{0x0001, 17, 0x08, usbUsageScrollLock},
{0x0001, 17, 0x04, usbUsagePrintScreen},
{0x0002, 17, 0x80, usbUsage2},
{0x0006, 17, 0x40, usbUsageB}, // for S[3:0]=2 and S[3:0]=4, 6=2+4
{0x0006, 17, 0x20, usbUsageK},
{0x0006, 17, 0x10, usbUsageT},
{0x0004, 17, 0x80, usbUsageCapsLock},
{0x0008, 17, 0x80, usbUsage9}, // for David's keypad
{0x0008, 17, 0x40, usbUsage8}, // for David's keypad
{0x0008, 17, 0x20, usbUsage7}, // for David's keypad
{0x0008, 17, 0x10, usbUsageEscape}, // for David's keypad
// {0x0008, 17, 0x08, usbUsageOut}, // for David's keypad
{0x0008, 17, 0x08, usbUsageLeftShift, 0x02}, // for David's keypad
{0x0008, 17, 0x04, usbUsageP}, // for David's keypad
{0x0008, 17, 0x02, usbUsageO}, // for David's keypad
{0x0008, 17, 0x01, usbUsageI}, // for David's keypad
{0x0001, 16, 0x80, usbUsageSpacebar},
{0x0001, 16, 0x40, usbUsageEnter},
{0x0001, 16, 0x20, usbUsageF5},
{0x0001, 16, 0x10, usbUsageBackspace},
{0x0001, 16, 0x04, usbUsageF10},
{0x0001, 16, 0x02, usbUsageF9},
{0x0001, 16, 0x01, usbUsageBackslash},
{0x0002, 16, 0x80, usbUsage3},
{0x0006, 16, 0x40, usbUsageC}, // for S[3:0]=2 and S[3:0]=4, 6=2+4
{0x0006, 16, 0x20, usbUsageL},
{0x0006, 16, 0x10, usbUsageU},
{0x0004, 16, 0x80, usbUsageScrollLock},
{0x0008, 16, 0x80, usbUsageA}, // for David's keypad
{0x0008, 16, 0x40, usbUsageS}, // for David's keypad
{0x0008, 16, 0x20, usbUsageD}, // for David's keypad
{0x0008, 16, 0x10, usbUsageF}, // for David's keypad
{0x0008, 16, 0x08, usbUsageG}, // for David's keypad
{0x0008, 16, 0x04, usbUsageH}, // for David's keypad
{0x0008, 16, 0x02, usbUsageJ}, // for David's keypad
{0x0008, 16, 0x01, usbUsageComma}, // for David's keypad
{0x0001, 15, 0x80, usbUsageSlash},
{0x0001, 15, 0x20, usbUsageApostrophe},
{0x0001, 15, 0x10, usbUsageLeftBracket},
{0x0001, 15, 0x08, usbUsageP},
{0x0001, 15, 0x04, usbUsage0},
{0x0001, 15, 0x02, usbUsageMinus},
{0x0001, 15, 0x01, usbUsageSemicolon},
{0x0006, 15, 0x80, usbUsage4}, // for S[3:0]=2 and S[3:0]=4, 6=2+4
{0x0006, 15, 0x40, usbUsageD},
{0x0006, 15, 0x20, usbUsageM},
{0x0006, 15, 0x10, usbUsageV},
{0x0008, 15, 0x80, usbUsage6}, // for David's keypad
{0x0008, 15, 0x40, usbUsage5}, // for David's keypad
{0x0008, 15, 0x20, usbUsage4}, // for David's keypad
{0x0008, 15, 0x10, usbUsageHome}, // for David's keypad
// {0x0008, 15, 0x08, usbUsageIn}, // for David's keypad
{0x0008, 15, 0x08, usbUsageRightAlt, 0x40}, // for David's keypad
{0x0008, 15, 0x04, usbUsageApostrophe}, // for David's keypad
{0x0008, 15, 0x02, usbUsageL}, // for David's keypad
{0x0008, 15, 0x01, usbUsageK}, // for David's keypad
{0x0001, 14, 0x40, usbUsagePeriod},
{0x0001, 14, 0x10, usbUsageF7},
{0x0001, 14, 0x08, usbUsageO},
{0x0001, 14, 0x04, usbUsage9},
{0x0001, 14, 0x02, usbUsageF8},
{0x0001, 14, 0x01, usbUsageL},
{0x0006, 14, 0x80, usbUsage5}, // for S[3:0]=2 and S[3:0]=4, 6=2+4
{0x0006, 14, 0x40, usbUsageE},
{0x0006, 14, 0x20, usbUsageN},
{0x0006, 14, 0x10, usbUsageW},
{0x0008, 14, 0x80, usbUsageZ}, // for David's keypad
{0x0008, 14, 0x40, usbUsageX}, // for David's keypad
{0x0008, 14, 0x20, usbUsageC}, // for David's keypad
{0x0008, 14, 0x10, usbUsageV}, // for David's keypad
{0x0008, 14, 0x08, usbUsageB}, // for David's keypad
{0x0008, 14, 0x04, usbUsageN}, // for David's keypad
{0x0008, 14, 0x02, usbUsageM}, // for David's keypad
{0x0008, 14, 0x01, usbUsageLeftControl, 0x01}, // for David's keypad
{0x0001, 13, 0x40, usbUsageComma},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -