Skip to content

Commit

Permalink
Merge pull request #68 from joernesdohr/master
Browse files Browse the repository at this point in the history
Adds Python 3 support, Python read(), write() functions consume and produce binary data now
  • Loading branch information
TMRh20 committed Nov 27, 2015
2 parents 612b569 + 61be0bf commit 53b0a75
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 37 deletions.
25 changes: 13 additions & 12 deletions RPi/pyRF24Network/examples/helloworld_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# RECEIVER NODE
# Listens for messages from the transmitter and prints them out.
#

from __future__ import print_function
import time
from struct import *
from RF24 import *
Expand All @@ -24,19 +24,19 @@
#radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)

# Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
#radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)

radio = RF24(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ)
network = RF24Network(radio)

# Address of our node in Octal format (01,021, etc)
this_node = 00

# Address of the other node
other_node = 01
millis = lambda: int(round(time.time() * 1000))
octlit = lambda n:int(n, 8)

interval = 2000 #ms - How often to send 'hello world' to the other unit
# Address of our node in Octal format (01, 021, etc)
this_node = octlit("00")

millis = lambda: int(round(time.time() * 1000))
# Address of the other node
other_node = octlit("01")

radio.begin()
time.sleep(0.1)
Expand All @@ -48,8 +48,9 @@
while 1:
network.update()
while network.available():
header, payload = network.read(12)
ms, number = unpack('<qi', payload)
print 'Received payload # ', number, ' at ', ms, ' from ', oct(header.from_node)
header, payload = network.read(8)
print("payload length ", len(payload))
ms, number = unpack('<LL', bytes(payload))
print('Received payload ', number, ' at ', ms, ' from ', oct(header.from_node))
time.sleep(1)

29 changes: 17 additions & 12 deletions RPi/pyRF24Network/examples/helloworld_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# TRANSMITTER NODE
# Sends messages from to receiver.
#

from __future__ import print_function
import time
from struct import *
from RF24 import *
Expand All @@ -24,36 +24,41 @@
#radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_8MHZ)

# Setup for GPIO 22 CE and CE0 CSN for RPi B+ with SPI Speed @ 8Mhz
radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)
#radio = RF24(RPI_BPLUS_GPIO_J8_22, RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ)

radio = RF24(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ)
network = RF24Network(radio)

millis = lambda: int(round(time.time() * 1000)) & 0xffffffff
octlit = lambda n:int(n, 8)

# Address of our node in Octal format (01,021, etc)
this_node = 01
this_node = octlit("01")

# Address of the other node
other_node = 00
other_node = octlit("00")

interval = 2000 #ms - How often to send 'hello world' to the other unit

millis = lambda: int(round(time.time() * 1000))
#ms - How long to wait before sending the next message
interval = 2000

radio.begin()
time.sleep(0.1);
network.begin(90, this_node) # channel 90
radio.printDetails()
packets_sent = 0
last_sent = 0

while 1:
network.update()
now = millis() # If it's time to send a message, send it!
now = millis()
# If it's time to send a message, send it!
if ( now - last_sent >= interval ):
last_sent = now
print 'Sending ..',
payload = pack('<qi', millis(), packets_sent )
print('Sending ..')
payload = pack('<LL', millis(), packets_sent )
packets_sent += 1
ok = network.write(RF24NetworkHeader(other_node), payload)
if ok:
print 'ok.'
print('ok.')
else:
print 'failed.'
print('failed.')
55 changes: 43 additions & 12 deletions RPi/pyRF24Network/pyRF24Network.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,59 @@
#include "boost/python.hpp"

#include "RF24Network/RF24Network.h"
#include "RF24/RF24.h"
#include "RF24Network/RF24Network.h"

namespace bp = boost::python;

// **************** expicit wrappers *****************
// where needed, especially where buffer is involved
//
void throw_ba_exception(void)
{
PyErr_SetString(PyExc_TypeError, "buf parameter must be bytes or bytearray");
bp::throw_error_already_set();
}

char *get_bytes_or_bytearray_str(bp::object buf)
{
PyObject *py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_AsString(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_AsString(py_ba);
else
throw_ba_exception();
return NULL;
}

int get_bytes_or_bytearray_ln(bp::object buf)
{
PyObject *py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_Size(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_Size(py_ba);
else
throw_ba_exception();
return 0;
}

bp::tuple read_wrap(RF24Network& ref, size_t maxlen)
{
size_t len;
char *buf = new char[maxlen+1];
char *buf = new char[maxlen+1];
RF24NetworkHeader header;

len=ref.read(header, buf, maxlen);
std::string str(buf, len);
delete[] buf;
return bp::make_tuple(header, str);
ref.read(header, buf, maxlen);
bp::object py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, maxlen)));
delete[] buf;

return bp::make_tuple(header, py_ba);
}

bool write_wrap(RF24Network& ref, RF24NetworkHeader& header, std::string message)
bool write_wrap(RF24Network& ref, RF24NetworkHeader& header, bp::object buf)
{
return ref.write(header, message.c_str(), message.length());
return ref.write(header, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}

std::string toString_wrap(RF24NetworkHeader& ref)
Expand Down Expand Up @@ -99,9 +130,9 @@ BOOST_PYTHON_MODULE(RF24Network){
}
{ //::RF24Network::write

typedef bool ( *write_function_type )( ::RF24Network&, ::RF24NetworkHeader&, std::string ) ;
typedef bool ( *write_function_type )( ::RF24Network&, ::RF24NetworkHeader&, bp::object ) ;

RF24Network_exposer.def("write", write_function_type( &write_wrap ), ( bp::arg("header"), bp::arg("message") ) );
RF24Network_exposer.def("write", write_function_type( &write_wrap ), ( bp::arg("header"), bp::arg("buf") ) );

}
RF24Network_exposer.def_readwrite( "txTimeout", &RF24Network::txTimeout );
Expand Down
8 changes: 7 additions & 1 deletion RPi/pyRF24Network/setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#!/usr/bin/env python

from distutils.core import setup, Extension
import sys

if sys.version_info >= (3,):
BOOST_LIB = 'boost_python3'
else:
BOOST_LIB = 'boost_python'

module_RF24Network = Extension('RF24Network',
libraries = ['rf24network', 'boost_python'],
libraries = ['rf24network', BOOST_LIB],
sources = ['pyRF24Network.cpp'])

setup(name='RF24Network',
Expand Down

0 comments on commit 53b0a75

Please sign in to comment.