This value is a bitmask, is it not?
You can have multiple buttons pressed at once?
yes, and they produce different values close to the same range.
I can send more details and the result values if it helps.
Then this is definitely a bit vector.
That means you should not be saying:
if button == 1:
# A button pressed
elif button == 2:
# B button pressed
elif button == 4:
# C button pressed
I'm doing exactly this, can you tell me why this is wrong please?
Because this value is not giving you one single piece of information. You are assuming that the value "4" means "button C and ONLY button C was pressed." That's not what this value tells you. Each BIT has a different meaning. Bit 0 is set if button A is pressed. Bit 1 is set if button B is pressed. Bit 2 is set if button C is pressed. Each of those events is COMPLETELY unrelated to the other events. Button A can be pressed whether or not any of the other buttons are pressed. If both buttons A and C are pressed, both bits 0 and 2 will be set.
But bit 11, for example, has some other meaning that you and I are not aware of. It might be set, or it might not, and we don't know why. But because this is a bit vector, we don't NEED to care. If all we care about is the question "was button A pressed", we should ONLY look at the value of bit 0. We ignore the other bits.
I have different mappings based on the kernel versions.
something like:
if kernel is '2.4.29':
A = 535553
B = 535554
C = 535556
elif kernel is '2.4.22':
A = 1
B = 2
C = 4and so on...
then doing:
if button == A:
# A button pressedand so on...
This is the wrong way to do this, because of the reasons I have described above.
Instead, you should be saying:
if button & 1:
# A button pressed
if button & 2:
# B button pressed
if button & 4:
# C button pressed
ok, so here there is something I don't understand quite well.this about if button & XX. this "&" is what is called a "logical and",
right? I guess I have some knowledge gap, so if you can explain this
part a little bit more, or direct me to some further doc, it will really
help me.
The & operator, called "bitwise and" acts on each bit of its operands separately. For each bit, if the corresponding bit in BOTH operands is a "1", the result is a "1". Otherwise, the result is a "0". Here is a "truth table" that devices the operation, bit by bit:
a b a & b
0 0 0
0 1 0
1 0 0
1 1 1
So, if you "and" an 8-bit value with 0xff, the result is the same as the original value. If you "and" an 8-bit value with 0, the result is always 0.
(The && operator, often called "logical and", has a different but related meaning. Rather than operating a bit at a time, it treats its two operands as a single True or False entity. The truth table above still works, but think of the "0" as the Python "false", and the "1" as the Python "true".)
We want to ask the question, "is bit 1 set," and ignore the other bits. To do that, we want to to a bitwise AND with a value where bit 1 is set and the other bits are 0. The result of the AND will have 0 everywhere except bit 1, and bit 1 of the result will be the same as bit one of the original value.
Hence, the statement "if button & 1:" asks the question "is bit 0 of button set, without caring what the other bits are", and "if button & 2:" aslks the question "is bit 1 of button set, without caring what the other bits are".
Boolean algebra is absolutely fundamental to computer science. You might check out some web pages on the subject, especially if you are working with devices. Hardware registers use bit masks extensively.
hmm, I guess I should try that out.
anyway an update: running latest kernel 2.6.11-3 released last week, it
seems I get the original "nice" values back, which seem bitmask-like: 1,
2, 4, 8, 16, etc..
But you cannot rely on that. Those other bits have a meaning that is not known to you. They might be set at any time, for unknown reasons.
···
On Wed, 16 Mar 2005 01:55:05 +0100, rama <medialist@xicnet.com>
El mar, 15-03-2005 a las 10:23 -0800, Tim Roberts escribió:
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.