PacketFormatter.cpp 3.7 KB

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