Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.6 KiB

  1. #include "boost/python.hpp"
  2. #include "RF24/RF24.h"
  3. #include "RF24Network/RF24Network.h"
  4. namespace bp = boost::python;
  5. // **************** expicit wrappers *****************
  6. // where needed, especially where buffer is involved
  7. //
  8. void throw_ba_exception(void)
  9. {
  10. PyErr_SetString(PyExc_TypeError, "buf parameter must be bytes or bytearray");
  11. bp::throw_error_already_set();
  12. }
  13. char *get_bytes_or_bytearray_str(bp::object buf)
  14. {
  15. PyObject *py_ba;
  16. py_ba = buf.ptr();
  17. if (PyByteArray_Check(py_ba))
  18. return PyByteArray_AsString(py_ba);
  19. else if (PyBytes_Check(py_ba))
  20. return PyBytes_AsString(py_ba);
  21. else
  22. throw_ba_exception();
  23. return NULL;
  24. }
  25. int get_bytes_or_bytearray_ln(bp::object buf)
  26. {
  27. PyObject *py_ba;
  28. py_ba = buf.ptr();
  29. if (PyByteArray_Check(py_ba))
  30. return PyByteArray_Size(py_ba);
  31. else if (PyBytes_Check(py_ba))
  32. return PyBytes_Size(py_ba);
  33. else
  34. throw_ba_exception();
  35. return 0;
  36. }
  37. bp::tuple read_wrap(RF24Network& ref, size_t maxlen)
  38. {
  39. char *buf = new char[maxlen+1];
  40. RF24NetworkHeader header;
  41. uint16_t len = ref.read(header, buf, maxlen);
  42. bp::object py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, len)));
  43. delete[] buf;
  44. return bp::make_tuple(header, py_ba);
  45. }
  46. bool write_wrap(RF24Network& ref, RF24NetworkHeader& header, bp::object buf)
  47. {
  48. return ref.write(header, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
  49. }
  50. std::string toString_wrap(RF24NetworkHeader& ref)
  51. {
  52. return std::string(ref.toString());
  53. }
  54. // **************** RF24Network exposed *****************
  55. //
  56. BOOST_PYTHON_MODULE(RF24Network){
  57. { //::RF24Network
  58. typedef bp::class_< RF24Network > RF24Network_exposer_t;
  59. RF24Network_exposer_t RF24Network_exposer = RF24Network_exposer_t( "RF24Network", bp::init< RF24 & >(( bp::arg("_radio") )) );
  60. bp::scope RF24Network_scope( RF24Network_exposer );
  61. bp::implicitly_convertible< RF24 &, RF24Network >();
  62. { //::RF24Network::available
  63. typedef bool ( ::RF24Network::*available_function_type )( ) ;
  64. RF24Network_exposer.def(
  65. "available"
  66. , available_function_type( &::RF24Network::available ) );
  67. }
  68. { //::RF24Network::begin
  69. typedef void ( ::RF24Network::*begin_function_type )( ::uint8_t,::uint16_t ) ;
  70. RF24Network_exposer.def(
  71. "begin"
  72. , begin_function_type( &::RF24Network::begin )
  73. , ( bp::arg("_channel"), bp::arg("_node_address") ) );
  74. }
  75. { //::RF24Network::parent
  76. typedef ::uint16_t ( ::RF24Network::*parent_function_type )( ) const;
  77. RF24Network_exposer.def(
  78. "parent"
  79. , parent_function_type( &::RF24Network::parent ) );
  80. }
  81. { //::RF24Network::read
  82. typedef bp::tuple ( *read_function_type )(::RF24Network&, size_t ) ;
  83. RF24Network_exposer.def(
  84. "read"
  85. //, read_function_type( &::RF24Network::read )
  86. , read_function_type( &read_wrap )
  87. , ( bp::arg("maxlen") ) );
  88. }
  89. { //::RF24Network::update
  90. typedef void ( ::RF24Network::*update_function_type )( ) ;
  91. RF24Network_exposer.def(
  92. "update"
  93. , update_function_type( &::RF24Network::update ) );
  94. }
  95. { //::RF24Network::write
  96. typedef bool ( *write_function_type )( ::RF24Network&, ::RF24NetworkHeader&, bp::object ) ;
  97. RF24Network_exposer.def("write", write_function_type( &write_wrap ), ( bp::arg("header"), bp::arg("buf") ) );
  98. }
  99. RF24Network_exposer.def_readwrite( "txTimeout", &RF24Network::txTimeout );
  100. }
  101. // **************** RF24NetworkHeader exposed *****************
  102. //
  103. bp::class_< RF24NetworkHeader >( "RF24NetworkHeader", bp::init< >() )
  104. .def( bp::init< uint16_t, bp::optional< unsigned char > >(( bp::arg("_to"), bp::arg("_type")=(unsigned char)(0) )) )
  105. .def("toString", &toString_wrap )
  106. .def_readwrite( "from_node", &RF24NetworkHeader::from_node )
  107. .def_readwrite( "id", &RF24NetworkHeader::id )
  108. .def_readwrite( "next_id", RF24NetworkHeader::next_id )
  109. .def_readwrite( "reserved", &RF24NetworkHeader::reserved )
  110. .def_readwrite( "to_node", &RF24NetworkHeader::to_node )
  111. .def_readwrite( "type", &RF24NetworkHeader::type );
  112. }