NRF24MiLightRadio.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Adapated from code from henryk
  2. #include <PL1167_nRF24.h>
  3. #include <NRF24MiLightRadio.h>
  4. #define PACKET_ID(packet, packet_length) ( (packet[1] << 8) | packet[packet_length - 1] )
  5. NRF24MiLightRadio::NRF24MiLightRadio(
  6. RF24& rf24,
  7. const MiLightRadioConfig& config,
  8. const std::vector<RF24Channel>& channels,
  9. RF24Channel listenChannel
  10. )
  11. : channels(channels),
  12. listenChannelIx(static_cast<size_t>(listenChannel)),
  13. _pl1167(PL1167_nRF24(rf24)),
  14. _config(config),
  15. _waiting(false)
  16. { }
  17. int NRF24MiLightRadio::begin() {
  18. int retval = _pl1167.open();
  19. if (retval < 0) {
  20. return retval;
  21. }
  22. retval = configure();
  23. if (retval < 0) {
  24. return retval;
  25. }
  26. available();
  27. return 0;
  28. }
  29. int NRF24MiLightRadio::configure() {
  30. int retval = _pl1167.setCRC(true);
  31. if (retval < 0) {
  32. return retval;
  33. }
  34. retval = _pl1167.setPreambleLength(3);
  35. if (retval < 0) {
  36. return retval;
  37. }
  38. retval = _pl1167.setTrailerLength(4);
  39. if (retval < 0) {
  40. return retval;
  41. }
  42. retval = _pl1167.setSyncword(_config.syncword0, _config.syncword3);
  43. if (retval < 0) {
  44. return retval;
  45. }
  46. // +1 to be able to buffer the length
  47. retval = _pl1167.setMaxPacketLength(_config.packetLength + 1);
  48. if (retval < 0) {
  49. return retval;
  50. }
  51. return 0;
  52. }
  53. bool NRF24MiLightRadio::available() {
  54. if (_waiting) {
  55. #ifdef DEBUG_PRINTF
  56. printf("_waiting\n");
  57. #endif
  58. return true;
  59. }
  60. if (_pl1167.receive(_config.channels[listenChannelIx]) > 0) {
  61. #ifdef DEBUG_PRINTF
  62. printf("NRF24MiLightRadio - received packet!\n");
  63. #endif
  64. size_t packet_length = sizeof(_packet);
  65. if (_pl1167.readFIFO(_packet, packet_length) < 0) {
  66. return false;
  67. }
  68. #ifdef DEBUG_PRINTF
  69. printf("NRF24MiLightRadio - Checking packet length (expecting %d, is %d)\n", _packet[0] + 1U, packet_length);
  70. #endif
  71. if (packet_length == 0 || packet_length != _packet[0] + 1U) {
  72. return false;
  73. }
  74. uint32_t packet_id = PACKET_ID(_packet, packet_length);
  75. #ifdef DEBUG_PRINTF
  76. printf("Packet id: %d\n", packet_id);
  77. #endif
  78. if (packet_id == _prev_packet_id) {
  79. _dupes_received++;
  80. } else {
  81. _prev_packet_id = packet_id;
  82. _waiting = true;
  83. }
  84. }
  85. return _waiting;
  86. }
  87. int NRF24MiLightRadio::read(uint8_t frame[], size_t &frame_length)
  88. {
  89. if (!_waiting) {
  90. frame_length = 0;
  91. return -1;
  92. }
  93. if (frame_length > sizeof(_packet) - 1) {
  94. frame_length = sizeof(_packet) - 1;
  95. }
  96. if (frame_length > _packet[0]) {
  97. frame_length = _packet[0];
  98. }
  99. memcpy(frame, _packet + 1, frame_length);
  100. _waiting = false;
  101. return _packet[0];
  102. }
  103. int NRF24MiLightRadio::write(uint8_t frame[], size_t frame_length) {
  104. if (frame_length > sizeof(_out_packet) - 1) {
  105. return -1;
  106. }
  107. memcpy(_out_packet + 1, frame, frame_length);
  108. _out_packet[0] = frame_length;
  109. int retval = resend();
  110. if (retval < 0) {
  111. return retval;
  112. }
  113. return frame_length;
  114. }
  115. int NRF24MiLightRadio::resend() {
  116. for (std::vector<RF24Channel>::const_iterator it = channels.begin(); it != channels.end(); ++it) {
  117. size_t channelIx = static_cast<uint8_t>(*it);
  118. uint8_t channel = _config.channels[channelIx];
  119. _pl1167.writeFIFO(_out_packet, _out_packet[0] + 1);
  120. _pl1167.transmit(channel);
  121. }
  122. return 0;
  123. }
  124. const MiLightRadioConfig& NRF24MiLightRadio::config() {
  125. return _config;
  126. }