sparkfun / SparkFun_AS3935_Lightning_Detector_Arduino_Library

Other
37 stars 19 forks source link

Fix setting of lightningThreshold and changeDivRatio #4

Closed mday64 closed 5 years ago

mday64 commented 5 years ago

The functions lightningThreshold() and changeDivRatio() appear to be calling writeRegister incorrectly. In both cases, when trying to set the register's bit field to 3 (both bits set), it passes pre-shifted values to writeRegister, but writeRegister then shifts that input value by _startPosition bits. This means it will set the wrong bits in the register.

I chose to refactor the code a little. It was already sanity checking the allowed input values. I combined that with converting the input value to the corresponding bit pattern in the register. Then there is a single call to writeRegister to store that bit pattern in the register. That makes the read and write routines more symmetric.

Also, I replaced LIGHT_MASK (zero bits for the field controlling the number of strikes to trigger an interrupt) to STRIKES_MASK (one bits for that field). That means that the mask doesn't have to be inverted, which seems more straightforward and cleaner to me. But I understand that could just be a personal style preference.

edspark commented 5 years ago

Thank you one last time. I ended up keeping what I have with the small caveat that I fixed the particular lines in the function calls that were incorrect. I'll practice less flagrant bit inversions in the future.