📄 usrusbkbdinit.c
字号:
/* plugs the name /usbKb/x into the kbdName variable */ sprintf (kbdName, "%s%d", USB_KBD_NAME, kbdCount); /* Create the keyboard device, do all the ios stuff (drvInstall, * devAdd...) */ if (usbKbdDevCreate (kbdName, pChan) != OK) { printf("usbKbdDevCreate() returned ERROR\n"); } /* now we can increment the counter */ kbdCount++; if (usbKbdDevFind (pChan, (USB_KBD_DEV *) &usbKbdDev) != OK) { printf("usbKbdDevFind() returned ERROR\n"); return; } printf("USB Keyboard attached as %s\n", kbdName); } } else if (attachCode == USB_KBD_REMOVE) { /* find the related device */ if (usbKbdDevFind (pChan, (USB_KBD_DEV *) &usbKbdDev) != OK) { printf ("usbKbdDevFind could not find channel 0x%d", pChan); return; } /* delete the device */ usbKbdDevDelete ((USB_KBD_DEV *) usbKbdDev); if (usbKeyboardSioChanUnlock (pChan) != OK) { printf("usbKeyboardSioChanUnlock () returned ERROR\n"); return; } sprintf (kbdName, "%s%d", USB_KBD_NAME, kbdCount-1); printf ("USB Keyboard %s removed\n", kbdName); } }/********************************************************************************* usbKbdOpen - open a usbKbdDrv serial device.** Increments a counter that holds the number of open paths to device. */LOCAL int usbKbdOpen ( USB_KBD_DEV * pUsbKbdDev, /* keyboard device to read from */ char * name, /* device name */ int flags, /* flags */ int mode /* mode selected */ ) { pUsbKbdDev->numOpen++; /* increment number of open paths */ sioIoctl (pUsbKbdDev->pSioChan, SIO_OPEN, NULL); return ((int) pUsbKbdDev); }/********************************************************************************* usbKbdDrvUnInit - shuts down an I/O USB keyboard driver*** This is supplied to for the user, but it should be noted that iosDrvRemove()* may cause unpredictable results.**/STATUS usbKbdDrvUnInit (void) { if (!usbKbdDrvNum) return (OK); /* remove the driver */ if (iosDrvRemove (usbKbdDrvNum, TRUE) != OK) { printf("iosDrvRemove () returned ERROR\n"); return (ERROR); } usbKbdDrvNum = 0; /* delete the mutex semaphores */ if (semDelete (usbKbdMutex) == ERROR) { printf("semDelete (usbKbdMutex) returned ERROR\n"); return (ERROR); } if (semDelete (usbKbdListMutex) == ERROR) { printf("semDelete (usbKbdListMutex) returned ERROR\n"); return (ERROR); } /* unregister */ if (usbKeyboardDynamicAttachUnRegister (kbdAttachCallback, NULL) != OK) { printf("usbKeyboardDynamicAttachUnRegister () returned ERROR\n"); return (ERROR); } /* shutdown */ if (usbKeyboardDevShutdown () != OK) { printf("usbKeyboardDynamicAttachUnRegister () returned ERROR\n"); return (ERROR); } return (OK); }/********************************************************************************* usbKbdClose - close a usbKbdDrv serial device.** Decrements the counter of open paths to device and alerts the driver * with an ioctl call when the count reaches zero. This scheme is used to* implement the HUPCL(hang up on last close). */LOCAL int usbKbdClose ( USB_KBD_DEV * pUsbKbdDev /* keyboard device to read from */ ) { /* if there are no open channels */ if (!(--pUsbKbdDev->numOpen)) { sioIoctl (pUsbKbdDev->pSioChan, SIO_HUP, NULL); } return ((int) pUsbKbdDev); }/********************************************************************************* usbKbdIoctl - issue device control commands** This routine issues device control commands to an USB keyboard device.** RETURNS: depends on the function invoked.*/LOCAL int usbKbdIoctl ( USB_KBD_DEV * pUsbKbdDev, /* keyboard device to read from */ int request, /* request code */ void * arg /* some argument */ ) { return (sioIoctl (pUsbKbdDev->pSioChan, request, arg)); }/********************************************************************************* usbKbdRead - read from the USB keyboard** This routines is a no-op for a read from the USB keyboard.** RETURNS: OK, always.*/LOCAL int usbKbdRead ( USB_KBD_DEV * pUsbKbdDev, /* keyboard device to read from */ UCHAR *buffer, UINT32 nBytes ) { int status = OK; /* holder for the return value */ return (pUsbKbdDev->pSioChan->pDrvFuncs->pollInput (pUsbKbdDev->pSioChan, buffer)); }/********************************************************************************* usbKbdWrite - write to the USB keyboard** * USB keyboards don't like to be written to. We don't support this feature.** RETURNS: ENOSYS*/int usbKbdWrite ( USB_KBD_DEV * pUsbKbdDev, /* keyboard device to read from */ UCHAR * buffer, /* buffer of data to write */ UINT32 nBytes /* number of bytes in buffer */ ) { return (ENOSYS); }/*************************************************************************** usbKbdTxCallback - feeds characters to USB keyboard SIO driver** USB keyboards don't like to be written to. The don't support this feature.** RETURNS: OK*/LOCAL STATUS usbKbdTxCallback ( void *callbackParam, char *txChar ) { return ENOSYS; }/******************************************************************************* usrUsbKbdInit - initialize the USB keyboard driver** This function initializes the USB keyboard driver and registers for attach * callbacks. ** RETURNS: Nothing */void usrUsbKbdInit (void) { /* Check if driver already installed */ if (usbKbdDrvNum > 0) { printf ("Keyboard already initilaized.\n"); } if (usbKeyboardDevInit () == OK) { printf ("usbKeyboardDevInit() returned OK\n"); if (usbKeyboardDynamicAttachRegister (kbdAttachCallback, (void *) NULL) != OK) { printf ("usbKeyboardDynamicAttachRegister() returned ERROR\n"); } } else printf ("usbKeyboardDevInit() returned ERROR\n"); }/*************************************************************************** enterThread - waits for user to press [enter]** RETURNS: N/A*/LOCAL void enterThread ( void ) { char bfr [256]; printf ("Press [enter] to terminate polling.\n"); gets (bfr); enterPressed = TRUE; }/*************************************************************************** playWithKeyboard - prints key presses to the terminal** RETURNS: OK or ERROR*/UINT16 playWithKeyboard ( void ) { int taskId; char inChar; int fd = 0; /* Poll for input or until user presses CTRL-Z on USB keyboard or * [enter] on main keyboard. */ if ((fd = open ("/usbKb/0", 2,0)) == ERROR) return (ERROR); /* Create thread to watch for keypress */ enterPressed = FALSE; if((taskId = taskSpawn ("tEnter", 5, /* priority */ 0, /* task options */ 0x4000, /* 16k stack space */ (FUNCPTR) enterThread, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )) ==ERROR) { printf(" TaskSpawn Error...!\n"); return ERROR; } printf ("Press CTRL-Z to terminate polling.\n"); while (!enterPressed) { if (read (fd, &inChar, 1) == OK) { printf ("ASCII %3d", inChar); if (inChar >= 32) printf (" '%c'", inChar); printf ("\n"); if (inChar == CTRL_Z) { printf ("Stopped by CTRL-Z\n"); break; } } taskDelay (2); } taskDelete (taskId);pipe_done: return OK; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -