chandrawi / LoRaRF-Arduino

Arduino library for basic transmitting and receiving data using LoRa and FSK modulation
MIT License
53 stars 15 forks source link

suspicious uint8_t shift #12

Open lunamera opened 1 year ago

lunamera commented 1 year ago

Hi,

the getStatus method isn't working due to wrong shift operation.

void sx126x_getStats(uint16_t* nbPktReceived, uint16_t* nbPktCrcError, uint16_t* nbPktHeaderErr)
{
    uint8_t buf[7];
    sx126x_transfer(0x10, buf, 7);
    *nbPktReceived = (buf[1] >> 8) | buf[2];
    *nbPktCrcError = (buf[3] >> 8) | buf[4];
    *nbPktHeaderErr = (buf[5] >> 8) | buf[6];
}

"(buf[x] >> 8)" always equals to zero and it's maybe also in the wrong direction.

Should be like: nbPktReceived = buf[1]; nbPktReceived <<= 8; *nbPktReceived += buf[2];

chandrawi commented 1 year ago

Yeah, it is a bug. It should be left shift instead right shift. Your suggestion is correct. Another solution to write correct code in one line is like the follow

*nbPktReceived = (uint16_t) buf[1] << 8 + buf[2];

You are welcome to create a pull request for this fix.