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.10.6.1  2009/10/14 10:03:27  iwai_yuma
15   Rool back into StackB.
16 
17   Revision 1.8  2007/10/08 18:44:55  henrch
18   per API change
19 
20   Revision 1.7  2007/10/03 22:48:01  henrch
21   changed to use new KBD / HID lib
22 
23   Revision 1.6  2007/07/13 23:37:18  carlmu
24   Changed from TrySetLeds to KBDSetLedsRetry.
25 
26   Revision 1.5  2007/06/21 22:46:50  carlmu
27   Updated for KBD 1.4 API (changed LED handling).
28 
29   Revision 1.4  2007/05/05 01:56:57  carlmu
30   Changes for 0.8 API
31 
32   Revision 1.3  2007/04/10 18:34:05  carlmu
33   Changed for compatibility with 0.4 API.
34 
35   Revision 1.2  2007/04/02 19:12:17  carlmu
36   Set callback functions modified for new return value.
37 
38   Revision 1.1  2007/03/21 18:04:57  carlmu
39   Initial version.
40 
41   $NoKeywords: $
42  *---------------------------------------------------------------------------*/
43 
44 /*
45   Example simple keyboard application to output the low-level API HID code
46   and the high-level API unicode to the console.
47  */
48 
49 #include <demo.h>
50 #include <revolution/kbd.h>
51 
52 
53 static u8 __kbd_mem[KBD_MEM_SIZE];
54 
55 //-----------------------------------------------------------------------------
56 
57 #define KBD_CALL(_fn_call)                         \
58   if ((_fn_call) != KBD_SUCCESS) {                 \
59     OSReport ("KBD error: calling %s @ %s:%i\n",   \
60               #_fn_call, __FILE__, __LINE__);      \
61   }
62 
63 //-----------------------------------------------------------------------------
64 
65 static void
kbdAppAttach(KBDDevEvent * kde)66 kbdAppAttach (KBDDevEvent *kde) {
67   OSReport ("kbd app: keyboard added on channel %d\n", kde->channel);
68   // for demo purposes, enable NumLock on newly attached keyboard
69   KBDSetModState(kde->channel, KBD_MS_NUM_LOCK);
70   KBDSetLedsRetry(kde->channel, KBD_LED_NUM_LOCK);
71 }
72 
73 
74 static void
kbdAppDetach(KBDDevEvent * kde)75 kbdAppDetach (KBDDevEvent *kde) {
76   OSReport ("kbd app: keyboard removed on channel %d\n", kde->channel);
77 }
78 
79 
80 static void
kbdAppKeyEvent(KBDKeyEvent * kke)81 kbdAppKeyEvent (KBDKeyEvent *kke) {
82   if (KBD_KEY_MODE_REPEAT(kke->mode)) {
83     return;
84   }
85 
86   if (KBD_KEY_MODE_UP(kke->mode))
87     OSReport ("\t\t\thid code: 0x%02x ^ up (chan %d)\n", kke->hid, kke->channel);
88   else
89     OSReport ("hid code: 0x%02x v down (chan %d)\n", kke->hid, kke->channel);
90 
91   // check for CapsLock, NumLock, or ScrollLock (handle LEDs)
92   if (kke->hid == KBD_HID_Caps_Lock ||
93       kke->hid == KBD_HID_Keypad_Num_Lock ||
94       kke->unicode == KBK_Scroll_Lock) {
95 
96       KBDModState ms;
97       KBDLedState leds;
98 
99       // First compute the new LED state
100       KBDGetModState(kke->channel, &ms);
101 
102       leds = (KBDLedState)
103       (((ms & KBD_MS_NUM_LOCK) == KBD_MS_NUM_LOCK) * KBD_LED_NUM_LOCK |
104        ((ms & KBD_MS_CAPS_LOCK) == KBD_MS_CAPS_LOCK) * KBD_LED_CAPS_LOCK |
105        ((ms & KBD_MS_SCROLL_LOCK) == KBD_MS_SCROLL_LOCK) * KBD_LED_SCROLL_LOCK);
106 
107       KBDSetLedsRetry(kke->channel, leds);
108   }
109 }
110 
111 extern void hid_open_async(void);
112 extern void hid_close_async(void);
113 
main(void)114 int main(void)
115 {
116   DEMOInit(NULL);
117 
118   hid_open_async();
119 
120   OSReport("\n\n");
121   OSReport("************************************************\n");
122   OSReport("kbdLowLevel Low-level keyboard demo\n");
123   OSReport("************************************************\n");
124   OSReport("Attach a USB keyboard and press some keys.\n");
125   OSReport("\n");
126 
127   KBD_CALL (KBDInit(&__kbd_mem, kbdAppAttach, kbdAppDetach, kbdAppKeyEvent));
128 
129   // One of these MUST be called after calling KBDInit.
130   // It's okay to call more than one.
131   KBDInitRegionUS();
132   KBDInitRegionJP();
133   KBDInitRegionEU();
134 
135   while(1)
136     OSYieldThread();
137 
138   hid_close_async();
139 
140   OSShutdownSystem(); // Never reached
141 
142   return 0;
143 }
144 
145 
146