1 /*---------------------------------------------------------------------------*
2   Project:  Revolution Low-Level USB keyboard demo
3   File:     kbdLowLevel.c
4 
5   Copyright 2007 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: kbdLowLevel.c,v $
14   Revision 1.6  2007/07/13 23:37:18  carlmu
15   Changed from TrySetLeds to KBDSetLedsRetry.
16 
17   Revision 1.5  2007/06/21 22:46:50  carlmu
18   Updated for KBD 1.4 API (changed LED handling).
19 
20   Revision 1.4  2007/05/05 01:56:57  carlmu
21   Changes for 0.8 API
22 
23   Revision 1.3  2007/04/10 18:34:05  carlmu
24   Changed for compatibility with 0.4 API.
25 
26   Revision 1.2  2007/04/02 19:12:17  carlmu
27   Set callback functions modified for new return value.
28 
29   Revision 1.1  2007/03/21 18:04:57  carlmu
30   Initial version.
31 
32   $NoKeywords: $
33  *---------------------------------------------------------------------------*/
34 
35 /*
36   Example simple keyboard application to output the low-level API HID code
37   and the high-level API unicode to the console.
38  */
39 
40 #include <demo.h>
41 #include <revolution/kbd.h>
42 
43 //-----------------------------------------------------------------------------
44 
45 #define KBD_CALL(_fn_call)                         \
46   if ((_fn_call) != KBD_SUCCESS) {                 \
47     OSReport ("KBD error: calling %s @ %s:%i\n",   \
48               #_fn_call, __FILE__, __LINE__);      \
49   }
50 
51 //-----------------------------------------------------------------------------
52 
53 static void
kbdAppAttach(KBDDevEvent * kde)54 kbdAppAttach (KBDDevEvent *kde) {
55   OSReport ("kbd app: keyboard added on channel %d\n", kde->channel);
56   // for demo purposes, enable NumLock on newly attached keyboard
57   KBDSetModState(kde->channel, KBD_MS_NUM_LOCK);
58   KBDSetLedsRetry(kde->channel, KBD_LED_NUM_LOCK);
59 }
60 
61 
62 static void
kbdAppDetach(KBDDevEvent * kde)63 kbdAppDetach (KBDDevEvent *kde) {
64   OSReport ("kbd app: keyboard removed on channel %d\n", kde->channel);
65 }
66 
67 
68 static void
kbdAppKeyEvent(KBDKeyEvent * kke)69 kbdAppKeyEvent (KBDKeyEvent *kke) {
70   if (KBD_KEY_MODE_REPEAT(kke->mode)) {
71     return;
72   }
73 
74   if (KBD_KEY_MODE_UP(kke->mode))
75     OSReport ("\t\t\thid code: 0x%02x ^ up (chan %d)\n", kke->hid, kke->channel);
76   else
77     OSReport ("hid code: 0x%02x v down (chan %d)\n", kke->hid, kke->channel);
78 
79   // check for CapsLock, NumLock, or ScrollLock (handle LEDs)
80   if (kke->hid == KBD_HID_Caps_Lock ||
81       kke->hid == KBD_HID_Keypad_Num_Lock ||
82       kke->unicode == KBK_Scroll_Lock) {
83 
84       KBDModState ms;
85       KBDLedState leds;
86 
87       // First compute the new LED state
88       KBDGetModState(kke->channel, &ms);
89 
90       leds = (KBDLedState)
91 	  (((ms & KBD_MS_NUM_LOCK) == KBD_MS_NUM_LOCK) * KBD_LED_NUM_LOCK |
92 	   ((ms & KBD_MS_CAPS_LOCK) == KBD_MS_CAPS_LOCK) * KBD_LED_CAPS_LOCK |
93 	   ((ms & KBD_MS_SCROLL_LOCK) == KBD_MS_SCROLL_LOCK) * KBD_LED_SCROLL_LOCK);
94 
95       KBDSetLedsRetry(kke->channel, leds);
96   }
97 }
98 
main(void)99 int main(void) {
100   DEMOInit (NULL);
101 
102   OSReport("\n\n");
103   OSReport("************************************************\n");
104   OSReport("kbdLowLevel Low-level keyboard demo\n");
105   OSReport("************************************************\n");
106   OSReport("Attach a USB keyboard and press some keys.\n");
107   OSReport("\n");
108 
109   // One of these MUST be called before calling KBDInit.
110   // It's okay to call more than one.
111   KBDInitRegionUS();
112   KBDInitRegionJP();
113   KBDInitRegionEU();
114 
115   // It's important to set the callbacks before calling KBDInit.
116   KBDSetAttachCallback(kbdAppAttach);
117   KBDSetDetachCallback(kbdAppDetach);
118   KBDSetKeyCallback(kbdAppKeyEvent);
119 
120   KBD_CALL (KBDInit());
121 
122   while(1) {
123     OSYieldThread();
124   }
125 
126   OSShutdownSystem(); // Never reached
127 
128   return 0;
129 }
130 
131 
132