Commit 90dd53e1 authored by TMRh20's avatar TMRh20

Merge pull request #164 from mz-fuzzy/python3

Python3 support added by the fuzzy one.
parents 36528902 cda3e18b
...@@ -7,69 +7,101 @@ namespace bp = boost::python; ...@@ -7,69 +7,101 @@ namespace bp = boost::python;
// for methods which need it - mostly for buffer operations // 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]; char *buf = new char[maxlen+1];
ref.read(buf, maxlen); 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; 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) bp::tuple whatHappened_wrap(RF24& ref)
......
#!/usr/bin/env python #!/usr/bin/env python
from distutils.core import setup, Extension 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', module_RF24 = Extension('RF24',
libraries = ['rf24-bcm', 'boost_python'], libraries = ['rf24-bcm', BOOST_LIB],
sources = ['pyRF24.cpp']) sources = ['pyRF24.cpp'])
setup(name='RF24', setup(name='RF24',
version='1.0', version='1.1',
ext_modules=[module_RF24] ext_modules=[module_RF24]
) )
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
# This is an example of how to use payloads of a varying (dynamic) size. # This is an example of how to use payloads of a varying (dynamic) size.
# #
from __future__ import print_function
import time import time
from RF24 import * from RF24 import *
...@@ -37,26 +38,26 @@ max_payload_size = 32 ...@@ -37,26 +38,26 @@ max_payload_size = 32
payload_size_increments_by = 1 payload_size_increments_by = 1
next_payload_size = min_payload_size next_payload_size = min_payload_size
inp_role = 'none' inp_role = 'none'
send_payload = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012' send_payload = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ789012'
millis = lambda: int(round(time.time() * 1000)) millis = lambda: int(round(time.time() * 1000))
print 'pyRF24/examples/pingpair_dyn/' print('pyRF24/examples/pingpair_dyn/')
radio.begin() radio.begin()
radio.enableDynamicPayloads() radio.enableDynamicPayloads()
radio.setRetries(5,15) radio.setRetries(5,15)
radio.printDetails() radio.printDetails()
print ' ************ Role Setup *********** ' print(' ************ Role Setup *********** ')
while (inp_role !='0') and (inp_role !='1'): 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': if inp_role == '0':
print 'Role: Pong Back, awaiting transmission' print('Role: Pong Back, awaiting transmission')
radio.openWritingPipe(pipes[1]) radio.openWritingPipe(pipes[1])
radio.openReadingPipe(1,pipes[0]) radio.openReadingPipe(1,pipes[0])
radio.startListening() radio.startListening()
else: else:
print 'Role: Ping Out, starting transmission' print('Role: Ping Out, starting transmission')
radio.openWritingPipe(pipes[0]) radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1,pipes[1]) radio.openReadingPipe(1,pipes[1])
...@@ -69,7 +70,7 @@ while 1: ...@@ -69,7 +70,7 @@ while 1:
radio.stopListening() radio.stopListening()
# Take the time, and send it. This will block until complete # 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]) radio.write(send_payload[:next_payload_size])
# Now, continue listening # Now, continue listening
...@@ -84,14 +85,14 @@ while 1: ...@@ -84,14 +85,14 @@ while 1:
# Describe the results # Describe the results
if timeout: if timeout:
print 'failed, response timed out.' print('failed, response timed out.')
else: else:
# Grab the response, compare, and send to debugging spew # Grab the response, compare, and send to debugging spew
len = radio.getDynamicPayloadSize() len = radio.getDynamicPayloadSize()
receive_payload = radio.read(len) receive_payload = radio.read(len)
# Spew it # 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. # Update size for next time.
next_payload_size += payload_size_increments_by next_payload_size += payload_size_increments_by
...@@ -109,14 +110,14 @@ while 1: ...@@ -109,14 +110,14 @@ while 1:
receive_payload = radio.read(len) receive_payload = radio.read(len)
# Spew it # 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 # First, stop listening so we can talk
radio.stopListening() radio.stopListening()
# Send the final one back. # Send the final one back.
radio.write(receive_payload) radio.write(receive_payload)
print 'Sent response.' print('Sent response.')
# Now, resume listening so we catch the next packets. # Now, resume listening so we catch the next packets.
radio.startListening() radio.startListening()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment