Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race condition with receiving messages #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions RFM69.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ def sendWithRetry(self, toAddress, buff = "", retries = 3, retryWaitTime = 10):
sentTime = time.time()
while (time.time() - sentTime) * 1000 < retryWaitTime:
if self.ACKReceived(toAddress):
self.receiveBegin()
return True
return False

Expand Down Expand Up @@ -295,6 +296,8 @@ def receiveBegin(self):
self.setMode(RF69_MODE_RX)

def receiveDone(self):
while self.intLock:
pass # wait for lock to be removed
if (self.mode == RF69_MODE_RX or self.mode == RF69_MODE_STANDBY) and self.PAYLOADLEN > 0:
self.setMode(RF69_MODE_STANDBY)
return True
Expand All @@ -303,10 +306,6 @@ def receiveDone(self):
# Russss figured out that if you leave alone long enough it times out
# tell it to stop being silly and listen for more packets
self.writeReg(REG_PACKETCONFIG2, (self.readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART)
elif self.mode == RF69_MODE_RX:
# already in RX no payload yet
return False
self.receiveBegin()
return False

def readRSSI(self, forceTrigger = False):
Expand Down
13 changes: 7 additions & 6 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
if test.sendWithRetry(2, "blah", 3, 20):
print "ack recieved"
print "reading"
test.receiveBegin()
while True:
test.receiveBegin()
while not test.receiveDone():
time.sleep(.1)
print "%s from %s RSSI:%s" % ("".join([chr(letter) for letter in test.DATA]), test.SENDERID, test.RSSI)
if test.ACKRequested():
test.sendACK()
if test.receiveDone():
print "%s from %s RSSI:%s" % ("".join([chr(letter) for letter in test.DATA]), test.SENDERID, test.RSSI)
if test.ACKRequested():
test.sendACK()
else:
test.receiveBegin()
print "shutting down"
test.shutdown()