MiLightRadio.cpp 2.9 KB

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