NRF24MiLightRadio.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. retval = _pl1167.setSyncword(_config.syncword0, _config.syncword3);
  31. if (retval < 0) {
  32. return retval;
  33. }
  34. // +1 to be able to buffer the length
  35. retval = _pl1167.setMaxPacketLength(_config.packetLength + 1);
  36. if (retval < 0) {
  37. return retval;
  38. }
  39. return 0;
  40. }
  41. bool NRF24MiLightRadio::available() {
  42. if (_waiting) {
  43. #ifdef DEBUG_PRINTF
  44. printf("_waiting\n");
  45. #endif
  46. return true;
  47. }
  48. if (_pl1167.receive(_config.channels[listenChannelIx]) > 0) {
  49. #ifdef DEBUG_PRINTF
  50. printf("NRF24MiLightRadio - received packet!\n");
  51. #endif
  52. size_t packet_length = sizeof(_packet);
  53. if (_pl1167.readFIFO(_packet, packet_length) < 0) {
  54. return false;
  55. }
  56. #ifdef DEBUG_PRINTF
  57. printf("NRF24MiLightRadio - Checking packet length (expecting %d, is %d)\n", _packet[0] + 1U, packet_length);
  58. #endif
  59. if (packet_length == 0 || packet_length != _packet[0] + 1U) {
  60. return false;
  61. }
  62. uint32_t packet_id = PACKET_ID(_packet, packet_length);
  63. #ifdef DEBUG_PRINTF
  64. printf("Packet id: %d\n", packet_id);
  65. #endif
  66. if (packet_id == _prev_packet_id) {
  67. _dupes_received++;
  68. } else {
  69. _prev_packet_id = packet_id;
  70. _waiting = true;
  71. }
  72. }
  73. return _waiting;
  74. }
  75. int NRF24MiLightRadio::read(uint8_t frame[], size_t &frame_length)
  76. {
  77. if (!_waiting) {
  78. frame_length = 0;
  79. return -1;
  80. }
  81. if (frame_length > sizeof(_packet) - 1) {
  82. frame_length = sizeof(_packet) - 1;
  83. }
  84. if (frame_length > _packet[0]) {
  85. frame_length = _packet[0];
  86. }
  87. memcpy(frame, _packet + 1, frame_length);
  88. _waiting = false;
  89. return _packet[0];
  90. }
  91. int NRF24MiLightRadio::write(uint8_t frame[], size_t frame_length) {
  92. if (frame_length > sizeof(_out_packet) - 1) {
  93. return -1;
  94. }
  95. memcpy(_out_packet + 1, frame, frame_length);
  96. _out_packet[0] = frame_length;
  97. int retval = resend();
  98. if (retval < 0) {
  99. return retval;
  100. }
  101. return frame_length;
  102. }
  103. int NRF24MiLightRadio::resend() {
  104. for (std::vector<RF24Channel>::const_iterator it = channels.begin(); it != channels.end(); ++it) {
  105. size_t channelIx = static_cast<uint8_t>(*it);
  106. uint8_t channel = _config.channels[channelIx];
  107. _pl1167.writeFIFO(_out_packet, _out_packet[0] + 1);
  108. _pl1167.transmit(channel);
  109. }
  110. return 0;
  111. }
  112. const MiLightRadioConfig& NRF24MiLightRadio::config() {
  113. return _config;
  114. }