usbhhidkeyboard.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. //*****************************************************************************
  2. //
  3. // usbhhidkeyboard.c - This file holds the application interfaces for USB
  4. // keyboard devices.
  5. //
  6. // Copyright (c) 2008-2011 Texas Instruments Incorporated. All rights reserved.
  7. // Software License Agreement
  8. //
  9. // Texas Instruments (TI) is supplying this software for use solely and
  10. // exclusively on TI's microcontroller products. The software is owned by
  11. // TI and/or its suppliers, and is protected under applicable copyright
  12. // laws. You may not combine this software with "viral" open-source
  13. // software in order to form a larger program.
  14. //
  15. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  16. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  17. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  19. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  20. // DAMAGES, FOR ANY REASON WHATSOEVER.
  21. //
  22. // This is part of revision 8049 of the Stellaris USB Library.
  23. //
  24. //*****************************************************************************
  25. #include "hw_types.h"
  26. #include "usblib.h"
  27. #include "usbhost.h"
  28. #include "usbhid.h"
  29. #include "usbhhid.h"
  30. #include "usbhhidkeyboard.h"
  31. //*****************************************************************************
  32. //
  33. //! \addtogroup usblib_host_device
  34. //! @{
  35. //
  36. //*****************************************************************************
  37. //*****************************************************************************
  38. //
  39. // Prototypes for local functions.
  40. //
  41. //*****************************************************************************
  42. static unsigned int USBHKeyboardCallback(void *pvCBData,
  43. unsigned int ulEvent,
  44. unsigned int ulMsgParam,
  45. void *pvMsgData);
  46. //*****************************************************************************
  47. //
  48. // The size of a USB keyboard report.
  49. //
  50. //*****************************************************************************
  51. #define USBHKEYB_REPORT_SIZE 8
  52. //*****************************************************************************
  53. //
  54. // These are the flags for the tUSBHKeyboard.ulHIDFlags member variable.
  55. //
  56. //*****************************************************************************
  57. #define USBHKEYB_DEVICE_PRESENT 0x00000001
  58. //*****************************************************************************
  59. //
  60. // This is the structure definition for a keyboard device instance.
  61. //
  62. //*****************************************************************************
  63. typedef struct
  64. {
  65. //
  66. // Global flags for an instance of a keyboard.
  67. //
  68. unsigned int ulHIDFlags;
  69. //
  70. // The applications registered callback.
  71. //
  72. tUSBCallback pfnCallback;
  73. //
  74. // The HID instance pointer for this keyboard instance.
  75. //
  76. unsigned int ulHIDInstance;
  77. //
  78. // NUM_LOCK, CAPS_LOCK, SCROLL_LOCK, COMPOSE or KANA keys.
  79. //
  80. unsigned char ucKeyModSticky;
  81. //
  82. // This is the current state of the keyboard modifier keys.
  83. //
  84. unsigned char ucKeyModState;
  85. //
  86. // This holds the keyboard usage codes for keys that are being held down.
  87. //
  88. unsigned char pucKeyState[6];
  89. //
  90. // This is a local buffer to hold the current HID report that comes up
  91. // from the HID driver layer.
  92. //
  93. unsigned char pucBuffer[USBHKEYB_REPORT_SIZE];
  94. }
  95. tUSBHKeyboard;
  96. //*****************************************************************************
  97. //
  98. // This is the per instance information for a keyboard device.
  99. //
  100. //*****************************************************************************
  101. static tUSBHKeyboard g_sUSBHKeyboard =
  102. {
  103. 0
  104. };
  105. //*****************************************************************************
  106. //
  107. //! This function is used open an instance of a keyboard.
  108. //!
  109. //! \param pfnCallback is the callback function to call when new events occur
  110. //! with the keyboard returned.
  111. //! \param pucBuffer is the memory used by the keyboard to interact with the
  112. //! USB keyboard.
  113. //! \param ulSize is the size of the buffer provided by \e pucBuffer.
  114. //!
  115. //! This function is used to open an instance of the keyboard. The value
  116. //! returned from this function should be used as the instance identifier for
  117. //! all other USBHKeyboard calls. The \e pucBuffer memory buffer is used to
  118. //! access the keyboard. The buffer size required is at least enough to hold
  119. //! a normal report descriptor for the device. If there is not enough space
  120. //! only a partial report descriptor will be read out.
  121. //!
  122. //! \return Returns the instance identifier for the keyboard that is attached.
  123. //! If there is no keyboard present this will return 0.
  124. //
  125. //*****************************************************************************
  126. unsigned int
  127. USBHKeyboardOpen(unsigned int ulIndex, tUSBCallback pfnCallback, unsigned char *pucBuffer,
  128. unsigned int ulSize)
  129. {
  130. //
  131. // Save the callback and data pointers.
  132. //
  133. g_sUSBHKeyboard.pfnCallback = pfnCallback;
  134. //
  135. // Save the instance pointer for the HID device that was opened.
  136. //
  137. g_sUSBHKeyboard.ulHIDInstance = USBHHIDOpen(ulIndex, USBH_HID_DEV_KEYBOARD,
  138. USBHKeyboardCallback,
  139. (unsigned int)&g_sUSBHKeyboard);
  140. return(g_sUSBHKeyboard.ulHIDInstance);
  141. }
  142. //*****************************************************************************
  143. //
  144. //! This function is used close an instance of a keyboard.
  145. //!
  146. //! \param ulInstance is the instance value for this keyboard.
  147. //!
  148. //! This function is used to close an instance of the keyboard that was opened
  149. //! with a call to USBHKeyboardOpen(). The \e ulInstance value is the value
  150. //! that was returned when the application called USBHKeyboardOpen().
  151. //!
  152. //! \return This function returns 0 to indicate success any non-zero value
  153. //! indicates an error condition.
  154. //
  155. //*****************************************************************************
  156. unsigned int
  157. USBHKeyboardClose(unsigned int ulInstance)
  158. {
  159. tUSBHKeyboard *pUSBHKeyboard;
  160. tHIDInstance *pHIDInstance;
  161. pHIDInstance = (tHIDInstance *) ulInstance;
  162. pUSBHKeyboard = (tUSBHKeyboard *) pHIDInstance->ulCBData;
  163. //
  164. // Recover the pointer to the instance data.
  165. //
  166. pUSBHKeyboard = (tUSBHKeyboard *)ulInstance;
  167. //
  168. // Reset the callback to null.
  169. //
  170. pUSBHKeyboard->pfnCallback = 0;
  171. //
  172. // Call the HID driver layer to close out this instance.
  173. //
  174. USBHHIDClose(pUSBHKeyboard->ulHIDInstance);
  175. return(0);
  176. }
  177. //*****************************************************************************
  178. //
  179. //! This function is used to map a USB usage ID to a printable character.
  180. //!
  181. //! \param ulInstance is the instance value for this keyboard.
  182. //! \param pTable is the table to use to map the usage ID to characters.
  183. //! \param ucUsageID is the USB usage ID to map to a character.
  184. //!
  185. //! This function is used to map a USB usage ID to a character. The provided
  186. //! \e pTable is used to perform the mapping and is described by the
  187. //! tHIDKeyboardUsageTable type defined structure. See the documentation on
  188. //! the tHIDKeyboardUsageTable structure for more details on the internals of
  189. //! this structure. This function uses the current state of the shift keys
  190. //! and the Caps Lock key to modify the data returned by this function. The
  191. //! pTable structure has values indicating which keys are modified by Caps Lock
  192. //! and alternate values for shifted cases. The number of bytes returned from
  193. //! this function depends on the \e pTable structure passed in as it holds the
  194. //! number of bytes per character in the table.
  195. //!
  196. //! \return Returns the character value for the given usage id.
  197. //
  198. //*****************************************************************************
  199. unsigned int
  200. USBHKeyboardUsageToChar(unsigned int ulInstance,
  201. const tHIDKeyboardUsageTable *pTable,
  202. unsigned char ucUsageID)
  203. {
  204. unsigned int ulValue;
  205. const unsigned char *pucKeyBoardMap;
  206. const unsigned short *pusKeyBoardMap;
  207. unsigned int ulOffset;
  208. unsigned int ulShift;
  209. tUSBHKeyboard *pUSBHKeyboard;
  210. tHIDInstance *pHIDInstance;
  211. //
  212. // Recover the pointer to the instance data.
  213. //
  214. pHIDInstance = (tHIDInstance *) ulInstance;
  215. pUSBHKeyboard = (tUSBHKeyboard *) pHIDInstance->ulCBData;
  216. //
  217. // The added offset for the shifted character value.
  218. //
  219. ulShift = 0;
  220. //
  221. // Offset in the table for the character.
  222. //
  223. ulOffset = (ucUsageID * pTable->ucBytesPerChar * 2);
  224. //
  225. // Handle the case where CAPS lock has been set.
  226. //
  227. if(pUSBHKeyboard->ucKeyModSticky &= HID_KEYB_CAPS_LOCK)
  228. {
  229. //
  230. // See if this usage ID is modified by Caps Lock by checking the packed
  231. // bit array in the pulShiftState member of the pTable array.
  232. //
  233. if((pTable->pulCapsLock[ucUsageID >> 5]) >> (ucUsageID & 0x1f) & 1)
  234. {
  235. ulShift = pTable->ucBytesPerChar;
  236. }
  237. }
  238. //
  239. // Now handle if a shift key is being held.
  240. //
  241. if((pUSBHKeyboard->ucKeyModState & 0x22) != 0)
  242. {
  243. //
  244. // Not shifted yet so we need to shift.
  245. //
  246. if(ulShift == 0)
  247. {
  248. ulShift = pTable->ucBytesPerChar;
  249. }
  250. else
  251. {
  252. //
  253. // Unshift because CAPS LOCK and shift were presed.
  254. //
  255. ulShift = 0;
  256. }
  257. }
  258. //
  259. // One byte per character.
  260. //
  261. if(pTable->ucBytesPerChar == 1)
  262. {
  263. //
  264. // Get the base address of the table.
  265. //
  266. pucKeyBoardMap = pTable->pCharMapping;
  267. ulValue = pucKeyBoardMap[ulOffset + ulShift];
  268. }
  269. //
  270. // Two bytes per character.
  271. //
  272. else if(pTable->ucBytesPerChar == 2)
  273. {
  274. //
  275. // Get the base address of the table.
  276. //
  277. pusKeyBoardMap = (unsigned short *)pTable->pCharMapping;
  278. ulValue = pusKeyBoardMap[ulOffset + ulShift];
  279. }
  280. //
  281. // All other sizes are unsupported for now.
  282. //
  283. else
  284. {
  285. ulValue = 0;
  286. }
  287. return(ulValue);
  288. }
  289. //*****************************************************************************
  290. //
  291. //! This function is used to set one of the fixed modifier keys on a keyboard.
  292. //!
  293. //! \param ulInstance is the instance value for this keyboard.
  294. //! \param ulModifiers is a bit mask of the modifiers to set on the keyboard.
  295. //!
  296. //! This function is used to set the modifier key states on a keyboard. The
  297. //! \e ulModifiers value is a bitmask of the following set of values:
  298. //! - HID_KEYB_NUM_LOCK
  299. //! - HID_KEYB_CAPS_LOCK
  300. //! - HID_KEYB_SCROLL_LOCK
  301. //! - HID_KEYB_COMPOSE
  302. //! - HID_KEYB_KANA
  303. //!
  304. //! Not all of these will be supported on all keyboards however setting values
  305. //! on a keyboard that does not have them should have no effect. The
  306. //! \e ulInstance value is the value that was returned when the application
  307. //! called USBHKeyboardOpen(). If the value \b HID_KEYB_CAPS_LOCK is used it
  308. //! will modify the values returned from the USBHKeyboardUsageToChar()
  309. //! function.
  310. //!
  311. //! \return This function returns 0 to indicate success any non-zero value
  312. //! indicates an error condition.
  313. //
  314. //*****************************************************************************
  315. unsigned int
  316. USBHKeyboardModifierSet(unsigned int ulInstance, unsigned int ulModifiers)
  317. {
  318. tUSBHKeyboard *pUSBHKeyboard;
  319. tHIDInstance *pHIDInstance;
  320. //
  321. // Recover the pointer to the instance data.
  322. //
  323. pHIDInstance = (tHIDInstance *) ulInstance;
  324. pUSBHKeyboard = (tUSBHKeyboard *) pHIDInstance->ulCBData;
  325. //
  326. // Remeber the fact that this is set.
  327. //
  328. pUSBHKeyboard->ucKeyModSticky = (unsigned char)ulModifiers;
  329. //
  330. // Set the LEDs on the keyboard.
  331. //
  332. USBHHIDSetReport(pUSBHKeyboard->ulHIDInstance, 0,
  333. (unsigned char *)&ulModifiers, 1);
  334. return(0);
  335. }
  336. //*****************************************************************************
  337. //
  338. //! This function is used to initialize a keyboard interface after a keyboard
  339. //! has been detected.
  340. //!
  341. //! \param ulInstance is the instance value for this keyboard.
  342. //!
  343. //! This function should be called after receiving a \b USB_EVENT_CONNECTED
  344. //! event in the callback function provided by USBHKeyboardOpen(), however this
  345. //! function should only be called outside the callback function. This will
  346. //! initialize the keyboard interface and determine the keyboard's
  347. //! layout and how it reports keys to the USB host controller. The
  348. //! \e ulInstance value is the value that was returned when the application
  349. //! called USBHKeyboardOpen(). This function only needs to be called once
  350. //! per connection event but it should be called every time a
  351. //! \b USB_EVENT_CONNECTED event occurs.
  352. //!
  353. //! \return This function returns 0 to indicate success any non-zero value
  354. //! indicates an error condition.
  355. //
  356. //*****************************************************************************
  357. unsigned int
  358. USBHKeyboardInit(unsigned int ulInstance)
  359. {
  360. unsigned char ucModData;
  361. tUSBHKeyboard *pUSBHKeyboard;
  362. tHIDInstance *pHIDInstance;
  363. //
  364. // Recover the pointer to the instance data.
  365. //
  366. pHIDInstance = (tHIDInstance *) ulInstance;
  367. pUSBHKeyboard = (tUSBHKeyboard *) pHIDInstance->ulCBData;
  368. //
  369. // Set the initial rate to only update on keyboard state changes.
  370. //
  371. USBHHIDSetIdle(pUSBHKeyboard->ulHIDInstance, 0, 0);
  372. //
  373. // Read out the Report Descriptor from the keyboard and parse it for
  374. // the format of the reports coming back from the keyboard.
  375. //
  376. USBHHIDGetReportDescriptor(pUSBHKeyboard->ulHIDInstance,
  377. pUSBHKeyboard->pucBuffer,
  378. USBHKEYB_REPORT_SIZE);
  379. //
  380. // Set the keyboard to boot protocol.
  381. //
  382. USBHHIDSetProtocol(pUSBHKeyboard->ulHIDInstance, 1);
  383. //
  384. // Used to clear the initial state of all on keyboard modifiers.
  385. //
  386. ucModData = 0;
  387. //
  388. // Update the keyboard LED state.
  389. //
  390. USBHHIDSetReport(pUSBHKeyboard->ulHIDInstance, 0, &ucModData, 1);
  391. return(0);
  392. }
  393. //*****************************************************************************
  394. //
  395. //! This function is used to set the automatic poll rate of the keyboard.
  396. //!
  397. //! \param ulInstance is the instance value for this keyboard.
  398. //! \param ulPollRate is the rate in ms to cause the keyboard to update the
  399. //! host regardless of no change in key state.
  400. //!
  401. //! This function will allow an application to tell the keyboard how often it
  402. //! should send updates to the USB host controller regardless of any changes
  403. //! in keyboard state. The \e ulInstance value is the value that was returned
  404. //! when the application called USBHKeyboardOpen(). The \e ulPollRate is the
  405. //! new value in ms for the update rate on the keyboard. This value is
  406. //! initially set to 0 which indicates that the keyboard should only to update
  407. //! when the keyboard state changes. Any value other than 0 can be used to
  408. //! force the keyboard to generate auto-repeat sequences for the application.
  409. //!
  410. //! \return This function returns 0 to indicate success any non-zero value
  411. //! indicates an error condition.
  412. //
  413. //*****************************************************************************
  414. unsigned int
  415. USBHKeyboardPollRateSet(unsigned int ulInstance, unsigned int ulPollRate)
  416. {
  417. tUSBHKeyboard *pUSBHKeyboard;
  418. tHIDInstance *pHIDInstance;
  419. //
  420. // Recover the pointer to the instance data.
  421. //
  422. pHIDInstance = (tHIDInstance *) ulInstance;
  423. pUSBHKeyboard = (tUSBHKeyboard *) pHIDInstance->ulCBData;
  424. //
  425. // Send the Set Idle command to the USB keyboard.
  426. //
  427. USBHHIDSetIdle(pUSBHKeyboard->ulHIDInstance, ulPollRate, 0);
  428. return(0);
  429. }
  430. //*****************************************************************************
  431. //
  432. // This is an internal function used to modify the current keyboard state.
  433. //
  434. // This function checks for changes in the keyboard state due to a new report
  435. // being received from the device. It first checks if this is a "roll-over"
  436. // case by seeing if 0x01 is in the first position of the new keyboard report.
  437. // This indicates that too many keys were pressed to handle and to ignore this
  438. // report. Next the keyboard modifier state is stored and if any changes are
  439. // detected a \b USBH_EVENT_HID_KB_MOD event is sent back to the application.
  440. // Then this function will check for any keys that have been released and send
  441. // a \b USBH_EVENT_HID_KB_REL even for each of these keys. The last check is
  442. // for any new keys that are pressed and a \b USBH_EVENT_HID_KB_PRESS event
  443. // will be sent for each new key pressed.
  444. //
  445. // \return None.
  446. //
  447. //*****************************************************************************
  448. static void
  449. UpdateKeyboardState(tUSBHKeyboard *pUSBHKeyboard)
  450. {
  451. int lNewKey, lOldKey;
  452. //
  453. // rollover code so ignore this buffer.
  454. //
  455. if(pUSBHKeyboard->pucBuffer[2] == 0x01)
  456. {
  457. return;
  458. }
  459. //
  460. // Handle the keyboard modifier states.
  461. //
  462. if(pUSBHKeyboard->ucKeyModState != pUSBHKeyboard->pucBuffer[0])
  463. {
  464. //
  465. // Notify the application of the event.
  466. //
  467. pUSBHKeyboard->pfnCallback(0, USBH_EVENT_HID_KB_MOD,
  468. pUSBHKeyboard->pucBuffer[0], 0);
  469. //
  470. // Save the new state of the modifier keys.
  471. //
  472. pUSBHKeyboard->ucKeyModState = pUSBHKeyboard->pucBuffer[0];
  473. }
  474. //
  475. // This loop checks for keys that have been released to make room for new
  476. // ones that may have been pressed.
  477. //
  478. for(lOldKey = 2; lOldKey < 8; lOldKey++)
  479. {
  480. //
  481. // If there is no old key pressed in this entry go to the next one.
  482. //
  483. if(pUSBHKeyboard->pucKeyState[lOldKey] == 0)
  484. {
  485. continue;
  486. }
  487. //
  488. // Check if this old key is still in the list of currently pressed
  489. // keys.
  490. //
  491. for(lNewKey = 2; lNewKey < 8; lNewKey++)
  492. {
  493. //
  494. // Break out if the key is still present.
  495. //
  496. if(pUSBHKeyboard->pucBuffer[lNewKey]
  497. == pUSBHKeyboard->pucKeyState[lOldKey])
  498. {
  499. break;
  500. }
  501. }
  502. //
  503. // If the old key was no longer in the list of pressed keys then
  504. // notify the application of the key release.
  505. //
  506. if(lNewKey == 8)
  507. {
  508. //
  509. // Send the key release notification to the application.
  510. //
  511. pUSBHKeyboard->pfnCallback(0,
  512. USBH_EVENT_HID_KB_REL,
  513. pUSBHKeyboard->pucKeyState[lOldKey],
  514. 0);
  515. //
  516. // Remove the old key from the currently held key list.
  517. //
  518. pUSBHKeyboard->pucKeyState[lOldKey] = 0;
  519. }
  520. }
  521. //
  522. // This loop checks for new keys that have been pressed.
  523. //
  524. for(lNewKey = 2; lNewKey < 8; lNewKey++)
  525. {
  526. //
  527. // The new list is empty so no new keys are pressed.
  528. //
  529. if(pUSBHKeyboard->pucBuffer[lNewKey] == 0)
  530. {
  531. break;
  532. }
  533. //
  534. // This loop checks if the current key was already pressed.
  535. //
  536. for(lOldKey = 2; lOldKey < 8; lOldKey++)
  537. {
  538. //
  539. // If it is in both lists then it was already pressed so ignore it.
  540. //
  541. if(pUSBHKeyboard->pucBuffer[lNewKey]
  542. == pUSBHKeyboard->pucKeyState[lOldKey])
  543. {
  544. break;
  545. }
  546. }
  547. //
  548. // The key in the new list was not found so it is new.
  549. //
  550. if(lOldKey == 8)
  551. {
  552. //
  553. // Look for a free location to store this key usage code.
  554. //
  555. for(lOldKey = 2; lOldKey < 8; lOldKey++)
  556. {
  557. //
  558. // If an empty location is found, store it and notify the
  559. // application.
  560. //
  561. if(pUSBHKeyboard->pucKeyState[lOldKey] == 0)
  562. {
  563. //
  564. // Save the newly pressed key.
  565. //
  566. pUSBHKeyboard->pucKeyState[lOldKey]
  567. = pUSBHKeyboard->pucBuffer[lNewKey];
  568. //
  569. // Notify the application of the new key that has been
  570. // pressed.
  571. //
  572. pUSBHKeyboard->pfnCallback(
  573. 0,
  574. USBH_EVENT_HID_KB_PRESS,
  575. pUSBHKeyboard->pucBuffer[lNewKey],
  576. 0);
  577. break;
  578. }
  579. }
  580. }
  581. }
  582. }
  583. //*****************************************************************************
  584. //
  585. //! This function handles event callbacks from the USB HID driver layer.
  586. //!
  587. //! \param pvCBData is the pointer that was passed in to the USBHHIDOpen()
  588. //! call.
  589. //! \param ulEvent is the event that has been passed up from the HID driver.
  590. //! \param ulMsgParam has meaning related to the \e ulEvent that occurred.
  591. //! \param pvMsgData has meaning related to the \e ulEvent that occurred.
  592. //!
  593. //! This function will receive all event updates from the HID driver layer.
  594. //! The keyboard driver itself will mostly be concerned with report callbacks
  595. //! from the HID driver layer and parsing them into keystrokes for the
  596. //! application that has registered for callbacks with the USBHKeyboardOpen()
  597. //! call.
  598. //!
  599. //! \return Non-zero values should be assumed to indicate an error condition.
  600. //
  601. //*****************************************************************************
  602. unsigned int
  603. USBHKeyboardCallback(void *pvCBData, unsigned int ulEvent,
  604. unsigned int ulMsgParam, void *pvMsgData)
  605. {
  606. tUSBHKeyboard *pUSBHKeyboard;
  607. //
  608. // Recover the pointer to the instance data.
  609. //
  610. pUSBHKeyboard = (tUSBHKeyboard *)pvCBData;
  611. switch (ulEvent)
  612. {
  613. //
  614. // New keyboard has been connected so notify the application.
  615. //
  616. case USB_EVENT_CONNECTED:
  617. {
  618. //
  619. // Remember that a keyboard is present.
  620. //
  621. pUSBHKeyboard->ulHIDFlags |= USBHKEYB_DEVICE_PRESENT;
  622. //
  623. // Notify the application that a new keyboard was connected.
  624. //
  625. pUSBHKeyboard->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
  626. break;
  627. }
  628. case USB_EVENT_DISCONNECTED:
  629. {
  630. //
  631. // No keyboard is present.
  632. //
  633. pUSBHKeyboard->ulHIDFlags &= ~USBHKEYB_DEVICE_PRESENT;
  634. //
  635. // Notify the application that the keyboard was disconnected.
  636. //
  637. pUSBHKeyboard->pfnCallback(0, ulEvent, ulMsgParam, pvMsgData);
  638. break;
  639. }
  640. case USB_EVENT_RX_AVAILABLE:
  641. {
  642. //
  643. // New keyboard report structure was received.
  644. //
  645. USBHHIDGetReport(pUSBHKeyboard->ulHIDInstance, 0,
  646. pUSBHKeyboard->pucBuffer,
  647. USBHKEYB_REPORT_SIZE);
  648. //
  649. // Update the application on the changes in the keyboard state.
  650. //
  651. UpdateKeyboardState(pUSBHKeyboard);
  652. break;
  653. }
  654. }
  655. return(0);
  656. }
  657. //*****************************************************************************
  658. //
  659. //! @}
  660. //
  661. //*****************************************************************************