Shop OBEX P1 Docs P2 Docs Learn Events
my CASE expression needs a fix — Parallax Forums

my CASE expression needs a fix

karlikarli Posts: 16
edited 2008-07-11 17:19 in Propeller 1
···· all is well up to·· Calling the PUB decode code:

······· if·ina[noparse][[/noparse]21] == 1·············· 'if my P21 push button has been pressed
·········· if ina[noparse][[/noparse]9] == 0·············· AND if my 2sec P9 Led gate initiated by a cog has timed out
············· outa[noparse][[/noparse]6..4]++··········· then I increment the bits in [noparse][[/noparse]6..4]

······· if ina[noparse][[/noparse]9] == 0···························· if [noparse][[/noparse]9] Led is still off· = P9 =·LOW
·········· waitcnt(clkfreq/100 + cnt)········ 10msec delay
·········· decode································· issue Call to· PUB decode
·········· launch·································· issue Call to· Pub Launch····· 'Main


·PUB· decode

······· CASE·· ina[noparse][[/noparse]6..4]········ I want to test for the binary pattern of the [noparse][[/noparse]6..4] bits
··········· 001:····· !outa[noparse][[/noparse]4]····· turn on the P4 led
··········· 010:····· !outa[noparse][[/noparse]5]····················· P5
··········· 011:······!outa[noparse][[/noparse]6]····················· P6
··········· 100:····· !outa[noparse][[/noparse]7]····················· P7
············101:····· !outa[noparse][[/noparse]8]······················P8
··········· OTHER:···· ???············· what code do I use to clear all bits in [noparse][[/noparse]6..4]·· ?
············································ and return to the decode Call and then call launch ?
············································ the·CASE decoding doesn't work but it returns to call
············································ launch ok and I can start a new P21 button press
············································ sequence.

·· Thanks for any insight,
···· karli
·

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-10 22:32
    Start with explaining what you want "decode" to do.

    1) ina[noparse][[/noparse] 6..4 ] gets the value of the I/O pins.· Depending on how you've hooked them up and what you've done to set the dira register, that may or may not be the same as the outa register bits.· 1xx is pin 6 and xx1 is pin 4 the way you've written it.· I don't know if that's what you wanted.

    2) !outa[noparse][[/noparse] x ] toggles the specified outa bit.· Again, that may or may not be what you actually want.

    3) To set bits 6..4 of outa to zero, you can just do: outa[noparse][[/noparse] 6..4 ] := 0

    4) All methods (subroutines) have an implicit return as the last statement

    5) The notation for binary values is %000, %001, %010, %011, %100, %101, %110, %111.· What you wrote was decimal (1, 10 (ten), 11 (eleven), 100 (one hundred), 101 (one hundred and one).
  • karlikarli Posts: 16
    edited 2008-07-11 04:48
    hi Mike,
    Thanks for coming back me.
    let me start at the beginning: I'm using P21 as button entry and P4...P9 as LED outputs in the
    standard PE lab configuration.
    I want to Mux(multiplex) the function of only one push button into 5 possible output states, in this case,
    P4..P8; P4 is LED#1 for state #1 and P8 is LED#5 for state#5
    P9 comes on when I first push P21 for 2 sec via a newcog operation
    from the moment P9 comes on I have 2 sec to hit P21 button up to 5 times
    when P9 turns off signalling the end of that 2sec gate; so, if I push P21 6 times, the first press is to start
    the P9 gate and that leaves a count of 5 for outa[noparse][[/noparse]6..4]; i.e., P6 and P4 LEDs are lit
    all this works to this point.


    now for decode: I want to translate the binary pattern in bits 6 5 4 in [noparse][[/noparse]6..4] into a single LED
    output; i.e., a binary 5 should turn on P8, which is output state#5 and P4 and P6 should extinguish.

    when I push P21 while P9 gate is active for 2sec I'm currently toggling the P4 P5 or P6 LEDs, but in my
    final version I would run this count process 'dark" with only P9 being lit while I do
    the P21 presses. and after P9 goes off then the state result should be displayed.

    In my CASE example I mistakenly skipped the % to show that the outa[noparse][[/noparse]6..4] values was supposed to be
    treated as a binary. I also copied the bitwise NOT ! from the Propeller Manual, not realizing that
    it was meant to invert my outputs in the decode routine.
    of course attention to detail is the ahllmark of programming !

    I hope I was clearer this time as to what I had in mind.

    In order to do my P21 toggling "in the dark" without P4 5 6 Leds turning on what other technique
    can I use ? instead of using outa[noparse][[/noparse]6..4]++ whenever I push P21 ?

    your help is much appreciated,
    karli
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-07-11 07:10
    hello karli,

    that's quite simple
    karli said...

    In order to do my P21 toggling "in the dark" without P4 5 6 Leds turning on what other technique
    can I use ? instead of using outa[noparse][[/noparse]6..4]++ whenever I push P21 ?


    as a quick answer to your question:
    use a variable instead of the output-register outa


    VAR
      long PushCount
      
    Pub Main  
      PushCount := 0
    
      if ina[noparse][[/noparse]21] == 1               'if my P21 push button has been pressed
      'why doing this if it has TIMEDOUT ?
      'shouldn't it be within the 2second-frame ??
         if ina[noparse][[/noparse]9] == 0             'AND if my 2sec P9 Led gate initiated by a cog has timed out
            PushCount++             'then increment the variable PushCount
    
      if ina[noparse][[/noparse]9] == 0                 'if [noparse][[/noparse]9] Led is still off  = P9 = LOW
         waitcnt(clkfreq/100 + cnt)  '10msec delay
         decode                      'issue Call to  PUB decode
         launch                      'issue Call to  Pub Launch      'Main
     
     
    PUB  decode
     
            CASE   PushCount             'I want to test for the binary pattern of the [noparse][[/noparse]6..4] bits
              1:      outa := 1        'turn on the P4 led
              2:      outa := 1       '             P5
              3:      outa[noparse][[/noparse]6] := 1       '             P6
              4:      outa[noparse][[/noparse]7] := 1       '             P7
              5:      outa[noparse][[/noparse]8] := 1      '              P8
              OTHER:  outa[noparse][[/noparse]4..8] := 0
    
    
    
    



    i would prefer ALWAYS using variables for MANIPULATING things
    like increment/decrement values or compare values.

    This would be done by copying the state of the ina- or outa-register to a variable
    do all the manipulation with the variable and finally write the FINALLY result to the
    ina/outa-register again.

    To show some more programming-techniques a modified version of your code:

    CON
      'using constants instead of numbers makes the code easier to read whats happening
      PushButton     = 21
      EnterModeLED   = 9
    
      'whenever you want to change the IO-pins this technique of using constants
      'reduces the change-effort to ONE central change and everything is done 
      ModeLED_Offset = 3
      Mode1 = ModeLED_Offset + 1
      Mode2 = Mode1 + 1
      Mode3 = Mode2 + 1
      Mode4 = Mode3 + 1
      Mode5 = Mode4 + 1
    
    VAR
      long PushCount
      
    Pub Main  
      PushCount := 0
    
      if ina[noparse][[/noparse]PushButton] == 1             'if my P21 push button has been pressed
         if ina[noparse][[/noparse]EnterModeLED] == 0        'AND if my 2sec P9 Led gate initiated by a cog has timed out
           PushCount++                   'then I increment the variable PushCount
    
      if ina[noparse][[/noparse]EnterModeLED] == 0      'if [noparse][[/noparse]9] Led is still off  = P9 = LOW
        waitcnt(clkfreq/100 + cnt)  '10msec delay
        decode                      'issue Call to  PUB decode
        launch                      'issue Call to  Pub Launch      'Main
     
     
    PUB  decode
     
      CASE PushCount             'I want to test for the binary pattern of the [noparse][[/noparse]6..4] bits
        1:      outa[noparse][[/noparse]Mode1] := 1 'turn on the P4 led
        2:      outa[noparse][[/noparse]Mode2] := 1 '             P5
        3:      outa[noparse][[/noparse]Mode3] := 1 '             P6
        4:      outa[noparse][[/noparse]Mode4] := 1 '             P7
        5:      outa[noparse][[/noparse]Mode5] := 1 '             P8
        OTHER:  outa[noparse][[/noparse]Mode1..Mode5] := 0
    
              
    PUB decode_compact(p_ModeValue)
    
      case p_ModeValue
        1..5   : outa[noparse][[/noparse]ModeLED_Offset + p_ModeValue] := 1 'the leading "p_" indicates this is a parameter of the method
        other  : outa[noparse][[/noparse]Mode1..Mode5] := 0 
    
    'this method would be called decode_compact(PushCount)        
    
    



    best regards

    Stefan
  • karlikarli Posts: 16
    edited 2008-07-11 17:19
    ·· Thank you, StefanL38 !

    ·· I'm immensely grateful for your detailed reply to my

    ·· CASE Expression query on 10July08; I printed out your

    ·· Spin code and will study it intently and try it out on my

    ·· Prop Lab setup. I'll send a post when I start hollering

    ·· "Eureka"· and wake up the neighborhood (hi)jumpin.gif

    ··· most sincerely yours,

    ··· karli
Sign In or Register to comment.