RT-Thread 20. 事件集

发布时间:2024年01月24日
1. 代码
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"

#define THREAD_PRIORITY 28
#define THREAD_TIMESLICE 10
#define EVENT_FLAG3 (1 << 3)
#define EVENT_FLAG5 (1 << 5)

#define LED4_PIN GET_PIN(B, 4)


/* 事 件 控 制 块 */
static struct rt_event event;
ALIGN(RT_ALIGN_SIZE)
static char thread31_stack[500];
static struct rt_thread thread31;
/* 线 程 1 入 口 函 数 */
static void thread31_recv_event(void *param)
{
  rt_uint32_t e;
  
  while(1)
  {
    if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5),
                      RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
                      RT_WAITING_FOREVER, &e) == RT_EOK)
    {
      rt_kprintf("thread1: recv event 0x%x\n", e);
      rt_pin_write(LED4_PIN, PIN_HIGH);
      rt_thread_mdelay(100);
      rt_pin_write(LED4_PIN, PIN_LOW);
    }
  }
}

ALIGN(RT_ALIGN_SIZE)
static char thread32_stack[500];
static struct rt_thread thread32;
/* 线 程 2 入 口 */
static void thread32_send_event(void *param)
{
  while(1)
  {
    rt_kprintf("thread2: send event3\n");
    rt_event_send(&event, EVENT_FLAG3);
    rt_thread_mdelay(8000);
  }
}

void EventKeyScanCallBack(void)
{
  rt_event_send(&event, EVENT_FLAG5);
}

int EventTaskInit(void)
{
  rt_err_t result;
  /* 初 始 化 事 件 对 象 */
  result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO);
  if(result != RT_EOK)
  {
    rt_kprintf("init event failed.\n");
    return -1;
  }
  rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
  rt_thread_init(&thread31,
                "thread31",
                thread31_recv_event,
                RT_NULL,
                &thread31_stack[0],
                sizeof(thread31_stack),
                THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  rt_thread_startup(&thread31);
  rt_thread_init(&thread32,
                "thread32",
                thread32_send_event,
                RT_NULL,
                &thread32_stack[0],
                sizeof(thread32_stack),
                THREAD_PRIORITY, THREAD_TIMESLICE);
  rt_thread_startup(&thread32);
  return 0;
}
2.输出
key press val is 1

thread2: send event3
thread1: recv event 0x28

在这里插入图片描述

文章来源:https://blog.csdn.net/lljss1980/article/details/135817138
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。