MiLightRadioConfig.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <Arduino.h>
  2. #include <MiLightRemoteType.h>
  3. #include <Size.h>
  4. #include <RadioUtils.h>
  5. #ifndef _MILIGHT_RADIO_CONFIG
  6. #define _MILIGHT_RADIO_CONFIG
  7. #define MILIGHT_MAX_PACKET_LENGTH 9
  8. class MiLightRadioConfig {
  9. public:
  10. static const size_t NUM_CHANNELS = 3;
  11. // We can set this to two possible values. It only has an affect on the nRF24 radio. The
  12. // LT8900/PL1167 radio will always use the raw syncwords. For the nRF24, this controls what
  13. // we set the "address" to, which roughly corresponds to the LT8900 syncword.
  14. //
  15. // The PL1167 packet is structured as follows (lengths in bits):
  16. // Preamble ( 8) | Syncword (32) | Trailer ( 4) | Packet Len ( 8) | Packet (...)
  17. //
  18. // 4 -- Use the raw syncword bits as the address. This means the Trailer will be included in
  19. // the packet data. Since the Trailer is 4 bits, packet data will not be byte-aligned,
  20. // and the data must be bitshifted every time it's received.
  21. //
  22. // 5 -- Include the Trailer in the syncword. Avoids us needing to bitshift packet data. The
  23. // downside is that the Trailer is hardcoded and assumed based on received packets.
  24. //
  25. // In general, this should be set to 5 unless packets that should be showing up are
  26. // mysteriously not present.
  27. static const uint8_t SYNCWORD_LENGTH = 5;
  28. MiLightRadioConfig(
  29. const uint16_t syncword0,
  30. const uint16_t syncword3,
  31. const size_t packetLength,
  32. const uint8_t channel0,
  33. const uint8_t channel1,
  34. const uint8_t channel2,
  35. const uint8_t preamble,
  36. const uint8_t trailer
  37. ) : syncword0(syncword0)
  38. , syncword3(syncword3)
  39. , packetLength(packetLength)
  40. {
  41. channels[0] = channel0;
  42. channels[1] = channel1;
  43. channels[2] = channel2;
  44. size_t ix = SYNCWORD_LENGTH;
  45. // precompute the syncword for the nRF24. we include the fixed preamble and trailer in the
  46. // syncword to avoid needing to bitshift packets. trailer is 4 bits, so the actual syncword
  47. // is no longer byte-aligned.
  48. if (SYNCWORD_LENGTH == 5) {
  49. syncwordBytes[ --ix ] = reverseBits(
  50. ((syncword0 << 4) & 0xF0) | (preamble & 0x0F)
  51. );
  52. syncwordBytes[ --ix ] = reverseBits((syncword0 >> 4) & 0xFF);
  53. syncwordBytes[ --ix ] = reverseBits(((syncword0 >> 12) & 0x0F) + ((syncword3 << 4) & 0xF0));
  54. syncwordBytes[ --ix ] = reverseBits((syncword3 >> 4) & 0xFF);
  55. syncwordBytes[ --ix ] = reverseBits(
  56. ((syncword3 >> 12) & 0x0F) | ((trailer << 4) & 0xF0)
  57. );
  58. } else {
  59. syncwordBytes[ --ix ] = reverseBits(syncword0 & 0xff);
  60. syncwordBytes[ --ix ] = reverseBits( (syncword0 >> 8) & 0xff);
  61. syncwordBytes[ --ix ] = reverseBits(syncword3 & 0xff);
  62. syncwordBytes[ --ix ] = reverseBits( (syncword3 >> 8) & 0xff);
  63. }
  64. }
  65. uint8_t channels[3];
  66. uint8_t syncwordBytes[SYNCWORD_LENGTH];
  67. uint16_t syncword0, syncword3;
  68. const size_t packetLength;
  69. static const size_t NUM_CONFIGS = 4;
  70. static MiLightRadioConfig ALL_CONFIGS[NUM_CONFIGS];
  71. };
  72. #endif