stepinside/Arduino-CRSF

UpdateChannels channels[i] bitwise operations

Opened this issue · 2 comments

Would you mind explaining the logic behind CRSF::UpdateChannels?

Specifically, why does each channels[i] get written with the data that it does? I can see the pattern in bitwise operations, but I don't know why you are doing what you are doing.

I'm not a library creator, but I can probably help you.

We need to get 11 bits (2048 values) per channel, but there are no data types for 11 bits, the closest size is 8 bits or 16 bits.

Let's go on with an example:
Let:

m_crsfData[3] = 11001100
m_crsfData[4] = 11101100

Then:

m_channels[0] = _ _ _ _ _ _ _ _  _ _ _ _ _ _ _ _

1. m_crsfData[3]
m_channels[0] = _ _ _ _ _ _ _ _  1 1 0 0 1 1 0 0

2. | m_crsfData[4] << 8 (shift 8 bits to the left and add to the previous value)
m_channels[0] = 1 1 1 0 1 1 0 0  1 1 0 0 1 1 0 0

3. & 0x07FF (remove the extra bit by using a mask 01111111 11111111)
m_channels[0] = 0 1 1 0 1 1 0 0  1 1 0 0 1 1 0 0
Done

This would be easier if there was documentation of the CRSF protocol, but I could not find it. For example, I don't understand why we should get 2048 values instead of 1024, although flight controllers work with ranges of about 1000 values