V2PacketFormatter.h 1.6 KB

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