Shop OBEX P1 Docs P2 Docs Learn Events
Keyboard input to change event — Parallax Forums

Keyboard input to change event

Kram11Kram11 Posts: 8
edited 2007-11-10 19:28 in Propeller 1
·I am using the propeller starter kit with a keyboard and I want to change an event depending on what key I press i.e Key 1 flash LED1 key 2 Flash LED 5 and 6 etc. And also show the key on·a LCD display.·Can anyone help?

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-11-10 17:06
    - initialize the keyboard driver
    - in a loop wait for a key
    - examine the key:
    -- Key1? Then set OUTA for LED1 and rset OUTA for LED2
    -- Key2? Then set OUTA for LED2 and reset OUTA for LED1
    -- otherwise: reset OUTA for both LEDs

    There are many more possibilities, e.g. you can switch off the LED aftera fixed time, independant of a key press...

    This is all the fun of programming smile.gif

    Read through the comments of the keybord driver to find out how to call it...
  • Kram11Kram11 Posts: 8
    edited 2007-11-10 18:07
    where will i find the keyboard driver, ive got a task called keyboard that I am calling.



    · repeat
    ···· term.hex(kb.getkey,3)· ' it waits here untill it gets a key press
    ··· 'term.hex(kb.key,3)· ' just looks at what key is pressed and moves on
    ···
    ···· 'if kb.gotkey ==1
    ···· 'if kb.key<>000
    ········· term.out(" ")
    ········· term.str(string("got to here",13))

    ····

    ····· !outa[noparse][[/noparse]Pin]·· ' toggle LED when keyboard pressed


    ··· ' when button pressed clear the screen··
    ··· if ina[noparse][[/noparse]3]==1·· ' looking for the button to be pressed ( and key )

    ····· term.out($00)· '· this clears the screen look at other TV modes
    ····· term.str(string("· Start Again",13))
    ····· term.str(string("··· ",13))

    ····· term.str(string("button pressed",13))





    at the moment I am waiting for the key to be pressed. I would like to scan through and only print to the screen if a key is pressed which would allow me to scan my input as well. I have tried a couple of methods using the gotkey but doesnt seem to work.



    · any help would be appreciated.
  • deSilvadeSilva Posts: 2,967
    edited 2007-11-10 18:42
    Alas, you posted only a section of your code.. What do you mean by :" Where do I get the keyboard driver?"
    You most likely already included it by
    kb: "keaboard.spin"
    


    otherwise your program qouldn't compile in the first place.
    - Have you initialized it ("start")??
    - Have you set the right pins wrt your board??

    Please read the comments in the routines:
    - KEY
    - GETKEY
    - NEWKEY
    - GOTKEY
    so you can understand when you need which...
  • Kram11Kram11 Posts: 8
    edited 2007-11-10 18:56
    Hi deSilva





    · My code is as follows



    CON

    ······· _clkmode······· = xtal1 + pll16x
    ······· _xinfreq······· = 5_000_000

    ··········· PIN=16·


    OBJ

    ······· term··· : "tv_text"
    ······· kb····· : "keyboard"


    PUB start | i


    ··· dira[noparse][[/noparse]Pin]~~·
    · 'start the tv terminal
    · term.start(12)
    · term.str(string("· DEMO",13))
    · term.str(string("··· ",13))
    · term.str(string("·· Enter a LED",13))

    · 'start the keyboard
    · kb.start(26, 27)

    · 'echo keystrokes in hex
    · repeat
    ···· term.hex(kb.getkey,3)· ' it waits here untill it gets a key press
    ··· 'term.hex(kb.key,3)· ' just looks at what key is pressed and moves on
    ···
    ···· 'if kb.gotkey ==1
    ···· 'if kb.key<>000
    ········· term.out(" ")
    ········· term.str(string("got to here",13))

    ····

    ····· !outa[noparse][[/noparse]Pin]·· ' toggle LED when keyboard pressed


    ··· ' when button pressed clear the screen··
    ··· if ina[noparse][[/noparse]3]==1·· ' looking for the button to be pressed ( and key )

    ····· term.out($00)· '· this clears the screen look at other TV modes
    ····· term.str(string("· Start Again",13))
    ····· term.str(string("··· ",13))

    ····· term.str(string("button pressed",13))······
    ····· '' Output a character
    ''
    ''···· $00 = clear screen
    ''···· $01 = home
    ''···· $08 = backspace
    ''···· $09 = tab (8 spaces per)
    ''···· $0A = set X position (X follows)
    ''···· $0B = set Y position (Y follows)
    ''···· $0C = set color (color follows)
    ''···· $0D = return
    ''· others = printable characters



    · I understand the key and getkey but I dont seem to be able get my If statement to work if a key is pressed.



    ·I have only had my starter kit about a week and I am still getting to grips with spin as most of my programming experience is on industrial PLCs in Ladder.
  • RaymanRayman Posts: 14,350
    edited 2007-11-10 19:07
    Use "kb.gotkey" to tell if a key has been pressed.· This returns either true or false.

    E.g.,
    repeat
      if (kb.gotkey)
        term.hex(kb.getkey,3)  'show key     
        !outa[noparse][[/noparse]Pin]   ' toggle LED when keyboard pressed
    


    ···
  • Mike GreenMike Green Posts: 23,101
    edited 2007-11-10 19:20
    If you want to check for a specific key, you have to use the character value:
    repeat
       if kb.gotkey
          case a := kb.getkey
             "1": !outa[noparse][[/noparse] 1 ]
             "2": !outa[noparse][[/noparse] 2 ]
             other:
                term.str(string("other key: $"))
                tern.hex(a,2)
    
  • Kram11Kram11 Posts: 8
    edited 2007-11-10 19:28
    thanks for your help
Sign In or Register to comment.