Shop OBEX P1 Docs P2 Docs Learn Events
Can I combine bitwise '&' with CASE? — Parallax Forums

Can I combine bitwise '&' with CASE?

I want to use bits 0-3 to pass a value and bits 4-7 to point to specific variables in an object that recieves values wirelessly.
I have in mind four variables, let's call them A, B, C and D. I want to point to them like this:
%0001_xxxx (copy to variable a)
%0010_xxxx (copy to variable b)
%0011_xxxx (copy to variable c)
%0100_xxxx (copy to variable d)
I really want to eliminate a lot of IF statements.

Comments

  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-01-20 17:54
    lardom wrote: »
    I want to use bits 0-3 to pass a value and bits 4-7 to point to specific variables in an object that recieves values wirelessly.
    I have in mind four variables, let's call them A, B, C and D. I want to point to them like this:
    %0001_xxxx (copy to variable a)
    %0010_xxxx (copy to variable b)
    %0011_xxxx (copy to variable c)
    %0100_xxxx (copy to variable d)
    I really want to eliminate a lot of IF statements.

    Yes, the argument to a case can be any expression.
    PUB foo(x)
    
      case x & %1111_0000
        %0001_0000:
          ' a
        %0010_0000:
          ' b
        %0011_0000:
          ' c
        %0100_0000:
          ' d
        other:
          ' invalid
    

    You can even put an assignment operation there if you like, such as "x++" or "x *= 23"
  • Electrodude, thank you!
  • lardomlardom Posts: 1,659
    edited 2016-01-20 23:31
    Electrodude, it compiled. I sat there admiring it. It's not in it's final form but it'll clearly do what I want it to. Thanks again.
    case CurrentSpeed & %1111_0000
       %0001_0000  : front_left  := CurrentSpeed - %0001_0000
       %0010_0000  : front_right := CurrentSpeed - %0010_0000 
       %0011_0000  : back_left   := CurrentSpeed - %0011_0000
       %0100_0000  : back_right  := CurrentSpeed - %0100_0000
    
  • ElectrodudeElectrodude Posts: 1,657
    edited 2016-01-20 23:45
    lardom wrote: »
    Electrodude, it compiled. I sat there admiring it. It's not in it's final form but it'll clearly do what I want it to. Thanks again.
    case CurrentSpeed & %1111_0000
       %0001_0000  : front_left  := CurrentSpeed - %0001_0000
       %0010_0000  : front_right := CurrentSpeed - %0010_0000 
       %0011_0000  : back_left   := CurrentSpeed - %0011_0000
       %0100_0000  : back_right  := CurrentSpeed - %0100_0000
    

    I would recommend that you instead write:
    case CurrentSpeed & %1111_0000
       %0001_0000  : front_left  := CurrentSpeed & %1111
       %0010_0000  : front_right := CurrentSpeed & %1111
       %0011_0000  : back_left   := CurrentSpeed & %1111
       %0100_0000  : back_right  := CurrentSpeed & %1111
    
    or even:
    value := CurrentSpeek & %1111
    case CurrentSpeed & %1111_0000
       %0001_0000  : front_left  := value
       %0010_0000  : front_right := value
       %0011_0000  : back_left   := value
       %0100_0000  : back_right  := value
    
    That way, you won't need to change the same thing in two places if you want to change the top part constant or add more cases.
  • lardomlardom Posts: 1,659
    edited 2016-01-21 00:41
    I'm gonna take a look at it on the Propeller tool. I'm not out of the woods by a long shot. I'm going to test what I've written in maybe two days. I still have some things to work out.
    I was successfully able to control the speed of one motor EDIT: (with a joystick) foward and reverse using the nrf24L01 transceiver.
    Wireless control of two motors differentially is a major jump (for me) because of the 8-bit bottleneck.
    1)Rotate left/right
    2)turn left/right forward
    3)turn left/right backward
    I also want to add pan/tilt for a camera and an arm with a gripper.
Sign In or Register to comment.