BLE indicator added; separation of BLE/WiFi activation from activation of devices using it

This commit is contained in:
KlausMu 2024-01-24 16:08:21 +01:00
parent 581015e9a9
commit 66c8195a31
14 changed files with 74 additions and 20 deletions

View file

@ -75,7 +75,7 @@ void executeCommandWithData(std::string command, commandData commandData, std::s
break; break;
} }
#ifdef ENABLE_KEYBOARD_MQTT #ifdef ENABLE_WIFI_AND_MQTT
case MQTT: { case MQTT: {
auto current = commandData.commandPayloads.begin(); auto current = commandData.commandPayloads.begin();
std::string topic = *current; std::string topic = *current;

View file

@ -1,6 +1,9 @@
#ifndef __COMMANDHANDLER_H__ #ifndef __COMMANDHANDLER_H__
#define __COMMANDHANDLER_H__ #define __COMMANDHANDLER_H__
#define ENABLE_WIFI_AND_MQTT // Comment out to disable WiFi
#define ENABLE_BLUETOOTH // Comment out to disable Bluetooth
#include <Arduino.h> #include <Arduino.h>
#include <string> #include <string>
#include <list> #include <list>
@ -47,7 +50,7 @@ enum commandHandlers {
IR_GC, IR_GC,
IR_NEC, IR_NEC,
IR_SAMSUNG, IR_SAMSUNG,
#ifdef ENABLE_KEYBOARD_MQTT #ifdef ENABLE_WIFI_AND_MQTT
MQTT, MQTT,
#endif #endif
#ifdef ENABLE_KEYBOARD_BLE #ifdef ENABLE_KEYBOARD_BLE

View file

@ -1,6 +1,7 @@
#include <BleKeyboard.h> #include <BleKeyboard.h>
#include "commandHandler.h" #include "commandHandler.h"
#include "device_keyboard_ble.h" #include "device_keyboard_ble.h"
#include "gui.h"
#ifdef ENABLE_KEYBOARD_BLE #ifdef ENABLE_KEYBOARD_BLE
@ -30,6 +31,26 @@ void init_keyboard_ble_commands() {
bleKeyboard.begin(); 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) { void keyboard_ble_write(uint8_t c) {
bleKeyboard.write(c); bleKeyboard.write(c);
} }

View file

@ -1,9 +1,19 @@
#ifndef __DEVICE_KEYBOARD_BLE_H__ #ifndef __DEVICE_KEYBOARD_BLE_H__
#define __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 #define ENABLE_KEYBOARD_BLE // Comment out to disable BLE
#ifdef ENABLE_KEYBOARD_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 <BleKeyboard.h> #include <BleKeyboard.h>
#define KEYBOARD_BLE_UP "Keyboard_ble_up" #define KEYBOARD_BLE_UP "Keyboard_ble_up"
@ -30,6 +40,7 @@ extern BleKeyboard bleKeyboard;
void init_keyboard_ble_commands(); void init_keyboard_ble_commands();
void keyboard_ble_executeCommand(std::string command, std::string additionalPayload = ""); void keyboard_ble_executeCommand(std::string command, std::string additionalPayload = "");
void update_keyboard_ble_status();
#endif #endif

View file

@ -5,9 +5,17 @@
// https://github.com/KlausMu/esp32-mqtt-keyboard // https://github.com/KlausMu/esp32-mqtt-keyboard
#define ENABLE_KEYBOARD_MQTT // Comment out to disable WiFi and MQTT #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 #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_UP "Keyboard_mqtt_up"
#define KEYBOARD_MQTT_DOWN "Keyboard_mqtt_down" #define KEYBOARD_MQTT_DOWN "Keyboard_mqtt_down"
#define KEYBOARD_MQTT_RIGHT "Keyboard_mqtt_right" #define KEYBOARD_MQTT_RIGHT "Keyboard_mqtt_right"

View file

@ -12,6 +12,7 @@
lv_obj_t* panel; lv_obj_t* panel;
lv_color_t color_primary = lv_color_hex(0x303030); // gray lv_color_t color_primary = lv_color_hex(0x303030); // gray
lv_obj_t* WifiLabel; lv_obj_t* WifiLabel;
lv_obj_t* BluetoothLabel;
lv_obj_t* objBattPercentage; lv_obj_t* objBattPercentage;
lv_obj_t* objBattIcon; lv_obj_t* objBattIcon;
byte currentScreen = 1; // Current Device to control (allows switching mappings between devices) 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_align(WifiLabel, LV_ALIGN_LEFT_MID, -8, 0);
lv_obj_set_style_text_font(WifiLabel, &lv_font_montserrat_12, LV_PART_MAIN); 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 ---------------------------------------------------------------------- // Battery ----------------------------------------------------------------------
objBattPercentage = lv_label_create(statusbar); objBattPercentage = lv_label_create(statusbar);
lv_label_set_text(objBattPercentage, ""); lv_label_set_text(objBattPercentage, "");

View file

@ -13,6 +13,7 @@ extern lv_obj_t* panel;
extern lv_color_t color_primary; extern lv_color_t color_primary;
extern lv_obj_t* WifiLabel; extern lv_obj_t* WifiLabel;
extern lv_obj_t* BluetoothLabel;
extern byte currentScreen; // Current screen that is shown extern byte currentScreen; // Current screen that is shown

View file

@ -2,7 +2,6 @@
// #include "assets.c" // #include "assets.c"
#include "tft.h" #include "tft.h"
#include "gui.h" #include "gui.h"
#include "device_keyboard_mqtt.h"
#include "commandHandler.h" #include "commandHandler.h"
// Virtual Keypad Event handler // Virtual Keypad Event handler

View file

@ -3,7 +3,7 @@
#include "tft.h" #include "tft.h"
#include "gui.h" #include "gui.h"
#include "mqtt.h" #include "mqtt.h"
#include "device_keyboard_mqtt.h" #include "commandHandler.h"
// LVGL declarations // LVGL declarations
LV_IMG_DECLARE(lightbulb); 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"); if(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)) strcpy(payload,"true");
else strcpy(payload,"false"); else strcpy(payload,"false");
// Publish an MQTT message based on the event user data // 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 == 1) publishMQTTMessage("bulb1_set", payload);
if((int)e->user_data == 2) publishMQTTMessage("bulb2_set", payload); if((int)e->user_data == 2) publishMQTTMessage("bulb2_set", payload);
#endif #endif
@ -26,7 +26,7 @@ static void smartHomeSlider_event_cb(lv_event_t * e){
char payload[8]; char payload[8];
dtostrf(lv_slider_get_value(slider), 1, 2, payload); dtostrf(lv_slider_get_value(slider), 1, 2, payload);
// Publish an MQTT message based on the event user data // 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 == 1) publishMQTTMessage("bulb1_setbrightness", payload);
if((int)e->user_data == 2) publishMQTTMessage("bulb2_setbrightness", payload); if((int)e->user_data == 2) publishMQTTMessage("bulb2_setbrightness", payload);
#endif #endif

View file

@ -6,7 +6,6 @@
#include "mqtt.h" #include "mqtt.h"
#include "device_samsung.h" #include "device_samsung.h"
#include "device_yamaha.h" #include "device_yamaha.h"
#include "device_keyboard_mqtt.h"
#include "commandHandler.h" #include "commandHandler.h"
const byte ROWS = 5; //five rows const byte ROWS = 5; //five rows

View file

@ -45,7 +45,7 @@ void setup() {
init_gui(); init_gui();
// init WiFi // init WiFi
#ifdef ENABLE_KEYBOARD_MQTT #ifdef ENABLE_WIFI_AND_MQTT
init_mqtt(); init_mqtt();
#endif #endif
@ -93,11 +93,18 @@ void loop() {
IMUTaskTimer = millis(); IMUTaskTimer = millis();
} }
// Update battery stats at 1Hz // Update battery and BLE stats at 1Hz
static unsigned long batteryTaskTimer = millis() + 1000; // add 1s to start immediately static unsigned long updateStatusTimer = millis();
if(millis() - batteryTaskTimer >= 1000){ if(millis() - updateStatusTimer >= 1000){
update_battery_stats(); 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()); // Serial.printf("heapSize: %lu, heapFree: %lu, heapMin: %lu, heapMax: %lu\r\n", ESP.getHeapSize(), ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getMaxAllocHeap());

View file

@ -2,10 +2,10 @@
#include <lvgl.h> #include <lvgl.h>
#include "secrets.h" #include "secrets.h"
#include "mqtt.h" #include "mqtt.h"
#include "device_keyboard_mqtt.h" #include "commandHandler.h"
#include "gui.h" #include "gui.h"
#ifdef ENABLE_KEYBOARD_MQTT #ifdef ENABLE_WIFI_AND_MQTT
WiFiClient espClient; WiFiClient espClient;
PubSubClient mqttClient(espClient); PubSubClient mqttClient(espClient);

View file

@ -1,9 +1,9 @@
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __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 "WiFi.h"
#include <PubSubClient.h> #include <PubSubClient.h>

View file

@ -10,8 +10,6 @@
#include "infrared_receiver.h" #include "infrared_receiver.h"
#include "battery.h" #include "battery.h"
#include "commandHandler.h" #include "commandHandler.h"
#include "device_keyboard_mqtt.h"
#include "device_keyboard_ble.h"
int motion = 0; int motion = 0;
uint32_t actualSleepTimeout; uint32_t actualSleepTimeout;
@ -116,7 +114,7 @@ void enterSleep(){
configIMUInterrupts(); configIMUInterrupts();
IMU.readRegister(&intDataRead, LIS3DH_INT1_SRC);//really clear interrupt IMU.readRegister(&intDataRead, LIS3DH_INT1_SRC);//really clear interrupt
#ifdef ENABLE_KEYBOARD_MQTT #ifdef ENABLE_WIFI_AND_MQTT
// Power down modem // Power down modem
WiFi.disconnect(); WiFi.disconnect();
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);