MiLightRadioConfig.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. static const uint8_t SYNCWORD_LENGTH = 5;
  12. MiLightRadioConfig(
  13. const uint16_t syncword0,
  14. const uint16_t syncword3,
  15. const size_t packetLength,
  16. const uint8_t channel0,
  17. const uint8_t channel1,
  18. const uint8_t channel2,
  19. const uint8_t preamble,
  20. const uint8_t trailer
  21. ) : syncword0(syncword0)
  22. , syncword3(syncword3)
  23. , packetLength(packetLength)
  24. {
  25. channels[0] = channel0;
  26. channels[1] = channel1;
  27. channels[2] = channel2;
  28. size_t ix = SYNCWORD_LENGTH;
  29. // precompute the syncword for the nRF24. we include the fixed preamble and trailer in the
  30. // syncword to avoid needing to bitshift packets. trailer is 4 bits, so the actual syncword
  31. // is no longer byte-aligned.
  32. syncwordBytes[ --ix ] = reverseBits(
  33. ((syncword0 << 4) & 0xF0) | (preamble & 0x0F)
  34. );
  35. syncwordBytes[ --ix ] = reverseBits((syncword0 >> 4) & 0xFF);
  36. syncwordBytes[ --ix ] = reverseBits(((syncword0 >> 12) & 0x0F) + ((syncword3 << 4) & 0xF0));
  37. syncwordBytes[ --ix ] = reverseBits((syncword3 >> 4) & 0xFF);
  38. syncwordBytes[ --ix ] = reverseBits(
  39. ((syncword3 >> 12) & 0x0F) | ((trailer << 4) & 0xF0)
  40. );
  41. }
  42. uint8_t channels[3];
  43. uint8_t syncwordBytes[SYNCWORD_LENGTH];
  44. uint16_t syncword0, syncword3;
  45. const size_t packetLength;
  46. static const size_t NUM_CONFIGS = 4;
  47. static MiLightRadioConfig ALL_CONFIGS[NUM_CONFIGS];
  48. };
  49. #endif