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.

326 lines
10 KiB

  1. #pragma once
  2. #include "common.h"
  3. #include "esphome/components/i2c/i2c.h"
  4. #include "esphome/core/component.h"
  5. #include "esphome/core/esphal.h"
  6. #include <array>
  7. namespace esphome {
  8. namespace xiaomi {
  9. namespace bslamp2 {
  10. static const uint8_t MSG_LEN = 7;
  11. using MSG = uint8_t[MSG_LEN];
  12. using LED = uint16_t;
  13. using EVENT = uint16_t;
  14. // clang-format off
  15. enum FrontPanelLeds {
  16. LED_NONE = 0,
  17. LED_POWER = 1 << 14,
  18. LED_COLOR = 1 << 12,
  19. LED_1 = 1 << 9,
  20. LED_2 = 1 << 8,
  21. LED_3 = 1 << 7,
  22. LED_4 = 1 << 6,
  23. LED_5 = 1 << 5,
  24. LED_6 = 1 << 4,
  25. LED_7 = 1 << 3,
  26. LED_8 = 1 << 2,
  27. LED_9 = 1 << 1,
  28. LED_10 = 1,
  29. };
  30. // Combinations of LEDs that are use by the original firmware to
  31. // indicate the current brightness setting of the lamp..
  32. static const LED LED_LEVEL_0 = LED_NONE;
  33. static const LED LED_LEVEL_1 = LED_POWER | LED_COLOR | LED_1;
  34. static const LED LED_LEVEL_2 = LED_LEVEL_1 | LED_2;
  35. static const LED LED_LEVEL_3 = LED_LEVEL_2 | LED_3;
  36. static const LED LED_LEVEL_4 = LED_LEVEL_3 | LED_4;
  37. static const LED LED_LEVEL_5 = LED_LEVEL_4 | LED_5;
  38. static const LED LED_LEVEL_6 = LED_LEVEL_5 | LED_6;
  39. static const LED LED_LEVEL_7 = LED_LEVEL_6 | LED_7;
  40. static const LED LED_LEVEL_8 = LED_LEVEL_7 | LED_8;
  41. static const LED LED_LEVEL_9 = LED_LEVEL_8 | LED_9;
  42. static const LED LED_LEVEL_10 = LED_LEVEL_9 | LED_10;
  43. // This I2C command is used during front panel event handling.
  44. static const MSG READY_FOR_EV = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  45. // Bit flags that are used for specifying an event.
  46. // Events are registered using the following bit pattern
  47. // (bit 1 being the least significant bit):
  48. //
  49. // BITS INDICATE PATTERN RESULT
  50. // 1 status 0 parsing event failed
  51. // 1 parsing event successful
  52. // 2-4 part 000 part unknown
  53. // 001 power button
  54. // 010 color button
  55. // 100 slider
  56. // 5-6 type 00 type unknown
  57. // 01 touch
  58. // 10 release
  59. // 7-11 slider 00000 level known (or part is not "slider")
  60. // level 00001 level 1
  61. // ... up to
  62. // 10101 level 21
  63. //
  64. static const EVENT FLAG_INIT = 0b00000000000;
  65. static const EVENT FLAG_ERR = 0b00000000000;
  66. static const EVENT FLAG_OK = 0b00000000001;
  67. static const EVENT FLAG_PART_SHIFT = 1;
  68. static const EVENT FLAG_PART_MASK = 0b00000001110;
  69. static const EVENT FLAG_PART_UNKNOWN = 0b00000000000;
  70. static const EVENT FLAG_PART_POWER = 0b00000000010;
  71. static const EVENT FLAG_PART_COLOR = 0b00000000100;
  72. static const EVENT FLAG_PART_SLIDER = 0b00000001000;
  73. static const EVENT FLAG_TYPE_SHIFT = 4;
  74. static const EVENT FLAG_TYPE_MASK = 0b00000110000;
  75. static const EVENT FLAG_TYPE_UNKNOWN = 0b00000000000;
  76. static const EVENT FLAG_TYPE_TOUCH = 0b00000010000;
  77. static const EVENT FLAG_TYPE_RELEASE = 0b00000100000;
  78. static const EVENT FLAG_LEVEL_SHIFT = 6;
  79. static const EVENT FLAG_LEVEL_MASK = 0b11111000000;
  80. static const EVENT FLAG_LEVEL_UNKNOWN = 0b00000000000;
  81. // clang-format on
  82. /**
  83. * This class implements a parser that translates event byte codes from the
  84. * Xiaomi Mijia Bedside Lamp 2 into usable events.
  85. */
  86. class FrontPanelEventParser {
  87. public:
  88. /**
  89. * Parse the provided event byte code (7 bytes long).
  90. * Returns a unique integer event code that describes the parsed event.
  91. */
  92. EVENT parse(uint8_t *m) {
  93. EVENT ev = FLAG_INIT;
  94. // All events use the prefix [04:04:01:00].
  95. if (m[0] != 0x04 || m[1] != 0x04 || m[2] != 0x01 || m[3] != 0x00) {
  96. return error_(ev, m, "prefix is not 04:04:01:00");
  97. }
  98. // The next byte determines the part that is touched.
  99. // All remaining bytes specify the event for that part.
  100. switch (m[4]) {
  101. case 0x01: // power button
  102. case 0x02: // color button
  103. ev |= (m[4] == 0x01 ? FLAG_PART_POWER : FLAG_PART_COLOR);
  104. if (m[5] == 0x01 && m[6] == (0x02 + m[4]))
  105. ev |= FLAG_TYPE_TOUCH;
  106. else if (m[5] == 0x02 && m[6] == (0x03 + m[4]))
  107. ev |= FLAG_TYPE_RELEASE;
  108. else
  109. return error_(ev, m, "invalid event type for button");
  110. break;
  111. case 0x03: // slider touch
  112. case 0x04: // slider release
  113. ev |= FLAG_PART_SLIDER;
  114. ev |= (m[4] == 0x03 ? FLAG_TYPE_TOUCH : FLAG_TYPE_RELEASE);
  115. if ((m[6] - m[5] - m[4] - 0x01) != 0)
  116. return error_(ev, m, "invalid slider level crc");
  117. else if (m[5] > 0x16 || m[5] < 0x01)
  118. return error_(ev, m, "out of bounds slider value");
  119. else {
  120. auto level = 0x17 - m[5];
  121. ev |= (level << FLAG_LEVEL_SHIFT);
  122. }
  123. break;
  124. default:
  125. return error_(ev, m, "invalid part id");
  126. return ev;
  127. }
  128. // All parsing rules passed. This event is valid.
  129. ESP_LOGD(TAG, "Front panel I2C event parsed: code=%d", ev);
  130. ev |= FLAG_OK;
  131. return ev;
  132. }
  133. protected:
  134. bool has_(EVENT ev, EVENT mask, EVENT flag) { return (ev & mask) == flag; }
  135. EVENT error_(EVENT ev, uint8_t *m, const char *msg) {
  136. ESP_LOGE(TAG, "Front panel I2C event error:");
  137. ESP_LOGE(TAG, " Error: %s", msg);
  138. ESP_LOGE(TAG, " Event: [%02x:%02x:%02x:%02x:%02x:%02x:%02x]", m[0], m[1], m[2], m[3], m[4], m[5], m[6]);
  139. ESP_LOGE(TAG, " Parsed part: %s", format_part(ev));
  140. ESP_LOGE(TAG, " Parsed event type: %s", format_event_type(ev));
  141. if (has_(ev, FLAG_PART_MASK, FLAG_PART_SLIDER)) {
  142. auto level = (ev & FLAG_LEVEL_MASK) >> FLAG_LEVEL_SHIFT;
  143. if (level > 0) {
  144. ESP_LOGE(TAG, " Parsed slider level: %d", level);
  145. }
  146. }
  147. return ev;
  148. }
  149. const char *format_part(EVENT ev) {
  150. if (has_(ev, FLAG_PART_MASK, FLAG_PART_POWER))
  151. return "power button";
  152. if (has_(ev, FLAG_PART_MASK, FLAG_PART_COLOR))
  153. return "color button";
  154. if (has_(ev, FLAG_PART_MASK, FLAG_PART_SLIDER))
  155. return "slider";
  156. return "n/a";
  157. }
  158. const char *format_event_type(EVENT ev) {
  159. if (has_(ev, FLAG_TYPE_MASK, FLAG_TYPE_TOUCH))
  160. return "touch";
  161. if (has_(ev, FLAG_TYPE_MASK, FLAG_TYPE_RELEASE))
  162. return "release";
  163. return "n/a";
  164. }
  165. };
  166. /**
  167. * This is a hardware abstraction layer that communicates with with front
  168. * panel of the Xiaomi Mijia Bedside Lamp 2.
  169. *
  170. * It serves as a hub component for other components that implement
  171. * the actual buttons and slider components.
  172. */
  173. class FrontPanelHAL : public Component, public i2c::I2CDevice {
  174. public:
  175. FrontPanelEventParser event;
  176. /**
  177. * Set the GPIO pin that is used by the front panel to notify the ESP
  178. * that a touch/release event can be read using I2C.
  179. */
  180. void set_trigger_pin(GPIOPin *pin) { trigger_pin_ = pin; }
  181. void add_on_event_callback(std::function<void(EVENT)> &&callback) { event_callback_.add(std::move(callback)); }
  182. void setup() {
  183. ESP_LOGCONFIG(TAG, "Setting up I2C trigger pin interrupt...");
  184. trigger_pin_->setup();
  185. trigger_pin_->attach_interrupt(FrontPanelHAL::isr, this, FALLING);
  186. }
  187. void dump_config() {
  188. ESP_LOGCONFIG(TAG, "FrontPanelHAL:");
  189. LOG_PIN(" I2C interrupt pin: ", trigger_pin_);
  190. }
  191. void loop() {
  192. // Read and publish front panel events.
  193. auto current_event_id = event_id_;
  194. if (current_event_id != last_event_id_) {
  195. last_event_id_ = current_event_id;
  196. MSG message;
  197. if (write_bytes_raw(READY_FOR_EV, MSG_LEN) && read_bytes_raw(message, MSG_LEN)) {
  198. auto ev = event.parse(message);
  199. if (ev & FLAG_OK) {
  200. event_callback_.call(ev);
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * Turn on one or more LEDs (leaving the state of the other LEDs intact).
  207. * The input value is a bitwise OR-ed set of LED constants.
  208. */
  209. void turn_on_leds(uint16_t leds) {
  210. set_leds_(led_state_ | leds);
  211. }
  212. /**
  213. * Turn off one or more LEDs (leaving the state of the other LEDs intact).
  214. * The input value is a bitwise OR-ed set of LED constants.
  215. */
  216. void turn_off_leds(uint16_t leds) {
  217. set_leds_(led_state_ & ~leds);
  218. }
  219. /**
  220. * Updates the state of the LEDs according to the provided input.
  221. * The input value is a bitwise OR-ed set of LED constants, representing the
  222. * LEDs that must be turned on. All other LEDs are turned off.
  223. */
  224. void set_leds(uint16_t leds) {
  225. set_leds_(leds);
  226. }
  227. /**
  228. * Sets the front panel illumination to the provided level (0.0 - 1.0).
  229. *
  230. * This implements the behavior of the original firmware for representing
  231. * the lamp's brightness.
  232. *
  233. * Level 0.0 means: turn off the front panel illumination.
  234. * The other levels are translated to one of the available levels,
  235. * represented by the level indicator (i.e. the illumination of the
  236. * slider bar.)
  237. */
  238. void set_light_level(float level) {
  239. if (level == 0.0f)
  240. set_leds(LED_LEVEL_0);
  241. else if (level < 0.15)
  242. set_leds(LED_LEVEL_1);
  243. else if (level < 0.25)
  244. set_leds(LED_LEVEL_2);
  245. else if (level < 0.35)
  246. set_leds(LED_LEVEL_3);
  247. else if (level < 0.45)
  248. set_leds(LED_LEVEL_4);
  249. else if (level < 0.55)
  250. set_leds(LED_LEVEL_5);
  251. else if (level < 0.65)
  252. set_leds(LED_LEVEL_6);
  253. else if (level < 0.75)
  254. set_leds(LED_LEVEL_7);
  255. else if (level < 0.85)
  256. set_leds(LED_LEVEL_8);
  257. else if (level < 0.95)
  258. set_leds(LED_LEVEL_9);
  259. else
  260. set_leds(LED_LEVEL_10);
  261. }
  262. protected:
  263. GPIOPin *trigger_pin_;
  264. static void isr(FrontPanelHAL *store);
  265. volatile int event_id_ = 0;
  266. int last_event_id_ = 0;
  267. CallbackManager<void(EVENT)> event_callback_{};
  268. MSG led_msg_ = {0x02, 0x03, 0x00, 0x00, 0x64, 0x00, 0x00};
  269. uint16_t led_state_ = 0;
  270. void set_leds_(uint16_t leds) {
  271. led_state_ = 0b0000110000000000 | leds;
  272. led_msg_[2] = led_state_ >> 8;
  273. led_msg_[3] = led_state_ & 0xff;
  274. write_bytes_raw(led_msg_, MSG_LEN);
  275. }
  276. };
  277. /**
  278. * This ISR is used to handle IRQ triggers from the front panel.
  279. *
  280. * The front panel pulls the trigger pin low for a short period of time
  281. * when a new event is available. All we do here to handle the interrupt,
  282. * is increment a simple event id counter. The main loop of the component
  283. * will take care of actually reading and processing the event.
  284. */
  285. void ICACHE_RAM_ATTR HOT FrontPanelHAL::isr(FrontPanelHAL *store) { store->event_id_++; }
  286. } // namespace bslamp2
  287. } // namespace xiaomi
  288. } // namespace esphome