PacketFormatter.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <Arduino.h>
  2. #include <inttypes.h>
  3. #include <functional>
  4. #include <MiLightConstants.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 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. virtual void updateStatus(MiLightStatus status, uint8_t groupId);
  37. virtual void command(uint8_t command, uint8_t arg);
  38. virtual void setHeld(bool held);
  39. // Mode
  40. virtual void updateMode(uint8_t value);
  41. virtual void modeSpeedDown();
  42. virtual void modeSpeedUp();
  43. virtual void nextMode();
  44. virtual void previousMode();
  45. virtual void pair();
  46. virtual void unpair();
  47. // Color
  48. virtual void updateHue(uint16_t value);
  49. virtual void updateColorRaw(uint8_t value);
  50. virtual void updateColorWhite();
  51. // White temperature
  52. virtual void increaseTemperature();
  53. virtual void decreaseTemperature();
  54. virtual void updateTemperature(uint8_t value);
  55. // Brightness
  56. virtual void updateBrightness(uint8_t value);
  57. virtual void increaseBrightness();
  58. virtual void decreaseBrightness();
  59. virtual void enableNightMode();
  60. virtual void updateSaturation(uint8_t value);
  61. virtual void reset();
  62. virtual PacketStream& buildPackets();
  63. virtual void prepare(uint16_t deviceId, uint8_t groupId);
  64. virtual void format(uint8_t const* packet, char* buffer);
  65. virtual BulbId parsePacket(const uint8_t* packet, JsonObject& result);
  66. static void formatV1Packet(uint8_t const* packet, char* buffer);
  67. size_t getPacketLength() const;
  68. protected:
  69. uint8_t* currentPacket;
  70. size_t packetLength;
  71. uint16_t deviceId;
  72. uint8_t groupId;
  73. uint8_t sequenceNum;
  74. size_t numPackets;
  75. bool held;
  76. PacketStream packetStream;
  77. GroupStateStore* stateStore = NULL;
  78. const Settings* settings = NULL;
  79. void pushPacket();
  80. // Get field into a desired state using only increment/decrement commands. Do this by:
  81. // 1. Driving it down to its minimum value
  82. // 2. Applying the appropriate number of increase commands to get it to the desired
  83. // value.
  84. // If the current state is already known, take that into account and apply the exact
  85. // number of rpeeats for the appropriate command.
  86. void valueByStepFunction(StepFunction increase, StepFunction decrease, uint8_t numSteps, uint8_t targetValue, int8_t knownValue = -1);
  87. virtual void initializePacket(uint8_t* packetStart) = 0;
  88. virtual void finalizePacket(uint8_t* packet);
  89. };
  90. #endif