From 980a823bf6c666911dd0c3b8d2d494c5174239d0 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 20 Jun 2022 09:06:36 +0200 Subject: [PATCH] Radio hardware: improve bluetooth settings --- radio/src/gui/colorlcd/CMakeLists.txt | 1 + radio/src/gui/colorlcd/hw_bluetooth.cpp | 136 ++++++++++++++++++++++ radio/src/gui/colorlcd/hw_bluetooth.h | 30 +++++ radio/src/gui/colorlcd/radio_hardware.cpp | 122 +------------------ 4 files changed, 173 insertions(+), 116 deletions(-) create mode 100644 radio/src/gui/colorlcd/hw_bluetooth.cpp create mode 100644 radio/src/gui/colorlcd/hw_bluetooth.h diff --git a/radio/src/gui/colorlcd/CMakeLists.txt b/radio/src/gui/colorlcd/CMakeLists.txt index dac0830b8..1c5f8f5eb 100644 --- a/radio/src/gui/colorlcd/CMakeLists.txt +++ b/radio/src/gui/colorlcd/CMakeLists.txt @@ -158,6 +158,7 @@ endif() if(BLUETOOTH) add_gui_src(trainer_bluetooth.cpp) + add_gui_src(hw_bluetooth.cpp) endif() set(SRC ${SRC} storage/modelslist.cpp) diff --git a/radio/src/gui/colorlcd/hw_bluetooth.cpp b/radio/src/gui/colorlcd/hw_bluetooth.cpp new file mode 100644 index 000000000..7d54011a5 --- /dev/null +++ b/radio/src/gui/colorlcd/hw_bluetooth.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "hw_bluetooth.h" +#include "opentx.h" + +#define SET_DIRTY() storageDirty(EE_GENERAL) + +static const lv_coord_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(3), + LV_GRID_TEMPLATE_LAST}; + +static const lv_coord_t row_dsc[] = {LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST}; + +class BTDetailsDialog : public Dialog +{ + StaticText* l_addr; + StaticText* r_addr; + + void setAddr(StaticText* label, const char* addr) + { + if (addr[0] == '\0') { + label->setText("---"); + } else { + label->setText(addr); + } + } + +public: + BTDetailsDialog(Window *parent) : + Dialog(parent, STR_BLUETOOTH, rect_t{}) + { + setCloseWhenClickOutside(true); + + auto form = &content->form; + form->setFlexLayout(); + + FlexGridLayout grid(col_dsc, row_dsc); + + if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY) { + auto line = form->newLine(&grid); + new StaticText(line, rect_t{}, STR_BLUETOOTH_PIN_CODE, 0, + COLOR_THEME_PRIMARY1); + new StaticText(line, rect_t{}, "000000", 0, COLOR_THEME_PRIMARY1); + } + + // Local MAC + auto line = form->newLine(&grid); + new StaticText(line, rect_t{}, STR_BLUETOOTH_LOCAL_ADDR, 0, + COLOR_THEME_PRIMARY1); + l_addr = new StaticText(line, rect_t{}, "", 0, COLOR_THEME_PRIMARY1); + + // Remote MAC + line = form->newLine(&grid); + new StaticText(line, rect_t{}, STR_BLUETOOTH_DIST_ADDR, 0, + COLOR_THEME_PRIMARY1); + r_addr = new StaticText(line, rect_t{}, "", 0, COLOR_THEME_PRIMARY1); + + content->setWidth(LCD_W * 0.8); + content->updateSize(); + } + + void checkEvents() override + { + setAddr(l_addr, bluetooth.localAddr); + setAddr(r_addr, bluetooth.distantAddr); + } +}; + +static void bt_mode_changed(lv_event_t* e) +{ + lv_obj_t* target = lv_event_get_target(e); + auto obj = (lv_obj_t*)lv_event_get_user_data(e); + auto choice = (Choice*)lv_obj_get_user_data(target); + if (!obj || !choice) return; + + if (choice->getIntValue() != BLUETOOTH_OFF) { + lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN); + } +} + +BluetoothConfigWindow::BluetoothConfigWindow(Window *parent) : + FormGroup(parent, rect_t{}) +{ + setFlexLayout(); + FlexGridLayout grid(col_dsc, row_dsc, 2); + + auto line = newLine(&grid); + new StaticText(line, rect_t{}, STR_MODE, 0, COLOR_THEME_PRIMARY1); + + auto box = new FormGroup(line, rect_t{}); + box->setFlexLayout(LV_FLEX_FLOW_ROW_WRAP, lv_dpx(8)); + lv_obj_set_style_grid_cell_x_align(box->getLvObj(), LV_GRID_ALIGN_STRETCH, 0); + lv_obj_set_style_flex_cross_place(box->getLvObj(), LV_FLEX_ALIGN_CENTER, 0); + + auto mode = new Choice(box, rect_t{}, STR_BLUETOOTH_MODES, BLUETOOTH_OFF, + BLUETOOTH_TRAINER, GET_SET_DEFAULT(g_eeGeneral.bluetoothMode)); + + auto btn = + new TextButton(box, rect_t{}, LV_SYMBOL_SETTINGS, [=]() -> uint8_t { + new BTDetailsDialog(Layer::back()); + return 0; + }); + btn->padAll(lv_dpx(4)); + btn->setWidth(LV_DPI_DEF / 2); + + lv_obj_add_event_cb(mode->getLvObj(), bt_mode_changed, + LV_EVENT_VALUE_CHANGED, btn->getLvObj()); + + // BT radio name + line = newLine(&grid); + new StaticText(line, rect_t{}, STR_NAME, 0, COLOR_THEME_PRIMARY1); + new RadioTextEdit(line, rect_t{}, g_eeGeneral.bluetoothName, LEN_BLUETOOTH_NAME); + + lv_obj_add_event_cb(mode->getLvObj(), bt_mode_changed, + LV_EVENT_VALUE_CHANGED, line->getLvObj()); +} diff --git a/radio/src/gui/colorlcd/hw_bluetooth.h b/radio/src/gui/colorlcd/hw_bluetooth.h new file mode 100644 index 000000000..6ae44a8b9 --- /dev/null +++ b/radio/src/gui/colorlcd/hw_bluetooth.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#pragma once + +#include "form.h" + +class BluetoothConfigWindow : public FormGroup +{ + public: + BluetoothConfigWindow(Window* parent); +}; diff --git a/radio/src/gui/colorlcd/radio_hardware.cpp b/radio/src/gui/colorlcd/radio_hardware.cpp index 7380fd062..030d7740c 100644 --- a/radio/src/gui/colorlcd/radio_hardware.cpp +++ b/radio/src/gui/colorlcd/radio_hardware.cpp @@ -31,6 +31,10 @@ #include "hw_serial.h" #include "hw_inputs.h" +#if defined(BLUETOOTH) +#include "hw_bluetooth.h" +#endif + #define SET_DIRTY() storageDirty(EE_GENERAL) static const lv_coord_t col_dsc[] = {LV_GRID_FR(1), LV_GRID_FR(2), @@ -43,121 +47,6 @@ RadioHardwarePage::RadioHardwarePage(): { } -#if defined(BLUETOOTH) -class ModeChoice : public Choice -{ - public: - ModeChoice(Window *parent, const rect_t &rect, const char **values, int vmin, - int vmax, std::function getValue, - std::function setValue = nullptr, - bool *menuOpen = nullptr) : - Choice(parent, rect, values, vmin, vmax, getValue, setValue), - menuOpen(menuOpen) - { - } - - protected: - void openMenu() - { - if (menuOpen) *menuOpen = true; - Choice::openMenu(); - } - - private: - bool *menuOpen; -}; - -class BluetoothConfigWindow : public FormGroup -{ - public: - BluetoothConfigWindow(Window *parent, const rect_t &rect) : - FormGroup(parent, rect, FORWARD_SCROLL) - { - update(); - } - - void checkEvents() override - { - FormGroup::checkEvents(); - if (!rte) return; - if (bluetooth.state != lastbluetoothstate) { - lastbluetoothstate = bluetooth.state; - if (!(modechoiceopen || rte->hasFocus())) update(); - } - } - - void update() - { - clear(); - - setFlexLayout(); - FlexGridLayout grid(col_dsc, row_dsc, 2); - lv_obj_set_style_pad_left(lvobj, lv_dpx(8), 0); - - auto line = newLine(&grid); - new StaticText(line, rect_t{}, STR_MODE, 0, COLOR_THEME_PRIMARY1); - - modechoiceopen = false; - btMode = new ModeChoice( - line, rect_t{}, STR_BLUETOOTH_MODES, BLUETOOTH_OFF, BLUETOOTH_TRAINER, - GET_DEFAULT(g_eeGeneral.bluetoothMode), - [=](int32_t newValue) { - g_eeGeneral.bluetoothMode = newValue; - update(); - modechoiceopen = false; - SET_DIRTY(); - }, - &modechoiceopen); - - if (g_eeGeneral.bluetoothMode != BLUETOOTH_OFF) { - // Pin code (displayed for information only, not editable) - if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY) { - line = newLine(&grid); - auto label = new StaticText(line, rect_t{}, STR_BLUETOOTH_PIN_CODE, 0, - COLOR_THEME_PRIMARY1); - lv_obj_set_style_pad_left(label->getLvObj(), lv_dpx(8), LV_PART_MAIN); - new StaticText(line, rect_t{}, "000000", 0, COLOR_THEME_PRIMARY1); - } - - // Local MAC - line = newLine(&grid); - auto label = new StaticText(line, rect_t{}, STR_BLUETOOTH_LOCAL_ADDR, 0, - COLOR_THEME_PRIMARY1); - lv_obj_set_style_pad_left(label->getLvObj(), lv_dpx(8), LV_PART_MAIN); - new StaticText( - line, rect_t{}, - bluetooth.localAddr[0] == '\0' ? "---" : bluetooth.localAddr, 0, - COLOR_THEME_PRIMARY1); - - // Remote MAC - line = newLine(&grid); - label = new StaticText(line, rect_t{}, STR_BLUETOOTH_DIST_ADDR, 0, - COLOR_THEME_PRIMARY1); - lv_obj_set_style_pad_left(label->getLvObj(), lv_dpx(8), LV_PART_MAIN); - new StaticText( - line, rect_t{}, - bluetooth.distantAddr[0] == '\0' ? "---" : bluetooth.distantAddr, 0, - COLOR_THEME_PRIMARY1); - - // BT radio name - line = newLine(&grid); - label = new StaticText(line, rect_t{}, STR_NAME, 0, COLOR_THEME_PRIMARY1); - lv_obj_set_style_pad_left(label->getLvObj(), lv_dpx(8), LV_PART_MAIN); - rte = new RadioTextEdit(line, rect_t{}, g_eeGeneral.bluetoothName, - LEN_BLUETOOTH_NAME); - } - } - - protected: - Choice *btMode = nullptr; - RadioTextEdit *rte = nullptr; - - private: - bool modechoiceopen = false; - uint8_t lastbluetoothstate = BLUETOOTH_STATE_OFF; -}; -#endif - void RadioHardwarePage::build(FormWindow * window) { window->setFlexLayout(); @@ -238,7 +127,8 @@ void RadioHardwarePage::build(FormWindow * window) #if defined(BLUETOOTH) new Subtitle(window, rect_t{}, STR_BLUETOOTH, 0, COLOR_THEME_PRIMARY1); - new BluetoothConfigWindow(window, rect_t{}); + auto bt = new BluetoothConfigWindow(window); + bt->padLeft(lv_dpx(8)); #endif new Subtitle(window, rect_t{}, STR_AUX_SERIAL_MODE, 0, COLOR_THEME_PRIMARY1);