Skip to content

Commit

Permalink
Work around issue with ESPHome 2023.7.0. Fixed in 2023.7.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudd-O committed Aug 2, 2023
1 parent 0afb63e commit 8040eec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
40 changes: 36 additions & 4 deletions components/ble_keyboard/ble_keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,37 @@ namespace ble_keyboard {
static const char *const TAG = "ble_keyboard";

void Esp32BleKeyboard::setup() {
ESP_LOGI(TAG, "Setting up...");
if (this->setup_) {
ESP_LOGW(TAG, "BLE keyboard already setup.");
return;
}

ESP_LOGCONFIG(TAG, "Setting up BLE keyboard...");

bleKeyboard.begin();

pServer = BLEDevice::getServer();

ESP_LOGD(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false");
ESP_LOGCONFIG(TAG, "advertiseOnDisconnect(%s)", this->reconnect_ ? "true" : "false");
pServer->advertiseOnDisconnect(this->reconnect_);

if (!this->advertise_on_start_) {
ESP_LOGD(TAG, "stopAdvertising()");
ESP_LOGCONFIG(TAG, "stopAdvertising() because advertise_on_start is false");
pServer->stopAdvertising();
}

bleKeyboard.releaseAll();
this->setup_ = true;
ESP_LOGCONFIG(TAG, "Finished setting up up BLE keyboard.");
}

void Esp32BleKeyboard::stop() {
ESP_LOGD("ble", "stop()");
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}

ESP_LOGD(TAG, "stop()");
if (this->reconnect_) {
ESP_LOGD(TAG, "advertiseOnDisconnect(false)");
pServer->advertiseOnDisconnect(false);
Expand All @@ -51,6 +63,10 @@ void Esp32BleKeyboard::stop() {
}

void Esp32BleKeyboard::start() {
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}
ESP_LOGD(TAG, "start()");
if (this->reconnect_) {
ESP_LOGD(TAG, "advertiseOnDisconnect(true)");
Expand Down Expand Up @@ -78,6 +94,10 @@ void Esp32BleKeyboard::update_timer() {
}

void Esp32BleKeyboard::press(std::string message) {
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}
if (this->is_connected()) {
if (message.length() >= 5) {
for (unsigned i = 0; i < message.length(); i += 5) {
Expand All @@ -94,6 +114,10 @@ void Esp32BleKeyboard::press(std::string message) {
}

void Esp32BleKeyboard::press(uint8_t key, bool with_timer) {
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}
if (this->is_connected()) {
if (with_timer) {
this->update_timer();
Expand All @@ -104,6 +128,10 @@ void Esp32BleKeyboard::press(uint8_t key, bool with_timer) {
}

void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) {
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}
if (this->is_connected()) {
if (with_timer) {
this->update_timer();
Expand All @@ -113,6 +141,10 @@ void Esp32BleKeyboard::press(MediaKeyReport key, bool with_timer) {
}

void Esp32BleKeyboard::release() {
if (!this->setup_) {
ESP_LOGW(TAG, "Attempting to use without setup. Not doing anything.");
return;
}
if (this->is_connected()) {
this->cancel_timeout((const std::string) TAG);
bleKeyboard.releaseAll();
Expand Down
4 changes: 3 additions & 1 deletion components/ble_keyboard/ble_keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace esphome {
namespace ble_keyboard {
class Esp32BleKeyboard : public PollingComponent {
public:
Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect, bool advertise_on_start)
Esp32BleKeyboard(std::string name, std::string manufacturer_id, uint8_t battery_level, bool reconnect,
bool advertise_on_start)
: PollingComponent(1000), bleKeyboard(name, manufacturer_id, battery_level) {
reconnect_ = reconnect;
advertise_on_start_ = advertise_on_start;
Expand Down Expand Up @@ -47,6 +48,7 @@ class Esp32BleKeyboard : public PollingComponent {
BLEServer *pServer;
BleKeyboard bleKeyboard;

bool setup_{false};
bool reconnect_{true};
bool advertise_on_start_{true};
uint32_t default_delay_{100};
Expand Down

0 comments on commit 8040eec

Please sign in to comment.