PacketFormatter.cpp 4.7 KB

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