PacketFormatter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <Arduino.h>
  2. #include <inttypes.h>
  3. #include <functional>
  4. #include <MiLightRemoteType.h>
  5. #include <ArduinoJson.h>
  6. #include <GroupState.h>
  7. #include <GroupStateStore.h>
  8. #include <Settings.h>
  9. #ifndef _PACKET_FORMATTER_H
  10. #define _PACKET_FORMATTER_H
  11. // Most packets sent is for CCT bulbs, which always includes 10 down commands
  12. // and can include up to 10 up commands. CCT packets are 7 bytes.
  13. // (10 * 7) + (10 * 7) = 140
  14. #define PACKET_FORMATTER_BUFFER_SIZE 140
  15. struct PacketStream {
  16. PacketStream();
  17. uint8_t* next();
  18. bool hasNext();
  19. uint8_t* packetStream;
  20. size_t numPackets;
  21. size_t packetLength;
  22. size_t currentPacket;
  23. };
  24. class PacketFormatter {
  25. public:
  26. PacketFormatter(const MiLightRemoteType deviceType, const size_t packetLength, const size_t maxPackets = 1);
  27. // Ideally these would be constructor parameters. We could accomplish this by
  28. // wrapping PacketFormaters in a factory, as Settings and StateStore are not
  29. // available at construction time.
  30. //
  31. // For now, just rely on the user calling this method.
  32. void initialize(GroupStateStore* stateStore, const Settings* settings);
  33. typedef void (PacketFormatter::*StepFunction)();
  34. virtual bool canHandle(const uint8_t* packet, const size_t len);
  35. void updateStatus(MiLightStatus status);
  36. void toggleStatus();
  37. virtual void updateStatus(MiLightStatus status, uint8_t groupId);
  38. virtual void command(uint8_t command, uint8_t arg);
  39. virtual void setHeld(bool held);
  40. // Mode
  41. virtual void updateMode(uint8_t value);
  42. virtual void modeSpeedDown();
  43. virtual void modeSpeedUp();
  44. virtual void nextMode();
  45. virtual void previousMode();
  46. virtual void pair();
  47. virtual void unpair();
  48. // Color
  49. virtual void updateHue(uint16_t value);
  50. virtual void updateColorRaw(uint8_t value);
  51. virtual void updateColorWhite();
  52. // White temperature
  53. virtual void increaseTemperature();
  54. virtual void decreaseTemperature();
  55. virtual void updateTemperature(uint8_t value);
  56. // Brightness
  57. virtual void updateBrightness(uint8_t value);
  58. virtual void increaseBrightness();
  59. virtual void decreaseBrightness();
  60. virtual void enableNightMode();
  61. virtual void updateSaturation(uint8_t value);
  62. virtual void reset();
  63. virtual PacketStream& buildPackets();
  64. virtual void prepare(uint16_t deviceId, uint8_t groupId);
  65. virtual void format(uint8_t const* packet, char* buffer);
  66. virtual BulbId parsePacket(const uint8_t* packet, JsonObject result);
  67. static void formatV1Packet(uint8_t const* packet, char* buffer);
  68. size_t getPacketLength() const;
  69. protected:
  70. const MiLightRemoteType deviceType;
  71. size_t packetLength;
  72. size_t numPackets;
  73. uint8_t* currentPacket;
  74. bool held;
  75. uint16_t deviceId;
  76. uint8_t groupId;
  77. uint8_t sequenceNum;
  78. PacketStream packetStream;
  79. GroupStateStore* stateStore = NULL;
  80. const Settings* settings = NULL;
  81. void pushPacket();
  82. // Get field into a desired state using only increment/decrement commands. Do this by:
  83. // 1. Driving it down to its minimum value
  84. // 2. Applying the appropriate number of increase commands to get it to the desired
  85. // value.
  86. // If the current state is already known, take that into account and apply the exact
  87. // number of rpeeats for the appropriate command.
  88. void valueByStepFunction(StepFunction increase, StepFunction decrease, uint8_t numSteps, uint8_t targetValue, int8_t knownValue = -1);
  89. virtual void initializePacket(uint8_t* packetStart) = 0;
  90. virtual void finalizePacket(uint8_t* packet);
  91. };
  92. #endif