Using the USB Keyboard

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Using the USB Keyboard

Post 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 =)
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

Post by Mega Man »

As far as I remember the module "fileXio.irx" needs to be loaded before the keyboard driver.
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post 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???
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post 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. =)
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post 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;
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
User avatar
Lukasz
Posts: 248
Joined: Mon Jan 19, 2004 8:37 pm
Location: Denmark
Contact:

Post 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 :-)
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post 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...
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
User avatar
Lukasz
Posts: 248
Joined: Mon Jan 19, 2004 8:37 pm
Location: Denmark
Contact:

Post 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 :-)
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

Thank you for the example. I'm sure it will be useful when needed.
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post by whatisdot »

How painfully obvious...
The default keymap is ASCII...
heh... =\

http://www.bbdsoft.com/ascii.html
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

When finished I think that would be a good idea to add the example to the SVN repository, don't you think?
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post 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.
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

Great.
But about the SVN, my suggestion was to the repository admins, since only they have "commit" rights.
User avatar
whatisdot
Posts: 43
Joined: Mon Apr 07, 2008 8:43 am
Location: Purdue University, USA

Post by whatisdot »

oh...heh...of course...
--------------------------------------------------------------
"A witty saying proves nothing."
--------------------------------------------------------------
User avatar
Lukasz
Posts: 248
Joined: Mon Jan 19, 2004 8:37 pm
Location: Denmark
Contact:

Post 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.
TracerH
Posts: 5
Joined: Sun Feb 24, 2008 8:55 pm

Post by TracerH »

Thanks for the example, whatisdot. It will come in very handy.
NoobWithBoobs
Posts: 23
Joined: Wed Jul 16, 2008 11:11 pm

Post 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.
Post Reply