Clean up, simplify, and correct some energy meter code #810
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Much of this code was copied from the low-level firmware side, where we don't have floating point available, and had to use some tricks to ensure we didn't lose precision. (I should know- I wrote it!) Since this doesn't apply in our ESP32 code, we can just multiply the three values (volts, amps, milliseconds) together to get milliwatt-seconds.
Additionally, we should store elapsed as a floating point value, not an integer number of seconds. As we're going through the pool loop, which repeats every 1000 ms, we might not hit that value exactly. Imagine we are closer to 1020 ms instead due to the work we need to do each iteration. That would allow our elapsed second counter, which stores a truncated value (1020ms gets rounded to integer 1 second), to drift by 1 second every 50 seconds.