/*---------------------------------------------------------------------------* Project: Revolution Low-Level USB keyboard demo File: kbdLowLevel.c Copyright 2007 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: kbdLowLevel.c,v $ Revision 1.6 2007/07/13 23:37:18 carlmu Changed from TrySetLeds to KBDSetLedsRetry. Revision 1.5 2007/06/21 22:46:50 carlmu Updated for KBD 1.4 API (changed LED handling). Revision 1.4 2007/05/05 01:56:57 carlmu Changes for 0.8 API Revision 1.3 2007/04/10 18:34:05 carlmu Changed for compatibility with 0.4 API. Revision 1.2 2007/04/02 19:12:17 carlmu Set callback functions modified for new return value. Revision 1.1 2007/03/21 18:04:57 carlmu Initial version. $NoKeywords: $ *---------------------------------------------------------------------------*/ /* Example simple keyboard application to output the low-level API HID code and the high-level API unicode to the console. */ #include #include //----------------------------------------------------------------------------- #define KBD_CALL(_fn_call) \ if ((_fn_call) != KBD_SUCCESS) { \ OSReport ("KBD error: calling %s @ %s:%i\n", \ #_fn_call, __FILE__, __LINE__); \ } //----------------------------------------------------------------------------- static void kbdAppAttach (KBDDevEvent *kde) { OSReport ("kbd app: keyboard added on channel %d\n", kde->channel); // for demo purposes, enable NumLock on newly attached keyboard KBDSetModState(kde->channel, KBD_MS_NUM_LOCK); KBDSetLedsRetry(kde->channel, KBD_LED_NUM_LOCK); } static void kbdAppDetach (KBDDevEvent *kde) { OSReport ("kbd app: keyboard removed on channel %d\n", kde->channel); } static void kbdAppKeyEvent (KBDKeyEvent *kke) { if (KBD_KEY_MODE_REPEAT(kke->mode)) { return; } if (KBD_KEY_MODE_UP(kke->mode)) OSReport ("\t\t\thid code: 0x%02x ^ up (chan %d)\n", kke->hid, kke->channel); else OSReport ("hid code: 0x%02x v down (chan %d)\n", kke->hid, kke->channel); // check for CapsLock, NumLock, or ScrollLock (handle LEDs) if (kke->hid == KBD_HID_Caps_Lock || kke->hid == KBD_HID_Keypad_Num_Lock || kke->unicode == KBK_Scroll_Lock) { KBDModState ms; KBDLedState leds; // First compute the new LED state KBDGetModState(kke->channel, &ms); leds = (KBDLedState) (((ms & KBD_MS_NUM_LOCK) == KBD_MS_NUM_LOCK) * KBD_LED_NUM_LOCK | ((ms & KBD_MS_CAPS_LOCK) == KBD_MS_CAPS_LOCK) * KBD_LED_CAPS_LOCK | ((ms & KBD_MS_SCROLL_LOCK) == KBD_MS_SCROLL_LOCK) * KBD_LED_SCROLL_LOCK); KBDSetLedsRetry(kke->channel, leds); } } int main(void) { DEMOInit (NULL); OSReport("\n\n"); OSReport("************************************************\n"); OSReport("kbdLowLevel Low-level keyboard demo\n"); OSReport("************************************************\n"); OSReport("Attach a USB keyboard and press some keys.\n"); OSReport("\n"); // One of these MUST be called before calling KBDInit. // It's okay to call more than one. KBDInitRegionUS(); KBDInitRegionJP(); KBDInitRegionEU(); // It's important to set the callbacks before calling KBDInit. KBDSetAttachCallback(kbdAppAttach); KBDSetDetachCallback(kbdAppDetach); KBDSetKeyCallback(kbdAppKeyEvent); KBD_CALL (KBDInit()); while(1) { OSYieldThread(); } OSShutdownSystem(); // Never reached return 0; }