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.
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.
The functions
lightningThreshold()
andchangeDivRatio()
appear to be callingwriteRegister
incorrectly. In both cases, when trying to set the register's bit field to 3 (both bits set), it passes pre-shifted values towriteRegister
, butwriteRegister
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) toSTRIKES_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.