/***************************************************************************** * FUNCTION * EvmStartTimer * DESCRIPTION * Start an Event Manager timer. * PARAMETERS * timer [IN] * ticks [IN] * stopHardware [IN] * RETURNS * void *****************************************************************************/ void EvmStartTimer(EvmTimer *timer, TimeT ticks, BOOL stopHardware) { /* Assert(timer->func != 0); */ if (stopHardware) { OS_StopHardware(); } /* Configure the timer's internals */ timer->startTime = OS_GetSystemTime(); timer->time = ticks; /* If the timer is already on the list, don't add it again. */ if (!IsNodeOnList(&BTC(timerList), &timer->node)) { InsertHeadList(&BTC(timerList), &timer->node); } /* Reset the time amount for the OS timer */ ResetOsTimer(timer->startTime); if (stopHardware) { OS_ResumeHardware(); } }
/***************************************************************************** * FUNCTION * ResetOsTimer * DESCRIPTION * Resets the actual OS timer based on the shortest remaining * timer. * PARAMETERS * curTime [IN] * RETURNS * void *****************************************************************************/ static void ResetOsTimer(TimeT curTime) { EvmTimer *timer; ListEntry *node; TimeT timeElapsed; TimeT minWait = (TimeT) (-1); /* Look for timers scheduled in the future */ if (!IsListEmpty(&BTC(timerList))) { node = GetHeadList(&BTC(timerList)); while (node != &BTC(timerList)) { timer = (EvmTimer*) node; node = node->Flink; timeElapsed = curTime - timer->startTime; /* If the timer must elapse sooner than minWait, adjust minWait */ if (timeElapsed < timer->time) { minWait = min(minWait, timer->time - timeElapsed); } else { OS_Report("[TIMER] Timer(%x) timeout (%u, %u) when reset OS timer. Just Fire timer", timer, timeElapsed, timer->time); TimerFired(); return; } } } /* Are any timers in the future? */ if (minWait < (TimeT) (-1)) { OS_StartTimer(minWait, TimerFired); } else { OS_CancelTimer(); } }
/***************************************************************************** * FUNCTION * EvmCancelTimer * DESCRIPTION * Cancel an Event Manager timer. * PARAMETERS * timer [IN] * stopHardware [IN] * RETURNS * void *****************************************************************************/ void EvmCancelTimer(EvmTimer *timer, BOOL stopHardware) { EvmTimer *curTimer; ListEntry *node; if (stopHardware) { OS_StopHardware(); } /* Look for the timer in our active timer list and remove it. */ if (!IsListEmpty(&BTC(timerList))) { node = GetHeadList(&BTC(timerList)); while (node != &BTC(timerList)) { curTimer = (EvmTimer*) node; node = node->Flink; if (curTimer == timer) { RemoveEntryList(&curTimer->node); /* Recalculates the correct OS timer period */ ResetOsTimer(OS_GetSystemTime()); break; } } } /* * If the timer was not found in our active list, simply assume it was * already cancelled. */ if (stopHardware) { OS_ResumeHardware(); } }
/***************************************************************************** * FUNCTION * EVM_Init * DESCRIPTION * Initialize the Event Manager. * PARAMETERS * void * RETURNS * *****************************************************************************/ BOOL EVM_Init(void) { #if XA_DEBUG == XA_ENABLED /* This is a debug-only test to verify that macros are compiled correctly. */ VerifyMacros(); #endif /* XA_DEBUG == XA_ENABLED */ /* Intialize the Bluetooth memory */ if (!BtInitMemory()) { return FALSE; } /* Initialize the timer list */ InitializeListHead(&BTC(timerList)); /* Initialize the Radio Manager */ if (RMGR_RadioInit() == BT_STATUS_FAILED) { return FALSE; } /* Initialize the Bluetooth stack */ if (ME_Init() == BT_STATUS_FAILED) { return FALSE; } #ifdef __BT_4_0_BLE__ BT_ATT_Init(); GATT_Init(); ATTDB_Init(); #endif /* XA_LOAD_LIST evaluates to series of module specific Init() function calls. */ if ((XA_LOAD_LIST TRUE) != TRUE) { return FALSE; } /* ... other initialization here ... */ return TRUE; }
/***************************************************************************** * FUNCTION * CheckTimers * DESCRIPTION * Look for expired timers and call their notify functions * PARAMETERS * void * RETURNS * void *****************************************************************************/ static void CheckTimers(void) { EvmTimer *timer; ListEntry *node; TimeT curTime; TimeT timeElapsed; U8 timerFired; //bt_prompt_trace(MOD_BT, "[BT] +CheckTimers"); OS_StopHardware(); timerFired = FALSE; /* See if we have any expired timers */ if (!IsListEmpty(&BTC(timerList))) { curTime = OS_GetSystemTime(); /* Look through all active timers */ node = GetHeadList(&BTC(timerList)); while (node != &BTC(timerList)) { timer = (EvmTimer*) node; node = node->Flink; /* Calculate elapsed time */ if(curTime >= timer->startTime){ timeElapsed = curTime - timer->startTime; }else{ bt_prompt_trace(MOD_BT, "[TIMER] timer overrun : curTime=%u, timer->startTime=%u", curTime, timer->startTime); timeElapsed = curTime + (0xFFFFFFFF - timer->startTime); } //bt_prompt_trace(MOD_BT, "[BT] timeElapsed=%d, wait time=%d",timeElapsed, timer->time); /* If its time is complete, remove the timer and fire it. */ if (timeElapsed >= timer->time) { RemoveEntryList(&timer->node); OS_ResumeHardware(); //bt_prompt_trace(MOD_BT, "[BT] timer->func(timer) : 0x%x", (U32)timer->func); timer->func(timer); //bt_prompt_trace(MOD_BT, "[BT] timer->func(timer) success"); timerFired = TRUE; OS_StopHardware(); /* * Start looking back at the beginning of the list * This is necessary because the contents of the list * might have been modified during timer->func. */ node = GetHeadList(&BTC(timerList)); curTime = OS_GetSystemTime(); } } /* * If any timer fired, we need to reset the OS timer and * re-schedule the stack task. */ if (timerFired) { OS_NotifyEvm(); ResetOsTimer(curTime); } } OS_ResumeHardware(); //bt_prompt_trace(MOD_BT, "[BT] -CheckTimers"); }
#define WINNER 1 #define LOSER 0 // Play button tone for 150 ms #define BUTTON_LENGTH_MS 150 // Button Tone and Count array entries generation macro #define BTC(freq) FREQLEN2TONECNT(freq, BUTTON_LENGTH_MS) // Current game variables uint8_t game_sequence[MAX_GAME_LEVEL]; // contains 0..3 button numbers for a game uint8_t game_position; // current game position from 0 uint8_t game_level = 5; // default game level if game starts with single button press. uint16_t BUTTONS[8] = { BTC(440.00), // (red, upper left) - 440Hz BTC(880.00), // (green, upper right, an octave higher than the upper right) - 880Hz BTC(587.33), // (blue, lower left, a perfect fourth higher than the upper left) - 587.33Hz BTC(784.00) // (yellow, lower right, a perfect fourth higher than the lower left) - 784Hz }; // Generates button tone and highlights the corresponding button void button_tone(uint8_t button) { set_leds(_BV(button)); // Turn on button led uint16_t *btc = &BUTTONS[2 * button]; // Pointer to BTC entry for the button buzzer_wait(*btc, *(btc + 1)); set_leds(0); // Turn off all LEDs } // Starts new game inline void new_game_sequence() {