Skip to content

Commit

Permalink
Merge pull request #164 from mz-fuzzy/python3
Browse files Browse the repository at this point in the history
Python3 support added by the fuzzy one.
  • Loading branch information
TMRh20 committed Nov 13, 2015
2 parents 3652890 + cda3e18 commit 90dd53e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 38 deletions.
82 changes: 57 additions & 25 deletions RPi/pyRF24/pyRF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,101 @@ namespace bp = boost::python;
// for methods which need it - mostly for buffer operations
//

std::string read_wrap(RF24& ref, int maxlen)
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::object read_wrap(RF24& ref, int maxlen)
{
char *buf = new char[maxlen+1];
ref.read(buf, maxlen);
std::string str(buf, maxlen<ref.getPayloadSize()?maxlen:ref.getPayloadSize());
bp::object py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, maxlen<ref.getPayloadSize()?maxlen:ref.getPayloadSize())));
delete[] buf;
return str;
return py_ba;
}

bool write_wrap1(RF24& ref, std::string buf)
bool write_wrap1(RF24& ref, bp::object buf)
{
return ref.write(buf.c_str(), buf.length());
return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}

bool write_wrap2(RF24& ref, std::string buf, const bool multicast)
bool write_wrap2(RF24& ref, bp::object buf, const bool multicast)
{
return ref.write(buf.c_str(), buf.length(), multicast);
return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}

void writeAckPayload_wrap(RF24& ref, uint8_t pipe, std::string buf)
void writeAckPayload_wrap(RF24& ref, uint8_t pipe, bp::object buf)
{
ref.writeAckPayload(pipe, buf.c_str(), buf.length());
ref.writeAckPayload(pipe, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));

}

bool writeFast_wrap1(RF24& ref, std::string buf)
bool writeFast_wrap1(RF24& ref, bp::object buf)
{
return ref.writeFast(buf.c_str(), buf.length());
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}

bool writeFast_wrap2(RF24& ref, std::string buf, const bool multicast)
bool writeFast_wrap2(RF24& ref, bp::object buf, const bool multicast)
{
return ref.writeFast(buf.c_str(), buf.length(), multicast);
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}

bool writeBlocking_wrap(RF24& ref, std::string buf, uint32_t timeout)
bool writeBlocking_wrap(RF24& ref, bp::object buf, uint32_t timeout)
{
return ref.writeBlocking(buf.c_str(), buf.length(), timeout);
return ref.writeBlocking(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), timeout);
}

void startFastWrite_wrap1(RF24& ref, std::string buf, const bool multicast)
void startFastWrite_wrap1(RF24& ref, bp::object buf, const bool multicast)
{
ref.startFastWrite(buf.c_str(), buf.length(), multicast);
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}

void startFastWrite_wrap2(RF24& ref, std::string buf, const bool multicast, bool startTx)
void startFastWrite_wrap2(RF24& ref, bp::object buf, const bool multicast, bool startTx)
{
ref.startFastWrite(buf.c_str(), buf.length(), multicast, startTx);
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast, startTx);
}

void startWrite_wrap(RF24& ref, std::string buf, const bool multicast)
void startWrite_wrap(RF24& ref, bp::object buf, const bool multicast)
{
ref.startWrite(buf.c_str(), buf.length(), multicast);
ref.startWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}

void openWritingPipe_wrap(RF24& ref, const std::string address)
void openWritingPipe_wrap(RF24& ref, const bp::object address)
{
ref.openWritingPipe((const uint8_t *)(address.c_str()));
ref.openWritingPipe((const uint8_t *)(get_bytes_or_bytearray_str(address)));
}

void openReadingPipe_wrap(RF24& ref, uint8_t number, const std::string address)
void openReadingPipe_wrap(RF24& ref, uint8_t number, const bp::object address)
{
ref.openReadingPipe(number, (const uint8_t *)(address.c_str()));
ref.openReadingPipe(number, (const uint8_t *)(get_bytes_or_bytearray_str(address)));
}

bp::tuple whatHappened_wrap(RF24& ref)
Expand Down
10 changes: 8 additions & 2 deletions RPi/pyRF24/setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
#!/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_RF24 = Extension('RF24',
libraries = ['rf24-bcm', 'boost_python'],
libraries = ['rf24-bcm', BOOST_LIB],
sources = ['pyRF24.cpp'])

setup(name='RF24',
version='1.0',
version='1.1',
ext_modules=[module_RF24]
)
23 changes: 12 additions & 11 deletions examples_RPi/pingpair_dyn.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# This is an example of how to use payloads of a varying (dynamic) size.
#

from __future__ import print_function
import time
from RF24 import *

Expand Down Expand Up @@ -37,26 +38,26 @@
payload_size_increments_by = 1
next_payload_size = min_payload_size
inp_role = 'none'
send_payload = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
send_payload = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
millis = lambda: int(round(time.time() * 1000))

print 'pyRF24/examples/pingpair_dyn/'
print('pyRF24/examples/pingpair_dyn/')
radio.begin()
radio.enableDynamicPayloads()
radio.setRetries(5,15)
radio.printDetails()

print ' ************ Role Setup *********** '
print(' ************ Role Setup *********** ')
while (inp_role !='0') and (inp_role !='1'):
inp_role = raw_input('Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) ')
inp_role = str(input('Choose a role: Enter 0 for receiver, 1 for transmitter (CTRL+C to exit) '))

if inp_role == '0':
print 'Role: Pong Back, awaiting transmission'
print('Role: Pong Back, awaiting transmission')
radio.openWritingPipe(pipes[1])
radio.openReadingPipe(1,pipes[0])
radio.startListening()
else:
print 'Role: Ping Out, starting transmission'
print('Role: Ping Out, starting transmission')
radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1,pipes[1])

Expand All @@ -69,7 +70,7 @@
radio.stopListening()

# Take the time, and send it. This will block until complete
print 'Now sending length ', next_payload_size, ' ... ',
print('Now sending length {} ... '.format(next_payload_size), end="")
radio.write(send_payload[:next_payload_size])

# Now, continue listening
Expand All @@ -84,14 +85,14 @@

# Describe the results
if timeout:
print 'failed, response timed out.'
print('failed, response timed out.')
else:
# Grab the response, compare, and send to debugging spew
len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len)

# Spew it
print 'got response size=', len, ' value="', receive_payload, '"'
print('got response size={} value="{}"'.format(len, receive_payload.decode('utf-8')))

# Update size for next time.
next_payload_size += payload_size_increments_by
Expand All @@ -109,14 +110,14 @@
receive_payload = radio.read(len)

# Spew it
print 'Got payload size=', len, ' value="', receive_payload, '"'
print('Got payload size={} value="{}"'.format(len, receive_payload.decode('utf-8')))

# First, stop listening so we can talk
radio.stopListening()

# Send the final one back.
radio.write(receive_payload)
print 'Sent response.'
print('Sent response.')

# Now, resume listening so we catch the next packets.
radio.startListening()
Expand Down

0 comments on commit 90dd53e

Please sign in to comment.