Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/boards/sf32lb52-xty-ai_base/bsp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@
#define LXT_LP_CYCLE 200
#endif

#ifndef PWRKEY_CNT_CLOCK_FREQ
#define PWRKEY_CNT_CLOCK_FREQ (32000)
#endif

#ifndef PWRKEY_HARD_RESET_TIME
#define PWRKEY_HARD_RESET_TIME (30) /* unit:s */
#endif

static uint16_t mpi1_div = 1;
static uint16_t mpi2_div = 1;

Expand Down Expand Up @@ -168,6 +176,7 @@ void HAL_PreInit(void)
HAL_HPAON_StartGTimer();
HAL_PMU_EnableRC32K(1);
HAL_PMU_LpCLockSelect(PMU_LPCLK_RC32);
hwp_pmuc->PWRKEY_CNT = PWRKEY_CNT_CLOCK_FREQ * PWRKEY_HARD_RESET_TIME;

HAL_PMU_EnableDLL(1);

Expand Down
2 changes: 2 additions & 0 deletions app/src/iot/screen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// C 接口头文件
extern "C" {
#include <rtthread.h>
extern void xiaozhi_ui_update_brightness(int brightness);
}

// 定义设备名
Expand Down Expand Up @@ -47,6 +48,7 @@ class Screen : public Thing {
if (lcd_device_) {
rt_device_control(lcd_device_, RTGRAPHIC_CTRL_SET_BRIGHTNESS, &brightness);
rt_kprintf("Brightness set to: %d", brightness);
xiaozhi_ui_update_brightness(brightness);
} else {
rt_kprintf("LCD device is NULL, cannot set brightness");
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/iot/speaker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// 添加 C 接口头文件
extern "C" {
#include "audio_server.h"
//#include "../xiaozhi_ui.h"
extern void xiaozhi_ui_update_volume(int volume);
}


Expand All @@ -28,6 +30,7 @@ class Speaker : public Thing {
}

audio_server_set_private_volume(AUDIO_TYPE_LOCAL_MUSIC, volume);
xiaozhi_ui_update_volume(volume);
});

// 新增方法:GetVolume(获取音量)
Expand Down
42 changes: 32 additions & 10 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <drivers/rt_drv_encoder.h>
#include "drv_flash.h"
#include "xiaozhi_weather.h"
#include "lv_timer.h"
#include "lv_display.h"
extern void xiaozhi_ui_update_ble(char *string);
extern void xiaozhi_ui_update_emoji(char *string);
extern void xiaozhi_ui_chat_status(char *string);
Expand All @@ -35,7 +37,12 @@ extern void xz_ws_audio_init();
extern rt_tick_t last_listen_tick;
extern xiaozhi_ws_t g_xz_ws;
extern rt_mailbox_t g_button_event_mb;
extern void ui_sleep_callback(lv_timer_t *timer);
extern lv_obj_t *standby_screen;
rt_mailbox_t g_battery_mb;
extern lv_timer_t *ui_sleep_timer;
extern lv_obj_t *shutdown_screen;
extern lv_obj_t *sleep_screen;
/* Common functions for RT-Thread based platform
* -----------------------------------------------*/
/**
Expand Down Expand Up @@ -213,18 +220,18 @@ static void battery_level_task(void *parameter)
// 获取到的是电池电压,单位是mV
// 假设电池电压范围是3.6V到4.2V,对应的电量范围是0%到100%
uint32_t battery_percentage = 0;
if (battery_level < 3600)
if (battery_level < 36000)
{
battery_percentage = 0; // 小于3.6V,电量为0
}
else if (battery_level > 4200)
else if (battery_level > 42000)
{
battery_percentage = 100; // 大于4.2V,电量为100
}
else
{
// 线性插值计算电量百分比
battery_percentage = ((battery_level - 3600) * 100) / (4200 - 3600);
battery_percentage = ((battery_level - 36000) * 100) / (42000 - 36000);
}

rt_mb_send(g_battery_mb, battery_percentage);
Expand Down Expand Up @@ -331,7 +338,7 @@ void pan_reconnect()

LOG_I("Attempting to reconnect PAN, attempt %d",
first_reconnect_attempts + 1);
xiaozhi_ui_chat_status("connecting pan...");
xiaozhi_ui_chat_status("重新连接 PAN...");
xiaozhi_ui_chat_output("正在重连PAN...");
xiaozhi_ui_standby_chat_output("正在重连PAN...");
if (first_reconnect_attempts < max_reconnect_attempts)
Expand Down Expand Up @@ -394,6 +401,11 @@ static int bt_app_interface_event_handle(uint16_t type, uint16_t event_id,
g_bt_app_env.bt_connected = FALSE;
xiaozhi_ui_chat_output("蓝牙断开连接");
xiaozhi_ui_standby_chat_output("蓝牙断开连接");//待机画面
lv_obj_t *now_screen = lv_screen_active();
if (now_screen != standby_screen && now_screen != sleep_screen && now_screen != shutdown_screen)
{
ui_swith_to_standby_screen();
}
// memset(&g_bt_app_env.bd_addr, 0xFF,
// sizeof(g_bt_app_env.bd_addr));
if (info->res == BT_NOTIFY_COMMON_SCO_DISCONNECTED)
Expand Down Expand Up @@ -567,9 +579,13 @@ static void check_poweron_reason(void)
#endif
else if (PMUC_WSR_PIN_ALL & pm_get_wakeup_src())
{
rt_thread_mdelay(2500); // 延时2.5秒
int val = rt_pin_read(43);
rt_kprintf("Power key(PA43) level after 2.5s: %d\n", val);
rt_thread_mdelay(1000); // 延时1秒
#ifdef BSP_USING_BOARD_SF32LB52_LCD_N16R8
int val = rt_pin_read(BSP_KEY1_PIN);
#else
int val = rt_pin_read(BSP_KEY2_PIN);
#endif
rt_kprintf("Power key level after 1s: %d\n", val);
if (val != KEY2_ACTIVE_LEVEL)
{
// 按键已松开,认为是误触发,直接关机
Expand Down Expand Up @@ -763,7 +779,7 @@ int main(void)
else if (value == BT_APP_CONNECT_PAN_SUCCESS)
{
rt_kputs("BT_APP_CONNECT_PAN_SUCCESS\r\n");
xiaozhi_ui_chat_output("初始化 请稍等...");
//xiaozhi_ui_chat_output("初始化 请稍等...");
xiaozhi_ui_standby_chat_output("初始化 请稍等...");
xiaozhi_ui_update_ble("open");
xiaozhi_ui_chat_status("初始化...");
Expand All @@ -773,16 +789,22 @@ int main(void)
rt_thread_mdelay(2000);
// 执行NTP与天气同步
xiaozhi_time_weather();
xiaozhi_ui_chat_output("连接小智中...");
//xiaozhi_ui_chat_output("连接小智中...");
xiaozhi_ui_standby_chat_output("请按键连接小智...");

#ifdef XIAOZHI_USING_MQTT
xiaozhi(0, NULL);
rt_kprintf("Select MQTT Version\n");
#else
xz_button_init();
xz_button_init();
// xiaozhi2(0, NULL); // Start Xiaozhi
#endif
// 在蓝牙和PAN连接成功后创建睡眠定时器
if (!ui_sleep_timer && g_pan_connected)
{
rt_kprintf("create sleep timer2\n");
ui_sleep_timer = lv_timer_create(ui_sleep_callback, 40000, NULL);
}
}
else if (value == KEEP_FIRST_PAN_RECONNECT)
{
Expand Down
8 changes: 6 additions & 2 deletions app/src/mcp/mcp_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
#include "rgbled_mcp.h"
// #include "lwip/apps/websocket_client.h" // 提供 wsock_write 和 OPCODE_TEXT 定义
#include "../xiaozhi2.h" // 提供 g_xz_ws 定义

extern xiaozhi_ws_t g_xz_ws;


extern "C" {
extern void xiaozhi_ui_update_volume(int volume);
extern void xiaozhi_ui_update_brightness(int brightness);
}



Expand Down Expand Up @@ -54,6 +56,7 @@ void McpServer::AddCommonTools() {
speaker->Invoke(command);
cJSON_Delete(command); // 使用完记得释放内存
}
xiaozhi_ui_update_volume(volume);
return true;
});

Expand Down Expand Up @@ -86,6 +89,7 @@ void McpServer::AddCommonTools() {
screen->Invoke(command);
cJSON_Delete(command);
}
xiaozhi_ui_update_brightness(brightness);
return true;
});

Expand Down
73 changes: 48 additions & 25 deletions app/src/xiaozhi2.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,13 @@ void xiaozhi2(int argc, char **argv);
extern rt_mailbox_t g_bt_app_mb;
extern lv_obj_t *main_container;
extern lv_obj_t *standby_screen;

static void xz_button_event_handler(int32_t pin, button_action_t action)
{
rt_kprintf("in button handle\n");
lv_display_trigger_activity(NULL);
gui_pm_fsm(GUI_PM_ACTION_WAKEUP); // 唤醒设备
rt_kprintf("in button handle2\n");
// 如果当前处于KWS模式,则退出KWS模式
if (g_kws_running)
{
Expand All @@ -319,13 +321,14 @@ static void xz_button_event_handler(int32_t pin, button_action_t action)

if (action == BUTTON_PRESSED)
{


lv_obj_t *now_screen = lv_screen_active();
rt_kprintf("pressed\r\n");
rt_kprintf("按键->对话");
ui_swith_to_xiaozhi_screen();


if (now_screen == standby_screen)
{
ui_swith_to_xiaozhi_screen();
}

// 1. 检查是否处于睡眠状态(WebSocket未连接)
if (!g_xz_ws.is_connected)
{
Expand Down Expand Up @@ -382,31 +385,32 @@ static rt_thread_t countdown_thread = RT_NULL;
extern rt_mailbox_t g_ui_task_mb;
static void xz_button2_event_handler(int32_t pin, button_action_t action)
{
static rt_tick_t press_tick = 0;
if (action == BUTTON_PRESSED)
{
press_tick = rt_tick_get();
rt_kprintf("xz_button2_event_handler\n");
lv_obj_t *now_screen = lv_screen_active();
if (now_screen != standby_screen)
{
rt_sem_release(g_activation_context.sem);
}
rt_kprintf("xz_button2_event_handler - pressed\n");
}
else if (action == BUTTON_LONG_PRESSED)
{
// 按下超过3秒,触发关机
rt_kprintf("xz_button2_event_handler - long pressed\n");
//检查设备是否已绑定设备码
// if (g_activation_context.is_activated)
// {
// rt_sem_release(g_activation_context.sem);
// }

// 长按3秒,直接发送关机消息到ui_task
rt_mb_send(g_ui_task_mb, UI_EVENT_SHUTDOWN);
}

else if (action == BUTTON_RELEASED)
{
if (press_tick != 0)
{
rt_tick_t now = rt_tick_get();
if ((now - press_tick) >= rt_tick_from_millisecond(3000))
{
if (g_activation_context.is_activated)
{
rt_sem_release(g_activation_context.sem);
}
else
{
// 长按3秒,直接发送关机消息到ui_task
rt_mb_send(g_ui_task_mb, UI_EVENT_SHUTDOWN);
}
}
}
press_tick = 0;
rt_kprintf("xz_button2_event_handler - released\n");
}
}

Expand Down Expand Up @@ -609,6 +613,23 @@ void xiaozhi_ws_connect(void)
xiaozhi_ui_update_emoji("embarrassed");
return;
}

if (g_activation_context.is_activated)
{
she_bei_ma = 0;
char str_temp[256];
snprintf(str_temp, sizeof(str_temp),
"设备未添加,请前往 xiaozhi.me "
"输入绑定码: \n %s \n ",
g_activation_context.code);
xiaozhi_ui_chat_output(str_temp);
xiaozhi_ui_standby_chat_output(str_temp);//待机界面也显示一份
rt_sem_take(g_activation_context.sem, RT_WAITING_FOREVER);
g_activation_context.is_activated = false;
she_bei_ma = 1;
lv_display_trigger_activity(NULL);

}
// 检查 WebSocket 的 TCP 控制块状态是否为 CLOSED
if (g_xz_ws.clnt.pcb != NULL && g_xz_ws.clnt.pcb->state != CLOSED)
{
Expand Down Expand Up @@ -659,7 +680,9 @@ void xiaozhi_ws_connect(void)
else
{
rt_kprintf("Waiting ws_connect ready%d... \r\n", retry);
xiaozhi_ui_chat_output("小智连接失败!");
rt_thread_mdelay(1000);
ui_swith_to_standby_screen();
}
}
}
Expand Down
Loading