初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
import sys
from distutils import unixccompiler
from distutils import ccompiler
def register():
sys.modules['distutils.crossunixccompiler'] = sys.modules[__name__]
ccompiler.compiler_class['crossunix'] = (__name__,
'CrossUnixCCompiler',
'UNIX-style compiler for cross compilation')
def try_remove_all(lst, starts):
lst[:] = [x for x in lst if not x.startswith(starts)]
class CrossUnixCCompiler(unixccompiler.UnixCCompiler):
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
try_remove_all(self.compiler_so, ('-m64', '-fstack-protector-strong', '-mtune=generic'))
try_remove_all(cc_args, '-I/usr')
try_remove_all(pp_opts, '-I/usr')
return unixccompiler.UnixCCompiler._compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
def link(self, target_desc, objects,
output_filename, output_dir=None, libraries=None,
library_dirs=None, runtime_library_dirs=None,
export_symbols=None, debug=0, extra_preargs=None,
extra_postargs=None, build_temp=None, target_lang=None):
try_remove_all(self.library_dirs, ('/usr'))
return unixccompiler.UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries,
library_dirs, runtime_library_dirs, export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)
def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
self.__class__ = unixccompiler.UnixCCompiler
ret = unixccompiler.UnixCCompiler._fix_lib_args(self, libraries, library_dirs, runtime_library_dirs)
self.__class__ = CrossUnixCCompiler
return ret

View File

@@ -0,0 +1,313 @@
#include <RF24/RF24.h>
#include <boost/python.hpp>
namespace bp = boost::python;
// ******************** explicit wrappers **************************
// for methods which need it - mostly for buffer operations
//
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);
bp::object
py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, maxlen < ref.getPayloadSize() ? maxlen : ref.getPayloadSize())));
delete[] buf;
return py_ba;
}
bool write_wrap1(RF24& ref, bp::object buf)
{
return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool write_wrap2(RF24& ref, bp::object buf, const bool 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, bp::object buf)
{
ref.writeAckPayload(pipe, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool writeFast_wrap1(RF24& ref, bp::object buf)
{
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool writeFast_wrap2(RF24& ref, bp::object buf, const bool multicast)
{
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
bool writeBlocking_wrap(RF24& ref, bp::object buf, uint32_t timeout)
{
return ref.writeBlocking(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), timeout);
}
void startFastWrite_wrap1(RF24& ref, bp::object buf, const bool multicast)
{
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
void startFastWrite_wrap2(RF24& ref, bp::object buf, const bool multicast, bool startTx)
{
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast, startTx);
}
void startWrite_wrap(RF24& ref, bp::object buf, const bool multicast)
{
ref.startWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
void openWritingPipe_wrap(RF24& ref, const bp::object address)
{
ref.openWritingPipe((const uint8_t*) (get_bytes_or_bytearray_str(address)));
}
void openReadingPipe_wrap(RF24& ref, uint8_t number, const bp::object address)
{
ref.openReadingPipe(number, (const uint8_t*) (get_bytes_or_bytearray_str(address)));
}
bp::tuple whatHappened_wrap(RF24& ref)
{
bool tx_ok;
bool tx_fail;
bool tx_ready;
ref.whatHappened(tx_ok, tx_fail, tx_ready);
return bp::make_tuple(tx_ok, tx_fail, tx_ready);
}
bp::tuple available_wrap(RF24& ref)
{
bool result;
uint8_t pipe;
result = ref.available(&pipe);
return bp::make_tuple(result, pipe);
}
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(txStandBy_wrap1, RF24::txStandBy,
0, 2)
//BOOST_PYTHON_FUNCTION_OVERLOADS(txStandBy_wrap2, RF24::txStandBy, 1, 2)
// ******************** enums **************************
// from both RF24 and bcm2835
//
BOOST_PYTHON_MODULE(RF24){
#ifdef BCM2835_H
bp::enum_< RPiGPIOPin>("RPiGPIOPin")
.value("RPI_GPIO_P1_03", RPI_GPIO_P1_03)
.value("RPI_GPIO_P1_05", RPI_GPIO_P1_05)
.value("RPI_GPIO_P1_07", RPI_GPIO_P1_07)
.value("RPI_GPIO_P1_08", RPI_GPIO_P1_08)
.value("RPI_GPIO_P1_10", RPI_GPIO_P1_10)
.value("RPI_GPIO_P1_11", RPI_GPIO_P1_11)
.value("RPI_GPIO_P1_12", RPI_GPIO_P1_12)
.value("RPI_GPIO_P1_13", RPI_GPIO_P1_13)
.value("RPI_GPIO_P1_15", RPI_GPIO_P1_15)
.value("RPI_GPIO_P1_16", RPI_GPIO_P1_16)
.value("RPI_GPIO_P1_18", RPI_GPIO_P1_18)
.value("RPI_GPIO_P1_19", RPI_GPIO_P1_19)
.value("RPI_GPIO_P1_21", RPI_GPIO_P1_21)
.value("RPI_GPIO_P1_22", RPI_GPIO_P1_22)
.value("RPI_GPIO_P1_23", RPI_GPIO_P1_23)
.value("RPI_GPIO_P1_24", RPI_GPIO_P1_24)
.value("RPI_GPIO_P1_26", RPI_GPIO_P1_26)
.value("RPI_V2_GPIO_P1_03", RPI_V2_GPIO_P1_03)
.value("RPI_V2_GPIO_P1_05", RPI_V2_GPIO_P1_05)
.value("RPI_V2_GPIO_P1_07", RPI_V2_GPIO_P1_07)
.value("RPI_V2_GPIO_P1_08", RPI_V2_GPIO_P1_08)
.value("RPI_V2_GPIO_P1_10", RPI_V2_GPIO_P1_10)
.value("RPI_V2_GPIO_P1_11", RPI_V2_GPIO_P1_11)
.value("RPI_V2_GPIO_P1_12", RPI_V2_GPIO_P1_12)
.value("RPI_V2_GPIO_P1_13", RPI_V2_GPIO_P1_13)
.value("RPI_V2_GPIO_P1_15", RPI_V2_GPIO_P1_15)
.value("RPI_V2_GPIO_P1_16", RPI_V2_GPIO_P1_16)
.value("RPI_V2_GPIO_P1_18", RPI_V2_GPIO_P1_18)
.value("RPI_V2_GPIO_P1_19", RPI_V2_GPIO_P1_19)
.value("RPI_V2_GPIO_P1_21", RPI_V2_GPIO_P1_21)
.value("RPI_V2_GPIO_P1_22", RPI_V2_GPIO_P1_22)
.value("RPI_V2_GPIO_P1_23", RPI_V2_GPIO_P1_23)
.value("RPI_V2_GPIO_P1_24", RPI_V2_GPIO_P1_24)
.value("RPI_V2_GPIO_P1_26", RPI_V2_GPIO_P1_26)
.value("RPI_V2_GPIO_P5_03", RPI_V2_GPIO_P5_03)
.value("RPI_V2_GPIO_P5_04", RPI_V2_GPIO_P5_04)
.value("RPI_V2_GPIO_P5_05", RPI_V2_GPIO_P5_05)
.value("RPI_V2_GPIO_P5_06", RPI_V2_GPIO_P5_06)
.value("RPI_BPLUS_GPIO_J8_03", RPI_BPLUS_GPIO_J8_03)
.value("RPI_BPLUS_GPIO_J8_05", RPI_BPLUS_GPIO_J8_05)
.value("RPI_BPLUS_GPIO_J8_07", RPI_BPLUS_GPIO_J8_07)
.value("RPI_BPLUS_GPIO_J8_08", RPI_BPLUS_GPIO_J8_08)
.value("RPI_BPLUS_GPIO_J8_10", RPI_BPLUS_GPIO_J8_10)
.value("RPI_BPLUS_GPIO_J8_11", RPI_BPLUS_GPIO_J8_11)
.value("RPI_BPLUS_GPIO_J8_12", RPI_BPLUS_GPIO_J8_12)
.value("RPI_BPLUS_GPIO_J8_13", RPI_BPLUS_GPIO_J8_13)
.value("RPI_BPLUS_GPIO_J8_15", RPI_BPLUS_GPIO_J8_15)
.value("RPI_BPLUS_GPIO_J8_16", RPI_BPLUS_GPIO_J8_16)
.value("RPI_BPLUS_GPIO_J8_18", RPI_BPLUS_GPIO_J8_18)
.value("RPI_BPLUS_GPIO_J8_19", RPI_BPLUS_GPIO_J8_19)
.value("RPI_BPLUS_GPIO_J8_21", RPI_BPLUS_GPIO_J8_21)
.value("RPI_BPLUS_GPIO_J8_22", RPI_BPLUS_GPIO_J8_22)
.value("RPI_BPLUS_GPIO_J8_23", RPI_BPLUS_GPIO_J8_23)
.value("RPI_BPLUS_GPIO_J8_24", RPI_BPLUS_GPIO_J8_24)
.value("RPI_BPLUS_GPIO_J8_26", RPI_BPLUS_GPIO_J8_26)
.value("RPI_BPLUS_GPIO_J8_29", RPI_BPLUS_GPIO_J8_29)
.value("RPI_BPLUS_GPIO_J8_31", RPI_BPLUS_GPIO_J8_31)
.value("RPI_BPLUS_GPIO_J8_32", RPI_BPLUS_GPIO_J8_32)
.value("RPI_BPLUS_GPIO_J8_33", RPI_BPLUS_GPIO_J8_33)
.value("RPI_BPLUS_GPIO_J8_35", RPI_BPLUS_GPIO_J8_35)
.value("RPI_BPLUS_GPIO_J8_36", RPI_BPLUS_GPIO_J8_36)
.value("RPI_BPLUS_GPIO_J8_37", RPI_BPLUS_GPIO_J8_37)
.value("RPI_BPLUS_GPIO_J8_38", RPI_BPLUS_GPIO_J8_38)
.value("RPI_BPLUS_GPIO_J8_40", RPI_BPLUS_GPIO_J8_40)
.export_values()
;
bp::enum_< bcm2835SPIClockDivider>("bcm2835SPIClockDivider")
.value("BCM2835_SPI_CLOCK_DIVIDER_65536", BCM2835_SPI_CLOCK_DIVIDER_65536)
.value("BCM2835_SPI_CLOCK_DIVIDER_32768", BCM2835_SPI_CLOCK_DIVIDER_32768)
.value("BCM2835_SPI_CLOCK_DIVIDER_16384", BCM2835_SPI_CLOCK_DIVIDER_16384)
.value("BCM2835_SPI_CLOCK_DIVIDER_8192", BCM2835_SPI_CLOCK_DIVIDER_8192)
.value("BCM2835_SPI_CLOCK_DIVIDER_4096", BCM2835_SPI_CLOCK_DIVIDER_4096)
.value("BCM2835_SPI_CLOCK_DIVIDER_2048", BCM2835_SPI_CLOCK_DIVIDER_2048)
.value("BCM2835_SPI_CLOCK_DIVIDER_1024", BCM2835_SPI_CLOCK_DIVIDER_1024)
.value("BCM2835_SPI_CLOCK_DIVIDER_512", BCM2835_SPI_CLOCK_DIVIDER_512)
.value("BCM2835_SPI_CLOCK_DIVIDER_256", BCM2835_SPI_CLOCK_DIVIDER_256)
.value("BCM2835_SPI_CLOCK_DIVIDER_128", BCM2835_SPI_CLOCK_DIVIDER_128)
.value("BCM2835_SPI_CLOCK_DIVIDER_64", BCM2835_SPI_CLOCK_DIVIDER_64)
.value("BCM2835_SPI_CLOCK_DIVIDER_32", BCM2835_SPI_CLOCK_DIVIDER_32)
.value("BCM2835_SPI_CLOCK_DIVIDER_16", BCM2835_SPI_CLOCK_DIVIDER_16)
.value("BCM2835_SPI_CLOCK_DIVIDER_8", BCM2835_SPI_CLOCK_DIVIDER_8)
.value("BCM2835_SPI_CLOCK_DIVIDER_4", BCM2835_SPI_CLOCK_DIVIDER_4)
.value("BCM2835_SPI_CLOCK_DIVIDER_2", BCM2835_SPI_CLOCK_DIVIDER_2)
.value("BCM2835_SPI_CLOCK_DIVIDER_1", BCM2835_SPI_CLOCK_DIVIDER_1)
.export_values();
bp::enum_< bcm2835SPIChipSelect>("bcm2835SPIChipSelect")
.value("BCM2835_SPI_CS0", BCM2835_SPI_CS0)
.value("BCM2835_SPI_CS1", BCM2835_SPI_CS1)
.value("BCM2835_SPI_CS2", BCM2835_SPI_CS2)
.value("BCM2835_SPI_CS_NONE", BCM2835_SPI_CS_NONE)
.export_values();
#endif // BCM2835_H
bp::enum_<rf24_crclength_e>("rf24_crclength_e").value("RF24_CRC_DISABLED", RF24_CRC_DISABLED).value("RF24_CRC_8", RF24_CRC_8).value(
"RF24_CRC_16", RF24_CRC_16).export_values()
;
bp::enum_< rf24_datarate_e>("rf24_datarate_e")
.value("RF24_1MBPS", RF24_1MBPS)
.value("RF24_2MBPS", RF24_2MBPS)
.value("RF24_250KBPS", RF24_250KBPS)
.export_values();
bp::enum_< rf24_pa_dbm_e>("rf24_pa_dbm_e")
.value("RF24_PA_MIN", RF24_PA_MIN)
.value("RF24_PA_LOW", RF24_PA_LOW)
.value("RF24_PA_HIGH", RF24_PA_HIGH)
.value("RF24_PA_MAX", RF24_PA_MAX)
.value("RF24_PA_ERROR", RF24_PA_ERROR)
.export_values();
// ******************** RF24 class **************************
//
bp::class_< RF24 >( "RF24", bp::init< uint8_t, uint8_t >(( bp::arg("_cepin"), bp::arg("_cspin"))))
#if defined (RF24_LINUX) && !defined (MRAA)
.def( bp::init< uint8_t, uint8_t, uint32_t >(( bp::arg("_cepin"), bp::arg("_cspin"), bp::arg("spispeed") )) )
#endif
.def("available", (bool (::RF24::* )( ))( &::RF24::available ))
.def("available_pipe", &available_wrap ) // needed to rename this method as python does not allow such overloading
.def("begin", &RF24::begin)
.def("closeReadingPipe", &RF24::closeReadingPipe)
.def("disableCRC", &RF24::disableCRC)
.def("enableAckPayload", &RF24::enableAckPayload)
.def("enableDynamicAck", &RF24::enableDynamicAck)
.def("enableDynamicPayloads", &RF24::enableDynamicPayloads)
.def("flush_tx", &RF24::flush_tx)
.def("getCRCLength", &RF24::getCRCLength)
.def("getDataRate", &RF24::getDataRate)
.def("getDynamicPayloadSize", &RF24::getDynamicPayloadSize)
.def("getPALevel", &RF24::getPALevel)
.def("isAckPayloadAvailable", &RF24::isAckPayloadAvailable)
.def("isPVariant", &RF24::isPVariant)
.def("isValid", &RF24::isValid)
.def("maskIRQ", &RF24::maskIRQ, ( bp::arg("tx_ok"), bp::arg("tx_fail"), bp::arg("rx_ready")))
.def("openReadingPipe", &openReadingPipe_wrap, (bp::arg("number"), bp::arg("address")))
.def("openReadingPipe", (void (::RF24::* )(::uint8_t,::uint64_t ))( &::RF24::openReadingPipe), (bp::arg("number"), bp::arg("address")))
.def("openWritingPipe", &openWritingPipe_wrap, (bp::arg("address")))
.def("openWritingPipe", (void (::RF24::* )(::uint64_t ))( &::RF24::openWritingPipe), ( bp::arg("address")))
.def("powerDown", &RF24::powerDown)
.def("powerUp", &RF24::powerUp)
.def("printDetails", &RF24::printDetails)
.def("reUseTX", &RF24::reUseTX)
.def("read", &read_wrap, (bp::arg("maxlen")))
.def("rxFifoFull", &RF24::rxFifoFull)
.def("setAddressWidth", &RF24::setAddressWidth)
.def("setAutoAck", (void (::RF24::* )( bool ))( &::RF24::setAutoAck ), ( bp::arg("enable")))
.def("setAutoAck", (void (::RF24::* )(::uint8_t, bool ))( &::RF24::setAutoAck ), ( bp::arg("pipe"), bp::arg("enable")))
.def("setCRCLength", &RF24::setCRCLength, ( bp::arg("length")))
.def("setChannel", &RF24::setChannel, ( bp::arg("channel")))
.def("setDataRate", &RF24::setDataRate, ( bp::arg("speed")))
.def("setPALevel", &RF24::setPALevel, ( bp::arg("level")))
.def("setRetries", &RF24::setRetries, (bp::arg("delay"), bp::arg("count")))
.def("startFastWrite", &startFastWrite_wrap1, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast")))
.def("startFastWrite", &startFastWrite_wrap2, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast"), bp::arg("startTx")))
.def("startListening", &RF24::startListening)
.def("startWrite", &startWrite_wrap, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast")))
.def("stopListening", &RF24::stopListening)
.def("testCarrier", &RF24::testCarrier)
.def("testRPD", &RF24::testRPD)
.def("txStandBy", (bool (::RF24::* )(::uint32_t, bool))(&RF24::txStandBy), txStandBy_wrap1( bp::args("timeout", "startTx")))
.def("whatHappened", &whatHappened_wrap)
.def("write", &write_wrap1, ( bp::arg("buf")))
.def("write", &write_wrap2, ( bp::arg("buf"), bp::arg("multicast")))
.def("writeAckPayload", writeAckPayload_wrap, ( bp::arg("pipe"), bp::arg("buf")))
.def("writeBlocking", &writeBlocking_wrap, ( bp::arg("buf"), bp::arg("timeout")))
.def("writeFast", &writeFast_wrap1, ( bp::arg("buf")))
.def("writeFast", &writeFast_wrap2, ( bp::arg("buf"), bp::arg("multicast")))
.add_property("payloadSize", &RF24::getPayloadSize, &RF24::setPayloadSize)
.def_readwrite( "failureDetected", &RF24::failureDetected );}

View File

@@ -0,0 +1,38 @@
import sys
from distutils import unixccompiler
from distutils import ccompiler
def register():
sys.modules['distutils.crossunixccompiler'] = sys.modules[__name__]
ccompiler.compiler_class['crossunix'] = (__name__,
'CrossUnixCCompiler',
'UNIX-style compiler for cross compilation')
def try_remove_all(lst, starts):
lst[:] = [x for x in lst if not x.startswith(starts)]
class CrossUnixCCompiler(unixccompiler.UnixCCompiler):
def _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts):
try_remove_all(self.compiler_so, ('-m64', '-fstack-protector-strong', '-mtune=generic'))
try_remove_all(cc_args, '-I/usr')
try_remove_all(pp_opts, '-I/usr')
return unixccompiler.UnixCCompiler._compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
def link(self, target_desc, objects,
output_filename, output_dir=None, libraries=None,
library_dirs=None, runtime_library_dirs=None,
export_symbols=None, debug=0, extra_preargs=None,
extra_postargs=None, build_temp=None, target_lang=None):
try_remove_all(self.library_dirs, ('/usr'))
return unixccompiler.UnixCCompiler.link(self, target_desc, objects, output_filename, output_dir, libraries,
library_dirs, runtime_library_dirs, export_symbols, debug,
extra_preargs, extra_postargs, build_temp, target_lang)
def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs):
self.__class__ = unixccompiler.UnixCCompiler
ret = unixccompiler.UnixCCompiler._fix_lib_args(self, libraries, library_dirs, runtime_library_dirs)
self.__class__ = CrossUnixCCompiler
return ret

View File

@@ -0,0 +1,311 @@
#include <RF24/RF24.h>
#include <boost/python.hpp>
namespace bp = boost::python;
// ******************** explicit wrappers **************************
// for methods which need it - mostly for buffer operations
//
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);
bp::object
py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, maxlen < ref.getPayloadSize() ? maxlen : ref.getPayloadSize())));
delete[] buf;
return py_ba;
}
bool write_wrap1(RF24& ref, bp::object buf)
{
return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool write_wrap2(RF24& ref, bp::object buf, const bool 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, bp::object buf)
{
ref.writeAckPayload(pipe, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool writeFast_wrap1(RF24& ref, bp::object buf)
{
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
bool writeFast_wrap2(RF24& ref, bp::object buf, const bool multicast)
{
return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
bool writeBlocking_wrap(RF24& ref, bp::object buf, uint32_t timeout)
{
return ref.writeBlocking(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), timeout);
}
void startFastWrite_wrap1(RF24& ref, bp::object buf, const bool multicast)
{
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
void startFastWrite_wrap2(RF24& ref, bp::object buf, const bool multicast, bool startTx)
{
ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast, startTx);
}
void startWrite_wrap(RF24& ref, bp::object buf, const bool multicast)
{
ref.startWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
}
void openWritingPipe_wrap(RF24& ref, const bp::object address)
{
ref.openWritingPipe((const uint8_t*) (get_bytes_or_bytearray_str(address)));
}
void openReadingPipe_wrap(RF24& ref, uint8_t number, const bp::object address)
{
ref.openReadingPipe(number, (const uint8_t*) (get_bytes_or_bytearray_str(address)));
}
bp::tuple whatHappened_wrap(RF24& ref)
{
bool tx_ok;
bool tx_fail;
bool tx_ready;
ref.whatHappened(tx_ok, tx_fail, tx_ready);
return bp::make_tuple(tx_ok, tx_fail, tx_ready);
}
bp::tuple available_wrap(RF24& ref)
{
bool result;
uint8_t pipe;
result = ref.available(&pipe);
return bp::make_tuple(result, pipe);
}
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(txStandBy_wrap1, RF24::txStandBy,
0, 2)
//BOOST_PYTHON_FUNCTION_OVERLOADS(txStandBy_wrap2, RF24::txStandBy, 1, 2)
// ******************** enums **************************
// from both RF24 and bcm2835
//
BOOST_PYTHON_MODULE(RF24){
#ifdef BCM2835_H
bp::enum_< RPiGPIOPin>("RPiGPIOPin")
.value("RPI_GPIO_P1_03", RPI_GPIO_P1_03)
.value("RPI_GPIO_P1_05", RPI_GPIO_P1_05)
.value("RPI_GPIO_P1_07", RPI_GPIO_P1_07)
.value("RPI_GPIO_P1_08", RPI_GPIO_P1_08)
.value("RPI_GPIO_P1_10", RPI_GPIO_P1_10)
.value("RPI_GPIO_P1_11", RPI_GPIO_P1_11)
.value("RPI_GPIO_P1_12", RPI_GPIO_P1_12)
.value("RPI_GPIO_P1_13", RPI_GPIO_P1_13)
.value("RPI_GPIO_P1_15", RPI_GPIO_P1_15)
.value("RPI_GPIO_P1_16", RPI_GPIO_P1_16)
.value("RPI_GPIO_P1_18", RPI_GPIO_P1_18)
.value("RPI_GPIO_P1_19", RPI_GPIO_P1_19)
.value("RPI_GPIO_P1_21", RPI_GPIO_P1_21)
.value("RPI_GPIO_P1_22", RPI_GPIO_P1_22)
.value("RPI_GPIO_P1_23", RPI_GPIO_P1_23)
.value("RPI_GPIO_P1_24", RPI_GPIO_P1_24)
.value("RPI_GPIO_P1_26", RPI_GPIO_P1_26)
.value("RPI_V2_GPIO_P1_03", RPI_V2_GPIO_P1_03)
.value("RPI_V2_GPIO_P1_05", RPI_V2_GPIO_P1_05)
.value("RPI_V2_GPIO_P1_07", RPI_V2_GPIO_P1_07)
.value("RPI_V2_GPIO_P1_08", RPI_V2_GPIO_P1_08)
.value("RPI_V2_GPIO_P1_10", RPI_V2_GPIO_P1_10)
.value("RPI_V2_GPIO_P1_11", RPI_V2_GPIO_P1_11)
.value("RPI_V2_GPIO_P1_12", RPI_V2_GPIO_P1_12)
.value("RPI_V2_GPIO_P1_13", RPI_V2_GPIO_P1_13)
.value("RPI_V2_GPIO_P1_15", RPI_V2_GPIO_P1_15)
.value("RPI_V2_GPIO_P1_16", RPI_V2_GPIO_P1_16)
.value("RPI_V2_GPIO_P1_18", RPI_V2_GPIO_P1_18)
.value("RPI_V2_GPIO_P1_19", RPI_V2_GPIO_P1_19)
.value("RPI_V2_GPIO_P1_21", RPI_V2_GPIO_P1_21)
.value("RPI_V2_GPIO_P1_22", RPI_V2_GPIO_P1_22)
.value("RPI_V2_GPIO_P1_23", RPI_V2_GPIO_P1_23)
.value("RPI_V2_GPIO_P1_24", RPI_V2_GPIO_P1_24)
.value("RPI_V2_GPIO_P1_26", RPI_V2_GPIO_P1_26)
.value("RPI_V2_GPIO_P5_03", RPI_V2_GPIO_P5_03)
.value("RPI_V2_GPIO_P5_04", RPI_V2_GPIO_P5_04)
.value("RPI_V2_GPIO_P5_05", RPI_V2_GPIO_P5_05)
.value("RPI_V2_GPIO_P5_06", RPI_V2_GPIO_P5_06)
.value("RPI_BPLUS_GPIO_J8_03", RPI_BPLUS_GPIO_J8_03)
.value("RPI_BPLUS_GPIO_J8_05", RPI_BPLUS_GPIO_J8_05)
.value("RPI_BPLUS_GPIO_J8_07", RPI_BPLUS_GPIO_J8_07)
.value("RPI_BPLUS_GPIO_J8_08", RPI_BPLUS_GPIO_J8_08)
.value("RPI_BPLUS_GPIO_J8_10", RPI_BPLUS_GPIO_J8_10)
.value("RPI_BPLUS_GPIO_J8_11", RPI_BPLUS_GPIO_J8_11)
.value("RPI_BPLUS_GPIO_J8_12", RPI_BPLUS_GPIO_J8_12)
.value("RPI_BPLUS_GPIO_J8_13", RPI_BPLUS_GPIO_J8_13)
.value("RPI_BPLUS_GPIO_J8_15", RPI_BPLUS_GPIO_J8_15)
.value("RPI_BPLUS_GPIO_J8_16", RPI_BPLUS_GPIO_J8_16)
.value("RPI_BPLUS_GPIO_J8_18", RPI_BPLUS_GPIO_J8_18)
.value("RPI_BPLUS_GPIO_J8_19", RPI_BPLUS_GPIO_J8_19)
.value("RPI_BPLUS_GPIO_J8_21", RPI_BPLUS_GPIO_J8_21)
.value("RPI_BPLUS_GPIO_J8_22", RPI_BPLUS_GPIO_J8_22)
.value("RPI_BPLUS_GPIO_J8_23", RPI_BPLUS_GPIO_J8_23)
.value("RPI_BPLUS_GPIO_J8_24", RPI_BPLUS_GPIO_J8_24)
.value("RPI_BPLUS_GPIO_J8_26", RPI_BPLUS_GPIO_J8_26)
.value("RPI_BPLUS_GPIO_J8_29", RPI_BPLUS_GPIO_J8_29)
.value("RPI_BPLUS_GPIO_J8_31", RPI_BPLUS_GPIO_J8_31)
.value("RPI_BPLUS_GPIO_J8_32", RPI_BPLUS_GPIO_J8_32)
.value("RPI_BPLUS_GPIO_J8_33", RPI_BPLUS_GPIO_J8_33)
.value("RPI_BPLUS_GPIO_J8_35", RPI_BPLUS_GPIO_J8_35)
.value("RPI_BPLUS_GPIO_J8_36", RPI_BPLUS_GPIO_J8_36)
.value("RPI_BPLUS_GPIO_J8_37", RPI_BPLUS_GPIO_J8_37)
.value("RPI_BPLUS_GPIO_J8_38", RPI_BPLUS_GPIO_J8_38)
.value("RPI_BPLUS_GPIO_J8_40", RPI_BPLUS_GPIO_J8_40)
.export_values()
;
bp::enum_< bcm2835SPIClockDivider>("bcm2835SPIClockDivider")
.value("BCM2835_SPI_CLOCK_DIVIDER_65536", BCM2835_SPI_CLOCK_DIVIDER_65536)
.value("BCM2835_SPI_CLOCK_DIVIDER_32768", BCM2835_SPI_CLOCK_DIVIDER_32768)
.value("BCM2835_SPI_CLOCK_DIVIDER_16384", BCM2835_SPI_CLOCK_DIVIDER_16384)
.value("BCM2835_SPI_CLOCK_DIVIDER_8192", BCM2835_SPI_CLOCK_DIVIDER_8192)
.value("BCM2835_SPI_CLOCK_DIVIDER_4096", BCM2835_SPI_CLOCK_DIVIDER_4096)
.value("BCM2835_SPI_CLOCK_DIVIDER_2048", BCM2835_SPI_CLOCK_DIVIDER_2048)
.value("BCM2835_SPI_CLOCK_DIVIDER_1024", BCM2835_SPI_CLOCK_DIVIDER_1024)
.value("BCM2835_SPI_CLOCK_DIVIDER_512", BCM2835_SPI_CLOCK_DIVIDER_512)
.value("BCM2835_SPI_CLOCK_DIVIDER_256", BCM2835_SPI_CLOCK_DIVIDER_256)
.value("BCM2835_SPI_CLOCK_DIVIDER_128", BCM2835_SPI_CLOCK_DIVIDER_128)
.value("BCM2835_SPI_CLOCK_DIVIDER_64", BCM2835_SPI_CLOCK_DIVIDER_64)
.value("BCM2835_SPI_CLOCK_DIVIDER_32", BCM2835_SPI_CLOCK_DIVIDER_32)
.value("BCM2835_SPI_CLOCK_DIVIDER_16", BCM2835_SPI_CLOCK_DIVIDER_16)
.value("BCM2835_SPI_CLOCK_DIVIDER_8", BCM2835_SPI_CLOCK_DIVIDER_8)
.value("BCM2835_SPI_CLOCK_DIVIDER_4", BCM2835_SPI_CLOCK_DIVIDER_4)
.value("BCM2835_SPI_CLOCK_DIVIDER_2", BCM2835_SPI_CLOCK_DIVIDER_2)
.value("BCM2835_SPI_CLOCK_DIVIDER_1", BCM2835_SPI_CLOCK_DIVIDER_1)
.export_values();
bp::enum_< bcm2835SPIChipSelect>("bcm2835SPIChipSelect")
.value("BCM2835_SPI_CS0", BCM2835_SPI_CS0)
.value("BCM2835_SPI_CS1", BCM2835_SPI_CS1)
.value("BCM2835_SPI_CS2", BCM2835_SPI_CS2)
.value("BCM2835_SPI_CS_NONE", BCM2835_SPI_CS_NONE)
.export_values();
#endif // BCM2835_H
bp::enum_<rf24_crclength_e>("rf24_crclength_e").value("RF24_CRC_DISABLED", RF24_CRC_DISABLED).value("RF24_CRC_8", RF24_CRC_8).value(
"RF24_CRC_16", RF24_CRC_16).export_values()
;
bp::enum_< rf24_datarate_e>("rf24_datarate_e")
.value("RF24_1MBPS", RF24_1MBPS)
.value("RF24_2MBPS", RF24_2MBPS)
.value("RF24_250KBPS", RF24_250KBPS)
.export_values();
bp::enum_< rf24_pa_dbm_e>("rf24_pa_dbm_e")
.value("RF24_PA_MIN", RF24_PA_MIN)
.value("RF24_PA_LOW", RF24_PA_LOW)
.value("RF24_PA_HIGH", RF24_PA_HIGH)
.value("RF24_PA_MAX", RF24_PA_MAX)
.value("RF24_PA_ERROR", RF24_PA_ERROR)
.export_values();
// ******************** RF24 class **************************
//
bp::class_< RF24 >( "RF24", bp::init< uint8_t, uint8_t >(( bp::arg("_cepin"), bp::arg("_cspin"))))
.def( bp::init< uint8_t, uint8_t, uint32_t >(( bp::arg("_cepin"), bp::arg("_cspin"), bp::arg("spispeed"))))
.def("available", (bool (::RF24::* )( ))( &::RF24::available ))
.def("available_pipe", &available_wrap ) // needed to rename this method as python does not allow such overloading
.def("begin", &RF24::begin)
.def("closeReadingPipe", &RF24::closeReadingPipe)
.def("disableCRC", &RF24::disableCRC)
.def("enableAckPayload", &RF24::enableAckPayload)
.def("enableDynamicAck", &RF24::enableDynamicAck)
.def("enableDynamicPayloads", &RF24::enableDynamicPayloads)
.def("flush_tx", &RF24::flush_tx)
.def("getCRCLength", &RF24::getCRCLength)
.def("getDataRate", &RF24::getDataRate)
.def("getDynamicPayloadSize", &RF24::getDynamicPayloadSize)
.def("getPALevel", &RF24::getPALevel)
.def("isAckPayloadAvailable", &RF24::isAckPayloadAvailable)
.def("isPVariant", &RF24::isPVariant)
.def("isValid", &RF24::isValid)
.def("maskIRQ", &RF24::maskIRQ, ( bp::arg("tx_ok"), bp::arg("tx_fail"), bp::arg("rx_ready")))
.def("openReadingPipe", &openReadingPipe_wrap, (bp::arg("number"), bp::arg("address")))
.def("openReadingPipe", (void (::RF24::* )(::uint8_t,::uint64_t ))( &::RF24::openReadingPipe), (bp::arg("number"), bp::arg("address")))
.def("openWritingPipe", &openWritingPipe_wrap, (bp::arg("address")))
.def("openWritingPipe", (void (::RF24::* )(::uint64_t ))( &::RF24::openWritingPipe), ( bp::arg("address")))
.def("powerDown", &RF24::powerDown)
.def("powerUp", &RF24::powerUp)
.def("printDetails", &RF24::printDetails)
.def("reUseTX", &RF24::reUseTX)
.def("read", &read_wrap, (bp::arg("maxlen")))
.def("rxFifoFull", &RF24::rxFifoFull)
.def("setAddressWidth", &RF24::setAddressWidth)
.def("setAutoAck", (void (::RF24::* )( bool ))( &::RF24::setAutoAck ), ( bp::arg("enable")))
.def("setAutoAck", (void (::RF24::* )(::uint8_t, bool ))( &::RF24::setAutoAck ), ( bp::arg("pipe"), bp::arg("enable")))
.def("setCRCLength", &RF24::setCRCLength, ( bp::arg("length")))
.def("setChannel", &RF24::setChannel, ( bp::arg("channel")))
.def("setDataRate", &RF24::setDataRate, ( bp::arg("speed")))
.def("setPALevel", &RF24::setPALevel, ( bp::arg("level")))
.def("setRetries", &RF24::setRetries, (bp::arg("delay"), bp::arg("count")))
.def("startFastWrite", &startFastWrite_wrap1, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast")))
.def("startFastWrite", &startFastWrite_wrap2, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast"), bp::arg("startTx")))
.def("startListening", &RF24::startListening)
.def("startWrite", &startWrite_wrap, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast")))
.def("stopListening", &RF24::stopListening)
.def("testCarrier", &RF24::testCarrier)
.def("testRPD", &RF24::testRPD)
.def("txStandBy", (bool (::RF24::* )(::uint32_t, bool))(&RF24::txStandBy), txStandBy_wrap1( bp::args("timeout", "startTx")))
.def("whatHappened", &whatHappened_wrap)
.def("write", &write_wrap1, ( bp::arg("buf")))
.def("write", &write_wrap2, ( bp::arg("buf"), bp::arg("multicast")))
.def("writeAckPayload", writeAckPayload_wrap, ( bp::arg("pipe"), bp::arg("buf")))
.def("writeBlocking", &writeBlocking_wrap, ( bp::arg("buf"), bp::arg("timeout")))
.def("writeFast", &writeFast_wrap1, ( bp::arg("buf")))
.def("writeFast", &writeFast_wrap2, ( bp::arg("buf"), bp::arg("multicast")))
.add_property("payloadSize", &RF24::getPayloadSize, &RF24::setPayloadSize)
.def_readwrite( "failureDetected", &RF24::failureDetected );}

View File

@@ -0,0 +1,2 @@
Python Wrapper for RF24
See http://tmrh20.github.io/RF24 for more information

View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
import os
import sys
import setuptools
import crossunixccompiler
version = ''
def process_configparams():
global version
with open('../../Makefile.inc') as f:
config_lines = f.read().splitlines()
cflags = os.getenv("CFLAGS", "")
for line in config_lines:
identifier, value = line.split('=', 1)
if identifier == "CPUFLAGS":
cflags += " " + value
elif identifier == "HEADER_DIR":
cflags += " -I" + os.path.dirname(value)
elif identifier == "LIB_DIR":
cflags += " -L" + value
elif identifier == "LIB_VERSION":
version = value
elif identifier in ("CC", "CXX"):
os.environ[identifier] = value
os.environ["CFLAGS"] = cflags
if sys.version_info >= (3,):
BOOST_LIB = 'boost_python3'
else:
BOOST_LIB = 'boost_python'
process_configparams()
crossunixccompiler.register()
module_RF24 = setuptools.Extension('RF24',
libraries=['rf24', BOOST_LIB],
sources=['pyRF24.cpp'])
setuptools.setup(name='RF24',
version=version,
ext_modules=[module_RF24])

View File

@@ -0,0 +1,21 @@
from RF24 import *
from RF24Network import *
from RF24Mesh import *
# radio setup for RPi B Rev2: CS0=Pin 24
radio = RF24(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ)
network = RF24Network(radio)
mesh = RF24Mesh(radio, network)
mesh.setNodeID(0)
mesh.begin(108, RF24_250KBPS)
radio.setPALevel(RF24_PA_MAX) # Power Amplifier
radio.printDetails()
while 1:
mesh.update()
mesh.DHCP()
while network.available():
print("Received message")
header, payload = network.read(10)

View File

@@ -0,0 +1,107 @@
#include "boost/python.hpp"
#include "RF24/RF24.h"
#include "RF24Network/RF24Network.h"
#include "RF24Mesh/RF24Mesh.h"
namespace bp = boost::python;
// ******************** explicit 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;
}
bool write_wrap1(RF24Mesh& ref, bp::object buf, uint8_t msg_type)
{
return ref.write(get_bytes_or_bytearray_str(buf), msg_type, get_bytes_or_bytearray_ln(buf));
}
bool write_wrap2(RF24Mesh& ref, bp::object buf, uint8_t msg_type, uint8_t nodeID)
{
return ref.write(get_bytes_or_bytearray_str(buf), msg_type, get_bytes_or_bytearray_ln(buf), nodeID);
}
bool write_to_node_wrap(RF24Mesh& ref, uint16_t to_node, bp::object buf, uint8_t msg_type)
{
return ref.write(to_node, get_bytes_or_bytearray_str(buf), msg_type, get_bytes_or_bytearray_ln(buf));
}
// ******************** overload wrappers **************************
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(begin_overload, RF24Mesh::begin,
0, 3)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getNodeID_overload, RF24Mesh::getNodeID,
0, 1)
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(renewAddress_overload, RF24Mesh::renewAddress,
0, 1)
// ******************** RF24Mesh exposed **************************
BOOST_PYTHON_MODULE(RF24Mesh)
{{ //::RF24Mesh
bp::class_<RF24Mesh>("RF24Mesh", bp::init<RF24&, RF24Network&>((bp::arg("_radio"), bp::arg("_network"))))
//bool begin(uint8_t channel = MESH_DEFAULT_CHANNEL, rf24_datarate_e data_rate = RF24_1MBPS, uint32_t timeout=MESH_RENEWAL_TIMEOUT );
.def("begin", &RF24Mesh::begin, begin_overload(bp::args("channel", "data_rate", "timeout")))
//uint8_t update();
.def("update", &RF24Mesh::update)
//bool write(const void* data, uint8_t msg_type, size_t size, uint8_t nodeID=0);
.def("write", &write_wrap1, (bp::arg("data"), bp::arg("msg_type"))).def("write", &write_wrap2,
(bp::arg("data"), bp::arg("msg_type"), bp::arg("nodeID")))
//bool write(uint16_t to_node, const void* data, uint8_t msg_type, size_t size );
.def("write", &write_to_node_wrap, (bp::arg("to_node"), bp::arg("data"), bp::arg("msg_type"), bp::arg("size")))
//void setNodeID(uint8_t nodeID);
.def("setNodeID", &RF24Mesh::setNodeID, (bp::arg("nodeID")))
//void DHCP();
.def("DHCP", &RF24Mesh::DHCP)
//int16_t getNodeID(uint16_t address=MESH_BLANK_ID);
.def("getNodeID", &RF24Mesh::getNodeID, getNodeID_overload(bp::args("address")))
//bool checkConnection();
.def("checkConnection", &RF24Mesh::checkConnection)
//uint16_t renewAddress(uint32_t timeout=MESH_RENEWAL_TIMEOUT);
.def("renewAddress", &RF24Mesh::renewAddress, getNodeID_overload(bp::args("timeout")))
//bool releaseAddress();
.def("releaseAddress", &RF24Mesh::releaseAddress)
//int16_t getAddress(uint8_t nodeID);
.def("getAddress", &RF24Mesh::getAddress, (bp::arg("nodeID")))
//void setChannel(uint8_t _channel);
.def("setChannel", &RF24Mesh::setChannel, (bp::arg("_channel")))
//void setChild(bool allow);
.def("setChild", &RF24Mesh::setChild, (bp::arg("allow")))
//void setAddress(uint8_t nodeID, uint16_t address);
.def("setAddress", &RF24Mesh::setAddress, (bp::arg("nodeID"), bp::arg("address")))
//void saveDHCP();
.def("saveDHCP", &RF24Mesh::saveDHCP)
//void loadDHCP();
.def("loadDHCP", &RF24Mesh::loadDHCP)
//void setStaticAddress(uint8_t nodeID, uint16_t address);
.def("setStaticAddress", &RF24Mesh::setStaticAddress, (bp::arg("nodeID"), bp::arg("address")));}}

View File

@@ -0,0 +1,17 @@
#!/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_RF24Mesh = Extension('RF24Mesh',
libraries=['rf24mesh', 'rf24network', BOOST_LIB],
sources=['pyRF24Mesh.cpp'])
setup(name='RF24Mesh',
version='1.0',
ext_modules=[module_RF24Mesh])

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env python
#
# Simplest possible example of using RF24Network,
#
# 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 *
from RF24Network import *
# CE Pin, CSN Pin, SPI Speed
# Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
# radio = radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ)
# Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
# radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ)
# Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
# 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_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ)
network = RF24Network(radio)
millis = lambda: int(round(time.time() * 1000))
octlit = lambda n: int(n, 8)
# Address of our node in Octal format (01, 021, etc)
this_node = octlit("00")
# Address of the other node
other_node = octlit("01")
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()
while network.available():
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)

View File

@@ -0,0 +1,64 @@
#!/usr/bin/env python
#
# Simplest possible example of using RF24Network,
#
# TRANSMITTER NODE
# Sends messages from to receiver.
#
from __future__ import print_function
import time
from struct import *
from RF24 import *
from RF24Network import *
# CE Pin, CSN Pin, SPI Speed
# Setup for GPIO 22 CE and GPIO 25 CSN with SPI Speed @ 1Mhz
# radio = radio(RPI_V2_GPIO_P1_22, RPI_V2_GPIO_P1_18, BCM2835_SPI_SPEED_1MHZ)
# Setup for GPIO 22 CE and CE0 CSN with SPI Speed @ 4Mhz
# radio = RF24(RPI_V2_GPIO_P1_15, BCM2835_SPI_CS0, BCM2835_SPI_SPEED_4MHZ)
# Setup for GPIO 22 CE and CE1 CSN with SPI Speed @ 8Mhz
# 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_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 = octlit("01")
# Address of the other node
other_node = octlit("00")
# 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!
if (now - last_sent >= interval):
last_sent = now
print('Sending ..')
payload = pack('<LL', millis(), packets_sent)
packets_sent += 1
ok = network.write(RF24NetworkHeader(other_node), payload)
if ok:
print('ok.')
else:
print('failed.')

View File

@@ -0,0 +1,133 @@
#include "boost/python.hpp"
#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)
{
char* buf = new char[maxlen + 1];
RF24NetworkHeader header;
uint16_t len = ref.read(header, buf, maxlen);
bp::object
py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, len)));
delete[] buf;
return bp::make_tuple(header, py_ba);
}
bool write_wrap(RF24Network& ref, RF24NetworkHeader& header, bp::object buf)
{
return ref.write(header, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
}
std::string toString_wrap(RF24NetworkHeader& ref)
{
return std::string(ref.toString());
}
// **************** RF24Network exposed *****************
//
BOOST_PYTHON_MODULE(RF24Network){{ //::RF24Network
typedef bp::class_< RF24Network > RF24Network_exposer_t;
RF24Network_exposer_t RF24Network_exposer = RF24Network_exposer_t( "RF24Network", bp::init< RF24 & >(( bp::arg("_radio"))));
bp::scope RF24Network_scope( RF24Network_exposer );
bp::implicitly_convertible< RF24 &, RF24Network >();
{ //::RF24Network::available
typedef bool ( ::RF24Network::*available_function_type )();
RF24Network_exposer.def("available", available_function_type(&::RF24Network::available));
}
{ //::RF24Network::begin
typedef void ( ::RF24Network::*begin_function_type )(::uint8_t, ::uint16_t);
RF24Network_exposer.def("begin", begin_function_type(&::RF24Network::begin),
(bp::arg("_channel"), bp::arg("_node_address")));
}
{ //::RF24Network::parent
typedef ::uint16_t ( ::RF24Network::*parent_function_type )() const;
RF24Network_exposer.def("parent", parent_function_type(&::RF24Network::parent));
}
{ //::RF24Network::read
typedef bp::tuple ( * read_function_type )(::RF24Network&, size_t);
RF24Network_exposer.def("read"
//, read_function_type( &::RF24Network::read )
, read_function_type(&read_wrap), (bp::arg("maxlen")));
}
{ //::RF24Network::update
typedef void ( ::RF24Network::*update_function_type )();
RF24Network_exposer.def("update", update_function_type(&::RF24Network::update));
}
{ //::RF24Network::write
typedef bool ( * write_function_type )(::RF24Network&, ::RF24NetworkHeader&, bp::object);
RF24Network_exposer.def("write", write_function_type(&write_wrap),
(bp::arg("header"), bp::arg("buf")));
}
RF24Network_exposer.def_readwrite( "txTimeout", &RF24Network::txTimeout );}
// **************** RF24NetworkHeader exposed *****************
//
bp::class_< RF24NetworkHeader >( "RF24NetworkHeader", bp::init< >())
.def( bp::init< uint16_t, bp::optional< unsigned char > >(( bp::arg("_to"), bp::arg("_type")=(unsigned char)(0))))
.def("toString", &toString_wrap )
.def_readwrite( "from_node", &RF24NetworkHeader::from_node )
.def_readwrite( "id", &RF24NetworkHeader::id )
.def_readwrite( "next_id", RF24NetworkHeader::next_id )
.def_readwrite( "reserved", &RF24NetworkHeader::reserved )
.def_readwrite( "to_node", &RF24NetworkHeader::to_node )
.def_readwrite( "type", &RF24NetworkHeader::type );}

View File

@@ -0,0 +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_RF24Network = Extension('RF24Network',
libraries=['rf24network', BOOST_LIB],
sources=['pyRF24Network.cpp'])
setup(name='RF24Network',
version='1.0',
ext_modules=[module_RF24Network]
)

View File

@@ -0,0 +1,2 @@
Python Wrapper for RF24
See http://tmrh20.github.io/RF24 for more information

View File

@@ -0,0 +1,48 @@
#!/usr/bin/env python
import os
import sys
import setuptools
import crossunixccompiler
version = ''
def process_configparams():
global version
with open('../Makefile.inc') as f:
config_lines = f.read().splitlines()
cflags = os.getenv("CFLAGS", "")
for line in config_lines:
identifier, value = line.split('=', 1)
if identifier == "CPUFLAGS":
cflags += " " + value
elif identifier == "HEADER_DIR":
cflags += " -I" + os.path.dirname(value)
elif identifier == "LIB_DIR":
cflags += " -L" + value
elif identifier == "LIB_VERSION":
version = value
elif identifier in ("CC", "CXX"):
os.environ[identifier] = value
os.environ["CFLAGS"] = cflags
if sys.version_info >= (3,):
BOOST_LIB = 'boost_python3'
else:
BOOST_LIB = 'boost_python'
process_configparams()
crossunixccompiler.register()
module_RF24 = setuptools.Extension('RF24',
libraries=['rf24', BOOST_LIB],
sources=['pyRF24.cpp'])
setuptools.setup(name='RF24',
version=version,
ext_modules=[module_RF24])