summaryrefslogtreecommitdiff
path: root/System.c
blob: 68742d4950cb84dee8a8a0fd2e7ae3e042fecf00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* *************************************
 * 	Includes
 * *************************************/

#include "System.h"

/* *************************************
 * 	Defines
 * *************************************/

#define SYSTEM_MAX_TIMERS 8
#define check_bb_collision(x1,y1,w1,h1,x2,y2,w2,h2) (!( ((x1)>=(x2)+(w2)) || ((x2)>=(x1)+(w1)) || \
                                                                ((y1)>=(y2)+(h2)) || ((y2)>=(y1)+(h1)) ))

/* *************************************
 * 	Local Prototypes
 * *************************************/

//static void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step);

/* *************************************
 * 	Local Variables
 * *************************************/

//Global timer (called by interrupt)
static volatile uint64_t global_timer;
//Tells whether rand seed has been set
static bool rand_seed;
//Timers
static bool one_second_timer;
static bool hundred_ms_timer;
//Critical section is entered
static bool system_busy;
//Timer array
static TYPE_TIMER timer_array[SYSTEM_MAX_TIMERS];

/* *************************************
 * @name: void SystemInit(void)
 * @date: 19/05/2016
 * @author: Xavier Del Campo
 * @brief:
 * *************************************/

void SystemInit(void)
{
	//Reset global timer
	global_timer = 0;
	//Reset 1 second timer
	one_second_timer = 0;
	//Reset all user-handled timers
	SystemResetTimers();
	//Initial value for system_busy
	system_busy = false;

	#if defined(USBCON)
	USBDevice.attach();
	#endif
}

void SystemSetRandSeed(void)
{
	if (rand_seed == false)
	{
		rand_seed = true;
		//Set random seed using global timer as reference
		srand((unsigned int)global_timer);
	}
}

bool SystemIsRandSeedSet(void)
{
	return rand_seed;
}

void SystemIncreaseGlobalTimer(void)
{
	global_timer++;
}

uint64_t SystemGetGlobalTimer(void)
{
	return global_timer;
}

bool System1SecondTick(void)
{
	return one_second_timer;
}

bool System100msTick(void)
{
	return hundred_ms_timer;
}

void SystemRunTimers(void)
{
/*	static uint64_t last_one_second_tick;
	static uint64_t last_100_ms_tick;

	SystemCheckTimer(&one_second_timer, &last_one_second_tick, REFRESH_FREQUENCY);
	SystemCheckTimer(&hundred_ms_timer, &last_100_ms_tick, 2);
	* */
}

void SystemCheckTimer(bool * timer, uint64_t * last_timer, uint8_t step)
{
	if (*timer != false)
	{
		*timer = false;
		*last_timer = global_timer;
	}

	if (global_timer >= (*last_timer + step) )
	{
		*timer = true;
	}
}

void SystemWaitCycles(uint32_t cycles)
{
	uint64_t currentTime = global_timer;

	while (global_timer < (currentTime + cycles) );
}

uint32_t SystemRand(uint32_t min, uint32_t max)
{
	return rand() % (max - min + 1) + min;
}

bool SystemIsBusy(void)
{
	return system_busy;
}

bool SystemContains_u8(uint8_t value, uint8_t* buffer, size_t sz)
{
	size_t i = 0;

	for (i = 0; i < sz; i++)
	{
		if (buffer[i] == value)
		{
			return true;
		}
	}

	return false;
}

bool SystemContains_u16(uint16_t value, uint16_t * buffer, size_t sz)
{
	size_t i = 0;

	for (i = 0; i < sz; i++)
	{
		if (buffer[i] == value)
		{
			return true;
		}
	}

	return false;
}

TYPE_TIMER * SystemCreateTimer(uint32_t seconds, bool rf, void (*timer_callback)(void) )
{
	bool success = false;
	uint8_t i;

	if (seconds == 0)
	{
		return NULL;
	}

	for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
	{
		if (timer_array[i].busy == false)
		{
			timer_array[i].Timeout_Callback = timer_callback;
			timer_array[i].time = seconds;
			timer_array[i].orig_time = seconds;
			timer_array[i].repeat_flag = rf;
			timer_array[i].busy = true;
			success = true;
			break;
		}
	}

	if (success == false)
	{
		return NULL;
	}

	return &timer_array[i];
}

void SystemResetTimers(void)
{
	uint8_t i;

	for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
	{
		timer_array[i].Timeout_Callback = NULL;
		timer_array[i].busy = false;
		timer_array[i].repeat_flag = false;
		timer_array[i].time = 0;
		timer_array[i].orig_time = 0;
	}
}

void SystemUserTimersHandler(void)
{
	uint8_t i;

	for (i = 0; i < SYSTEM_MAX_TIMERS; i++)
	{
		if (timer_array[i].busy != false)
		{
			if (System1SecondTick() != false)
			{
				timer_array[i].time--;

				if (timer_array[i].time == 0)
				{
					timer_array[i].Timeout_Callback();

					if (timer_array[i].repeat_flag != false)
					{
						timer_array[i].time = timer_array[i].orig_time;
					}
					else
					{
						// Clean timer data
						timer_array[i].busy = false;
						timer_array[i].orig_time = 0;
						timer_array[i].Timeout_Callback = NULL;
					}
				}
			}
		}
	}
}

void SystemTimerRestart(TYPE_TIMER * timer)
{
	timer->time = timer->orig_time;
}

void SystemTimerRemove(TYPE_TIMER * timer)
{
	timer->time = 0;
	timer->orig_time = 0;
	timer->Timeout_Callback = NULL;
	timer->busy = false;
	timer->repeat_flag = false;
}

bool SystemArrayCompare(unsigned short * arr1, unsigned short * arr2, size_t sz)
{
	size_t i;

	for (i = 0; i < sz; i++)
	{
		if (arr1[i] != arr2[i])
		{
			return false;
		}
	}

	return true;
}

bool SystemCollisionCheck(TYPE_COLLISION_BLOCK* c1, TYPE_COLLISION_BLOCK* c2)
{
	return (bool)check_bb_collision(	c1->x, c1->y, c1->w, c1->h,
										c2->x, c2->y, c2->w, c2->h	);
}

bool Systemitoa(char* str, size_t sz, int16_t value)
{
    if (sz != 0)
    {
        bool first_digit_found = false;
        uint16_t i;
        uint8_t j = 0;

        /* Example: 65535 */
        /* Another example: -32767 */

        if (value & 0x8000)
        {
            /* Sign bit */
            str[j++] = '-';
        }

        for (i = 10000; i >= 1; i /= 10)
        {
            uint8_t digit = (uint8_t)(value / i);
            value -= (uint16_t)(digit * i);

            if (digit != 0)
            {
                if (first_digit_found == false)
                {
                    first_digit_found = true;
                }
            }
            else if (first_digit_found == false)
            {
                continue;
            }

            str[j++] = digit + '0';

            if (j >= (sz - 1))
            {
                return false;
            }
        }

        str[j] = '\0';

        return true;
    }

    return false;
}