Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No watchForRangeSpecific(mailbox, id1, id2, extended) #9

Open
WhyDoIDothisToMyself opened this issue Dec 16, 2020 · 0 comments
Open

Comments

@WhyDoIDothisToMyself
Copy link

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)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant