Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Spin case statement — Parallax Forums

Problem with Spin case statement

RsadeikaRsadeika Posts: 3,837
edited 2010-03-03 17:37 in Propeller 1
In the 'main', the 'repeat' statement has a 'case' statement which is not working as expected, and I cannot figure out why. What I expect to occur is when I select the device 'tv', I only should get an LED activity on the power btn, but I am also getting an LED activity on the 'r arrow head' btn, which should only become active when the device is set to 'vcr'. I double checked my 'indents', and they seem to be in the correct position, so what is wrong with this?

Thanks

Ray


con

  _clkmode  = xtal1 + pll16x
  _xinfreq  = 5_000_000


con
' PST formmatting control
  #1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS


obj

  ir   : "jm_sircs_rx"                        ' sircs decoding
  term : "jm_txserial"                             ' tx serial
  misc : "tools"


pub main | code, bc, devcode, keycode

  ir.init(0)
  ''ir.init(27)                               ' IR demod on p27
  
  term.init(30, 115_200)                ' term on programming port

  misc.waitms(1000)

  repeat
    code := ir.getir
    devcode := code & $f80
    keycode := code & $7f                       ' wait for key
    bc := ir.bitcount                           ' get bitcount
    ''case bc
      ''12:
         case devcode
           %000010000000 :       ''device tv
               showcode(devcode) ''devcode = %000010000000
               case keycode
                  %000000010101 :   ''Power key
                      LED1
                      term.bin(devcode, 12)
                      term.tx(CR)
                      term.bin(keycode, 12)
                      term.tx(CR)
           %010110000000 :        ''device vcr
               showcode(devcode)  ''devcode = %010110000000
               case keycode
                  %000000011010 :   ''r arrow head
                       LED1
                       term.bin(devcode, 12)
                       term.tx(CR)
                       term.bin(keycode, 12)
                       term.tx(CR)


    misc.waitms(400)
    devcode := 0
    keycode := 0


PRI tv_power
  LED1

PRI vcr_r_a_head
  LED1

PRI showcode(devcode)
  term.bin(devcode, 12)
  term.tx(CR)

PRI LED1
  misc.high(1)
  misc.waitms(250)
  misc.low(1)


Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-03 01:45
    I wouldn't use stacked case statements like you're doing -- makes the code really hard to follow, especially since you either have 12- or 20-bit codes (which drives the use of if-else). Since you're truncating the keycode to 7 bits (I thought the 20-bit codes used an 8-bit key), you could re-code like this:

    repeat
      code := ir.getir
      devcode := code & $f80
      keycode := code & $7f                       ' wait for key
      bc := ir.bitcount                           ' get bitcount
    
      if (ir.bitcount == 12)                      ' tv code
        case keycode
          %0010101 : LED1
          other    : LED2
        term.bin(devcode, 5)
    
      else
        case keycode
          %0011010 : LED1
          other    : LED2
        term.bin(devcode, 13)
    
      term.tx(".")
      term.bin(keycode, 7)
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-03 08:39
    Why is there a need to split code up into devcode and keycode?

             case code
               '' codes for tv start with %00001_
               %00001_0010101 :       ''device tv
                   showcode(code>>7) '' get rid of the key-code-part
                   LED1
                   term.bin(code>>7, 12)
                   term.tx(CR)
                   term.bin(code & $7f, 12)
                   term.tx(CR)
               '' codes for vcr start with %01011_
               %01011_0011010 :        ''device vcr
                   showcode(code>>7)  ''
                   LED1
                   term.bin(code>>7, 12)
                   term.tx(CR)
                   term.bin(code & $7f, 12)
                   term.tx(CR)
    
    

    Post Edited (MagIO2) : 3/3/2010 2:21:48 PM GMT
  • RsadeikaRsadeika Posts: 3,837
    edited 2010-03-03 11:32
    @MagIO2, that is the way my brain works. If I have two major settings, tv or vcr, and a lot of setting specific key codes, I figure you isolate which device is being used, first, and then you isolate which key code is being used, which will provide an action.

    This investigation started with the fact that I was not certain as to what variable value 'devcode := (code & $f80) >> 7' was providing. So, I borrowed some of JonnyMac's code to find out exactly what the values are, since I am certain that his program is providing the correct values. The first thing I noticed was that devcode = 0, when I used 'devcode := (code & $f80) >> 7', I changed it to 'devcode := code & $f80' which is now providing the type of value that I was expecting.

    The thing that has me a bit flustered is that in the program above, I have applied some sensible logic IMO, but the outcome is not what I am expecting. So, now I have to figure out what part of my logic is not translating to the programs functionality, to provide the correct outcome; using the saying of "garbage in, garbage out", it must BE MY FAULT. I think this is going to take awhile ...


    Ray
  • RsadeikaRsadeika Posts: 3,837
    edited 2010-03-03 14:06
    In the program below, I changed it so there are only two conditions to check for, '%00001_0010101', and '%01011_0011010'. With the setting of '%00001', tv, it flashes the LED for '%00001_0010101', and '%01011_0011010', which is not what I expect. But when I change the setting to '%01011', vcr, it only flashes the LED for '%01011_0011010', which is what I expect. The question is, when it is set to '%00001', why is it flashing the LED when the setting is '%01011_0011010'? In the first pass, why are the values being changed or interpreted, from '%01011_0011010' to '%00001_0011010'? My showcode method shows that the numbers are not changed, for some reason on the first pass the value is not being interpreted correctly by the case statement, or at least that is my impression. Now how would I verify something like that, or is this just an oddity for this particular situation?

    Ray



    con
    
      _clkmode  = xtal1 + pll16x
      _xinfreq  = 5_000_000
    
    
    con
    ' PST formmatting control
      #1, HOME, #8, BKSP, TAB, LF, CLREOL, CLRDN, CR, #16, CLS
    
    
    obj
    
      ir   : "jm_sircs_rx"                        ' sircs decoding
      term : "jm_txserial"                             ' tx serial
      misc : "tools"
    
    
    pub main | code, bc, devcode, keycode
    
      ir.init(0)
      ''ir.init(27)                               ' IR demod on p27
      
      term.init(30, 115_200)                ' term on programming port
    
      misc.waitms(1000)
    
      repeat
        code := ir.getir
        devcode := code & $f80
        keycode := code & $7f                       ' wait for key
        ''bc := ir.bitcount                           ' get bitcount
        ''case bc
          ''12:
             case code
               %000010010101 :       ''device tv
                   showcode(code) ''devcode = %000010000000
                   ''case keycode
                     '' %000000010101 :   ''Power key
                          LED1
                          term.bin(devcode, 12)
                          term.tx(CR)
                          term.bin(keycode, 12)
                          term.tx(CR)
               %010110011010 :        ''device vcr
                   showcode(code)  ''devcode = %010110000000
                   ''case keycode
                      ''%000000011010 :   ''r arrow head
                           LED1
                           term.bin(devcode, 12)
                           term.tx(CR)
                           term.bin(keycode, 12)
                           term.tx(CR)
    
    
        misc.waitms(400)
    
    
    PRI tv_power
      LED1
    
    PRI vcr_r_a_head
      LED1
    
    PRI showcode(devcode)
      term.bin(devcode, 12)
      term.tx(CR)
    
    PRI LED1
      misc.high(1)
      misc.waitms(250)
      misc.low(1)
    
    
    
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-03-03 14:16
    Well ... I'm not sure that in microcontroller-programming it's a good idea to do it as the brain works. It should rather be done as the microcontroller works because code is then shorter/faster and in this special case it's not worse to read either (at least in my opinion ;o)

    But I can't believe what you say about the devcode:
    assume code is $00001_0010101
    then code & $f80 (which is %11111_0000000) will give you %00001_0000000
    and %00001_0000000 >> 7 is %00001
  • JonnyMacJonnyMac Posts: 9,208
    edited 2010-03-03 17:37
    I did a little more research on SIRCS this morning, and I also sent a note to a friend who works for Sony asking him if he could get me a copy of the official specification.

    I ran across a bit of information on the codes that is new to me:

    The 15-bit code is a wired code; the device code is eight bits, the key code is seven bits. The 20-bit code is 8 + 12 (not confirmed yet). Based on that you might do something like this:

    raw := ir.getir
    bc := ir.bitcount
    
    case bc
      12:
        keycode := raw & $3F
        devcode := (raw >> 7) & $1F
        extcode := 0
    
      15:
        keycode := raw & $3F
        devcode := (raw >> 7) & $FF
        extcode := 0
    
      20:
        keycode := raw & $3F
        devcode := (raw >> 7) & $1F
        extcode := (raw >> 12) & $FF
    



    With this structure you've got the constituent parts of the code that just came in and it might be easier to work with.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
Sign In or Register to comment.