openenergymonitor / emontx3

EmonTx V3.4.x Hardware and older discreet firmware, see EmonTxV3CM repo for latest
https://shop.openenergymonitor.com/emontx-v3-electricity-monitoring-transmitter/
GNU General Public License v3.0
57 stars 46 forks source link

Fix two possible underflows that can generate long delays in loop #10

Closed betarho closed 5 years ago

betarho commented 5 years ago

In case the runtime calculated at the end of the loop is greater than 9900 ms, the calculated sleeptime underflows. Being it an unsigned long, the resulting value would be about 2^32 milliseconds (about 50 days). This value is used as parameter in a delay() function to wait for the next acqusition cycle, if AC-AC adapter is used. Even in case of battery powered system, the time calculated inside the Sleepy::loseSomeTime() function could underflow if sleeptime is smaller than 500 ms. Being the parameter a word, the resulting value would be about 2^16 (about 65 seconds).

This patch avoids both the underflows. In case of underflow, a delay(0) or a Sleepy::loseSomeTime(0) is executed, bringing the system to immediately start the next acquisition cycle.

Signed-off-by: Roberto Bonacina roby.bonacina@gmail.com

glynhudson commented 5 years ago

Thanks, that's really useful. I will setup a test and include this fix in V3.2 beta release