You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
9.5 KiB

  1. #pragma once
  2. #include <array>
  3. #include "common.h"
  4. #include "esphome/core/component.h"
  5. #include "esphome/core/esphal.h"
  6. #include "esphome/components/i2c/i2c.h"
  7. namespace esphome {
  8. namespace yeelight {
  9. namespace bs2 {
  10. static const uint8_t MSG_LEN = 7;
  11. using MSG = uint8_t[7];
  12. // The commands that are supported by the front panel component.
  13. static const MSG READY_FOR_EV = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  14. static const MSG TURN_ON = { 0x02, 0x03, 0x5E, 0x00, 0x64, 0x00, 0x00 };
  15. static const MSG TURN_OFF = { 0x02, 0x03, 0x0C, 0x00, 0x64, 0x00, 0x00 };
  16. static const MSG SET_LEVEL_1 = { 0x02, 0x03, 0x5E, 0x00, 0x64, 0x00, 0x00 };
  17. static const MSG SET_LEVEL_2 = { 0x02, 0x03, 0x5F, 0x00, 0x64, 0x00, 0x00 };
  18. static const MSG SET_LEVEL_3 = { 0x02, 0x03, 0x5F, 0x80, 0x64, 0x00, 0x00 };
  19. static const MSG SET_LEVEL_4 = { 0x02, 0x03, 0x5F, 0xC0, 0x64, 0x00, 0x00 };
  20. static const MSG SET_LEVEL_5 = { 0x02, 0x03, 0x5F, 0xE0, 0x64, 0x00, 0x00 };
  21. static const MSG SET_LEVEL_6 = { 0x02, 0x03, 0x5F, 0xF0, 0x64, 0x00, 0x00 };
  22. static const MSG SET_LEVEL_7 = { 0x02, 0x03, 0x5F, 0xF8, 0x64, 0x00, 0x00 };
  23. static const MSG SET_LEVEL_8 = { 0x02, 0x03, 0x5F, 0xFC, 0x64, 0x00, 0x00 };
  24. static const MSG SET_LEVEL_9 = { 0x02, 0x03, 0x5F, 0xFE, 0x64, 0x00, 0x00 };
  25. static const MSG SET_LEVEL_10 = { 0x02, 0x03, 0x5F, 0xFF, 0x64, 0x00, 0x00 };
  26. using EVENT = uint16_t;
  27. // Bit flags that are used for specifying an event.
  28. // Events are registered using the following bit pattern
  29. // (bit 1 being the least significant bit):
  30. //
  31. // BITS INDICATE PATTERN RESULT
  32. // 1 status 0 parsing event failed
  33. // 1 parsing event successful
  34. // 2-3 part 00 part unknown
  35. // 01 power button
  36. // 10 color button
  37. // 11 slider
  38. // 4-5 type 00 type unknown
  39. // 01 touch
  40. // 10 release
  41. // 6-10 slider 00000 level known (or part is not "slider")
  42. // level 00001 level 1
  43. // ... up to
  44. // 10101 level 21
  45. //
  46. static const EVENT FLAG_INIT = 0b0000000000;
  47. static const EVENT FLAG_ERR = 0b0000000000;
  48. static const EVENT FLAG_OK = 0b0000000001;
  49. static const EVENT FLAG_PART_SHIFT = 1;
  50. static const EVENT FLAG_PART_MASK = 0b0000000110;
  51. static const EVENT FLAG_PART_UNKNOWN = 0b0000000000;
  52. static const EVENT FLAG_PART_POWER = 0b0000000010;
  53. static const EVENT FLAG_PART_COLOR = 0b0000000100;
  54. static const EVENT FLAG_PART_SLIDER = 0b0000000110;
  55. static const EVENT FLAG_TYPE_SHIFT = 3;
  56. static const EVENT FLAG_TYPE_MASK = 0b0000011000;
  57. static const EVENT FLAG_TYPE_UNKNOWN = 0b0000000000;
  58. static const EVENT FLAG_TYPE_TOUCH = 0b0000001000;
  59. static const EVENT FLAG_TYPE_RELEASE = 0b0000010000;
  60. static const EVENT FLAG_LEVEL_SHIFT = 5;
  61. static const EVENT FLAG_LEVEL_MASK = 0b1111100000;
  62. static const EVENT FLAG_LEVEL_UNKNOWN = 0b0000000000;
  63. /**
  64. * This class implements a parser that translates event byte codes from the
  65. * Yeelight Bedside Lamp 2 into usable events.
  66. */
  67. class FrontPanelEventParser {
  68. public:
  69. /**
  70. * Parse the provided event byte code (7 bytes long).
  71. * Returns a unique integer event code that describes the parsed event.
  72. */
  73. EVENT parse(uint8_t *m) {
  74. EVENT ev = FLAG_INIT;
  75. // All events use the prefix [04:04:01:00].
  76. if (m[0] != 0x04 || m[1] != 0x04 || m[2] != 0x01 || m[3] != 0x00) {
  77. return error_(ev, m, "prefix is not 04:04:01:00");
  78. }
  79. // The next byte determines the part that is touched.
  80. // All remaining bytes specify the event for that part.
  81. switch (m[4]) {
  82. case 0x01: // power button
  83. case 0x02: // color button
  84. ev |= (m[4] == 0x01 ? FLAG_PART_POWER : FLAG_PART_COLOR);
  85. if (m[5] == 0x01 && m[6] == (0x02 + m[4])) {
  86. ev |= FLAG_TYPE_TOUCH;
  87. } else if (m[5] == 0x02 && m[6] == (0x03 + m[4])) {
  88. ev |= FLAG_TYPE_RELEASE;
  89. } else {
  90. return error_(ev, m, "invalid event type for button");
  91. }
  92. break;
  93. case 0x03: // slider touch
  94. case 0x04: // slider release
  95. ev |= FLAG_PART_SLIDER;
  96. ev |= (m[4] == 0x03 ? FLAG_TYPE_TOUCH : FLAG_TYPE_RELEASE);
  97. if ((m[6] - m[5] - m[4] - 0x01) != 0) {
  98. return error_(ev, m, "invalid slider level crc");
  99. } else if (m[5] > 0x16 || m[5] < 0x01) {
  100. return error_(ev, m, "out of bounds slider value");
  101. } else {
  102. auto level = 0x17 - m[5];
  103. ev |= (level << FLAG_LEVEL_SHIFT);
  104. }
  105. break;
  106. default:
  107. return error_(ev, m, "invalid part id");
  108. return ev;
  109. }
  110. // All parsing rules passed. This event is valid.
  111. ESP_LOGD(TAG, "Front panel I2C event parsed: code=%d", ev);
  112. ev |= FLAG_OK;
  113. return ev;
  114. }
  115. protected:
  116. bool has_(EVENT ev, EVENT mask, EVENT flag) {
  117. return (ev & mask) == flag;
  118. }
  119. EVENT error_(EVENT ev, uint8_t *m, const char* msg) {
  120. ESP_LOGE(TAG, "Front panel I2C event error:");
  121. ESP_LOGE(TAG, " Error: %s", msg);
  122. ESP_LOGE(TAG, " Event: [%02x:%02x:%02x:%02x:%02x:%02x:%02x]",
  123. m[0], m[1], m[2], m[3], m[4], m[5], m[6]);
  124. ESP_LOGE(TAG, " Parsed part: %s",
  125. (has_(ev, FLAG_PART_MASK, FLAG_PART_POWER) ? "power button" :
  126. has_(ev, FLAG_PART_MASK, FLAG_PART_COLOR) ? "color button" :
  127. has_(ev, FLAG_PART_MASK, FLAG_PART_SLIDER) ? "slider" : "n/a"));
  128. ESP_LOGE(TAG, " Parsed event type: %s",
  129. (has_(ev, FLAG_TYPE_MASK, FLAG_TYPE_TOUCH) ? "touch" :
  130. has_(ev, FLAG_TYPE_MASK, FLAG_TYPE_RELEASE) ? "release" : "n/a"));
  131. if (has_(ev, FLAG_PART_MASK, FLAG_PART_SLIDER)) {
  132. auto level = (ev & FLAG_LEVEL_MASK) >> FLAG_LEVEL_SHIFT;
  133. if (level > 0) {
  134. ESP_LOGE(TAG, " Parsed slider level: %d", level);
  135. }
  136. }
  137. return ev;
  138. }
  139. };
  140. /**
  141. * This is a hardware abstraction layer that communicates with with front
  142. * panel of the Yeelight Bedside Lamp 2.
  143. *
  144. * It serves as a hub component for other components that implement
  145. * the actual buttons and slider components.
  146. */
  147. class FrontPanelHAL : public Component, public i2c::I2CDevice {
  148. public:
  149. FrontPanelEventParser event;
  150. /**
  151. * Set the GPIO pin that is used by the front panel to notify the ESP
  152. * that a touch/release event can be read using I2C.
  153. */
  154. void set_trigger_pin(GPIOPin *pin) { trigger_pin_ = pin; }
  155. void add_on_event_callback(std::function<void(EVENT)> &&callback) {
  156. event_callback_.add(std::move(callback));
  157. }
  158. void setup() {
  159. ESP_LOGCONFIG(TAG, "Setting up I2C trigger pin interrupt...");
  160. trigger_pin_->setup();
  161. trigger_pin_->attach_interrupt(
  162. FrontPanelHAL::isr, this, FALLING);
  163. }
  164. void dump_config() {
  165. ESP_LOGCONFIG(TAG, "I2C interrupt");
  166. LOG_PIN(" Interrupt pin: ", trigger_pin_);
  167. }
  168. void loop() {
  169. // Read and publish front panel events.
  170. auto current_event_id = event_id_;
  171. if (current_event_id != last_event_id_) {
  172. last_event_id_ = current_event_id;
  173. MSG message;
  174. if (write_bytes_raw(READY_FOR_EV, MSG_LEN) &&
  175. read_bytes_raw(message, MSG_LEN)) {
  176. auto ev = event.parse(message);
  177. if (ev & FLAG_OK) {
  178. event_callback_.call(ev);
  179. }
  180. }
  181. }
  182. }
  183. /**
  184. * Sets the front panel light to the provided level (0.0 - 1.0).
  185. *
  186. * Level 0.0 means: turn off the front panel light.
  187. * The other levels are translate to one of the avialable levels,
  188. * represented by the backlight of the slider bar.
  189. */
  190. void set_light_level(float level) {
  191. if (level == 0.0f)
  192. write_bytes_raw(TURN_OFF, MSG_LEN);
  193. else if (level < 0.15)
  194. write_bytes_raw(SET_LEVEL_1, MSG_LEN);
  195. else if (level < 0.25)
  196. write_bytes_raw(SET_LEVEL_2, MSG_LEN);
  197. else if (level < 0.35)
  198. write_bytes_raw(SET_LEVEL_3, MSG_LEN);
  199. else if (level < 0.45)
  200. write_bytes_raw(SET_LEVEL_4, MSG_LEN);
  201. else if (level < 0.55)
  202. write_bytes_raw(SET_LEVEL_5, MSG_LEN);
  203. else if (level < 0.65)
  204. write_bytes_raw(SET_LEVEL_6, MSG_LEN);
  205. else if (level < 0.75)
  206. write_bytes_raw(SET_LEVEL_7, MSG_LEN);
  207. else if (level < 0.85)
  208. write_bytes_raw(SET_LEVEL_8, MSG_LEN);
  209. else if (level < 0.95)
  210. write_bytes_raw(SET_LEVEL_9, MSG_LEN);
  211. else
  212. write_bytes_raw(SET_LEVEL_10, MSG_LEN);
  213. }
  214. protected:
  215. GPIOPin *trigger_pin_;
  216. static void isr(FrontPanelHAL *store);
  217. volatile int event_id_ = 0;
  218. int last_event_id_ = 0;
  219. CallbackManager<void(EVENT)> event_callback_{};
  220. };
  221. /**
  222. * This ISR is used to handle IRQ triggers from the front panel.
  223. *
  224. * The front panel pulls the trigger pin low for a short period of time
  225. * when a new event is available. All we do here to handle the interrupt,
  226. * is increment a simple event id counter. The main loop of the component
  227. * will take care of actually reading and processing the event.
  228. */
  229. void ICACHE_RAM_ATTR HOT FrontPanelHAL::isr(FrontPanelHAL *store) {
  230. store->event_id_++;
  231. }
  232. } // namespace bs2
  233. } // namespace yeelight
  234. } // namespace esphome