Skip to content

Commit

Permalink
Update get*_()
Browse files Browse the repository at this point in the history
- avoids a condition where only pipe 0 has auto_ack enabled and the attribute disables it.

- refresh internal data when calling get_*() and decorated setters. Applies to auto_ack, dynamic_payloads, and payload_length
  • Loading branch information
2bndy5 authored Jan 29, 2021
1 parent 813002e commit 9b8e3c2
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions circuitpython_nrf24l01/rf24.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def dynamic_payloads(self, enable):
elif isinstance(enable, int):
self._dyn_pl = 0x3F & enable
elif isinstance(enable, (list, tuple)):
self._dyn_pl = self._reg_read(DYN_PL_LEN)
for i, val in enumerate(enable):
if i < 6 and val >= 0: # skip pipe if val is negative
self._dyn_pl = (self._dyn_pl & ~(1 << i)) | (bool(val) << i)
Expand All @@ -501,7 +502,7 @@ def set_dynamic_payloads(self, enable, pipe_number=None):
if pipe_number is None:
self.dynamic_payloads = bool(enable)
elif 0 <= pipe_number <= 5:
self._dyn_pl &= ~(1 << pipe_number)
self._dyn_pl = self._reg_read(DYN_PL_LEN) & ~(1 << pipe_number)
self.dynamic_payloads = self._dyn_pl | (bool(enable) << pipe_number)
else:
raise IndexError("pipe_number must be in range [0, 5]")
Expand Down Expand Up @@ -541,6 +542,7 @@ def set_payload_length(self, length, pipe_number=None):
def get_payload_length(self, pipe_number=0):
"""Returns an `int` describing the current setting of a specified data
pipe's expected static payload length."""
self._pl_len[pipe_number] = self._reg_read(RX_PL_LENG + pipe_number)
return self._pl_len[pipe_number]

@property
Expand Down Expand Up @@ -602,12 +604,13 @@ def auto_ack(self, enable):
self._aa = 0x3F & enable
elif isinstance(enable, (list, tuple)):
for i, val in enumerate(enable):
self._aa = self._reg_read(AUTO_ACK)
if i < 6 and val >= 0: # skip pipe if val is negative
self._aa = (self._aa & ~(1 << i)) | (bool(val) << i)
else:
raise ValueError("auto_ack: {} is not a valid input" % enable)
if bool(self._aa & 1) != bool(self._aa & 0x3E):
self._aa &= 1
if bool(self._aa & 1) != bool(self._aa & 0x3E) and self._aa & 0x3E:
self._aa |= 1
self._reg_write(AUTO_ACK, self._aa)

def set_auto_ack(self, enable, pipe_number=None):
Expand All @@ -616,7 +619,7 @@ def set_auto_ack(self, enable, pipe_number=None):
if pipe_number is None:
self.auto_ack = bool(enable)
elif 0 <= pipe_number <= 5:
self._aa &= ~(1 << pipe_number)
self._aa = self._reg_read(AUTO_ACK) & ~(1 << pipe_number)
self.auto_ack = self._aa | (bool(enable) << pipe_number)
else:
raise IndexError("pipe_number must be in range [0, 5]")
Expand All @@ -625,6 +628,7 @@ def get_auto_ack(self, pipe_number=0):
"""Returns a `bool` describing the automatic acknowledgement feature
setting about a specific data pipe."""
if 0 <= pipe_number <= 5:
self._aa = self._reg_read(AUTO_ACK)
return bool(self._aa & (1 << pipe_number))
raise IndexError("pipe_number must be in range [0, 5]")

Expand Down

0 comments on commit 9b8e3c2

Please sign in to comment.