mirror of
https://github.com/m5stack/StackChan.git
synced 2026-05-20 11:49:45 -07:00
151 lines
4.2 KiB
C++
151 lines
4.2 KiB
C++
/*
|
|
* SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
#include "hal_bridge.h"
|
|
#include "stackchan_display.h"
|
|
#include <esp_log.h>
|
|
#include <esp_err.h>
|
|
#include <nvs.h>
|
|
#include <nvs_flash.h>
|
|
#include <driver/gpio.h>
|
|
#include <esp_event.h>
|
|
#include <application.h>
|
|
#include <board.h>
|
|
#include <display.h>
|
|
#include <mutex>
|
|
#include <assets.h>
|
|
#include <settings.h>
|
|
|
|
static const char* _tag = "HAL_BRIDGE";
|
|
|
|
static constexpr std::string_view _xiaozhi_config_nvs_ns = "xiaozhi";
|
|
static constexpr std::string_view _xiaozhi_config_idle_shutdown_time_key = "idle_sec";
|
|
static constexpr std::string_view _xiaozhi_config_allow_shutdown_when_charging_key = "ext_pwr";
|
|
static constexpr std::string_view _xiaozhi_config_idle_random_movement_key = "idle_lv";
|
|
|
|
namespace hal_bridge {
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* State and touch point */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
static std::mutex _mutex;
|
|
static Data_t _data;
|
|
|
|
void lock()
|
|
{
|
|
_mutex.lock();
|
|
}
|
|
|
|
void unlock()
|
|
{
|
|
_mutex.unlock();
|
|
}
|
|
|
|
Data_t& get_data()
|
|
{
|
|
return _data;
|
|
}
|
|
|
|
void set_touch_point(int num, int x, int y)
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
_data.touchPoint.num = num;
|
|
_data.touchPoint.x = x;
|
|
_data.touchPoint.y = y;
|
|
}
|
|
|
|
TouchPoint_t get_touch_point()
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
return _data.touchPoint;
|
|
}
|
|
|
|
bool is_xiaozhi_mode()
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
return _data.isXiaozhiMode;
|
|
}
|
|
|
|
void set_xiaozhi_mode(bool mode)
|
|
{
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
_data.isXiaozhiMode = mode;
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Display */
|
|
/* -------------------------------------------------------------------------- */
|
|
#define DISPLAY_TYPE StackChanAvatarDisplay
|
|
|
|
lv_disp_t* display_get_lvgl_display()
|
|
{
|
|
auto display = static_cast<DISPLAY_TYPE*>(Board::GetInstance().GetDisplay());
|
|
return display->GetLvglDisplay();
|
|
}
|
|
|
|
void disply_lvgl_lock()
|
|
{
|
|
auto display = static_cast<DISPLAY_TYPE*>(Board::GetInstance().GetDisplay());
|
|
display->LvglLock();
|
|
}
|
|
|
|
void disply_lvgl_unlock()
|
|
{
|
|
auto display = static_cast<DISPLAY_TYPE*>(Board::GetInstance().GetDisplay());
|
|
display->LvglUnlock();
|
|
}
|
|
|
|
/* -------------------------------------------------------------------------- */
|
|
/* Application */
|
|
/* -------------------------------------------------------------------------- */
|
|
|
|
void xiaozhi_board_init()
|
|
{
|
|
// Init board
|
|
auto& board = Board::GetInstance();
|
|
}
|
|
|
|
void start_xiaozhi_app()
|
|
{
|
|
set_xiaozhi_mode(true);
|
|
|
|
// Initialize and run the application
|
|
auto& app = Application::GetInstance();
|
|
app.Initialize();
|
|
app.Run(); // This function runs the main event loop and never returns
|
|
}
|
|
|
|
XiaozhiConfig_t get_xiaozhi_config()
|
|
{
|
|
XiaozhiConfig_t config;
|
|
|
|
Settings settings(_xiaozhi_config_nvs_ns.data(), false);
|
|
config.idleShutdownTimeSeconds = settings.GetInt(_xiaozhi_config_idle_shutdown_time_key.data(),
|
|
static_cast<int>(config.idleShutdownTimeSeconds));
|
|
config.allowShutdownWhenCharging =
|
|
settings.GetBool(_xiaozhi_config_allow_shutdown_when_charging_key.data(), config.allowShutdownWhenCharging);
|
|
config.idleRandomMovementLevel =
|
|
settings.GetInt(_xiaozhi_config_idle_random_movement_key.data(), config.idleRandomMovementLevel);
|
|
|
|
return config;
|
|
}
|
|
|
|
void set_xiaozhi_config(const XiaozhiConfig_t& config)
|
|
{
|
|
Settings settings(_xiaozhi_config_nvs_ns.data(), true);
|
|
settings.SetInt(_xiaozhi_config_idle_shutdown_time_key.data(), config.idleShutdownTimeSeconds);
|
|
settings.SetBool(_xiaozhi_config_allow_shutdown_when_charging_key.data(), config.allowShutdownWhenCharging);
|
|
settings.SetInt(_xiaozhi_config_idle_random_movement_key.data(), config.idleRandomMovementLevel);
|
|
}
|
|
|
|
void app_play_sound(const std::string_view& sound)
|
|
{
|
|
auto& app = Application::GetInstance();
|
|
app.PlaySound(sound);
|
|
}
|
|
|
|
} // namespace hal_bridge
|