aboutsummaryrefslogtreecommitdiff
path: root/Source/Timer.c
diff options
context:
space:
mode:
authorXaviDCR92 <xavi.dcr@gmail.com>2017-08-10 22:39:31 +0200
committerXaviDCR92 <xavi.dcr@gmail.com>2017-08-10 22:39:31 +0200
commitf17b15bdffe45810eebc7695c10f1d7fc34af014 (patch)
tree273e3459303a063c03a84ffbc7c95415eb216364 /Source/Timer.c
parentb807ee7ca59c13bbc698595da5e56eb6dd6daa2f (diff)
downloadairport-f17b15bdffe45810eebc7695c10f1d7fc34af014.tar.gz
+ System timer functions now moved to a separate source file, Timer.c/Timer.h.
* Added some more comments on Game.c. * On GamePathToTile(), duplicate checks for existing tile have been replaced by calls to GameWaypointCheckExisting().
Diffstat (limited to 'Source/Timer.c')
-rw-r--r--Source/Timer.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/Source/Timer.c b/Source/Timer.c
new file mode 100644
index 0000000..841979e
--- /dev/null
+++ b/Source/Timer.c
@@ -0,0 +1,173 @@
+/* *************************************
+ * Includes
+ * *************************************/
+
+#include "Timer.h"
+#include "GameStructures.h"
+
+/* *************************************
+ * Defines
+ * *************************************/
+
+#define MAX_TIMERS 16
+
+/* *************************************
+ * Local Variables
+ * *************************************/
+
+//Timer array.
+static TYPE_TIMER timer_array[MAX_TIMERS];
+
+/* *************************************
+ * Local Prototypes
+ * *************************************/
+
+/* ********************************************************************************************
+ *
+ * @name TYPE_TIMER* TimerCreate(uint32_t t, bool rf, void (*timer_callback)(void) )
+ *
+ * @author: Xavier Del Campo
+ *
+ * @brief: fills a TYPE_TIMER structure with input parameters
+ *
+ * @param: uint32_t t:
+ * Timeout value (1 unit = 10 ms)
+ * bool rf:
+ * Repeat flag
+ * void (*timer_callback)(void)
+ * Function to be called on timeout
+ *
+ * @return: pointer to TYPE_TIMER structure if filled correctly, NULL pointer otherwise.
+ *
+ * ********************************************************************************************/
+
+TYPE_TIMER* TimerCreate(uint32_t t, bool rf, void (*timer_callback)(void) )
+{
+ bool success = false;
+ uint8_t i;
+
+ if(t == 0)
+ {
+ Serial_printf("Cannot create timer with time == 0!\n");
+ return NULL;
+ }
+
+ for(i = 0; i < MAX_TIMERS; i++)
+ {
+ if(timer_array[i].busy == false)
+ {
+ timer_array[i].Timeout_Callback = timer_callback;
+ timer_array[i].time = t;
+ timer_array[i].orig_time = t;
+ timer_array[i].repeat_flag = rf;
+ timer_array[i].busy = true;
+ success = true;
+ break;
+ }
+ }
+
+ if(success == false)
+ {
+ Serial_printf("Could not find any free timer!\n");
+ return NULL;
+ }
+
+ return &timer_array[i];
+}
+
+/* *******************************************
+ *
+ * @name void TimerReset(void)
+ *
+ * @author: Xavier Del Campo
+ *
+ * @brief: reportedly, removes all timers.
+ *
+ * *******************************************/
+
+void TimerReset(void)
+{
+ uint8_t i;
+
+ for(i = 0; i < MAX_TIMERS; i++)
+ {
+ TimerRemove(&timer_array[i]);
+ }
+}
+
+/* *****************************************************
+ *
+ * @name void TimerHandler(void)
+ *
+ * @author: Xavier Del Campo
+ *
+ * @brief: reportedly, handles all available timers.
+ *
+ * @remarks: calls callback on timeout.
+ *
+ * *****************************************************/
+
+void TimerHandler(void)
+{
+ uint8_t i;
+
+ for(i = 0; i < MAX_TIMERS; i++)
+ {
+ if(timer_array[i].busy == true)
+ {
+ if(System100msTick() == true)
+ {
+ timer_array[i].time--;
+
+ if(timer_array[i].time == 0)
+ {
+ timer_array[i].Timeout_Callback();
+
+ if(timer_array[i].repeat_flag == true)
+ {
+ timer_array[i].time = timer_array[i].orig_time;
+ }
+ }
+ }
+ }
+ }
+}
+
+/* *********************************************************************
+ *
+ * @name void TimerRestart(TYPE_TIMER* timer)
+ *
+ * @author: Xavier Del Campo
+ *
+ * @brief: sets time left for TYPE_TIMER instance to initial value.
+ *
+ * @remarks: specially used when TYPE_TIMER.rf is enabled.
+ *
+ * *********************************************************************/
+
+void TimerRestart(TYPE_TIMER* timer)
+{
+ timer->time = timer->orig_time;
+}
+
+/* *********************************************************************
+ *
+ * @name void TimerRemove(TYPE_TIMER* timer)
+ *
+ * @author: Xavier Del Campo
+ *
+ * @brief: Resets timer parameters to default values so timer instance
+ * can be recycled.
+ *
+ * @remarks:
+ *
+ * *********************************************************************/
+
+void TimerRemove(TYPE_TIMER* timer)
+{
+ timer->time = 0;
+ timer->orig_time = 0;
+ timer->Timeout_Callback = NULL;
+ timer->busy = false;
+ timer->repeat_flag = false;
+}