#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <string.h>
#include "lvgl/lvgl.h"
#include "fonts/free_font.h"
#include "main_screen.h"
#include "asr/asr_offline_sample.h" // 引入ASR头文件
#include "time_manager.h" // 引入时间管理头文件
#include "weather/weather_hour.h" // 添加天气客户端头文件
/* LED控制相关宏定义 */
#define TEST_MAGIC 'x'
#define LED_ON 0
#define LED_OFF 1
#define LED1 _IO(TEST_MAGIC, 0)
#define LED2 _IO(TEST_MAGIC, 1)
#define LED3 _IO(TEST_MAGIC, 2)
#define LED4 _IO(TEST_MAGIC, 3)
/* 蜂鸣器控制相关宏定义 - 根据测试代码修改 */
#define BEEP_ON 0
#define BEEP_OFF 1
/* 全局状态变量 */
static int led_state = 0; // 0-关闭 1-开启
static int buzzer_state = 0; // 0-关闭 1-开启
static lv_obj_t *asr_status_label = NULL;
static lv_obj_t *led_switch_obj = NULL; // 全局LED开关对象
static lv_obj_t *buzzer_switch_obj = NULL; // 全局蜂鸣器开关对象
static lv_obj_t *time_label_func = NULL; // 功能界面时间标签
/* 天气数据显示相关变量 */
static lv_obj_t *today_weather_btn = NULL;
static lv_obj_t *weather_img = NULL;
static lv_obj_t *weather_city_label = NULL;
static lv_obj_t *weather_temp_label = NULL;
static lv_obj_t *weather_desc_label = NULL;
static lv_obj_t *weather_humidity_label = NULL;
static lv_obj_t *weather_wind_label = NULL;
/* 函数声明 */
static void led_switch_event_handler(lv_event_t *e);
static void buzzer_switch_event_handler(lv_event_t *e);
static void return_to_login_cb(lv_event_t *e);
static void update_asr_status_timer_cb(lv_timer_t *timer);
static void weather_data_callback(const char* data);
static void create_weather_display(lv_obj_t *parent);
void led_control(int state);
void buzzer_control(int state);
void update_led_switch_state(int state);
void update_buzzer_switch_state(int state);
/* LED开关事件回调 */
static void led_switch_event_handler(lv_event_t *e)
{
lv_obj_t *switch_obj = lv_event_get_target(e);
led_state = lv_obj_has_state(switch_obj, LV_STATE_CHECKED) ? 1 : 0;
led_control(led_state);
}
/* 蜂鸣器开关事件回调 */
static void buzzer_switch_event_handler(lv_event_t *e)
{
lv_obj_t *switch_obj = lv_event_get_target(e);
buzzer_state = lv_obj_has_state(switch_obj, LV_STATE_CHECKED) ? 1 : 0;
buzzer_control(buzzer_state);
}
/* 返回登录界面的回调函数 */
static void return_to_login_cb(lv_event_t *e);
// 定时器回调函数,用于更新语音识别状态显示
static void update_asr_status_timer_cb(lv_timer_t *timer) {
lv_obj_t *label = (lv_obj_t *)timer->user_data;
if (led_state) {
lv_label_set_text(label, "");
} else {
lv_label_set_text(label, "");
}
lv_timer_del(timer); // 只执行一次
}
/* LED控制函数 */
void led_control(int state)
{
printf("LED控制调用: 状态=%d\n", state);
int fd = open("/dev/Led", O_RDWR);
if(fd == -1) {
perror("open:/dev/Led");
return;
}
if(state == 1) {
ioctl(fd, LED1, LED_ON);
ioctl(fd, LED2, LED_ON);
ioctl(fd, LED3, LED_ON);
ioctl(fd, LED4, LED_ON);
printf("所有LED灯亮\n");
// 更新界面显示
if (asr_status_label) {
lv_label_set_text(asr_status_label, "");
}
// 更新开关状态
update_led_switch_state(1);
} else if(state == 0) {
ioctl(fd, LED1, LED_OFF);
ioctl(fd, LED2, LED_OFF);
ioctl(fd, LED3, LED_OFF);
ioctl(fd, LED4, LED_OFF);
printf("所有LED灯灭\n");
// 更新界面显示
if (asr_status_label) {
lv_label_set_text(asr_status_label, "");
}
// 更新开关状态
update_led_switch_state(0);
}
close(fd);
}
/* 蜂鸣器控制函数 - 根据测试代码修改 */
void buzzer_control(int state)
{
printf("蜂鸣器控制调用: 状态=%d\n", state);
int fd = open("/dev/beep", O_RDWR);
if(fd == -1) {
perror("open:/dev/beep");
printf("尝试其他可能的设备名...\n");
// 如果蜂鸣器设备不存在,尝试其他可能的设备名
fd = open("/dev/Beep", O_RDWR);
if(fd == -1) {
perror("open:/dev/Beep");
return;
}
}
if(state == 1) {
// 根据测试代码,第二个参数是命令,第三个参数是值
ioctl(fd, BEEP_ON, 1);
printf("蜂鸣器开启\n");
// 更新开关状态
update_buzzer_switch_state(1);
} else if(state == 0) {
// 根据测试代码,第二个参数是命令,第三个参数是值
ioctl(fd, BEEP_OFF, 1);
printf("蜂鸣器关闭\n");
// 更新开关状态
update_buzzer_switch_state(0);
}
close(fd);
}
/* 更新LED开关状态函数 */
void update_led_switch_state(int state)
{
if (led_switch_obj == NULL) {
printf("LED开关对象未初始化\n");
return;
}
if (state == 1) {
// 打开开关
lv_obj_add_state(led_switch_obj, LV_STATE_CHECKED);
printf("界面LED开关状态已更新: 打开\n");
} else {
// 关闭开关
lv_obj_clear_state(led_switch_obj, LV_STATE_CHECKED);
printf("界面LED开关状态已更新: 关闭\n");
}
// 更新全局LED状态
led_state = state;
}
/* 更新蜂鸣器开关状态函数 */
void update_buzzer_switch_state(int state)
{
if (buzzer_switch_obj == NULL) {
printf("蜂鸣器开关对象未初始化\n");
return;
}
if (state == 1) {
// 打开开关
lv_obj_add_state(buzzer_switch_obj, LV_STATE_CHECKED);
printf("界面蜂鸣器开关状态已更新: 打开\n");
} else {
// 关闭开关
lv_obj_clear_state(buzzer_switch_obj, LV_STATE_CHECKED);
printf("界面蜂鸣器开关状态已更新: 关闭\n");
}
// 更新全局蜂鸣器状态
buzzer_state = state;
}
/* 天气数据回调函数 */
static void weather_data_callback(const char* data) {
printf("收到天气数据回调: %s\n", data);
update_weather_display(data);
}
/* 解析天气数据并更新显示 */
void update_weather_display(const char* weather_data) {
if (today_weather_btn == NULL) {
printf("天气按钮未初始化,无法更新天气显示\n");
return;
}
printf("正在解析天气数据: %s\n", weather_data);
// 解析天气数据(假设格式为:城市|温度|天气描述|湿度|风速)
char city[50] = "未知城市";
char temp[20] = "--°C";
char desc[50] = "未知天气";
char humidity[20] = "湿度: --%";
char wind[30] = "风速: --m/s";
// 简单的解析逻辑,根据实际数据格式调整
char data_copy[512];
strncpy(data_copy, weather_data, sizeof(data_copy)-1);
data_copy[sizeof(data_copy)-1] = '\0';
char *token = strtok(data_copy, "|");
if (token) {
strncpy(city, token, sizeof(city)-1);
city[sizeof(city)-1] = '\0';
token = strtok(NULL, "|");
if (token) {
strncpy(temp, token, sizeof(temp)-1);
temp[sizeof(temp)-1] = '\0';
token = strtok(NULL, "|");
if (token) {
strncpy(desc, token, sizeof(desc)-1);
desc[sizeof(desc)-1] = '\0';
token = strtok(NULL, "|");
if (token) {
snprintf(humidity, sizeof(humidity), "湿度: %s%%", token);
token = strtok(NULL, "|");
if (token) {
snprintf(wind, sizeof(wind), "风速: %sm/s", token);
}
}
}
}
}
// 更新天气显示
if (weather_city_label) {
lv_label_set_text(weather_city_label, city);
}
if (weather_temp_label) {
lv_label_set_text(weather_temp_label, temp);
}
if (weather_desc_label) {
lv_label_set_text(weather_desc_label, desc);
}
if (weather_humidity_label) {
lv_label_set_text(weather_humidity_label, humidity);
}
if (weather_wind_label) {
lv_label_set_text(weather_wind_label, wind);
}
printf("天气显示已更新: %s %s %s %s %s\n", city, temp, desc, humidity, wind);
}
/* 创建天气显示组件 */
static void create_weather_display(lv_obj_t *parent) {
// 创建天气图片
weather_img = lv_img_create(parent);
lv_img_set_src(weather_img, "A:/imgs/today.png");
lv_obj_set_size(weather_img, 300, 380);
lv_obj_align(weather_img, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_style_radius(weather_img, 20, 0);
lv_obj_set_style_clip_corner(weather_img, true, 0);
// 创建城市名称标签
weather_city_label = lv_label_create(parent);
lv_label_set_text(weather_city_label, "加载中...");
lv_obj_add_style(weather_city_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(weather_city_label, lv_color_hex(0x000000), 0);
lv_obj_align(weather_city_label, LV_ALIGN_TOP_MID, 0, 30);
// 创建温度标签
weather_temp_label = lv_label_create(parent);
lv_label_set_text(weather_temp_label, "--°C");
lv_obj_add_style(weather_temp_label, FontStyleInit3(), 0);
lv_obj_set_style_text_color(weather_temp_label, lv_color_hex(0xFF4500), 0);
lv_obj_align(weather_temp_label, LV_ALIGN_CENTER, 0, -40);
// 创建天气描述标签
weather_desc_label = lv_label_create(parent);
lv_label_set_text(weather_desc_label, "未知天气");
lv_obj_add_style(weather_desc_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(weather_desc_label, lv_color_hex(0x000000), 0);
lv_obj_align(weather_desc_label, LV_ALIGN_CENTER, 0, 0);
// 创建湿度标签
weather_humidity_label = lv_label_create(parent);
lv_label_set_text(weather_humidity_label, "湿度: --%");
lv_obj_add_style(weather_humidity_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(weather_humidity_label, lv_color_hex(0x000000), 0);
lv_obj_align(weather_humidity_label, LV_ALIGN_CENTER, 0, 40);
// 创建风速标签
weather_wind_label = lv_label_create(parent);
lv_label_set_text(weather_wind_label, "风速: --m/s");
lv_obj_add_style(weather_wind_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(weather_wind_label, lv_color_hex(0x000000), 0);
lv_obj_align(weather_wind_label, LV_ALIGN_CENTER, 0, 70);
}
/* 创建功能界面 */
void function_screen()
{
// 创建功能界面屏幕
lv_obj_t *func_scr = lv_obj_create(NULL);
lv_scr_load(func_scr);
// 功能界面背景
lv_obj_t *bg = lv_img_create(func_scr);
lv_img_set_src(bg, "A:/imgs/weather_bg2.png");
lv_obj_set_size(bg, 800, 480);
// 时间显示标签 - 右上角
time_label_func = lv_label_create(func_scr);
lv_label_set_text(time_label_func, get_current_time_string());
lv_obj_add_style(time_label_func, FontStyleInit(), 0);
lv_obj_set_style_text_color(time_label_func, lv_color_hex(0x000000), 0);
lv_obj_align(time_label_func, LV_ALIGN_TOP_RIGHT, -20, 20);
// 设置功能界面时间标签
set_func_time_label(time_label_func);
// 语音识别状态提示
asr_status_label = lv_label_create(func_scr);
lv_label_set_text(asr_status_label, "");
lv_obj_add_style(asr_status_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(asr_status_label, lv_color_hex(0x000000), 0);
lv_obj_align(asr_status_label, LV_ALIGN_CENTER, 0, -60);
// LED控制标签
lv_obj_t *led_label = lv_label_create(func_scr);
lv_label_set_text(led_label, "LED控制");
lv_obj_add_style(led_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(led_label, lv_color_hex(0x000000), 0);
lv_obj_align(led_label, LV_ALIGN_CENTER, -100, 60);
// 创建LED开关
led_switch_obj = lv_switch_create(func_scr); // 赋值给全局变量
lv_obj_align(led_switch_obj, LV_ALIGN_CENTER, -100, 90);
lv_obj_add_event_cb(led_switch_obj, led_switch_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_set_style_bg_color(led_switch_obj, lv_color_hex(0x666666), LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(led_switch_obj, lv_color_hex(0x00BB00), LV_STATE_CHECKED);
// 蜂鸣器控制标签
lv_obj_t *buzzer_label = lv_label_create(func_scr);
lv_label_set_text(buzzer_label, "蜂鸣器控制");
lv_obj_add_style(buzzer_label, FontStyleInit(), 0);
lv_obj_set_style_text_color(buzzer_label, lv_color_hex(0x000000), 0);
lv_obj_align(buzzer_label, LV_ALIGN_CENTER, 100, 60);
// 创建蜂鸣器开关
buzzer_switch_obj = lv_switch_create(func_scr); // 赋值给全局变量
lv_obj_align(buzzer_switch_obj, LV_ALIGN_CENTER, 100, 90);
lv_obj_add_event_cb(buzzer_switch_obj, buzzer_switch_event_handler, LV_EVENT_VALUE_CHANGED, NULL);
lv_obj_set_style_bg_color(buzzer_switch_obj, lv_color_hex(0x666666), LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(buzzer_switch_obj, lv_color_hex(0x00BB00), LV_STATE_CHECKED);
// 根据当前状态设置开关初始状态
if (led_state == 1) {
lv_obj_add_state(led_switch_obj, LV_STATE_CHECKED);
} else {
lv_obj_clear_state(led_switch_obj, LV_STATE_CHECKED);
}
if (buzzer_state == 1) {
lv_obj_add_state(buzzer_switch_obj, LV_STATE_CHECKED);
} else {
lv_obj_clear_state(buzzer_switch_obj, LV_STATE_CHECKED);
}
// 返回登录界面按钮
lv_obj_t *return_btn = lv_btn_create(func_scr);
lv_obj_set_size(return_btn, 150, 50);
lv_obj_align(return_btn, LV_ALIGN_BOTTOM_MID, 0, -30);
lv_obj_set_style_radius(return_btn, 10, 0);
lv_obj_t *return_label = lv_label_create(return_btn);
lv_label_set_text(return_label, "返回登录");
lv_obj_center(return_label);
lv_obj_add_style(return_label, FontStyleInit(), 0);
// 添加返回事件处理
lv_obj_add_event_cb(return_btn, return_to_login_cb, LV_EVENT_CLICKED, NULL);
// 创建天气显示按钮
today_weather_btn = lv_btn_create(func_scr);
lv_obj_set_pos(today_weather_btn, 20, 20);
lv_obj_set_size(today_weather_btn, 300, 380);
lv_obj_set_style_radius(today_weather_btn, 20, 0);
lv_obj_set_style_bg_color(today_weather_btn, lv_color_white(), 0);
lv_obj_set_style_bg_opa(today_weather_btn, LV_OPA_COVER, 0);
// 创建天气显示组件
create_weather_display(today_weather_btn);
printf("检查语音识别文件...\n");
if (access("/usrCode/asr/common.jet", F_OK) != 0) {
printf("警告: 语音识别资源文件不存在\n");
lv_label_set_text(asr_status_label, "");
} else if (access("/usrCode/asr/call.bnf", F_OK) != 0) {
printf("警告: 语法文件不存在\n");
lv_label_set_text(asr_status_label, "");
} else if (access("/usrCode/asr/GrmBuild", F_OK) != 0) {
printf("创建语法构建目录\n");
system("mkdir -p /usrCode/asr/GrmBuild");
} else {
printf("语音识别文件检查通过\n");
}
// 检查录音设备
if (access("/dev/dsp", F_OK) != 0 && access("/dev/audio", F_OK) != 0) {
printf("警告: 未找到录音设备\n");
lv_label_set_text(asr_status_label, "");
} else {
printf("录音设备检查通过\n");
// 启动语音识别功能
start_asr();
// 延迟更新状态显示(2秒后)
lv_timer_create(update_asr_status_timer_cb, 2000, asr_status_label);
}
// 初始化天气客户端
printf("初始化天气客户端...\n");
if (weather_client_init(weather_data_callback) == 0) {
printf("天气客户端初始化成功\n");
// 发送初始天气请求
lv_timer_t *weather_timer = lv_timer_create([](lv_timer_t *timer) {
send_weather_request("get_weather");
lv_timer_del(timer);
}, 1000, NULL);
} else {
printf("天气客户端初始化失败\n");
// 显示默认天气信息
update_weather_display("北京|25°C|晴|45|3.2");
}
}
/* 返回登录界面的回调函数 */
static void return_to_login_cb(lv_event_t *e) {
// 停止语音识别
stop_asr();
// 关闭天气客户端
weather_client_close();
// 关闭LED灯和蜂鸣器再返回登录界面
led_control(0);
buzzer_control(0);
led_state = 0;
buzzer_state = 0;
// 重置开关对象指针
led_switch_obj = NULL;
buzzer_switch_obj = NULL;
// 清理天气显示对象
today_weather_btn = NULL;
weather_img = NULL;
weather_city_label = NULL;
weather_temp_label = NULL;
weather_desc_label = NULL;
weather_humidity_label = NULL;
weather_wind_label = NULL;
// 清理时间标签引用
cleanup_time_labels();
// 显示登录界面
show_login_screen();
}
/* 清理功能界面资源 */
void cleanup_function_screen()
{
// 停止语音识别
stop_asr();
// 关闭天气客户端
weather_client_close();
// 清理时关闭LED和蜂鸣器
led_control(0);
buzzer_control(0);
led_state = 0;
buzzer_state = 0;
// 重置开关对象指针
led_switch_obj = NULL;
buzzer_switch_obj = NULL;
// 清理天气显示对象
today_weather_btn = NULL;
weather_img = NULL;
weather_city_label = NULL;
weather_temp_label = NULL;
weather_desc_label = NULL;
weather_humidity_label = NULL;
weather_wind_label = NULL;
// 清理时间标签引用
cleanup_time_labels();
}
修改报错代码:luo@virtual:/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master$ make -j64
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c: In function ‘weather_data_callback’:
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:213:5: warning: implicit declaration of function ‘update_weather_display’ [-Wimplicit-function-declaration]
update_weather_display(data);
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c: At top level:
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:217:6: warning: conflicting types for ‘update_weather_display’
void update_weather_display(const char* weather_data) {
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:213:5: note: previous implicit declaration of ‘update_weather_display’ was here
update_weather_display(data);
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c: In function ‘function_screen’:
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:461:53: error: expected expression before ‘[’ token
lv_timer_t *weather_timer = lv_timer_create([](lv_timer_t *timer) {
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:461:75: error: expected ‘)’ before ‘{’ token
lv_timer_t *weather_timer = lv_timer_create([](lv_timer_t *timer) {
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:461:37: error: too few arguments to function ‘lv_timer_create’
lv_timer_t *weather_timer = lv_timer_create([](lv_timer_t *timer) {
^
In file included from /mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/lvgl/lvgl.h:26:0,
from /mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:7:
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/lvgl/src/misc/lv_timer.h:106:14: note: declared here
lv_timer_t * lv_timer_create(lv_timer_cb_t timer_xcb, uint32_t period, void * u
^
/mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.c:461:21: warning: unused variable ‘weather_timer’ [-Wunused-variable]
lv_timer_t *weather_timer = lv_timer_create([](lv_timer_t *timer) {
^
make: *** [Makefile:47: /mnt/hgfs/lv_code/lv_port_linux_frame_buffer-master/usrCode/func_screen.o] Error 1
最新发布