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