From cda3e18b0d011fca4e65100918ab6bb3477655b1 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 23 Oct 2015 02:27:42 +0000 Subject: [PATCH] python3 support added Signed-off-by: root --- RPi/pyRF24/pyRF24.cpp | 82 +++++++++++++++++++++++++----------- RPi/pyRF24/setup.py | 10 ++++- examples_RPi/pingpair_dyn.py | 23 +++++----- 3 files changed, 77 insertions(+), 38 deletions(-) diff --git a/RPi/pyRF24/pyRF24.cpp b/RPi/pyRF24/pyRF24.cpp index 8a3ade46b..a7f8c5bcc 100644 --- a/RPi/pyRF24/pyRF24.cpp +++ b/RPi/pyRF24/pyRF24.cpp @@ -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(PyByteArray_FromStringAndSize(buf, maxlen= (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] ) diff --git a/examples_RPi/pingpair_dyn.py b/examples_RPi/pingpair_dyn.py index d386f7383..0459ef4cb 100755 --- a/examples_RPi/pingpair_dyn.py +++ b/examples_RPi/pingpair_dyn.py @@ -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 * @@ -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]) @@ -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 @@ -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 @@ -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()