Shop OBEX P1 Docs P2 Docs Learn Events
Decoding Lines of code from buttons — Parallax Forums

Decoding Lines of code from buttons

So I have been designing a robotics controller for my school over the past year. as of right now I'm testing the button data being sent to the xbee on the receiver from the transmitting controller. The Button data (pins 0-7) are being sent in a 8-bit packet. The issue I'm having right now is to know which button is being pressed from those bits on the receiver (ex. 10000000 (This would be the 1st button on pin 0 being pressed on the transmitter)). I will upload my code. I've been searching everywhere for awhile, Xbee manuals, Spin references. Please let me know any ideas that would help me complete this project sooner

the temporary code 10.12.19 is for the transmitter

Thanks,
Ryan Canaway

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2019-10-13 05:08
    You can use a simple method to determine any bit state in any value -- something like this:
    pub bit_state(value, bitpos)
      return (value >> bitpos) & 1
    

    If you want to check for bit7 of the buttons value sent accross XBee you could do this:
      if bit_state(buttons, 7)
        ' do something about button connected to bit7
    

    Since you don't have many buttons you could create mask constants and do something like this:
      if (buttons & B7_MASK)
        ' do something about button connected to bit7
    
    ...which would be a little faster as it doesn't have to call the bit_state() method. I think it would make the code more readable as well.
  • JonnyMac wrote: »
    You can use a simple method to determine any bit state in any value -- something like this:
    pub bit_state(value, bitpos)
      return (value >> bitpos) & 1
    

    If you want to check for bit7 of the buttons value sent accross XBee you could do this:
      if bit_state(buttons, 7)
        ' do something about button connected to bit7
    

    Since you don't have many buttons you could create mask constants and do something like this:
      if (buttons & B7_MASK)
        ' do something about button connected to bit7
    
    ...which would be a little faster as it doesn't have to call the bit_state() method. I think it would make the code more readable as well.


    I'm thinking of doing the second option. however whenever i put it in my code and try to upload it. I get a "Expected end of line" message on the left parenthesis in bit_state(buttons, 7)
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2019-10-13 18:56
    Ryan, I'll focus on a couple of details in your code.

    On the transmit side, you are sending all 4 bytes of the buttons variable. Why? There are only 8 bits for the buttons and they are all in the first one sent.
    repeat  4                     'Transmits Button Data to the RX Xbee
          xbee.tx(buttons)
          buttons >>= 8
    

    On the receive side, you have it waiting for 1 byte after the !
    pst.bin(xbee.rx, 8)
    
    but the other three bytes that were sent have to be cleared out. Right after that, you have
    if xbee.rx & |< 1
    
    That will receive the second byte of the transmission, which by my view will always be a zero. The last two bytes of the trasmission will be cleared out when waiting for the next key "!". It could be simplified to the one byte you need to transmit. Just send the one relevant bytes, buttons (no "!" necessary) and have one instruction to read the byte to buttons on the receive side.
    buttons := xbee.rx.
    

    The syntax of that statement, (xbee.rx & |< 1) is weird. For decoding buttons try one of the methods that Jon suggested. You have to define the method
    PUB bit_state(value, bitpos)
    Unless you define it, it will throw the error you saw.


    In your receiver code the variable temp is defined as local in the PUB call,
    PUB button_control | temp, index
    
    but then you have,
    longfill(@temp, 0, 8 ) 
    
    temp is a single long, but the longfill will also zero out index and six additional longs in memory. Can have unintended bad consequences. :-O
  • Ryan, I'll focus on a couple of details in your code.

    On the transmit side, you are sending all 4 bytes of the buttons variable. Why? There are only 8 bits for the buttons and they are all in the first one sent.
    repeat  4                     'Transmits Button Data to the RX Xbee
          xbee.tx(buttons)
          buttons >>= 8
    

    On the receive side, you have it waiting for 1 byte after the !
    pst.bin(xbee.rx, 8)
    
    but the other three bytes that were sent have to be cleared out. Right after that, you have
    if xbee.rx & |< 1
    
    That will receive the second byte of the transmission, which by my view will always be a zero. The last two bytes of the trasmission will be cleared out when waiting for the next key "!". It could be simplified to the one byte you need to transmit. Just send the one relevant bytes, buttons (no "!" necessary) and have one instruction to read the byte to buttons on the receive side.
    buttons := xbee.rx.
    

    The syntax of that statement, (xbee.rx & |< 1) is weird. For decoding buttons try one of the methods that Jon suggested. You have to define the method
    PUB bit_state(value, bitpos)
    Unless you define it, it will throw the error you saw.


    In your receiver code the variable temp is defined as local in the PUB call,
    PUB button_control | temp, index
    
    but then you have,
    longfill(@temp, 0, 8 ) 
    
    temp is a single long, but the longfill will also zero out index and six additional longs in memory. Can have unintended bad consequences. :-O

    I have taken out the repeat on the transmit side so It just sends one bit of data at a time. I'm still having issues reading individual bytes. if you could help me out, that would be spectacular .
Sign In or Register to comment.