V2PacketFormatter.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <inttypes.h>
  2. #include <PacketFormatter.h>
  3. #ifndef _V2_PACKET_FORMATTER
  4. #define _V2_PACKET_FORMATTER
  5. #define V2_PACKET_LEN 9
  6. #define V2_PROTOCOL_ID_INDEX 1
  7. #define V2_COMMAND_INDEX 4
  8. #define V2_ARGUMENT_INDEX 5
  9. // Default number of values to allow before and after strictly defined range for V2 scales
  10. #define V2_DEFAULT_RANGE_BUFFER 0x13
  11. class V2PacketFormatter : public PacketFormatter {
  12. public:
  13. V2PacketFormatter(uint8_t protocolId, uint8_t numGroups);
  14. virtual bool canHandle(const uint8_t* packet, const size_t packetLen);
  15. virtual void initializePacket(uint8_t* packet);
  16. virtual void updateStatus(MiLightStatus status, uint8_t group);
  17. virtual void command(uint8_t command, uint8_t arg);
  18. virtual void format(uint8_t const* packet, char* buffer);
  19. virtual void unpair();
  20. virtual void finalizePacket(uint8_t* packet);
  21. uint8_t groupCommandArg(MiLightStatus status, uint8_t groupId);
  22. /*
  23. * Some protocols have scales which have the following characteristics:
  24. * Start at some value X, goes down to 0, then up to Y.
  25. * eg:
  26. * 0x8F, 0x8D, ..., 0, 0x2, ..., 0x20
  27. * This is a parameterized method to convert from [0, 100] TO this scale
  28. */
  29. static uint8_t tov2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true);
  30. /*
  31. * Method to convert FROM the scale described above to [0, 100].
  32. *
  33. * An extra parameter is exposed: `buffer`, which allows for a range of values before/after the
  34. * max that will be mapped to 0 and 100, respectively.
  35. */
  36. static uint8_t fromv2scale(uint8_t value, uint8_t endValue, uint8_t interval, bool reverse = true, uint8_t buffer = V2_DEFAULT_RANGE_BUFFER);
  37. protected:
  38. const uint8_t protocolId;
  39. const uint8_t numGroups;
  40. void switchMode(const GroupState& currentState, BulbMode desiredMode);
  41. };
  42. #endif