PacketFormatter.cpp 5.2 KB

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