diff --git a/Platformio/src/commandHandler.cpp b/Platformio/src/commandHandler.cpp index 148364f..e65ae6c 100644 --- a/Platformio/src/commandHandler.cpp +++ b/Platformio/src/commandHandler.cpp @@ -75,7 +75,7 @@ void executeCommandWithData(std::string command, commandData commandData, std::s break; } - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT case MQTT: { auto current = commandData.commandPayloads.begin(); std::string topic = *current; diff --git a/Platformio/src/commandHandler.h b/Platformio/src/commandHandler.h index e8aae39..e07ea44 100644 --- a/Platformio/src/commandHandler.h +++ b/Platformio/src/commandHandler.h @@ -1,6 +1,9 @@ #ifndef __COMMANDHANDLER_H__ #define __COMMANDHANDLER_H__ +#define ENABLE_WIFI_AND_MQTT // Comment out to disable WiFi +#define ENABLE_BLUETOOTH // Comment out to disable Bluetooth + #include #include #include @@ -47,7 +50,7 @@ enum commandHandlers { IR_GC, IR_NEC, IR_SAMSUNG, - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT MQTT, #endif #ifdef ENABLE_KEYBOARD_BLE diff --git a/Platformio/src/device_keyboard_ble.cpp b/Platformio/src/device_keyboard_ble.cpp index 9623d15..ee66dae 100644 --- a/Platformio/src/device_keyboard_ble.cpp +++ b/Platformio/src/device_keyboard_ble.cpp @@ -1,6 +1,7 @@ #include #include "commandHandler.h" #include "device_keyboard_ble.h" +#include "gui.h" #ifdef ENABLE_KEYBOARD_BLE @@ -30,6 +31,26 @@ void init_keyboard_ble_commands() { bleKeyboard.begin(); } +// if bluetooth is in pairing mode (pairing mode is always on, if not connected), but not connected, then blink +unsigned long blinkBluetoothLabelLastChange = millis(); +bool blinkBluetoothLabelIsOn = false; + +void update_keyboard_ble_status() { + if (bleKeyboard.isConnected()) { + lv_label_set_text(BluetoothLabel, LV_SYMBOL_BLUETOOTH); + } else { + if(millis() - blinkBluetoothLabelLastChange >= 1000){ + blinkBluetoothLabelIsOn = !blinkBluetoothLabelIsOn; + if (blinkBluetoothLabelIsOn) { + lv_label_set_text(BluetoothLabel, LV_SYMBOL_BLUETOOTH); + } else { + lv_label_set_text(BluetoothLabel, ""); + } + blinkBluetoothLabelLastChange = millis(); + } + } +} + void keyboard_ble_write(uint8_t c) { bleKeyboard.write(c); } diff --git a/Platformio/src/device_keyboard_ble.h b/Platformio/src/device_keyboard_ble.h index 1411c76..4e0fe45 100644 --- a/Platformio/src/device_keyboard_ble.h +++ b/Platformio/src/device_keyboard_ble.h @@ -1,9 +1,19 @@ #ifndef __DEVICE_KEYBOARD_BLE_H__ #define __DEVICE_KEYBOARD_BLE_H__ +// Advertising is started automatically. +// As soon as a device is connected, a small indicator in the top left corner of the screen will appear + #define ENABLE_KEYBOARD_BLE // Comment out to disable BLE + #ifdef ENABLE_KEYBOARD_BLE +#include "commandHandler.h" + +#if defined(ENABLE_KEYBOARD_BLE) && !defined(ENABLE_BLUETOOTH) +static_assert(false, "You have to use \"#define ENABLE_BLUETOOTH\" in \"commandHandler.h\" when having \"#define ENABLE_KEYBOARD_BLE\""); +#endif + #include #define KEYBOARD_BLE_UP "Keyboard_ble_up" @@ -30,6 +40,7 @@ extern BleKeyboard bleKeyboard; void init_keyboard_ble_commands(); void keyboard_ble_executeCommand(std::string command, std::string additionalPayload = ""); +void update_keyboard_ble_status(); #endif diff --git a/Platformio/src/device_keyboard_mqtt.h b/Platformio/src/device_keyboard_mqtt.h index a53dd7b..8926bb5 100644 --- a/Platformio/src/device_keyboard_mqtt.h +++ b/Platformio/src/device_keyboard_mqtt.h @@ -5,9 +5,17 @@ // https://github.com/KlausMu/esp32-mqtt-keyboard #define ENABLE_KEYBOARD_MQTT // Comment out to disable WiFi and MQTT -// if you activate the MQTT keyboard, consider changing the mapping of the keyboard commands to the MQTT keyboard in file "commandHandler.h" + #ifdef ENABLE_KEYBOARD_MQTT +// if you activate the MQTT keyboard, consider changing the mapping of the keyboard commands to the MQTT keyboard in file "commandHandler.h" + +#include "commandHandler.h" + +#if defined(ENABLE_KEYBOARD_MQTT) && !defined(ENABLE_WIFI_AND_MQTT) +static_assert(false, "You have to use \"#define ENABLE_WIFI_AND_MQTT\" in \"commandHandler.h\" when having \"#define ENABLE_KEYBOARD_MQTT\""); +#endif + #define KEYBOARD_MQTT_UP "Keyboard_mqtt_up" #define KEYBOARD_MQTT_DOWN "Keyboard_mqtt_down" #define KEYBOARD_MQTT_RIGHT "Keyboard_mqtt_right" diff --git a/Platformio/src/gui.cpp b/Platformio/src/gui.cpp index 62f8f90..8d4590a 100644 --- a/Platformio/src/gui.cpp +++ b/Platformio/src/gui.cpp @@ -12,6 +12,7 @@ lv_obj_t* panel; lv_color_t color_primary = lv_color_hex(0x303030); // gray lv_obj_t* WifiLabel; +lv_obj_t* BluetoothLabel; lv_obj_t* objBattPercentage; lv_obj_t* objBattIcon; byte currentScreen = 1; // Current Device to control (allows switching mappings between devices) @@ -198,6 +199,12 @@ void init_gui(void) { lv_obj_align(WifiLabel, LV_ALIGN_LEFT_MID, -8, 0); lv_obj_set_style_text_font(WifiLabel, &lv_font_montserrat_12, LV_PART_MAIN); + // Bluetooth -------------------------------------------------------------------- + BluetoothLabel = lv_label_create(statusbar); + lv_label_set_text(BluetoothLabel, ""); + lv_obj_align(BluetoothLabel, LV_ALIGN_LEFT_MID, 12, 0); + lv_obj_set_style_text_font(BluetoothLabel, &lv_font_montserrat_12, LV_PART_MAIN); + // Battery ---------------------------------------------------------------------- objBattPercentage = lv_label_create(statusbar); lv_label_set_text(objBattPercentage, ""); diff --git a/Platformio/src/gui.h b/Platformio/src/gui.h index d7f3065..9c82340 100644 --- a/Platformio/src/gui.h +++ b/Platformio/src/gui.h @@ -13,6 +13,7 @@ extern lv_obj_t* panel; extern lv_color_t color_primary; extern lv_obj_t* WifiLabel; +extern lv_obj_t* BluetoothLabel; extern byte currentScreen; // Current screen that is shown diff --git a/Platformio/src/gui_numpad.cpp b/Platformio/src/gui_numpad.cpp index d341388..2dc4d1e 100644 --- a/Platformio/src/gui_numpad.cpp +++ b/Platformio/src/gui_numpad.cpp @@ -2,7 +2,6 @@ // #include "assets.c" #include "tft.h" #include "gui.h" -#include "device_keyboard_mqtt.h" #include "commandHandler.h" // Virtual Keypad Event handler diff --git a/Platformio/src/gui_smarthome.cpp b/Platformio/src/gui_smarthome.cpp index 97390b9..051d149 100644 --- a/Platformio/src/gui_smarthome.cpp +++ b/Platformio/src/gui_smarthome.cpp @@ -3,7 +3,7 @@ #include "tft.h" #include "gui.h" #include "mqtt.h" -#include "device_keyboard_mqtt.h" +#include "commandHandler.h" // LVGL declarations LV_IMG_DECLARE(lightbulb); @@ -14,7 +14,7 @@ static void smartHomeToggle_event_cb(lv_event_t * e){ if(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)) strcpy(payload,"true"); else strcpy(payload,"false"); // Publish an MQTT message based on the event user data - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT if((int)e->user_data == 1) publishMQTTMessage("bulb1_set", payload); if((int)e->user_data == 2) publishMQTTMessage("bulb2_set", payload); #endif @@ -26,7 +26,7 @@ static void smartHomeSlider_event_cb(lv_event_t * e){ char payload[8]; dtostrf(lv_slider_get_value(slider), 1, 2, payload); // Publish an MQTT message based on the event user data - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT if((int)e->user_data == 1) publishMQTTMessage("bulb1_setbrightness", payload); if((int)e->user_data == 2) publishMQTTMessage("bulb2_setbrightness", payload); #endif diff --git a/Platformio/src/keys.cpp b/Platformio/src/keys.cpp index ef7be01..837114b 100644 --- a/Platformio/src/keys.cpp +++ b/Platformio/src/keys.cpp @@ -6,7 +6,6 @@ #include "mqtt.h" #include "device_samsung.h" #include "device_yamaha.h" -#include "device_keyboard_mqtt.h" #include "commandHandler.h" const byte ROWS = 5; //five rows diff --git a/Platformio/src/main.cpp b/Platformio/src/main.cpp index eec5f79..38ad934 100644 --- a/Platformio/src/main.cpp +++ b/Platformio/src/main.cpp @@ -45,7 +45,7 @@ void setup() { init_gui(); // init WiFi - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT init_mqtt(); #endif @@ -93,11 +93,18 @@ void loop() { IMUTaskTimer = millis(); } - // Update battery stats at 1Hz - static unsigned long batteryTaskTimer = millis() + 1000; // add 1s to start immediately - if(millis() - batteryTaskTimer >= 1000){ + // Update battery and BLE stats at 1Hz + static unsigned long updateStatusTimer = millis(); + if(millis() - updateStatusTimer >= 1000){ update_battery_stats(); - batteryTaskTimer = millis(); + updateStatusTimer = millis(); + + #ifdef ENABLE_BLUETOOTH + // adjust this if you implement other bluetooth devices than the BLE keyboard + #ifdef ENABLE_KEYBOARD_BLE + update_keyboard_ble_status(); + #endif + #endif // Serial.printf("heapSize: %lu, heapFree: %lu, heapMin: %lu, heapMax: %lu\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap()); diff --git a/Platformio/src/mqtt.cpp b/Platformio/src/mqtt.cpp index 3712831..bb9850b 100644 --- a/Platformio/src/mqtt.cpp +++ b/Platformio/src/mqtt.cpp @@ -2,10 +2,10 @@ #include #include "secrets.h" #include "mqtt.h" -#include "device_keyboard_mqtt.h" +#include "commandHandler.h" #include "gui.h" -#ifdef ENABLE_KEYBOARD_MQTT +#ifdef ENABLE_WIFI_AND_MQTT WiFiClient espClient; PubSubClient mqttClient(espClient); diff --git a/Platformio/src/mqtt.h b/Platformio/src/mqtt.h index ef10762..875c0e4 100644 --- a/Platformio/src/mqtt.h +++ b/Platformio/src/mqtt.h @@ -1,9 +1,9 @@ #ifndef __MQTT_H__ #define __MQTT_H__ -#include "device_keyboard_mqtt.h" +#include "commandHandler.h" -#ifdef ENABLE_KEYBOARD_MQTT +#ifdef ENABLE_WIFI_AND_MQTT #include "WiFi.h" #include diff --git a/Platformio/src/sleep.cpp b/Platformio/src/sleep.cpp index 4f638e5..3c6134f 100644 --- a/Platformio/src/sleep.cpp +++ b/Platformio/src/sleep.cpp @@ -10,8 +10,6 @@ #include "infrared_receiver.h" #include "battery.h" #include "commandHandler.h" -#include "device_keyboard_mqtt.h" -#include "device_keyboard_ble.h" int motion = 0; uint32_t actualSleepTimeout; @@ -116,7 +114,7 @@ void enterSleep(){ configIMUInterrupts(); IMU.readRegister(&intDataRead, LIS3DH_INT1_SRC);//really clear interrupt - #ifdef ENABLE_KEYBOARD_MQTT + #ifdef ENABLE_WIFI_AND_MQTT // Power down modem WiFi.disconnect(); WiFi.mode(WIFI_OFF);