|
| 1 | +#include "cardkb.h" |
| 2 | +#include "esphome/core/hal.h" |
| 3 | +#include "esphome/core/log.h" |
| 4 | + |
| 5 | +namespace esphome { |
| 6 | +namespace cardkb { |
| 7 | + |
| 8 | +static const char *TAG = "cardkb"; |
| 9 | + |
| 10 | +void CardKB::loop() { |
| 11 | + uint8_t c; |
| 12 | + static uint32_t last_error = 0; |
| 13 | + c = this->pressed_key_; |
| 14 | + if (c) { |
| 15 | + for (auto &listener : this->listeners_) |
| 16 | + listener->key_released(c); |
| 17 | + this->pressed_key_ = 0; |
| 18 | + } |
| 19 | + i2c::ErrorCode res = this->read(&c, 1); |
| 20 | + if (res != i2c::NO_ERROR) { |
| 21 | + if (millis() - last_error >= 1000) { |
| 22 | + ESP_LOGD(TAG, "i2c error %d", res); |
| 23 | + last_error = millis(); |
| 24 | + } |
| 25 | + return; |
| 26 | + } |
| 27 | + if (c == 0) { |
| 28 | + if (millis() - last_error >= 1000) { |
| 29 | + ESP_LOGD(TAG, "received null"); |
| 30 | + last_error = millis(); |
| 31 | + } |
| 32 | + return; |
| 33 | + } |
| 34 | + if (c < 32) |
| 35 | + ESP_LOGD(TAG, "keycode '%d' pressed", c); |
| 36 | + else |
| 37 | + ESP_LOGD(TAG, "key '%c' pressed", c); |
| 38 | + for (auto &listener : this->listeners_) |
| 39 | + listener->key_pressed(c); |
| 40 | + this->pressed_key_ = c; |
| 41 | + this->send_key_(c); |
| 42 | +} |
| 43 | + |
| 44 | +void CardKB::dump_config() { |
| 45 | + ESP_LOGCONFIG(TAG, "CardKB:"); |
| 46 | +} |
| 47 | + |
| 48 | +void CardKB::register_listener(CardKBListener *listener) { |
| 49 | + this->listeners_.push_back(listener); |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace cardkb |
| 53 | +} // namespace esphome |
| 54 | + |
0 commit comments