BLE indicator added; separation of BLE/WiFi activation from activation of devices using it
This commit is contained in:
parent
581015e9a9
commit
66c8195a31
14 changed files with 74 additions and 20 deletions
|
@ -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;
|
||||
|
|
|
@ -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 <Arduino.h>
|
||||
#include <string>
|
||||
#include <list>
|
||||
|
@ -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
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <BleKeyboard.h>
|
||||
#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);
|
||||
}
|
||||
|
|
|
@ -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 <BleKeyboard.h>
|
||||
|
||||
#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
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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, "");
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
#include <lvgl.h>
|
||||
#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);
|
||||
|
||||
|
|
|
@ -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 <PubSubClient.h>
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue