You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using this library to monitor status messages that have a variety of ranges and specific needs for each packet on the Due.
Just a simple addition has simplified my code maybe others would want it too.
Also fair warning I'm probably and idiot and this already exists in some way that wasn't apparent to me.
int CAN_COMMON::watchForRangeSpecific(uint8_t mailbox, uint32_t id1, uint32_t id2, bool extended)
{
uint32_t id = 0;
uint32_t mask = 0;
uint32_t temp;
if (id1 > id2)
{ //looks funny I know. In place swap with no temporary storage. Neato!
id1 = id1 ^ id2;
id2 = id1 ^ id2; //note difference here.
id1 = id1 ^ id2;
}
id = id1;
if (id2 <= 0x7FF) mask = 0x7FF;
else mask = 0x1FFFFFFF;
/* Here is a quick overview of the theory behind these calculations.
We start with mask set to 11 or 29 set bits (all 1's)
and id set to the lowest ID in the range.
From there we go through every single ID possible in the range. For each ID
we AND with the current ID. At the end only bits that never changed and were 1's
will still be 1's. This yields the ID we can match against to let these frames through
The mask is calculated by finding the bitfield difference between the lowest ID and
the current ID. This calculation will be 1 anywhere the bits were different. We invert
this so that it is 1 anywhere the bits where the same. Then we AND with the current Mask.
At the end the mask will be 1 anywhere the bits never changed. This is the perfect mask.
*/
for (uint32_t c = id1; c <= id2; c++)
{
id &= c;
temp = (~(id1 ^ c)) & 0x1FFFFFFF;
mask &= temp;
}
//output of the above crazy loop is actually the end result.
return setRXFilter(uint8_t mailbox, uint32_t id, uint32_t mask, bool extended)
}
The text was updated successfully, but these errors were encountered:
I'm using this library to monitor status messages that have a variety of ranges and specific needs for each packet on the Due.
Just a simple addition has simplified my code maybe others would want it too.
Also fair warning I'm probably and idiot and this already exists in some way that wasn't apparent to me.
int CAN_COMMON::watchForRangeSpecific(uint8_t mailbox, uint32_t id1, uint32_t id2, bool extended)
{
uint32_t id = 0;
uint32_t mask = 0;
uint32_t temp;
}
The text was updated successfully, but these errors were encountered: