PacketFormatter.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <PacketFormatter.h>
  2. static uint8_t* PACKET_BUFFER = new uint8_t[PACKET_FORMATTER_BUFFER_SIZE];
  3. PacketStream::PacketStream()
  4. : packetStream(PACKET_BUFFER),
  5. numPackets(0),
  6. packetLength(0),
  7. currentPacket(0)
  8. { }
  9. bool PacketStream::hasNext() {
  10. return currentPacket < numPackets;
  11. }
  12. uint8_t* PacketStream::next() {
  13. uint8_t* packet = packetStream + (currentPacket * packetLength);
  14. currentPacket++;
  15. return packet;
  16. }
  17. PacketFormatter::PacketFormatter(const size_t packetLength, const size_t maxPackets)
  18. : packetLength(packetLength),
  19. numPackets(0),
  20. currentPacket(NULL),
  21. held(false)
  22. {
  23. packetStream.packetLength = packetLength;
  24. }
  25. bool PacketFormatter::canHandle(const uint8_t *packet, const size_t len) {
  26. return len == packetLength;
  27. }
  28. void PacketFormatter::finalizePacket(uint8_t* packet) { }
  29. void PacketFormatter::updateStatus(MiLightStatus status) {
  30. updateStatus(status, groupId);
  31. }
  32. void PacketFormatter::setHeld(bool held) {
  33. this->held = held;
  34. }
  35. void PacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) { }
  36. void PacketFormatter::updateBrightness(uint8_t value) { }
  37. void PacketFormatter::updateMode(uint8_t value) { }
  38. void PacketFormatter::modeSpeedDown() { }
  39. void PacketFormatter::modeSpeedUp() { }
  40. void PacketFormatter::nextMode() { }
  41. void PacketFormatter::previousMode() { }
  42. void PacketFormatter::command(uint8_t command, uint8_t arg) { }
  43. void PacketFormatter::updateHue(uint16_t value) { }
  44. void PacketFormatter::updateColorRaw(uint8_t value) { }
  45. void PacketFormatter::updateColorWhite() { }
  46. void PacketFormatter::increaseTemperature() { }
  47. void PacketFormatter::decreaseTemperature() { }
  48. void PacketFormatter::increaseBrightness() { }
  49. void PacketFormatter::decreaseBrightness() { }
  50. void PacketFormatter::enableNightMode() { }
  51. void PacketFormatter::updateTemperature(uint8_t value) { }
  52. void PacketFormatter::updateSaturation(uint8_t value) { }
  53. BulbId PacketFormatter::parsePacket(const uint8_t *packet, JsonObject &result) {
  54. return DEFAULT_BULB_ID;
  55. }
  56. void PacketFormatter::pair() {
  57. for (size_t i = 0; i < 5; i++) {
  58. updateStatus(ON);
  59. }
  60. }
  61. void PacketFormatter::unpair() {
  62. pair();
  63. }
  64. PacketStream& PacketFormatter::buildPackets() {
  65. if (numPackets > 0) {
  66. finalizePacket(currentPacket);
  67. }
  68. packetStream.numPackets = numPackets;
  69. packetStream.currentPacket = 0;
  70. return packetStream;
  71. }
  72. void PacketFormatter::valueByStepFunction(StepFunction increase, StepFunction decrease, uint8_t numSteps, uint8_t targetValue, int8_t knownValue) {
  73. StepFunction fn;
  74. size_t numCommands = 0;
  75. // If current value is not known, drive down to minimum value. Then we can assume that we
  76. // know the state (it'll be 0).
  77. if (knownValue == -1) {
  78. for (size_t i = 0; i < numSteps; i++) {
  79. (this->*decrease)();
  80. }
  81. fn = increase;
  82. numCommands = targetValue;
  83. } else if (targetValue < knownValue) {
  84. fn = decrease;
  85. numCommands = (knownValue - targetValue);
  86. } else if (targetValue > knownValue) {
  87. fn = increase;
  88. numCommands = (targetValue - knownValue);
  89. }
  90. // Get to the desired value
  91. for (size_t i = 0; i < numCommands; i++) {
  92. (this->*fn)();
  93. }
  94. }
  95. void PacketFormatter::prepare(uint16_t deviceId, uint8_t groupId, GroupStateStore* stateStore, const Settings* settings) {
  96. this->deviceId = deviceId;
  97. this->groupId = groupId;
  98. this->stateStore = stateStore;
  99. this->settings = settings;
  100. reset();
  101. }
  102. void PacketFormatter::reset() {
  103. this->numPackets = 0;
  104. this->currentPacket = PACKET_BUFFER;
  105. this->held = false;
  106. }
  107. void PacketFormatter::pushPacket() {
  108. if (numPackets > 0) {
  109. finalizePacket(currentPacket);
  110. }
  111. // Make sure there's enough buffer to add another packet.
  112. if ((currentPacket + packetLength) >= PACKET_BUFFER + PACKET_FORMATTER_BUFFER_SIZE) {
  113. Serial.println(F("ERROR: packet buffer full! Cannot buffer a new packet. THIS IS A BUG!"));
  114. return;
  115. }
  116. currentPacket = PACKET_BUFFER + (numPackets * packetLength);
  117. numPackets++;
  118. initializePacket(currentPacket);
  119. }
  120. void PacketFormatter::format(uint8_t const* packet, char* buffer) {
  121. for (int i = 0; i < packetLength; i++) {
  122. sprintf_P(buffer, "%02X ", packet[i]);
  123. buffer += 3;
  124. }
  125. sprintf_P(buffer, "\n\n");
  126. }
  127. void PacketFormatter::formatV1Packet(uint8_t const* packet, char* buffer) {
  128. buffer += sprintf_P(buffer, "Request type : %02X\n", packet[0]) ;
  129. buffer += sprintf_P(buffer, "Device ID : %02X%02X\n", packet[1], packet[2]);
  130. buffer += sprintf_P(buffer, "b1 : %02X\n", packet[3]);
  131. buffer += sprintf_P(buffer, "b2 : %02X\n", packet[4]);
  132. buffer += sprintf_P(buffer, "b3 : %02X\n", packet[5]);
  133. buffer += sprintf_P(buffer, "Sequence Num. : %02X", packet[6]);
  134. }
  135. size_t PacketFormatter::getPacketLength() const {
  136. return packetLength;
  137. }