This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE break loop on new command received

Hello, 

I am creating my first bluetooth application and am having difficulties figuring out the interrupts. The application is working and is very simple - please see pseudo code example below

case byte received = 0x01 execute loop1 

case byte received = 0x02 execute loop2 

case byte received = 0x03 execute loop3

ect .... 

What I would like to do is keep the selected loop going and break on new command. So, if 0x01 is received loop1 will continue until until a new command is received. Everything i've tried thus far cannot break the loop and device stops accepting new commands because the processor isn't correctly checking to see if a new command is available within the loop. Does anyone have any suggestions or example that could be used as reference? 

If I set a short execution time on each loop and allow it to finish then I can send a new command but I would like to have the currently selected loop keep going until a new command is received. Any help is appreciated. 

Parents
  • Hi,

     You can set a flag in the ble event handler that is set based on content that is received. You can then check the status of this flag for each iteration of the loop in main. Something like this:

    int flag = 0;
    
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        Case BLE_GATTC_EVT_HVX:
        
        //Check payload and set flag
    
        break:
    
    }
    
    
    int main(void)
    {
    
        while()
        {
            while( flag == loop1){}
            
            while(flag == loop2){}
            
            while(flag == loop3){}
        
        }
    
    
    
    
    }

    regards

    Jared

Reply
  • Hi,

     You can set a flag in the ble event handler that is set based on content that is received. You can then check the status of this flag for each iteration of the loop in main. Something like this:

    int flag = 0;
    
    static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
    {
        Case BLE_GATTC_EVT_HVX:
        
        //Check payload and set flag
    
        break:
    
    }
    
    
    int main(void)
    {
    
        while()
        {
            while( flag == loop1){}
            
            while(flag == loop2){}
            
            while(flag == loop3){}
        
        }
    
    
    
    
    }

    regards

    Jared

Children
Related