PacketFormatter.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <PacketFormatter.h>
  2. PacketStream::PacketStream()
  3. : packetStream(NULL),
  4. numPackets(0),
  5. packetLength(0),
  6. currentPacket(0)
  7. { }
  8. bool PacketStream::hasNext() {
  9. return currentPacket < numPackets;
  10. }
  11. uint8_t* PacketStream::next() {
  12. uint8_t* packet = packetStream + (currentPacket * packetLength);
  13. currentPacket++;
  14. return packet;
  15. }
  16. PacketFormatter::PacketFormatter(const size_t packetLength, const size_t maxPackets)
  17. : packetLength(packetLength),
  18. packetBuffer(new uint8_t[packetLength * maxPackets]),
  19. numPackets(0),
  20. currentPacket(NULL)
  21. {
  22. packetStream.packetLength = packetLength;
  23. packetStream.packetStream = packetBuffer;
  24. }
  25. void PacketFormatter::finalizePacket(uint8_t* packet) { }
  26. void PacketFormatter::updateStatus(MiLightStatus status) {
  27. updateStatus(status, groupId);
  28. }
  29. void PacketFormatter::updateStatus(MiLightStatus status, uint8_t groupId) { }
  30. void PacketFormatter::updateBrightness(uint8_t value) { }
  31. void PacketFormatter::updateMode(uint8_t value) { }
  32. void PacketFormatter::modeSpeedDown() { }
  33. void PacketFormatter::modeSpeedUp() { }
  34. void PacketFormatter::nextMode() { }
  35. void PacketFormatter::previousMode() { }
  36. void PacketFormatter::command(uint8_t command, uint8_t arg) { }
  37. void PacketFormatter::updateHue(uint16_t value) { }
  38. void PacketFormatter::updateColorRaw(uint8_t value) { }
  39. void PacketFormatter::updateColorWhite() { }
  40. void PacketFormatter::increaseTemperature() { }
  41. void PacketFormatter::decreaseTemperature() { }
  42. void PacketFormatter::increaseBrightness() { }
  43. void PacketFormatter::decreaseBrightness() { }
  44. void PacketFormatter::updateTemperature(uint8_t value) { }
  45. void PacketFormatter::updateSaturation(uint8_t value) { }
  46. void PacketFormatter::pair() {
  47. for (size_t i = 0; i < 5; i++) {
  48. updateStatus(ON);
  49. }
  50. }
  51. void PacketFormatter::unpair() {
  52. pair();
  53. }
  54. PacketStream& PacketFormatter::buildPackets() {
  55. if (numPackets > 0) {
  56. finalizePacket(currentPacket);
  57. }
  58. packetStream.numPackets = numPackets;
  59. packetStream.currentPacket = 0;
  60. return packetStream;
  61. }
  62. void PacketFormatter::valueByStepFunction(StepFunction increase, StepFunction decrease, uint8_t numSteps, uint8_t value) {
  63. for (size_t i = 0; i < numSteps; i++) {
  64. (this->*decrease)();
  65. }
  66. for (size_t i = 0; i < value; i++) {
  67. (this->*increase)();
  68. }
  69. }
  70. void PacketFormatter::prepare(uint16_t deviceId, uint8_t groupId) {
  71. this->deviceId = deviceId;
  72. this->groupId = groupId;
  73. reset();
  74. }
  75. void PacketFormatter::reset() {
  76. this->numPackets = 0;
  77. this->currentPacket = currentPacket;
  78. }
  79. void PacketFormatter::pushPacket() {
  80. if (numPackets > 0) {
  81. finalizePacket(currentPacket);
  82. }
  83. currentPacket = packetBuffer + (numPackets * packetLength);
  84. numPackets++;
  85. initializePacket(currentPacket);
  86. }
  87. void PacketFormatter::format(uint8_t const* packet, char* buffer) {
  88. for (int i = 0; i < packetLength; i++) {
  89. sprintf(buffer, "%02X ", packet[i]);
  90. buffer += 3;
  91. }
  92. sprintf(buffer, "\n\n");
  93. }
  94. void PacketFormatter::formatV1Packet(uint8_t const* packet, char* buffer) {
  95. String format = String("Request type : %02X\n")
  96. + "Device ID : %02X%02X\n"
  97. + "b1 : %02X\n"
  98. + "b2 : %02X\n"
  99. + "b3 : %02X\n"
  100. + "Sequence Num. : %02X";
  101. sprintf(
  102. buffer,
  103. format.c_str(),
  104. packet[0],
  105. packet[1], packet[2],
  106. packet[3],
  107. packet[4],
  108. packet[5],
  109. packet[6]
  110. );
  111. }
  112. size_t PacketFormatter::getPacketLength() const {
  113. return packetLength;
  114. }