Files
fusion/mw/has_task_msg_manager/msg_example.c
2025-12-29 14:32:03 +08:00

114 lines
4.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
#include <stdint.h>
#include "has_task_msg.h"
/* -------------------------- gui.c -------------------------- */
int gui_handle_cb(unsigned char module_id, const unsigned char *buf, unsigned int len)
{
switch (module_id)
{
case GUI: // 允许自己给自己发消息
printf("GUI 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
break;
case SENSOR:
printf("GUI 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
break;
case ACM:
printf("GUI 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
break;
case WIFI:
printf("GUI 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
break;
case VOICE:
printf("GUI 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
break;
default:
break;
}
}
void *gui_function(void *arg)
{
uint8_t gui_publish_buffer1[5] = {1, 2, 3, 4, 5}; // GUI发布的数据1
uint8_t gui_publish_buffer2[5] = {2, 7, 8, 9, 10}; // GUI发布的数据2
uint8_t gui_publish_buffer3[5] = {3, 12, 13, 14, 15}; // GUI发布的数据3
has_msg_publish(GUI, gui_publish_buffer1, sizeof(gui_publish_buffer1)); // GUI发布消息
has_msg_publish(GUI, gui_publish_buffer2, sizeof(gui_publish_buffer2)); // GUI发布第二条消息
has_msg_publish(GUI, gui_publish_buffer3, sizeof(gui_publish_buffer3)); // GUI发布第三条消息
while (1)
{
usleep(500 * 1000);
has_msg_handle(GUI, gui_handle_cb); // 处理一条GUI收到的消息先入先出
}
}
/* -------------------------- sensor.c -------------------------- */
int sensor_handle_cb(unsigned char module_id, const unsigned char *buf, unsigned int len)
{
printf("SENSOR 正在处理模块 %d 的消息 消息长度:%d 数据[0]:%x\n", module_id, len, buf[0]);
}
void *sensor_function(void *arg)
{
const has_module_ID_e moude_ID = SENSOR;
uint8_t sensor_publish_buffer1[5] = {1, 4, 3, 2, 1}; // SENSOR发布的数据
uint8_t sensor_publish_buffer2[5] = {2, 9, 8, 7, 6}; // SENSOR发布的数据
has_msg_publish(moude_ID, sensor_publish_buffer1, sizeof(sensor_publish_buffer1)); // SENSOR发布消息
has_msg_publish(moude_ID, sensor_publish_buffer2, sizeof(sensor_publish_buffer2)); // SENSOR发布第二条消息
while (1)
{
usleep(500 * 1000);
has_msg_handle(moude_ID, sensor_handle_cb); // 处理一条SENSOR收到的消息先入先出
has_msg_handle_latest(moude_ID, sensor_handle_cb); // 处理一条SENSOR最新收到的消息
if (has_msg_is_message_empty(moude_ID) != 0) // 判断是否还有消息未处理
{
printf("模块:%d 还有%d条消息待处理\n", moude_ID, has_msg_get_message_number(moude_ID));
}
has_msg_handle_by_module(moude_ID, sensor_handle_cb, GUI); // 处理一条SENSOR收到来自GUI的消息先入先出不处理其他模块发布的消息
has_msg_delete_all_message(moude_ID); // 删除SENSOR收到但还未处理的所有消息
}
}
int main(int argc, char const *argv[])
{
pthread_t threads1;
pthread_t threads2;
int result;
has_module_ID_e moude_ID; // 每个模块有属于自己的ID号
printf("hello\n");
has_msg_init(); // 消息初始化
/* -------------------------- gui -------------------------- */
result = pthread_create(&threads1, NULL, gui_function, NULL);
if (result != 0)
{
perror("创建线程失败");
return EXIT_FAILURE;
}
/* ---------------------------------------------------- */
/* -------------------------- sensor -------------------------- */
result = pthread_create(&threads2, NULL, sensor_function, NULL);
if (result != 0)
{
perror("创建线程失败");
return EXIT_FAILURE;
}
/* ---------------------------------------------------- */
while (1)
{
sleep(5);
}
return EXIT_SUCCESS;
}