mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-06-19 04:15:06 +00:00
* Bluetooth LE addition * fixes * use the psram! helps a little on S3 (t-deck) * custom device name * Update symbols.c * Feedback + fixes Fixes external app start/stop server (child devices) Fixes BtManage causing a full system hang upon disabling bt when a device is connected to the host. * updoot * more updoot * move back! * Revert "move back!" This reverts commit d3694365c634acc5db62ac59771c496cb971a727. * fix some of the things * Addressing feedback? hmm * Fixes Bug 1 — Reconnect loop / Reconnect not working fixed Bug 2 — Name-only advertising overwrites HID advertising Bug 3 — BleHidDeviceCtx leak on re-enable Enhancement — HID device auto-start on radio re-enable * stuff... * update for consistency with others * fix crashes and some bonus symbols * a few symbols, i2c speed, cdn message 100kHz i2c speed seems to be more compatible with m5stack modules...and probably in general. cdn message no longer applies * Hide BT Settings when bt not enabled * Addressing things and device fixes * Missed one! * stuff
296 lines
9.7 KiB
C++
296 lines
9.7 KiB
C++
#include <Tactility/lvgl/Statusbar.h>
|
|
|
|
#include <Tactility/Logger.h>
|
|
#include <Tactility/Mutex.h>
|
|
#include <Tactility/Paths.h>
|
|
#include <Tactility/Timer.h>
|
|
#include <Tactility/hal/power/PowerDevice.h>
|
|
#include <Tactility/hal/sdcard/SdCardDevice.h>
|
|
#include <Tactility/lvgl/Lvgl.h>
|
|
#include <Tactility/lvgl/LvglSync.h>
|
|
#include <Tactility/service/ServiceContext.h>
|
|
#include <Tactility/service/ServicePaths.h>
|
|
#include <Tactility/service/ServiceRegistration.h>
|
|
#include <Tactility/bluetooth/Bluetooth.h>
|
|
#include <tactility/drivers/bluetooth.h>
|
|
#include <tactility/drivers/bluetooth_serial.h>
|
|
#include <tactility/drivers/bluetooth_midi.h>
|
|
#include <Tactility/service/gps/GpsService.h>
|
|
#include <Tactility/service/wifi/Wifi.h>
|
|
#include <tactility/check.h>
|
|
|
|
#include <tactility/lvgl_icon_statusbar.h>
|
|
|
|
namespace tt::service::statusbar {
|
|
|
|
static const auto LOGGER = Logger("StatusbarService");
|
|
|
|
// GPS
|
|
extern const ServiceManifest manifest;
|
|
|
|
const char* getWifiStatusIconForRssi(int rssi) {
|
|
if (rssi >= -60) {
|
|
return LVGL_ICON_STATUSBAR_SIGNAL_WIFI_4_BAR;
|
|
} else if (rssi >= -70) {
|
|
return LVGL_ICON_STATUSBAR_NETWORK_WIFI_3_BAR;
|
|
} else if (rssi >= -80) {
|
|
return LVGL_ICON_STATUSBAR_NETWORK_WIFI_2_BAR;
|
|
} else if (rssi >= -90) {
|
|
return LVGL_ICON_STATUSBAR_NETWORK_WIFI_1_BAR;
|
|
} else {
|
|
return LVGL_ICON_STATUSBAR_SIGNAL_WIFI_BAD;
|
|
}
|
|
}
|
|
|
|
static const char* getWifiStatusIcon(wifi::RadioState state) {
|
|
int rssi;
|
|
switch (state) {
|
|
using enum wifi::RadioState;
|
|
case On:
|
|
case OnPending:
|
|
case ConnectionPending:
|
|
return LVGL_ICON_STATUSBAR_SIGNAL_WIFI_0_BAR;
|
|
case OffPending:
|
|
case Off:
|
|
return LVGL_ICON_STATUSBAR_SIGNAL_WIFI_OFF;
|
|
case ConnectionActive:
|
|
rssi = wifi::getRssi();
|
|
return getWifiStatusIconForRssi(rssi);
|
|
default:
|
|
check(false, "not implemented");
|
|
}
|
|
}
|
|
|
|
static const char* getBluetoothStatusIcon(tt::bluetooth::RadioState state, bool scanning, bool connected) {
|
|
switch (state) {
|
|
using enum tt::bluetooth::RadioState;
|
|
case Off:
|
|
case OffPending:
|
|
return nullptr; // hidden when off
|
|
case OnPending:
|
|
case On:
|
|
if (connected) return LVGL_ICON_STATUSBAR_BLUETOOTH_CONNECTED;
|
|
if (scanning) return LVGL_ICON_STATUSBAR_BLUETOOTH_SEARCHING;
|
|
return LVGL_ICON_STATUSBAR_BLUETOOTH;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
static const char* getSdCardStatusIcon(bool mounted) {
|
|
if (mounted) return LVGL_ICON_STATUSBAR_SD_CARD;
|
|
return LVGL_ICON_STATUSBAR_SD_CARD_ALERT;
|
|
}
|
|
|
|
static const char* getPowerStatusIcon() {
|
|
// TODO: Support multiple power devices?
|
|
std::shared_ptr<hal::power::PowerDevice> power;
|
|
hal::findDevices<hal::power::PowerDevice>(hal::Device::Type::Power, [&power](const auto& device) {
|
|
if (device->supportsMetric(hal::power::PowerDevice::MetricType::ChargeLevel)) {
|
|
power = device;
|
|
return false;
|
|
}
|
|
return true;
|
|
});
|
|
|
|
if (power == nullptr) {
|
|
return nullptr;
|
|
}
|
|
|
|
hal::power::PowerDevice::MetricData charge_level;
|
|
if (!power->getMetric(hal::power::PowerDevice::MetricType::ChargeLevel, charge_level)) {
|
|
return nullptr;
|
|
}
|
|
|
|
uint8_t charge = charge_level.valueAsUint8;
|
|
|
|
if (charge >= 95) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_FULL;
|
|
} else if (charge >= 80) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_6;
|
|
} else if (charge >= 64) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_5;
|
|
} else if (charge >= 48) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_4;
|
|
} else if (charge >= 32) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_3;
|
|
} else if (charge >= 16) {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_2;
|
|
} else {
|
|
return LVGL_ICON_STATUSBAR_BATTERY_ANDROID_FRAME_1;
|
|
}
|
|
}
|
|
|
|
class StatusbarService final : public Service {
|
|
|
|
Mutex mutex;
|
|
std::unique_ptr<Timer> updateTimer;
|
|
int8_t gps_icon_id;
|
|
bool gps_last_state = false;
|
|
int8_t bt_icon_id;
|
|
const char* bt_last_icon = nullptr;
|
|
int8_t wifi_icon_id;
|
|
const char* wifi_last_icon = nullptr;
|
|
int8_t sdcard_icon_id;
|
|
const char* sdcard_last_icon = nullptr;
|
|
int8_t power_icon_id;
|
|
const char* power_last_icon = nullptr;
|
|
|
|
void lock() const {
|
|
mutex.lock();
|
|
}
|
|
|
|
void unlock() const {
|
|
mutex.unlock();
|
|
}
|
|
|
|
void updateGpsIcon() {
|
|
auto gps_state = gps::findGpsService()->getState();
|
|
bool show_icon = (gps_state == gps::State::OnPending) || (gps_state == gps::State::On);
|
|
if (gps_last_state != show_icon) {
|
|
if (show_icon) {
|
|
lvgl::statusbar_icon_set_image(gps_icon_id, LVGL_ICON_STATUSBAR_LOCATION_ON);
|
|
lvgl::statusbar_icon_set_visibility(gps_icon_id, true);
|
|
} else {
|
|
lvgl::statusbar_icon_set_visibility(gps_icon_id, false);
|
|
}
|
|
gps_last_state = show_icon;
|
|
}
|
|
}
|
|
|
|
void updateBluetoothIcon() {
|
|
auto radio_state = tt::bluetooth::getRadioState();
|
|
struct Device* btdev = tt::bluetooth::findFirstDevice();
|
|
bool scanning = btdev ? bluetooth_is_scanning(btdev) : false;
|
|
struct Device* serial_dev = bluetooth_serial_get_device();
|
|
struct Device* midi_dev = bluetooth_midi_get_device();
|
|
bool connected = (serial_dev && bluetooth_serial_is_connected(serial_dev)) ||
|
|
(midi_dev && bluetooth_midi_is_connected(midi_dev));
|
|
const char* desired_icon = getBluetoothStatusIcon(radio_state, scanning, connected);
|
|
if (bt_last_icon != desired_icon) {
|
|
if (desired_icon != nullptr) {
|
|
lvgl::statusbar_icon_set_image(bt_icon_id, desired_icon);
|
|
lvgl::statusbar_icon_set_visibility(bt_icon_id, true);
|
|
} else {
|
|
lvgl::statusbar_icon_set_visibility(bt_icon_id, false);
|
|
}
|
|
bt_last_icon = desired_icon;
|
|
}
|
|
}
|
|
|
|
void updateWifiIcon() {
|
|
wifi::RadioState radio_state = wifi::getRadioState();
|
|
const char* desired_icon = getWifiStatusIcon(radio_state);
|
|
if (wifi_last_icon != desired_icon) {
|
|
if (desired_icon != nullptr) {
|
|
lvgl::statusbar_icon_set_image(wifi_icon_id, desired_icon);
|
|
lvgl::statusbar_icon_set_visibility(wifi_icon_id, true);
|
|
} else {
|
|
lvgl::statusbar_icon_set_visibility(wifi_icon_id, false);
|
|
}
|
|
wifi_last_icon = desired_icon;
|
|
}
|
|
}
|
|
|
|
void updatePowerStatusIcon() {
|
|
const char* desired_icon = getPowerStatusIcon();
|
|
if (power_last_icon != desired_icon) {
|
|
if (desired_icon != nullptr) {
|
|
lvgl::statusbar_icon_set_image(power_icon_id, desired_icon);
|
|
lvgl::statusbar_icon_set_visibility(power_icon_id, true);
|
|
} else {
|
|
lvgl::statusbar_icon_set_visibility(power_icon_id, false);
|
|
}
|
|
power_last_icon = desired_icon;
|
|
}
|
|
}
|
|
|
|
void updateSdCardIcon() {
|
|
auto* sdcard_fs = findSdcardFileSystem(false);
|
|
// TODO: Support multiple SD cards
|
|
if (sdcard_fs != nullptr) {
|
|
auto mounted = file_system_is_mounted(sdcard_fs);
|
|
auto* desired_icon = getSdCardStatusIcon(mounted);
|
|
if (sdcard_last_icon != desired_icon) {
|
|
lvgl::statusbar_icon_set_image(sdcard_icon_id, desired_icon);
|
|
lvgl::statusbar_icon_set_visibility(sdcard_icon_id, true);
|
|
sdcard_last_icon = desired_icon;
|
|
}
|
|
} else {
|
|
if (sdcard_last_icon != nullptr) {
|
|
lvgl::statusbar_icon_set_visibility(sdcard_icon_id, false);
|
|
sdcard_last_icon = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
void update() {
|
|
if (lvgl::isStarted()) {
|
|
if (lvgl::lock(100)) {
|
|
updateGpsIcon();
|
|
updateBluetoothIcon();
|
|
updateWifiIcon();
|
|
updateSdCardIcon();
|
|
updatePowerStatusIcon();
|
|
lvgl::unlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
public:
|
|
|
|
StatusbarService() {
|
|
gps_icon_id = lvgl::statusbar_icon_add();
|
|
bt_icon_id = lvgl::statusbar_icon_add();
|
|
sdcard_icon_id = lvgl::statusbar_icon_add();
|
|
wifi_icon_id = lvgl::statusbar_icon_add();
|
|
power_icon_id = lvgl::statusbar_icon_add();
|
|
}
|
|
|
|
~StatusbarService() override {
|
|
lvgl::statusbar_icon_remove(wifi_icon_id);
|
|
lvgl::statusbar_icon_remove(sdcard_icon_id);
|
|
lvgl::statusbar_icon_remove(bt_icon_id);
|
|
lvgl::statusbar_icon_remove(power_icon_id);
|
|
lvgl::statusbar_icon_remove(gps_icon_id);
|
|
}
|
|
|
|
bool onStart(ServiceContext& serviceContext) override {
|
|
if (lv_screen_active() == nullptr) {
|
|
LOGGER.error("No display found");
|
|
return false;
|
|
}
|
|
|
|
// TODO: Make thread-safe for LVGL
|
|
lvgl::statusbar_icon_set_visibility(wifi_icon_id, true);
|
|
|
|
auto service = findServiceById<StatusbarService>(manifest.id);
|
|
assert(service);
|
|
service->update();
|
|
|
|
updateTimer = std::make_unique<Timer>(Timer::Type::Periodic, pdMS_TO_TICKS(1000), [service] {
|
|
service->update();
|
|
});
|
|
|
|
updateTimer->setCallbackPriority(Thread::Priority::Lower);
|
|
|
|
// We want to try and scan more often in case of startup or scan lock failure
|
|
updateTimer->start();
|
|
|
|
return true;
|
|
}
|
|
|
|
void onStop(ServiceContext& service) override{
|
|
updateTimer->stop();
|
|
updateTimer = nullptr;
|
|
}
|
|
};
|
|
|
|
extern const ServiceManifest manifest = {
|
|
.id = "Statusbar",
|
|
.createService = create<StatusbarService>
|
|
};
|
|
|
|
// endregion service
|
|
|
|
} // namespace
|