-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinhibitors.c
55 lines (44 loc) · 1.4 KB
/
inhibitors.c
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
#include <stdio.h>
#include "ac.h"
#include "pulseaudio.h"
#include "config.h"
#include "inhibitors.h"
#include "idled.h"
// arrays of inhibitor functions to call, as long as there are under 255 functions per action ;)
funcPtr idle_1_inhibitors[] = { isAcOnline };
funcPtr idle_2_inhibitors[] = { isPulsePlaying };
funcPtr idle_3_inhibitors[] = { isAcOnline, isPulsePlaying };
byte shouldInhibitAction(byte idleAction) {
byte numFuncs = 0;
// declare a pointer to the array of inhibitor functions
funcPtr *inhibitors;
// need to count the number of functions before the inhibitors pointer is created
// because the size of the pointer cannot be known
switch(idleAction) {
case IDLE_1:
numFuncs = sizeof(idle_1_inhibitors) / sizeof(funcPtr);
inhibitors = idle_1_inhibitors;
break;
case IDLE_2:
numFuncs = sizeof(idle_2_inhibitors) / sizeof(funcPtr);
inhibitors = idle_2_inhibitors;
break;
case IDLE_3:
numFuncs = sizeof(idle_3_inhibitors) / sizeof(funcPtr);
inhibitors = idle_3_inhibitors;
break;
}
// iterate over function pointers
// if any one returns 1 then return 1
return runInhibitors(inhibitors, numFuncs);
}
byte runInhibitors(funcPtr *inhibitors, byte len) {
for (byte i=0; i<len; i++) {
byte result = (*inhibitors[i])();
if (result == 1) {
printf("Inhibiting action.\n");
return 1;
}
}
return 0;
}