Shop OBEX P1 Docs P2 Docs Learn Events
Keyboard version of gamepad_001 -help — Parallax Forums

Keyboard version of gamepad_001 -help

Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
edited 2008-01-23 01:01 in Propeller 1
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
Got an SD card? - PropDOS
A Living Propeller FAQ - The Propeller Wiki
(Got the Knowledge? Got a Moment? Add something today!)

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2008-01-22 04:07
    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)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-01-22 04:17
    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
    Got an SD card? - PropDOS
    A Living Propeller FAQ - The Propeller Wiki
    (Got the Knowledge? Got a Moment? Add something today!)

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-01-22 05:53
    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
    Propeller Applications Engineer

    Parallax, Inc.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-22 08:44
    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 smile.gif
  • BaggersBaggers Posts: 3,019
    edited 2008-01-22 08:59
    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[noparse][[/noparse]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[noparse][[/noparse]6] << 8)
        repeat i from 0 to 6
          outa:=1
          outa:=0
          bits:=(bits<<1) + ( ina | (ina[noparse][[/noparse]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[noparse][[/noparse]6]:=14
      if key.keystate($d9)      'F10
        tvparams:=0
        tvparams[noparse][[/noparse]6]:=12
                  
    
    
    

  • deSilvadeSilva Posts: 2,967
    edited 2008-01-22 09:17
    ... 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...
  • BaggersBaggers Posts: 3,019
    edited 2008-01-22 09:54
    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. [noparse];)[/noparse]

    Baggers.
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-22 10:28
    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.
  • BaggersBaggers Posts: 3,019
    edited 2008-01-22 11:08
    yeah, I know, I like tables they're really handy [noparse];)[/noparse]
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2008-01-23 01:01
    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
    Got an SD card? - PropDOS
    A Living Propeller FAQ - The Propeller Wiki
    (Got the Knowledge? Got a Moment? Add something today!)

Sign In or Register to comment.