Protocols that require tweaking at bit level
Spannertech
Posts: 53
Thankfully most serial protocols are in English ASCII, but occasionally you come across the weirdest things. I think this is rather old fashioned actually.
I have one where you tweak individual bits within a byte to control different things. This byte is part of a much longer string. Just wondering the most elegant way. Say for example I have 8 toggling buttons controlling 8 bits in a byte. I could have a variable associated with each button. If button 1 were pressed, its variable would be 1, otherwise 0 if off. If button 2 were pressed, its variable would be at 2, otherwise 0, button 3 value of 4, button 4 value of 8 etc. Then you add all the variables up and that's my byte. Is this the best way or am I missing something?
How about the reverse - parsing a byte down to bit level for feedback...do I have to keep dividing by 2 and looking at remainders? Or something with the Bitwise operators?
Thanks
OP
I have one where you tweak individual bits within a byte to control different things. This byte is part of a much longer string. Just wondering the most elegant way. Say for example I have 8 toggling buttons controlling 8 bits in a byte. I could have a variable associated with each button. If button 1 were pressed, its variable would be 1, otherwise 0 if off. If button 2 were pressed, its variable would be at 2, otherwise 0, button 3 value of 4, button 4 value of 8 etc. Then you add all the variables up and that's my byte. Is this the best way or am I missing something?
How about the reverse - parsing a byte down to bit level for feedback...do I have to keep dividing by 2 and looking at remainders? Or something with the Bitwise operators?
Thanks
OP
0
Comments
DEFINE_VARIABLE
MY_BYTE = $3A
INTEGER VAL_BIT0
INTEGER VAL_BIT1
INTEGER VAL_BIT2
INTEGER VAL_BIT3
INTEGER VAL_BIT4
INTEGER VAL_BIT5
INTEGER VAL_BIT6
INTEGER VAL_BIT7
DEFINE_START
VAL_BIT7 = MY_BYTE BAND $80 // result is 0
VAL_BIT6 = MY_BYTE BAND $40 // result is 0
VAL_BIT5 = MY_BYTE BAND $20 // result is $20 (32)
VAL_BIT4 = MY_BYTE BAND $10 // result is $10 (16)
VAL_BIT3 = MY_BYTE BAND $08 // result is $08 (8)
VAL_BIT2 = MY_BYTE BAND $04 // result is 0
VAL_BIT1 = MY_BYTE BAND $02 // result is $02
VAL_BIT0 = MY_BYTE BAND $01 // result is 0
So in this way, if a bit is set, the result is >0, and 0 if the bit is not set.
Of course this can be done in some other and/or automated way. The above is just to show.
Back-in-the-day, when I was writing command-line DOS .com programs for a 640K environment, every bit counted. I used the method Marc illustrated - masks and bitwise comparisons. For the NetLinx environment, I just haven't seen a case where it is necessary. I have, however, run into some serial devices that work on that basis, so it is still a good technique to know.
So I wrote a parser. For G3 panels, it weeds out anything outside of the single-byte ASCII range and replaces it with a "I can't show this symbol"* ASCII character. For G4 panels it was even more fun, translating from UTF-8 unicode to AMX's version of unicode.
Long story short, (ooops - too late) lots of bit shifting and bit masking going on...
- Chip
* - user defined...