回调函数加消息队列的使用

OS_Thread_t motor_angel_thread;     //消息队列线程
OS_Queue_t  motor_queue;               //消息队列

/**
• Function: // motor_angel_pth
• Description: // 消息队列接收
• Called By: // 调用本函数的函数清单
• Input: // void *param
• Output: // 对输出参数的说明
• Return: // 函数返回值的说明
**/
void motor_angel_pth(void *param)
{
	event_obj_t msg;
    int ret;
	while(1)
	{
		memset(&msg, 0x0, sizeof(msg));
		ret = OS_QueueReceive(&q_motor_pir_t.motor_queue, &msg, 200);

		if ((ret == 0) && (msg.event_cb)) {
			ret = msg.event_cb(msg.args, msg.msg_type);
			// printf();
		}
	}
}

/**
• Function: // motor_angel
• Description: // 初始化消息队列及队列接收线程
• Called By: // 调用本函数的函数清单
• Input: // void 
• Output: // 对输出参数的说明
• Return: // success return 0 erro return -1
**/
int motor_angel(void)
{
	printf("motor_angel\r\n");
	int ret;

	ret = OS_QueueCreate(&q_motor_pir_t.motor_queue, 30, sizeof(event_obj_t));
	if (ret != 0) {
		printf("Create queue err:%d\n", ret);
        return -1;
	}

	if((ret = OS_ThreadCreate(&q_motor_pir_t.motor_angel_thread, "motor_angel_pth", 
									motor_angel_pth, NULL, 1, 2048)) < 0) {                                        
        printf("Create motor_angel_pth err:%d\n", ret);
        return -1;
    }
		
	return 0;
} 

/**
• Function: // event_push_event_to_queue
• Description: // 消息队列发送
• Called By: // 调用本函数的函数清单
• Input: // type:消息类型 cb:回调函数 arg:NULL
• Output: // 对输出参数的说明
• Return: // success return 0
**/
int event_push_event_to_queue(int type, pfunc_event_cb_ex cb, void *arg)
{
	int ret = 0;
	event_obj_t msg;
	msg.args = arg;
	msg.event_cb = cb;
	msg.msg_type = type;

	ret = OS_QueueSend(&q_motor_pir_t.motor_queue, &msg, 0);
	
	return ret;
}

/*.h
typedef int (*pfunc_event_cb_ex)(void *args, int type);

typedef struct __event_obj{
    int msg_type;
    pfunc_event_cb_ex event_cb;
    void *args;
} event_obj_t;
触发回调函数后 使用event_push_event_to_queue 向消息队列发送  收到后进行相应处理
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quieeeet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值