本帖最后由 wangpengcheng 于 2016-3-25 23:07 编辑
其中应用了MQX中的事件!闲话没有,立马就来!
首先定义如下一个结构体:
typedef struct
{
uint32_t on_time; //Bell响的时间长度
uint32_t off_time; //Bell不响的时间长度
uint32_t times; //周期循环次数
uint32_t wait_time; //每个周期之间的间隔时间
}STRUCT_BELL_PARA, *pSTRUCT_BELL_PARA;
然后用这个结构体定义一个结构体常量数组:
static const STRUCT_BELL_PARA bell_event_para[] =
{
{150, 30, 1, 1000},
{150, 30, 3, 1000},
{150, 30, 1, 1000},
{150, 150, 5, 1000},
{0, 0, 0, 0},
};
这是用来区分事件的,假如我们现在有四个事件如下:
typedef enum
{
BELL_KEY_EVENT, //按键事件
BELL_ERROR_EVENT, //错误事件
BELL_OK_EVENT, //OK事件
BELL_POWER_OFF_EVENT, //关机事件
BELL_EVENT_NUMBER,
}ENUM_BELL_EVENT_INDEX;
#define BELL_EVENT_ALL ((1 << BELL_KEY_EVENT) | (1 << BELL_ERROR_EVENT) | (1 << BELL_OK_EVENT) | (1 << BELL_POWER_OFF_EVENT))
然后上面四个事件分别对应着结构体常量数组中的常量。也就是说每个事件蜂鸣器的叫法不一样!
好了,现在我们应该做个BELL模块,如下:
typedef struct _bell *pSTRUCT_BELL;
typedef struct _bell
{
void *para;
void (*release)(pSTRUCT_BELL me);
_task_id (*task_create)(pSTRUCT_BELL me, TASK_TEMPLATE_STRUCT_PTR template_list, _mqx_uint template_index);
uint32_t (*set_on)(pSTRUCT_BELL me, uint32_t event_index);
}STRUCT_BELL;
从上面能看出来Bell模块里面包含三个函数以及一个参数入口变量,我们分别把这几个函数及变量补充起来:
//私有变量
typedef struct
{
pSTRUCT_GPIO bell_port;
char thread_name[MAX_NAME_SIZE];
LWEVENT_STRUCT bell_event;
}STRUCT_PRIVATE, *pSTRUCT_PRIVATE;
//蜂鸣器事件发送函数
static uint32_t set_on(pSTRUCT_BELL me, uint32_t event_index)
{
pSTRUCT_PRIVATE para = me->para;
LWEVENT_STRUCT_PTR bell_event = ¶->bell_event;
if(event_index < BELL_EVENT_NUMBER)
{
_lwevent_set(bell_event, 1 << event_index);
}
return MQX_OK;
}
//任务
void bell_task(uint32_t task_para)
{
pSTRUCT_BELL me = (pSTRUCT_BELL)task_para;
pSTRUCT_PRIVATE para = me->para;
pSTRUCT_GPIO bell_port = para->bell_port;
LWEVENT_STRUCT_PTR bell_event = ¶->bell_event;
pSTRUCT_BELL_PARA bell_para;
uint32_t event_bit, times;
bell_port->init(bell_port, LWGPIO_VALUE_LOW);
_lwevent_create(bell_event, LWEVENT_AUTO_CLEAR);
while(1)
{
if(MQX_OK == _lwevent_wait_for(bell_event, BELL_EVENT_ALL, FALSE, NULL))
{
event_bit = _lwevent_get_signalled();
do
{
if(event_bit & (1 << BELL_KEY_EVENT)){ bell_para = (pSTRUCT_BELL_PARA)&bell_event_para[BELL_KEY_EVENT];break; }
if(event_bit & (1 << BELL_ERROR_EVENT)){ bell_para = (pSTRUCT_BELL_PARA)&bell_event_para[BELL_ERROR_EVENT];break; }
if(event_bit & (1 << BELL_DOWN_OK_EVENT)){ bell_para = (pSTRUCT_BELL_PARA)&bell_event_para[BELL_DOWN_OK_EVENT];break;}
if(event_bit & (1 << BELL_POWER_OFF_EVENT)){ bell_para = (pSTRUCT_BELL_PARA)&bell_event_para[BELL_POWER_OFF_EVENT];break; }
} while (0);
}
for(times = 0; times < bell_para->times; times++)
{
bell_port->write(bell_port, BELL_ON);
_time_delay(bell_para->on_time);
bell_port->write(bell_port, BELL_OFF);
_time_delay(bell_para->off_time);
}
}
}
//任务创建,此处第二个以及第三个参数来自MQX的任务模板表
static _task_id task_create(pSTRUCT_BELL me, TASK_TEMPLATE_STRUCT_PTR template_list, _mqx_uint template_index)
{
pSTRUCT_PRIVATE para = me->para;
uint32_t task_index;
_task_id task_id;
for(task_index = 0; template_list[task_index].TASK_TEMPLATE_INDEX != 0; task_index++)
{
if(template_list[task_index].TASK_TEMPLATE_INDEX == template_index)
{
break;
}
}
assert(template_list[task_index].TASK_TEMPLATE_INDEX != 0);
task_id = _task_get_id_from_name(template_list[task_index].TASK_NAME);
if(task_id == MQX_NULL_TASK_ID)
{
task_id = _task_create(0, template_index, (uint32_t)me);
assert(task_id != MQX_NULL_TASK_ID);
strcpy(para->thread_name, template_list[task_index].TASK_NAME);
return task_id;
}
return MQX_NULL_TASK_ID;
}
//模块释放
static void release(pSTRUCT_BELL me)
{
pSTRUCT_PRIVATE para = me->para;
_task_id task_id = _task_get_id_from_name(para->thread_name);
if(task_id != MQX_NULL_TASK_ID)
{
_task_destroy(task_id);
}
_lwevent_destroy(¶->bell_event);
para->bell_port->release(para->bell_port);
_mem_free(para);
_mem_free(me);
}
//模块创建,此处应用了pSTRUCT_GPIO_ITEM模块,该模块参见附件
pSTRUCT_BELL bell_create(pSTRUCT_GPIO_ITEM bell_port_group, uint32_t pin_number)
{
pSTRUCT_BELL me = _mem_alloc_system_zero(sizeof(STRUCT_BELL));
assert(me != NULL);
pSTRUCT_PRIVATE para = me->para = _mem_alloc_system_zero(sizeof(STRUCT_PRIVATE));
assert(para != NULL);
para->bell_port = gpio_create(pin_number, bell_port_group, LWGPIO_DIR_OUTPUT);
assert(para->bell_port != NULL);
me->release = release;
me->set_on = set_on;
me->task_create = task_create;
return me;
}
以上的代码就完成了BELL模块,应用起来也就比较简单,先用 bell_create函数创建一个Bell模块,创建完成后使用模块中的task_create 创建Bell任务,最后用模块中的set_on 使任务中的事件进行触发!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>