pkuzhx 发表于 2016-12-7 16:38:11

source例程有一段代码完全不理解

本帖最后由 pkuzhx 于 2016-12-7 16:41 编辑

在source_states.c中,有一个函数states_set_state,按字面和注释理解,应该实现的功能是进入一个新的state,代码如下。
/****************************************************************************
NAME   
    states_set_state - Sets the new application state
*/
void states_set_state(SOURCE_STATE_T new_state)
{
    if (new_state < SOURCE_STATES_MAX)
    {
      SOURCE_STATE_T old_state = theSource->app_data.app_state;
      
      /* leaving current state */
      states_exit_state(new_state);
      
      /* store new state */
      theSource->app_data.app_state = new_state;
      STATES_DEBUG(("STATE: new state [%s]\n", state_strings));
      
      /* entered new state */
      states_enter_state(old_state);               
      
      /* fudge states reported to Host, so that IDLE state is converted to a known state */
      if (new_state == SOURCE_STATE_IDLE)
      {
            theSource->app_data.pre_idle_state = old_state;
            new_state = theSource->app_data.pre_idle_state;
      }   
      if (old_state == SOURCE_STATE_IDLE)
      {
            old_state = theSource->app_data.pre_idle_state;
      }
      if (old_state != new_state)
      {
            /* send new state via Vendor USB command */
            usb_send_vendor_state();

#ifdef INCLUDE_LEDS            
            /* update LED state indication */
            leds_show_state(new_state);
#endif /* INCLUDE_LEDS */            
      }
    }
}我的理解是,应该先退出旧的状态,再进入新的状态,即:      /* leaving current state */
      states_exit_state(old_state);
      
      /* entered new state */
      states_enter_state(new_state);
可是例程中确实完全相反的,它是先退出新状态,然后又进入了旧的状态,我完全不能理解。

但是例程不可能是错的啊,谁能告诉我,我哪里理解错了?






luckcee 发表于 2016-12-7 16:38:12

pkuzhx 发表于 2016-12-7 16:52
好吧,是我自己想当然了states_enter_state并不是进入参数中的state,而是读取source state后,进入这个sta ...
对,就是这样啦,进入static void states_exit_state(SOURCE_STATE_T new_state)
{
    switch (states_get_state())
    {
      case SOURCE_STATE_INITIALISING:
      {
            states_exit_state_initialising();
      }
      break;

之后,switch里面调用的函数是 states_get_state()
不是使用开头的的参数;

pkuzhx 发表于 2016-12-7 16:42:10

论坛的代码功能<>好像有时会有bug,修改了好几次才搞好。

pkuzhx 发表于 2016-12-7 16:52:39

好吧,是我自己想当然了states_enter_state并不是进入参数中的state,而是读取source state后,进入这个state

pkuzhx 发表于 2016-12-8 08:49:58

luckcee 发表于 2016-12-7 18:06
对,就是这样啦,进入

之后,switch里面调用的函数是


嗯,多谢。刚开始确实想当然了

co9 发表于 2016-12-8 08:58:55

传入的参数不是用来改变状态的

pkuzhx 发表于 2016-12-8 09:00:28

co9 发表于 2016-12-8 08:58
传入的参数不是用来改变状态的

是的,刚开始没有看函数实现,按照字面去理解了

co9 发表于 2016-12-8 09:25:45

函数名和参数看起来确实有很大的歧义

liuyong1351 发表于 2016-12-9 12:15:38

:lol,学习了!

dalang365 发表于 2017-5-9 11:30:19

了解一下.
页: [1]
查看完整版本: source例程有一段代码完全不理解