Shop OBEX P1 Docs P2 Docs Learn Events
Help converting CASE SELECT to IF THEN ELSE statements (BS2 to SX/B) — Parallax Forums

Help converting CASE SELECT to IF THEN ELSE statements (BS2 to SX/B)

T&E EngineerT&E Engineer Posts: 1,396
edited 2007-02-08 01:31 in General Discussion
I am trying to convert this routine from a BS2 to SX/B:

Main:
DO
· GOSUB getkey
· 'DEBUG TAB,HEX3 scancode
· IF scancode<$FF THEN
··· SELECT scancode
····· CASE $12,$59
······· shiftbit=~F0bit
······· F0bit=0
······· ascii=0
····· CASE $E0·· 'extended key
········· ascii=0
····· CASE $F0· 'erase key release
······· F0bit=1
······· ascii=0
····· CASE >143
······· ascii=0
····· CASE ELSE
······· IF F0bit=0 THEN READ (144 * shiftbit + scancode),ascii
······· F0bit=0
··· ENDSELECT
··· IF ascii > 0 THEN DEBUG ascii
· ENDIF
LOOP

The SELECT and CASE Statements are getting a bit hairy on the IF THEN conversion.

Any help would be appreciated. I understand that the GOSUB keyword statement needs to be removed and that DEBUG needs to be removed too.

Thanks.

Comments

  • Clock LoopClock Loop Posts: 2,069
    edited 2007-02-07 00:54
    Use lookup and lookdown.

    I have found that those work better as a replaement for select case, over if then statements.

    Look at the sx/b help file for examples on lookup and lookdown.

    If you still don't see how those can be used, post here again what you came up with, and I can help you.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-02-07 01:39
    PBASIC 2.5 SELECT-CASE structures generate a lot of internal (unseen by you) code -- so it's likely your IF-THENs will be a little "hairy" doing a manual conversion. Using LOOKDOWN is an option, just set your comparisons in ascending order.

    Update: It turns out not to be that bad -- I left plenty of whitespace to make it easy to read:

    Main:
      scanCode = GET_KEY
      IF scanCode = $FF THEN Main
    
      temp = scanCode
      LOOKDOWN scanCode, $12, $59, $E0, $F0, temp
    
      IF temp < 2 THEN                ' $12 or $59
        shiftBit = ~F0bit
        F0bit = 0
        ascii = 0
        GOTO Main_2
      ENDIF
    
      IF temp = 2 THEN                ' $E0
        ascii = 0
        GOTO Main_2
      ENDIF
    
      IF temp = 3 THEN                ' $F0
        F0bit = 1
        ascii = 0
        GOTO Main_2
      ENDIF
    
      IF temp > 143 THEN
        ascii = 0
      ELSE
        IF F0bit = 0 THEN
          addr = 144 * shiftBit
          addr = addr + scanCode
          READ addr, ascii
        ENDIF
      ENDIF
      
    Main_2:
      ' next step
    
    

    Post Edited (JonnyMac) : 2/7/2007 1:55:30 AM GMT
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-02-07 02:18
    Thanks for all the help. However, I beleive I have figured the IF THEN part out - now it has errors with START: when trying to compile.

    I have the program attached and I followed the SX Template.

    Any Help. Also if my IF THEN portion looks wrong then I will attemp a LOOKDOWN approach. I like this approach too - but I don't want to change it if I get no compile errors on this section.

    Very weird. See attached.
  • JonnyMacJonnyMac Posts: 8,942
    edited 2007-02-07 18:03
    Start is in the template as the beginning point of the program -- and you're trying to use that as a variable name (change the var, that fixes the error).

    Other notes:
    -- Using OUTPUT with the PIN directive takes care of the TRIS register for you (so you don't need the TRIS = %00000000 line)
    -- You're using INPUT with a variable; are you trying to float a pin pointed to by that variable?
    -- With the TRIS register set you don't need to use HIGH and LOW; just do ThePin = 1 or ThePin = 0 (saves a bit of code space)
    -- On your SUB definition, set the parameters count to 0; this will save a bit of code
    -- You need to identify your tables with labels as data storage does not start a address 0 as does the BASIC Stamp
    -- READ needs the starting addess of table, plus and offset. Use one of the labels from above and add the calculated offset.

    Post Edited (JonnyMac) : 2/7/2007 6:10:23 PM GMT
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-02-08 00:20
    JonnyMac,

    Great analysis! All is·compiling except for the SHIFTIN statement - once I incorporated your comments. Thank you so much!!

    SHIFTIN datain, clock, MSBPRE, [noparse][[/noparse]scancode]

    ERROR is: BYTE VARIABLE EXPECTED "[noparse][[/noparse]scancode]"

    Any ideas?

    Thanks again.
  • BeanBean Posts: 8,129
    edited 2007-02-08 00:54
    SX/B doesn't use brackets. Just use:

    SHIFTIN datain, clock MSBPRE, scancode

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com
    Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1

    "USA Today has come out with a new survey - apparently, three out of every four people make up 75% of the population." - David Letterman
  • T&amp;E EngineerT&amp;E Engineer Posts: 1,396
    edited 2007-02-08 01:31
    Thanks Bean.

    I am out on business travel in Indianapolis for the next couple of days but I will try this code when I get back over the weekend. It looks like it wasn't too hard to convert it from BS2 to SX/B with everyone's great help!

    Thanks again to all!

    Tim
Sign In or Register to comment.