summaryrefslogtreecommitdiff
path: root/avr/background.c
blob: 9c8b5a6f72ac2cc893b79e05a90869f1d05b7cd4 (plain)
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
/*
 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include "common.h"
#include "background.h"


#define BG_FUNC_MAX 5

static struct {
	bg_func fct;
	int param;
} func_tab[BG_FUNC_MAX];

static int_fast8_t fcount;

int bg_register(bg_func f, int initval)
{
	if (fcount < BG_FUNC_MAX) {
		func_tab[fcount].fct = f;
		func_tab[fcount].param = initval;
		return ++fcount - 1;
	}
	return -1;
}

int bg_setstat(int handle, int val)
{
	if (handle < fcount) {
		func_tab[handle].param = val;
		return 1;
	}

	return 0;
}


int bg_getstat(int handle)
{
	if (handle < fcount) {
		return func_tab[handle].param;
	}
	return 0;
}


void bg_shed(void)
{
	static int_fast8_t current;

	if (func_tab[current].fct) {
		int v = func_tab[current].fct(func_tab[current].param);
		func_tab[current].param = v;
	}
	if (++current >= fcount)
		current = 0;
}