3 Replies Latest reply on Mar 10, 2020 3:03 AM by GaneshD_41

    Implement a Bluetooth interrupt to break a forever loop function

    EmBa_4588821

      Hello,

       

      I am trying to create a device that controls an array of LED's. I can communicate to the CYBLE-416045-02 module with Bluetooth connection and call different functions that turn the LED's on and off per specific patterns. A particular pattern is controlling the LED's with an infinite loop implemented with for (;;).

       

      If I once start this function, I cannot stop it with another Bluetooth message. I couldn't find how to implement an interrupt mechanism that would break the forever loop and start another patter of LED's blinking.

       

      Do I need to consider to use tasks rather than functions with forever loops? I just want the blinking pattern to run until I send another mode command via Bluetooth or turn the device off.

       

      Thanks

        • 1. Re: Implement a Bluetooth interrupt to break a forever loop function
          GaneshD_41

          Hi,

           

          I would recommend you to call CyBle_ProcessEvents() API in a separate Timer ISR. The Timer ISR can be called once per connection interval.  This method executes the events that the BLE controller receive irrespective of what the CPU is currently executing.

           

          Whenever the central sends data, the BLE peripheral would receive it through event. Based on the the LED sequence you want to set, you can store the value to the Global variiable.

           

          In for loop you can check for that global variable and call 'break;' statement whenever the light setting changed.

           

          Hope this idea helps !

           

          Thanks

          Ganesh

          • 2. Re: Implement a Bluetooth interrupt to break a forever loop function
            EmBa_4588821

            Hello Ganesh,

             

            Thank you for your answer. I am getting closer o the solution, but still not there. I have implemented the timer ISR and I can see that a debug message is printed each second with the function below. ( I am using FreeRTOS btw, so Bluetooth is handled by a task.)

             

            void TimerInterruptHandler()

            {

                Cy_TCPWM_ClearInterrupt(Timer_HW, Timer_CNT_NUM, CY_TCPWM_INT_ON_TC);

                printf("Timer Interrupt\r\n");

                if (running) {

                    xSemaphoreTake(bleSemaphore,portMAX_DELAY);

                    Cy_BLE_ProcessEvents();

                    xSemaphoreGive(bleSemaphore);

                }

            }

             

            The global variable "running" is set to true, when an LED loop starts running. With this code, now I can stop the loop but then the Bluetooth advertising stops as well. I didn't make any change to the existing Bluetooth task. Can you see something obvious wrong?

             

            Thanks.

            • 3. Re: Implement a Bluetooth interrupt to break a forever loop function
              GaneshD_41

              Hi,

               

              Cy_BLE_ProcessEvents() should be called periodically to maintain BLE connection irrespective of other processes running in the application.

               

              For your present application,based on what LED pattern you want, you can send corresponding command through the BLE. And in the for loop (or task in which you are running LED patterns) you can maintain a switch statement that selects the case for all the cases/patterns you want.

               

              Thanks

              Ganesh