Skip to content

Commit 6c1cab8

Browse files
committed
it seems sometimes reading mclr button over i2c can fail, add retry loop
1 parent cbb71df commit 6c1cab8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

pysense-2/lib/pycoproc.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,16 @@ def calibrate_rtc(self):
320320
time.sleep(0.5)
321321

322322
def button_pressed(self):
323-
button = self.read_bit(PORTA_ADDR, 3)
324-
return not button
323+
retry = 0
324+
while True:
325+
try:
326+
button = self.read_bit(PORTA_ADDR, 3)
327+
return not button
328+
except Exception as e:
329+
if retry > 10:
330+
raise Exception('Failed to read button state: {}'.format(e))
331+
print("Failed to read button state, retry ... ({}, {})".format(retry, e))
332+
retry += 1
325333

326334
def read_battery_voltage(self):
327335
self.set_bits_in_memory(ADCON0_ADDR, _ADCON0_GO_nDONE_MASK)

0 commit comments

Comments
 (0)