PacketFormatter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, GroupStateStore* stateStore) {
  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 value) {
  73. for (size_t i = 0; i < numSteps; i++) {
  74. (this->*decrease)();
  75. }
  76. for (size_t i = 0; i < value; i++) {
  77. (this->*increase)();
  78. }
  79. }
  80. void PacketFormatter::prepare(uint16_t deviceId, uint8_t groupId) {
  81. this->deviceId = deviceId;
  82. this->groupId = groupId;
  83. reset();
  84. }
  85. void PacketFormatter::reset() {
  86. this->numPackets = 0;
  87. this->currentPacket = currentPacket;
  88. this->held = false;
  89. }
  90. void PacketFormatter::pushPacket() {
  91. if (numPackets > 0) {
  92. finalizePacket(currentPacket);
  93. }
  94. // Make sure there's enough buffer to add another packet.
  95. if ((currentPacket + packetLength) > PACKET_BUFFER + PACKET_FORMATTER_BUFFER_SIZE) {
  96. Serial.println(F("ERROR: packet buffer full! Cannot buffer a new packet. THIS IS A BUG!"));
  97. return;
  98. }
  99. currentPacket = PACKET_BUFFER + (numPackets * packetLength);
  100. numPackets++;
  101. initializePacket(currentPacket);
  102. }
  103. void PacketFormatter::format(uint8_t const* packet, char* buffer) {
  104. for (int i = 0; i < packetLength; i++) {
  105. sprintf_P(buffer, "%02X ", packet[i]);
  106. buffer += 3;
  107. }
  108. sprintf_P(buffer, "\n\n");
  109. }
  110. void PacketFormatter::formatV1Packet(uint8_t const* packet, char* buffer) {
  111. buffer += sprintf_P(buffer, "Request type : %02X\n", packet[0]) ;
  112. buffer += sprintf_P(buffer, "Device ID : %02X%02X\n", packet[1], packet[2]);
  113. buffer += sprintf_P(buffer, "b1 : %02X\n", packet[3]);
  114. buffer += sprintf_P(buffer, "b2 : %02X\n", packet[4]);
  115. buffer += sprintf_P(buffer, "b3 : %02X\n", packet[5]);
  116. buffer += sprintf_P(buffer, "Sequence Num. : %02X", packet[6]);
  117. }
  118. size_t PacketFormatter::getPacketLength() const {
  119. return packetLength;
  120. }