z-stack中的voidHalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period)函数的深度解析
扫描二维码
随时随地手机看文章
z-stack中的voidHalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period)函数的深度解析 :
参数:
Leds: 哪个led;
numBlinks: 闪烁次数
percent: 闪烁的占空比(就是高电平持续的时间)
period: 一个闪烁的周期多少
///////////////////////////////////////////////////////////////////////////////
HalLedBlink函数原型------
第一段代码:
void HalLedBlink (uint8 leds, uint8numBlinks, uint8 percent, uint16 period)
{
#if (defined (BLINK_LEDS)) &&(HAL_LED == TRUE)
uint8 led;
HalLedControl_t *sts;
if(leds && percent && period)
{
if (percent < 100)
{
led = HAL_LED_1;
leds &= HAL_LED_ALL;
sts = HalLedStatusControl.HalLedControlTable;
while (leds) //循环通过led <<= 1 找出需要闪烁的灯;sts++相对应的状态
{
if (leds & led) //找出我们设置闪烁的灯后满足条件进入设置对应的参数,这里涉及两个结构体
//HalLedControl_t和HalLedStatus_t,里面分别定义了相应的属性(请看其定义这里不列出);
// HalLedControlTable这个是HalLedControl_t结构体变量,用来存放控制灯模式的表,
//这个在HalLedStatus_t结构体里面定义,其实就相当一个结构体数组,
{
/* Store the current state of the led before going to blinking if notalready blinking */
if(sts->mode < HAL_LED_MODE_BLINK )
preBlinkState |= (led &HalLedState);
sts->mode =HAL_LED_MODE_OFF; /*Stop previous blink */
sts->time = period; /* Time for oneon/off cycle */
sts->onPct = percent; /* % of cycle LED ison */
sts->todo = numBlinks; /* Number of blinkcycles */
if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */
sts->next = osal_GetSystemClock(); /* Start now */
sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */
leds ^= led;
}
led <<= 1;
sts++;
}
// Cancel any overlapping timer for blink events
osal_stop_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT);
osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT); //设置led闪烁事件,通过这个事件对灯进行控制,跳到第二段程序执行
}
else
{
HalLedSet (leds, HAL_LED_MODE_ON); /* >= 100%, turn on */
}
}
else
{
HalLedSet (leds, HAL_LED_MODE_OFF); /* No on time, turn off */
}
#elif (HAL_LED == TRUE)
percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON;
HalLedOnOff (leds, percent); /* Toggle */
#else
//HAL LED is disabled, suppress unused argument warnings
(void) leds;
(void) numBlinks;
(void) percent;
(void) period;
#endif /* BLINK_LEDS && HAL_LED */
}
////////////////////////////////////////////////////////
第二段代码:
if ( events & HAL_LED_BLINK_EVENT )
{
#if (defined (BLINK_LEDS)) &&(HAL_LED == TRUE)
HalLedUpdate(); //如果定义了BLINK_LEDS和HAL_LED == TRUE就执行此句,下面继续看第三段代码
#endif /* BLINK_LEDS && HAL_LED */
return events ^ HAL_LED_BLINK_EVENT;
}
/////////////////////////////////////////////////////////
第三段代码:
void HalLedUpdate (void)
{
……
……
while (leds)
{
if (leds & led)
{
if (sts->mode & HAL_LED_MODE_BLINK)
{
time = osal_GetSystemClock();
if (time >= sts->next)
{
if (sts->mode & HAL_LED_MODE_ON)
{
pct = 100 - sts->onPct; /* Percentage of cycle for off */
sts->mode &=~HAL_LED_MODE_ON; /* Say it's not on */
HalLedOnOff (led,HAL_LED_MODE_OFF); /* Turn it off */ //重点来了,这个就是操作led电平函数,继续看第四段代码
//由于这个函数太多没有完全列出来,也没分析完我们就看重点
if (!(sts->mode &HAL_LED_MODE_FLASH))
{
sts->todo--; /* Not continuous,reduce count */
}
}
else if ( (!sts->todo) && !(sts->mode &HAL_LED_MODE_FLASH) )
{
sts->mode ^=HAL_LED_MODE_BLINK; /* No moreblinks */
}
else
{
pct = sts->onPct; /* Percentage of cycle foron */
sts->mode |= HAL_LED_MODE_ON; /* Say it's on */
HalLedOnOff (led,HAL_LED_MODE_ON); /* Turn it on */
}
……
……
///////////////////////////////////
第四段代码
void HalLedOnOff (uint8 leds, uint8 mode)
{
if(leds & HAL_LED_1)
{
if (mode == HAL_LED_MODE_ON)
{
HAL_TURN_ON_LED1(); //继续追踪下去,就找到对应的管脚,这里就不多说了,先介绍到这自己搞定后面的
}
else
{
HAL_TURN_OFF_LED1();
}
}
……
……
}
本人,刚写博客,这是我学习心得,有过之处望指出