Page 1 of 1

Using the USB Keyboard

Posted: Wed Apr 30, 2008 8:40 am
by whatisdot
Hello again,

I have been messing around with the USB Keyboard and I have been having some problems with getting it to work. If someone could shed some light on this for me, I'll document it in the Wiki, because I have seen a lot of other people with the same problems.
After looking at the code for quake and uLaunchELF, I have tried to diagnose what is the absolute minimum neccessary to get the USB keyboard up and working, but it just won't initialize for some reason.

At first I tried this:

Code: Select all

char *myStr;

PS2KbdInit();
PS2KbdSetReadmode( PS2KBD_READMODE_NORMAL );

while( 1 ) {
.....
}
It didn't seem to initialize, so I did some searching and saw that there were some other things I might need to do in order to get it working. I then tried this:

Code: Select all

	SifInitRpc(0);

	SifExecModuleBuffer(usbd, size_usbd, 0, NULL, &ret);
	SifExecModuleBuffer(ps2kbd, size_ps2kbd, 0, NULL, &ret);

	// Initialize the keyboard library.
	scr_printf("Initializing Keyboard...\n");
	if( PS2KbdInit() == 0 ) {
		scr_printf("Could not initialize keyboard.\n");
	}

	PS2KbdSetReadmode( PS2KBD_READMODE_RAW );
but the darn thing still won't initialize. I'm just trying to get the keyboard working or at least a response from it, but I don't know what is necessary and what is not. Could someone give me a clue???

Thanks in advance =)

Posted: Wed Apr 30, 2008 9:13 am
by Mega Man
As far as I remember the module "fileXio.irx" needs to be loaded before the keyboard driver.

Posted: Wed Apr 30, 2008 9:35 am
by whatisdot
Thanks for the quick response, Mega Man.

After copying "fileXio.irx" and "ps2kbd.irx" from my PS2SDK/iop/irx directory into the directory my compiled program was running in, I changed the code to this:

Code: Select all

	SifLoadModule("host0:fileXio.irx", 0, NULL);
	SifLoadModule("host0:ps2kbd.irx", 0, NULL);

	// Initialize the keyboard library.
	scr_printf("Initializing Keyboard...\n");
	if( PS2KbdInit() == 0 ) {
		scr_printf("Could not initialize keyboard.\n");
	}
The fileXio module loaded correctly, but not the ps2kbd module... Am I not passing the correct arguments???

Posted: Wed Apr 30, 2008 9:53 am
by whatisdot
It's OK, I got it! =D

Here's what I had to do:

Code: Select all

	SifLoadModule("host0:fileXio.irx", 0, NULL);
	SifLoadModule("host0:usbd.irx", 0, NULL);
	SifLoadModule("host0:ps2kbd.irx", 0, NULL);

	// Initialize the keyboard library.
	PS2KbdInit();
Console output, where the name of the file being executed is "keyboard.elf":

Code: Select all

Loaded, host:keyboard.elf
start address 0x1000e0
gp address 00000000
loadmodule: fname host0:fileXio.irx args 0 arg 
fileXio: fileXio RPC Server v1.00
Copyright (c) 2003 adresd
loadmodule: id 34, ret 0
loadmodule: fname host0:usbd.irx args 0 arg 
FreeUsbd v.0.1.2
loadmodule: id 35, ret 0
loadmodule: fname host0:ps2kbd.irx args 0 arg 
PS2KBD - USB Keyboard Library
loadmodule: id 36, ret 0
open name usbkbd:dev flag 1 data 41378
open fd = 2
PS2KBD: Found a keyboard device
PS2KBD: Connected device
It turns out the order the modules are loaded in important. It makes sense in hind sight. FileXio, then USBD, then PS2KBD. I hope this helps anyone else who are curious. =)

Posted: Wed Apr 30, 2008 10:39 am
by whatisdot
Follow up:

Here's a very simple blurb of code that can be used to get simple input from the keyboard and display it on the screen. I'll post this in the "Getting Started" section of the Wiki for future reference.

Makefile

Code: Select all

EE_BIN = keyboard.elf
EE_OBJS = keyboard.o
EE_LIBS = -lkbd -ldebug

all: $(EE_BIN)

clean:
	rm -f *.elf *.o *.a

include $(PS2SDK)/samples/Makefile.pref
include $(PS2SDK)/samples/Makefile.eeglobal
keyboard.c

Code: Select all

#include <tamtypes.h>
#include <stdio.h>
#include <debug.h>
#include <libkbd.h>
#include <loadfile.h>

int main&#40;&#41; &#123;
	char newChar = 0;

	init_scr&#40;&#41;;

	SifLoadModule&#40;"host0&#58;fileXio.irx", 0, NULL&#41;;
	SifLoadModule&#40;"host0&#58;usbd.irx", 0, NULL&#41;;
	SifLoadModule&#40;"host0&#58;ps2kbd.irx", 0, NULL&#41;;

	PS2KbdInit&#40;&#41;;

	PS2KbdSetReadmode&#40; PS2KBD_READMODE_NORMAL &#41;;

	//////Do stuff//////
	scr_printf&#40;"Type something!\n"&#41;;
	while&#40; 1 &#41; &#123;
		while &#40;!PS2KbdRead&#40;&newChar&#41;&#41; ;;;
		scr_printf&#40;"%c", newChar&#41;;
	&#125;
	////////////////////
	// Shut down the keyboard library.
	PS2KbdClose&#40;&#41;;

	// End program.
	return 0;
&#125;

Posted: Wed Apr 30, 2008 9:45 pm
by Lukasz
Normally I wouldn't care, but since you are targeting the USB keyboard example towards beginners, I'd recommend not having the infinite loop and the unreachable PS2KbdClose() afterwards.

I'd just make the example exit with the ESC button or something similar, to avoid confusion :-)

Posted: Thu May 01, 2008 4:37 am
by whatisdot
Is there a list of the default character mapping somewhere? I really hate infinite loops, and I would have done as you said, but I didn't know how. Tthere isn't much info on character mapping with the USB Keyboard. If only there was a reference somewhere...

Posted: Thu May 01, 2008 5:47 am
by Lukasz
Try PS2KbdReadRaw (libkbd.h) and have look inside ps2kbd.h.

I've never used USB keyboard in PS2SDK myself, but digging through headers (and sometimes source) is the way forward with PS2SDK :-)

Posted: Thu May 01, 2008 6:56 am
by cosmito
Thank you for the example. I'm sure it will be useful when needed.

Posted: Thu May 01, 2008 8:13 am
by whatisdot
How painfully obvious...
The default keymap is ASCII...
heh... =\

http://www.bbdsoft.com/ascii.html

Posted: Thu May 01, 2008 8:18 pm
by cosmito
When finished I think that would be a good idea to add the example to the SVN repository, don't you think?

Posted: Fri May 02, 2008 3:04 am
by whatisdot
Yeah, I don't have a lot of experience with Subversion, but I'll look into it. This little adventure has also brought to my attention the need for a few more functions in LIBKBD. I was going to implement some "scanf" type functions that pull straight from the file stream instead of calling "PS2KbdRead()" continuously. LIBKBD right now is useful for detecting key presses, but we need some more versatile functions for keyboard input.

Posted: Fri May 02, 2008 4:24 am
by cosmito
Great.
But about the SVN, my suggestion was to the repository admins, since only they have "commit" rights.

Posted: Fri May 02, 2008 7:17 am
by whatisdot
oh...heh...of course...

Posted: Fri May 02, 2008 4:51 pm
by Lukasz
Contact Oobles for SVN access, otherwise if the patch/addition is minor, you can just post here in the forums and someone will commit it for you.

Posted: Sat May 10, 2008 6:08 am
by TracerH
Thanks for the example, whatisdot. It will come in very handy.

Posted: Sun Aug 03, 2008 4:22 pm
by NoobWithBoobs
could i just add those sets of code u posted above,to a different makefile ,and add keyboard.c to the compiling folder? Was lookin to add keyboard support to a project.Im very new to this,and have been messin around with the different examples.
Only thing is i noticed in the makefile code u posted,it is creating keyboard.elf,so i would have to edit that,i assume.
After tinkerin around with it,i see i need to do more than simply add this to the makefile,lol,not sure what though.