PDA

View Full Version : Keyboard version of gamepad_001 -help



Oldbitcollector (Jeff)
01-22-2008, 10:21 AM
I'm attempting to write a gamepad_001 object that will spit out the correct bits when a key is pressed, but I've run into a little snag. For some reason it doesn't seem to work fast enough to do the job. It works, but movement is jerky.

Would someone who understands the gamepad_001 code take a look at this and tell me why it doesn't seem to move fast enough? I've tried both comboKeyboard & keyboard with the same results.




' Keyboard gamepad_001 driver
' AUTHOR: Jeff Ledger

OBJ
key : "Keyboard" ' Keyboard Driver

PUB start : okay

key.start(26, 27)

PUB stop


PUB read : joy_bits | keystroke

' NES bit encodings -- Provided as reference
' NES_RIGHT = %00000001
' NES_LEFT = %00000010
' NES_DOWN = %00000100
' NES_UP = %00001000
' NES_START = %00010000
' NES_SELECT = %00100000
' NES_B = %01000000
' NES_A = %10000000

joy_bits := %00000000

keystroke := key.key

if keystroke == 122 'Keyboard(Z) = A
joy_bits := %10000000

if keystroke == 120 'Keyboard(X) = B
joy_bits := %01000000

if keystroke == 115 'Keyboard(S) = Select
joy_bits := %00100000

if keystroke == 32 'Keyboard(SPACE) = Start
joy_bits := %00010000

if keystroke == 193 'Right Arrow on Keyboard
joy_bits := %00000001

if keystroke == 192 'Left Arrow on Keyboard
joy_bits := %00000010

if keystroke == 195 'Down Arrow on Keyboard
joy_bits := %00000100

if keystroke == 194 'Up Arrow on Keyboard
joy_bits := %00001000


PUB button(WhichOne)
{{ Return value: true or false }}
if WhichOne == read
return true
else
return false


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?

Getting started with the Protoboard? - Propeller Cookbook (http://ucontroller.com/Propeller%20Protoboard%20Designs%20for%20the%20Beg inner.pdf)
Got an SD card? - PropDOS (http://www.orrtech.net/propdos/)
A Living Propeller FAQ - The Propeller Wiki (http://propeller.wikispaces.com/)
(Got the Knowledge? Got a Moment? Add something today!)

deSilva
01-22-2008, 11:07 AM
Jeff,
I think the main problem would be that the keyboard sends "keys" with a low repetition rate only. When you "poll" it too fast there will be just no key in the buffer, and KEY.KEY will return zero.

You should use KEY.KEYSTATE(k) to make it behave more like a gamepad.

BTW: Do you know LOOKUP and LOOKDOWN?

Oldbitcollector (Jeff)
01-22-2008, 11:17 AM
Ah, that makes sense... Will try it in the morning!

You've got me back in the book with LOOKUP/LOOKDOWN, not sure how I'd apply it to something like this, but
it does look like it would save a serious amount of code. Thanks!

OBC

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?

Getting started with the Protoboard? - Propeller Cookbook (http://ucontroller.com/Propeller%20Protoboard%20Designs%20for%20the%20Beg inner.pdf)
Got an SD card? - PropDOS (http://www.orrtech.net/propdos/)
A Living Propeller FAQ - The Propeller Wiki (http://propeller.wikispaces.com/)
(Got the Knowledge? Got a Moment? Add something today!)

Paul Baker
01-22-2008, 12:53 PM
Here's what deSilva was talking about:



keystroke := key.key
joybits := lookdown(keystroke: 193, 192, 195, 32, 115, 120, 122)
if not joybits
joybits--
|< joybits
'joybits is now in bitmask form

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker (mailto:pbaker@parallax.com)
Propeller Applications Engineer
[/url][url=http://www.parallax.com] (http://www.parallax.com)
Parallax, Inc. (http://www.parallax.com)

deSilva
01-22-2008, 03:44 PM
http://forums.parallax.com/images/smilies/smile.gif ... and writing:

PUB button(WhichOne)
RETURN WhichOne == read


will make a real short piece of code... But this is not the program you want http://forums.parallax.com/images/smilies/smile.gif

Baggers
01-22-2008, 03:59 PM
Oldbit, look in Manic Miner source or Jetpac source, or Snake source, in there there is my readpad mods, which use keyboard too.

JOYACTIVE is a CON set to 1 if you have one connected, otherwise it'll skip the reading and writing to pins 3-6 ( joypad controller pins )

I've even left in the F9, F10 code that I use to toggle PAL / NTSC on the fly, ( if your using any of my drivers or the original parallax drivers. PS, tvparams[6] is hx PAL is whatever you have NTSC set to plus 2, tvparams is mode PAL=1 NTSC=0 )




PUB Read_Gamepad : bits | i
if JOYACTIVE
dira:=%11000 ' output
outa:=0
outa := 1 ' JOY_SH/LDn = 1
outa := 0 ' JOY_SH/LDn = 0
bits:=0
bits:=ina | (ina[6] << 8)
repeat i from 0 to 6
outa:=1
outa:=0
bits:=(bits<<1) + ( ina | (ina[6] << 8) )
bits:=(!bits & $FFFF)
if bits&$ff==$ff
bits:=bits&$ff00
if bits&$ff00==$ff00
bits:=bits&$ff
else
bits:=0


' Keyboard (mapped onto buttons)
if(key.keystate($C2)) 'Up Arrow
bits|=NES_UP
if(key.keystate($C3)) 'Down Arrow
bits|=NES_DOWN
if(key.keystate($C0)) 'Left Arrow
bits|=NES_LEFT
if(key.keystate($C1)) 'Right Arrow
bits|=NES_RIGHT
if(key.keystate($0D)) 'Enter
bits|=NES_START
if(key.keystate(" ")) 'Space
bits|=NES_A

if key.keystate($d8) 'F9
tvparams:=1
tvparams[6]:=14
if key.keystate($d9) 'F10
tvparams:=0
tvparams[6]:=12

deSilva
01-22-2008, 04:17 PM
... and to exercise the underestimated LOOKUP..


joybits := 0
REPEAT bitNmb FROM 0 TO 7
joybits |= (key.keystate(LOOKUPZ(bitNmb: 193, 192, 195, " ", 115, "X", "Z"))&1)<<bitNmb



Well, maybe I overdid it now...

Baggers
01-22-2008, 04:54 PM
deSilva, I'm not dissing you or your method, btw,

believe me I had thought of that, but for board compatibility, you would also have to have the entries in the correct bit order, it would therefore not be joypad & nespad compatible, unless you had those values in the CON comment section too along with the direction bits, so again, it might look tidy there, but it'd make a larger CON section, hence, I use my rolled out version. ;)

Baggers.

deSilva
01-22-2008, 05:28 PM
Baggers, this was not at all addressed to you but meant as an exercise for everybody.
There are lots of reasons to be more explicite, to make tables, etc.

Baggers
01-22-2008, 06:08 PM
yeah, I know, I like tables they're really handy ;)

Oldbitcollector (Jeff)
01-23-2008, 08:01 AM
Desilva and Jim right on the money!

Works perfectly! Seemed like something that would have already been in obex.
Too Simple?

OBC

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
New to the Propeller?

Getting started with the Protoboard? - Propeller Cookbook (http://ucontroller.com/Propeller%20Protoboard%20Designs%20for%20the%20Beg inner.pdf)
Got an SD card? - PropDOS (http://www.orrtech.net/propdos/)
A Living Propeller FAQ - The Propeller Wiki (http://propeller.wikispaces.com/)
(Got the Knowledge? Got a Moment? Add something today!)