How filter/masks work

When you filter things in VSCP you use a filter and a mask. What is this?

It is simple. We have to show the truth table first

Mask bit n Filter bit n Checked bit n Accept or reject bit n
0 x x Accept
1 0 0 Accept
1 0 1 Reject
1 1 0 Reject
1 1 1 Accept

What does this table say?

It says that for a bit where the mask is set to 1 the filter bit and the tested bit should be the same for an accept. If the mask bit is 0 the bit will be accepted if the filter bit is either 0 or 1.

So if we want to see a specific byte say 0x55?

Then we set the mask to 0xFF and the filter to 0x55. This will give an accept for 0x55. This is also the most common use of the filter/mask pairs.

But they are more powerful. Say you want to accept all bytes 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F. That you can do with mask=0xF0 and filter=0x50.

More complex scenarios is possible of course. If you just want to see 0x50 and 0x59. The way to create the mask is to put them under each other in binary

  00101 0000 = 0x50
  00101 1001 = 0x59
  1111 0110 = 0xF6 = mask

You set a one where all bits of the bytes are the same and a 0 where they are not. Most often you get bytes your don't actually want as above you will get 0x50 and 0x59 as expected but also 0x58 and 0x51.

The mask filter method is created for to select out bitfields and work well for that.

Go back

howto/how_filters_s_work.txt · Last modified: 2018/02/08 17:47 (external edit)
[unknown button type]