Skip to content

Commit

Permalink
update python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Jun 16, 2022
1 parent e9271fd commit a20c632
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 83 deletions.
56 changes: 0 additions & 56 deletions RPi/pyRF24Network/examples/dev_test.py

This file was deleted.

30 changes: 13 additions & 17 deletions RPi/pyRF24Network/examples/helloworld_rx.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Simplest possible example of using RF24Network in RX role.
Listens for messages from the transmitter and prints them out.
"""
import time
import struct
from RF24 import RF24
from RF24Network import RF24Network
Expand Down Expand Up @@ -45,20 +44,17 @@
radio.printPrettyDetails()

radio.startListening() # put radio in RX mode
start = time.monotonic()
while time.monotonic() - start <= 6: # listen for 6 seconds
network.update()
while network.available():
header, payload = network.read(8)
print("payload length ", len(payload))
millis, number = struct.unpack('<LL', bytes(payload))
print(
"Received payload {} from {} to {} at (origin's timestamp) {}".format(
number,
oct(header.from_node),
oct(header.to_node),
millis,
try:
while True:
network.update()
while network.available():
header, payload = network.read(8)
print("payload length ", len(payload))
millis, number = struct.unpack("<LL", bytes(payload))
print(
f"Received payload {number} from {oct(header.from_node)}",
f"to {oct(header.to_node)} at (origin's timestamp) {millis}",
)
)
start = time.monotonic()
time.sleep(0.05)
except KeyboardInterrupt:
print("powering down radio and exiting.")
radio.powerDown()
24 changes: 14 additions & 10 deletions RPi/pyRF24Network/examples/helloworld_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@
packets_sent = 0
last_sent = 0

while 1:
network.update()
now = time.monotonic_ns() / 1000
# If it's time to send a message, send it!
if now - last_sent >= interval:
last_sent = now
payload = struct.pack('<LL', now, packets_sent)
packets_sent += 1
ok = network.write(RF24NetworkHeader(other_node), payload)
print("Sending %d..." % packets_sent, "ok." if ok else "failed.")
try:
while True:
network.update()
now = int(time.monotonic_ns() / 1000000)
# If it's time to send a message, send it!
if now - last_sent >= interval:
last_sent = now
packets_sent += 1
payload = struct.pack("<LL", now, packets_sent)
ok = network.write(RF24NetworkHeader(other_node), payload)
print(f"Sending {packets_sent}...", "ok." if ok else "failed.")
except KeyboardInterrupt:
print("powering down radio and exiting.")
radio.powerDown()

0 comments on commit a20c632

Please sign in to comment.