AnsweredAssumed Answered

problem with events in multi-threaded code

Question asked by Michele Marchetti on Oct 13, 2011
Latest reply on Oct 18, 2011 by Markku Pulsa

Dear all,

I am writing a simple app using K40, MQX 3.7, where I have 2 threads.
In one thread (main) I handle capacitive touch and corresponding to which key gets pushed, in a second thread (audio) a different audio file should be played.
The whole point of having threads is that while one audio is playing and a different button is pressed, then the current audio should stop an a new one should start.
I am a beginner with MQX, so it is quite likely that I am doing something silly...

DESIGN:
I thought I could handle this with events, setting an event in main thread, and reading it from the audio thread.
Is this the best way of doing this?

PROBLEM:
when one audio is played, the board does not seem to respond to capacitive touch, so any new key press is ignored until the current audio terminates.
Both of the audios are played properly, but they are not interrupted by the other one.


Some code follows, please help if you notice anything obvious:

TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
   {MAIN_TASK,   MainTask,   1000,  11,   "main",    MQX_AUTO_START_TASK},
   {AUDIO_TASK,  AudioTask,  3000,  10,      "audio",     MQX_AUTO_START_TASK|MQX_TIME_SLICE_TASK},
   {0,           0,           0,     0,   0,      0,                 }
};

/* main.c FILE */
/*****************************************************************/
[..] Main Task calls following:

void MainElCheck(void)
{
    _Electrodes_Check();

    if (ElectrodeFlags & TSS_ELECTRODE_0)
    {       
        _event_set(eventPtr, SOUND1);
    }   
    else if (ElectrodeFlags & TSS_ELECTRODE_1)
    {
        _event_set(eventPtr, SOUND2);
    }

    ElectrodeFlags = 0;   
}


/* audio.c */
/**********************************************************/
volatile BOOL isWriteSample = FALSE;
static BOOL isPlaying = FALSE;
static volatile Sound currentSound = AUDIO_OFF;
static pointer eventPtr = NULL;


/* ******************************************************************** */
void AudioTask(U32 initial_data)
{
    PlayerInit();             // initialization, follows
   
    while ( currentSound != AUDIO_OFF )
    {
        static _mqx_uint eventBits = 0;

        if ( _event_wait_any(eventPtr, ANY_SOUND, 0) != MQX_OK )
        {
            printf("\nERROR! AudioTask, event_wait_any failed!\n");
        }
       
        if ( _event_get_value(eventPtr, &eventBits) != MQX_OK )
        {
            printf("\nERROR! AudioTask, event_get_value failed!\n");
        }
        printf("DBG: Audio - passed wait for any, value: %u\n", eventBits);
   
        switch ( eventBits )
        {
            case SOUND1:
                {
                    currentSound = SOUND1;
                    isPlaying = TRUE;
                    printf("DBG: playing sound 1\n");
                    _event_clear( eventPtr, SOUND1 );
                   
                    while( isPlaying && (currentSound == SOUND1) )
                    {
                        isPlaying = !( PlayWave(SOUND1, array1Len) );
                        _event_get_value(eventPtr, &eventBits);

                        if ( eventBits )
                        {
                            printf("DBG: event changed! new sound is %d\n", eventBits);     ///!!!!! I never see this one
                            currentSound = eventBits;
                            isPlaying = FALSE;
                        }
                    }
                       
                    isPlaying = FALSE;
                }
                break;
            case SOUND2:
                {
                    currentSound = SOUND2;
                    isPlaying = TRUE;
                    printf("DBG: playing sound 2\n");
                    _event_clear( eventPtr, SOUND2 );
                   
                    while( isPlaying && (currentSound == SOUND2) )
                    {
                        isPlaying = !( PlayWave(SOUND2, array2Len) );
                        _event_get_value(eventPtr, &eventBits);

                        if ( eventBits != AUDIO_READY && eventBits != SOUND2 )
                        {
                            printf("DBG: event changed! new sound is %d\n", eventBits);    //!!!!! I never see this one
                            currentSound = eventBits;
                            isPlaying = FALSE;
                        }
                    }

                    isPlaying = FALSE;
                }
                break;

            default:
                currentSound = AUDIO_READY;
                eventBits = AUDIO_READY;
                _event_get_value(eventPtr, &eventBits);
                break;               
        }
    }   
   
    _event_close(eventPtr);
    _mqx_exit(0);
}

I am a bit blind here... any help appreciated!
Thanks
Mik

Outcomes