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.

330 lines
15 KiB

  1. #include <RF24/RF24.h>
  2. #include <boost/python.hpp>
  3. namespace bp = boost::python;
  4. // ******************** explicit wrappers **************************
  5. // for methods which need it - mostly for buffer operations
  6. //
  7. void throw_ba_exception(void)
  8. {
  9. PyErr_SetString(PyExc_TypeError, "buf parameter must be bytes or bytearray");
  10. bp::throw_error_already_set();
  11. }
  12. char *get_bytes_or_bytearray_str(bp::object buf)
  13. {
  14. PyObject *py_ba;
  15. py_ba = buf.ptr();
  16. if (PyByteArray_Check(py_ba))
  17. return PyByteArray_AsString(py_ba);
  18. else if (PyBytes_Check(py_ba))
  19. return PyBytes_AsString(py_ba);
  20. else
  21. throw_ba_exception();
  22. return NULL;
  23. }
  24. int get_bytes_or_bytearray_ln(bp::object buf)
  25. {
  26. PyObject *py_ba;
  27. py_ba = buf.ptr();
  28. if (PyByteArray_Check(py_ba))
  29. return PyByteArray_Size(py_ba);
  30. else if (PyBytes_Check(py_ba))
  31. return PyBytes_Size(py_ba);
  32. else
  33. throw_ba_exception();
  34. return 0;
  35. }
  36. bp::object read_wrap(RF24& ref, int maxlen)
  37. {
  38. char *buf = new char[maxlen+1];
  39. ref.read(buf, maxlen);
  40. bp::object py_ba(bp::handle<>(PyByteArray_FromStringAndSize(buf, maxlen<ref.getPayloadSize()?maxlen:ref.getPayloadSize())));
  41. delete[] buf;
  42. return py_ba;
  43. }
  44. bool write_wrap1(RF24& ref, bp::object buf)
  45. {
  46. return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
  47. }
  48. bool write_wrap2(RF24& ref, bp::object buf, const bool multicast)
  49. {
  50. return ref.write(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
  51. }
  52. void writeAckPayload_wrap(RF24& ref, uint8_t pipe, bp::object buf)
  53. {
  54. ref.writeAckPayload(pipe, get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
  55. }
  56. bool writeFast_wrap1(RF24& ref, bp::object buf)
  57. {
  58. return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf));
  59. }
  60. bool writeFast_wrap2(RF24& ref, bp::object buf, const bool multicast)
  61. {
  62. return ref.writeFast(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
  63. }
  64. bool writeBlocking_wrap(RF24& ref, bp::object buf, uint32_t timeout)
  65. {
  66. return ref.writeBlocking(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), timeout);
  67. }
  68. void startFastWrite_wrap1(RF24& ref, bp::object buf, const bool multicast)
  69. {
  70. ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
  71. }
  72. void startFastWrite_wrap2(RF24& ref, bp::object buf, const bool multicast, bool startTx)
  73. {
  74. ref.startFastWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast, startTx);
  75. }
  76. void startWrite_wrap(RF24& ref, bp::object buf, const bool multicast)
  77. {
  78. ref.startWrite(get_bytes_or_bytearray_str(buf), get_bytes_or_bytearray_ln(buf), multicast);
  79. }
  80. void openWritingPipe_wrap(RF24& ref, const bp::object address)
  81. {
  82. ref.openWritingPipe((const uint8_t *)(get_bytes_or_bytearray_str(address)));
  83. }
  84. void openReadingPipe_wrap(RF24& ref, uint8_t number, const bp::object address)
  85. {
  86. ref.openReadingPipe(number, (const uint8_t *)(get_bytes_or_bytearray_str(address)));
  87. }
  88. bp::tuple whatHappened_wrap(RF24& ref)
  89. {
  90. bool tx_ok;
  91. bool tx_fail;
  92. bool tx_ready;
  93. ref.whatHappened(tx_ok, tx_fail, tx_ready);
  94. return bp::make_tuple(tx_ok, tx_fail, tx_ready);
  95. }
  96. bp::tuple available_wrap(RF24& ref)
  97. {
  98. bool result;
  99. uint8_t pipe;
  100. result = ref.available(&pipe);
  101. return bp::make_tuple(result, pipe);
  102. }
  103. BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(txStandBy_wrap1, RF24::txStandBy, 0, 2)
  104. //BOOST_PYTHON_FUNCTION_OVERLOADS(txStandBy_wrap2, RF24::txStandBy, 1, 2)
  105. // ******************** enums **************************
  106. // from both RF24 and bcm2835
  107. //
  108. BOOST_PYTHON_MODULE(RF24){
  109. #ifdef BCM2835_H
  110. bp::enum_< RPiGPIOPin>("RPiGPIOPin")
  111. .value("RPI_GPIO_P1_03", RPI_GPIO_P1_03)
  112. .value("RPI_GPIO_P1_05", RPI_GPIO_P1_05)
  113. .value("RPI_GPIO_P1_07", RPI_GPIO_P1_07)
  114. .value("RPI_GPIO_P1_08", RPI_GPIO_P1_08)
  115. .value("RPI_GPIO_P1_10", RPI_GPIO_P1_10)
  116. .value("RPI_GPIO_P1_11", RPI_GPIO_P1_11)
  117. .value("RPI_GPIO_P1_12", RPI_GPIO_P1_12)
  118. .value("RPI_GPIO_P1_13", RPI_GPIO_P1_13)
  119. .value("RPI_GPIO_P1_15", RPI_GPIO_P1_15)
  120. .value("RPI_GPIO_P1_16", RPI_GPIO_P1_16)
  121. .value("RPI_GPIO_P1_18", RPI_GPIO_P1_18)
  122. .value("RPI_GPIO_P1_19", RPI_GPIO_P1_19)
  123. .value("RPI_GPIO_P1_21", RPI_GPIO_P1_21)
  124. .value("RPI_GPIO_P1_22", RPI_GPIO_P1_22)
  125. .value("RPI_GPIO_P1_23", RPI_GPIO_P1_23)
  126. .value("RPI_GPIO_P1_24", RPI_GPIO_P1_24)
  127. .value("RPI_GPIO_P1_26", RPI_GPIO_P1_26)
  128. .value("RPI_V2_GPIO_P1_03", RPI_V2_GPIO_P1_03)
  129. .value("RPI_V2_GPIO_P1_05", RPI_V2_GPIO_P1_05)
  130. .value("RPI_V2_GPIO_P1_07", RPI_V2_GPIO_P1_07)
  131. .value("RPI_V2_GPIO_P1_08", RPI_V2_GPIO_P1_08)
  132. .value("RPI_V2_GPIO_P1_10", RPI_V2_GPIO_P1_10)
  133. .value("RPI_V2_GPIO_P1_11", RPI_V2_GPIO_P1_11)
  134. .value("RPI_V2_GPIO_P1_12", RPI_V2_GPIO_P1_12)
  135. .value("RPI_V2_GPIO_P1_13", RPI_V2_GPIO_P1_13)
  136. .value("RPI_V2_GPIO_P1_15", RPI_V2_GPIO_P1_15)
  137. .value("RPI_V2_GPIO_P1_16", RPI_V2_GPIO_P1_16)
  138. .value("RPI_V2_GPIO_P1_18", RPI_V2_GPIO_P1_18)
  139. .value("RPI_V2_GPIO_P1_19", RPI_V2_GPIO_P1_19)
  140. .value("RPI_V2_GPIO_P1_21", RPI_V2_GPIO_P1_21)
  141. .value("RPI_V2_GPIO_P1_22", RPI_V2_GPIO_P1_22)
  142. .value("RPI_V2_GPIO_P1_23", RPI_V2_GPIO_P1_23)
  143. .value("RPI_V2_GPIO_P1_24", RPI_V2_GPIO_P1_24)
  144. .value("RPI_V2_GPIO_P1_26", RPI_V2_GPIO_P1_26)
  145. .value("RPI_V2_GPIO_P5_03", RPI_V2_GPIO_P5_03)
  146. .value("RPI_V2_GPIO_P5_04", RPI_V2_GPIO_P5_04)
  147. .value("RPI_V2_GPIO_P5_05", RPI_V2_GPIO_P5_05)
  148. .value("RPI_V2_GPIO_P5_06", RPI_V2_GPIO_P5_06)
  149. .value("RPI_BPLUS_GPIO_J8_03", RPI_BPLUS_GPIO_J8_03)
  150. .value("RPI_BPLUS_GPIO_J8_05", RPI_BPLUS_GPIO_J8_05)
  151. .value("RPI_BPLUS_GPIO_J8_07", RPI_BPLUS_GPIO_J8_07)
  152. .value("RPI_BPLUS_GPIO_J8_08", RPI_BPLUS_GPIO_J8_08)
  153. .value("RPI_BPLUS_GPIO_J8_10", RPI_BPLUS_GPIO_J8_10)
  154. .value("RPI_BPLUS_GPIO_J8_11", RPI_BPLUS_GPIO_J8_11)
  155. .value("RPI_BPLUS_GPIO_J8_12", RPI_BPLUS_GPIO_J8_12)
  156. .value("RPI_BPLUS_GPIO_J8_13", RPI_BPLUS_GPIO_J8_13)
  157. .value("RPI_BPLUS_GPIO_J8_15", RPI_BPLUS_GPIO_J8_15)
  158. .value("RPI_BPLUS_GPIO_J8_16", RPI_BPLUS_GPIO_J8_16)
  159. .value("RPI_BPLUS_GPIO_J8_18", RPI_BPLUS_GPIO_J8_18)
  160. .value("RPI_BPLUS_GPIO_J8_19", RPI_BPLUS_GPIO_J8_19)
  161. .value("RPI_BPLUS_GPIO_J8_21", RPI_BPLUS_GPIO_J8_21)
  162. .value("RPI_BPLUS_GPIO_J8_22", RPI_BPLUS_GPIO_J8_22)
  163. .value("RPI_BPLUS_GPIO_J8_23", RPI_BPLUS_GPIO_J8_23)
  164. .value("RPI_BPLUS_GPIO_J8_24", RPI_BPLUS_GPIO_J8_24)
  165. .value("RPI_BPLUS_GPIO_J8_26", RPI_BPLUS_GPIO_J8_26)
  166. .value("RPI_BPLUS_GPIO_J8_29", RPI_BPLUS_GPIO_J8_29)
  167. .value("RPI_BPLUS_GPIO_J8_31", RPI_BPLUS_GPIO_J8_31)
  168. .value("RPI_BPLUS_GPIO_J8_32", RPI_BPLUS_GPIO_J8_32)
  169. .value("RPI_BPLUS_GPIO_J8_33", RPI_BPLUS_GPIO_J8_33)
  170. .value("RPI_BPLUS_GPIO_J8_35", RPI_BPLUS_GPIO_J8_35)
  171. .value("RPI_BPLUS_GPIO_J8_36", RPI_BPLUS_GPIO_J8_36)
  172. .value("RPI_BPLUS_GPIO_J8_37", RPI_BPLUS_GPIO_J8_37)
  173. .value("RPI_BPLUS_GPIO_J8_38", RPI_BPLUS_GPIO_J8_38)
  174. .value("RPI_BPLUS_GPIO_J8_40", RPI_BPLUS_GPIO_J8_40)
  175. .export_values()
  176. ;
  177. bp::enum_< bcm2835SPIClockDivider>("bcm2835SPIClockDivider")
  178. .value("BCM2835_SPI_CLOCK_DIVIDER_65536", BCM2835_SPI_CLOCK_DIVIDER_65536)
  179. .value("BCM2835_SPI_CLOCK_DIVIDER_32768", BCM2835_SPI_CLOCK_DIVIDER_32768)
  180. .value("BCM2835_SPI_CLOCK_DIVIDER_16384", BCM2835_SPI_CLOCK_DIVIDER_16384)
  181. .value("BCM2835_SPI_CLOCK_DIVIDER_8192", BCM2835_SPI_CLOCK_DIVIDER_8192)
  182. .value("BCM2835_SPI_CLOCK_DIVIDER_4096", BCM2835_SPI_CLOCK_DIVIDER_4096)
  183. .value("BCM2835_SPI_CLOCK_DIVIDER_2048", BCM2835_SPI_CLOCK_DIVIDER_2048)
  184. .value("BCM2835_SPI_CLOCK_DIVIDER_1024", BCM2835_SPI_CLOCK_DIVIDER_1024)
  185. .value("BCM2835_SPI_CLOCK_DIVIDER_512", BCM2835_SPI_CLOCK_DIVIDER_512)
  186. .value("BCM2835_SPI_CLOCK_DIVIDER_256", BCM2835_SPI_CLOCK_DIVIDER_256)
  187. .value("BCM2835_SPI_CLOCK_DIVIDER_128", BCM2835_SPI_CLOCK_DIVIDER_128)
  188. .value("BCM2835_SPI_CLOCK_DIVIDER_64", BCM2835_SPI_CLOCK_DIVIDER_64)
  189. .value("BCM2835_SPI_CLOCK_DIVIDER_32", BCM2835_SPI_CLOCK_DIVIDER_32)
  190. .value("BCM2835_SPI_CLOCK_DIVIDER_16", BCM2835_SPI_CLOCK_DIVIDER_16)
  191. .value("BCM2835_SPI_CLOCK_DIVIDER_8", BCM2835_SPI_CLOCK_DIVIDER_8)
  192. .value("BCM2835_SPI_CLOCK_DIVIDER_4", BCM2835_SPI_CLOCK_DIVIDER_4)
  193. .value("BCM2835_SPI_CLOCK_DIVIDER_2", BCM2835_SPI_CLOCK_DIVIDER_2)
  194. .value("BCM2835_SPI_CLOCK_DIVIDER_1", BCM2835_SPI_CLOCK_DIVIDER_1)
  195. .export_values();
  196. bp::enum_< bcm2835SPIChipSelect>("bcm2835SPIChipSelect")
  197. .value("BCM2835_SPI_CS0", BCM2835_SPI_CS0)
  198. .value("BCM2835_SPI_CS1", BCM2835_SPI_CS1)
  199. .value("BCM2835_SPI_CS2", BCM2835_SPI_CS2)
  200. .value("BCM2835_SPI_CS_NONE", BCM2835_SPI_CS_NONE)
  201. .export_values();
  202. // exposing '#define's for SPI speed as this is needed for RF24 constructor
  203. bp::scope().attr("BCM2835_SPI_SPEED_64MHZ") = BCM2835_SPI_SPEED_64MHZ;
  204. bp::scope().attr("BCM2835_SPI_SPEED_32MHZ") = BCM2835_SPI_SPEED_32MHZ;
  205. bp::scope().attr("BCM2835_SPI_SPEED_16MHZ") = BCM2835_SPI_SPEED_16MHZ;
  206. bp::scope().attr("BCM2835_SPI_SPEED_8MHZ") = BCM2835_SPI_SPEED_8MHZ;
  207. bp::scope().attr("BCM2835_SPI_SPEED_4MHZ") = BCM2835_SPI_SPEED_4MHZ;
  208. bp::scope().attr("BCM2835_SPI_SPEED_2MHZ") = BCM2835_SPI_SPEED_2MHZ;
  209. bp::scope().attr("BCM2835_SPI_SPEED_1MHZ") = BCM2835_SPI_SPEED_1MHZ;
  210. bp::scope().attr("BCM2835_SPI_SPEED_512KHZ") = BCM2835_SPI_SPEED_512KHZ;
  211. bp::scope().attr("BCM2835_SPI_SPEED_256KHZ") = BCM2835_SPI_SPEED_256KHZ;
  212. bp::scope().attr("BCM2835_SPI_SPEED_128KHZ") = BCM2835_SPI_SPEED_128KHZ;
  213. bp::scope().attr("BCM2835_SPI_SPEED_64KHZ") = BCM2835_SPI_SPEED_64KHZ;
  214. bp::scope().attr("BCM2835_SPI_SPEED_32KHZ") = BCM2835_SPI_SPEED_32KHZ;
  215. bp::scope().attr("BCM2835_SPI_SPEED_16KHZ") = BCM2835_SPI_SPEED_16KHZ;
  216. bp::scope().attr("BCM2835_SPI_SPEED_8KHZ") = BCM2835_SPI_SPEED_8KHZ;
  217. #endif // BCM2835_H
  218. bp::enum_< rf24_crclength_e>("rf24_crclength_e")
  219. .value("RF24_CRC_DISABLED", RF24_CRC_DISABLED)
  220. .value("RF24_CRC_8", RF24_CRC_8)
  221. .value("RF24_CRC_16", RF24_CRC_16)
  222. .export_values()
  223. ;
  224. bp::enum_< rf24_datarate_e>("rf24_datarate_e")
  225. .value("RF24_1MBPS", RF24_1MBPS)
  226. .value("RF24_2MBPS", RF24_2MBPS)
  227. .value("RF24_250KBPS", RF24_250KBPS)
  228. .export_values()
  229. ;
  230. bp::enum_< rf24_pa_dbm_e>("rf24_pa_dbm_e")
  231. .value("RF24_PA_MIN", RF24_PA_MIN)
  232. .value("RF24_PA_LOW", RF24_PA_LOW)
  233. .value("RF24_PA_HIGH", RF24_PA_HIGH)
  234. .value("RF24_PA_MAX", RF24_PA_MAX)
  235. .value("RF24_PA_ERROR", RF24_PA_ERROR)
  236. .export_values()
  237. ;
  238. // ******************** RF24 class **************************
  239. //
  240. bp::class_< RF24 >( "RF24", bp::init< uint8_t, uint8_t >(( bp::arg("_cepin"), bp::arg("_cspin") )) )
  241. #if defined (RF24_LINUX) && !defined (MRAA)
  242. .def( bp::init< uint8_t, uint8_t, uint32_t >(( bp::arg("_cepin"), bp::arg("_cspin"), bp::arg("spispeed") )) )
  243. #endif
  244. .def("available", (bool ( ::RF24::* )( ) )( &::RF24::available ) )
  245. .def("available_pipe", &available_wrap ) // needed to rename this method as python does not allow such overloading
  246. .def("begin", &RF24::begin)
  247. .def("closeReadingPipe", &RF24::closeReadingPipe)
  248. .def("disableCRC", &RF24::disableCRC)
  249. .def("enableAckPayload", &RF24::enableAckPayload)
  250. .def("enableDynamicAck", &RF24::enableDynamicAck)
  251. .def("enableDynamicPayloads", &RF24::enableDynamicPayloads)
  252. .def("flush_tx", &RF24::flush_tx)
  253. .def("getCRCLength", &RF24::getCRCLength)
  254. .def("getDataRate", &RF24::getDataRate)
  255. .def("getDynamicPayloadSize", &RF24::getDynamicPayloadSize)
  256. .def("getPALevel", &RF24::getPALevel)
  257. .def("isAckPayloadAvailable", &RF24::isAckPayloadAvailable)
  258. .def("isPVariant", &RF24::isPVariant)
  259. .def("isValid", &RF24::isValid)
  260. .def("maskIRQ", &RF24::maskIRQ, ( bp::arg("tx_ok"), bp::arg("tx_fail"), bp::arg("rx_ready")))
  261. .def("openReadingPipe", &openReadingPipe_wrap, (bp::arg("number"), bp::arg("address")))
  262. .def("openReadingPipe", (void ( ::RF24::* )( ::uint8_t,::uint64_t ) )( &::RF24::openReadingPipe), (bp::arg("number"), bp::arg("address")))
  263. .def("openWritingPipe", &openWritingPipe_wrap, (bp::arg("address")))
  264. .def("openWritingPipe", (void ( ::RF24::* )( ::uint64_t ) )( &::RF24::openWritingPipe), ( bp::arg("address") ) )
  265. .def("powerDown", &RF24::powerDown)
  266. .def("powerUp", &RF24::powerUp)
  267. .def("printDetails", &RF24::printDetails)
  268. .def("reUseTX", &RF24::reUseTX)
  269. .def("read", &read_wrap, (bp::arg("maxlen")))
  270. .def("rxFifoFull", &RF24::rxFifoFull)
  271. .def("setAddressWidth", &RF24::setAddressWidth)
  272. .def("setAutoAck", (void ( ::RF24::* )( bool ) )( &::RF24::setAutoAck ), ( bp::arg("enable") ) )
  273. .def("setAutoAck", (void ( ::RF24::* )( ::uint8_t,bool ) )( &::RF24::setAutoAck ), ( bp::arg("pipe"), bp::arg("enable") ) )
  274. .def("setCRCLength", &RF24::setCRCLength, ( bp::arg("length") ) )
  275. .def("setChannel", &RF24::setChannel, ( bp::arg("channel") ) )
  276. .def("setDataRate", &RF24::setDataRate, ( bp::arg("speed") ) )
  277. .def("setPALevel", &RF24::setPALevel, ( bp::arg("level") ) )
  278. .def("setRetries", &RF24::setRetries , (bp::arg("delay"), bp::arg("count")))
  279. .def("startFastWrite", &startFastWrite_wrap1, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast") ) )
  280. .def("startFastWrite", &startFastWrite_wrap2, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast"), bp::arg("startTx") ) )
  281. .def("startListening", &RF24::startListening)
  282. .def("startWrite", &startWrite_wrap, ( bp::arg("buf"), bp::arg("len"), bp::arg("multicast") ) )
  283. .def("stopListening", &RF24::stopListening)
  284. .def("testCarrier", &RF24::testCarrier)
  285. .def("testRPD", &RF24::testRPD)
  286. .def("txStandBy", (bool ( ::RF24::* )( ::uint32_t,bool))(&RF24::txStandBy), txStandBy_wrap1( bp::args("timeout", "startTx") ) )
  287. .def("whatHappened", &whatHappened_wrap)
  288. .def("write", &write_wrap1, ( bp::arg("buf") ) )
  289. .def("write", &write_wrap2, ( bp::arg("buf"), bp::arg("multicast") ) )
  290. .def("writeAckPayload", writeAckPayload_wrap, ( bp::arg("pipe"), bp::arg("buf") ) )
  291. .def("writeBlocking", &writeBlocking_wrap, ( bp::arg("buf"), bp::arg("timeout") ) )
  292. .def("writeFast", &writeFast_wrap1, ( bp::arg("buf") ) )
  293. .def("writeFast", &writeFast_wrap2, ( bp::arg("buf"), bp::arg("multicast") ) )
  294. .add_property("payloadSize", &RF24::getPayloadSize, &RF24::setPayloadSize)
  295. .def_readwrite( "failureDetected", &RF24::failureDetected );
  296. }