-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcbar.c
More file actions
193 lines (164 loc) · 6.62 KB
/
cbar.c
File metadata and controls
193 lines (164 loc) · 6.62 KB
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
/*
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
* This program is free software. It comes without any warranty, to the extent
* permitted by applicable law. You can redistribute it and/or modify it under
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
* published by Sam Hocevar. See the COPYING file for more details.
*/
#include "cbar.h"
#include <assert.h>
#include <limits.h>
void cbar_init(struct cbar *cbar, const struct cbar_line_config *configs, struct cbar_line *lines)
{
cbar->configs = configs;
cbar->lines = lines;
pthread_mutex_init(&cbar->mutex, NULL);
for (int id=0; cbar->configs[id].type; id++) {
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
/* All lines are initially at zero. */
line->value = 0;
switch (config->type) {
case CBAR_INPUT: {
line->input.input_value = line->value;
} break;
case CBAR_EXTERNAL: {
} break;
case CBAR_THRESHOLD: {
} break;
case CBAR_DEBOUNCE: {
/* Make the debouncer start counting immediately. */
line->debounce.value = INT_MIN;
} break;
case CBAR_REQUEST: {
} break;
case CBAR_CALCULATED: {
} break;
case CBAR_MONITOR: {
/* Make the monitor fire immediately on the initial state. */
line->monitor.previous = INT_MIN;
} break;
case CBAR_PERIODIC: {
line->periodic.elapsed = 0;
} break;
}
}
cbar_recalculate(cbar, 0);
}
void cbar_recalculate(struct cbar *cbar, int delay)
{
pthread_mutex_lock(&cbar->mutex);
for (int id=0; cbar->configs[id].type; id++) {
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
switch (config->type) {
case CBAR_INPUT: {
line->value = line->input.input_value;
} break;
case CBAR_EXTERNAL: {
int input = config->external.get(config->external.priv);
line->value = config->external.invert ? !input : input;
} break;
case CBAR_THRESHOLD: {
int input = cbar->lines[config->debounce.input].value;
if (line->value)
line->value = (input >= config->threshold.threshold_down);
else
line->value = (input >= config->threshold.threshold_up);
/* Swapping the up/down values inverts logic. */
if (config->threshold.threshold_up < config->threshold.threshold_down)
line->value = !line->value;
} break;
case CBAR_DEBOUNCE: {
int input = cbar->lines[config->debounce.input].value;
int timeout = input ? config->debounce.timeout_up : config->debounce.timeout_down;
if (line->debounce.value != input) {
// Line state just changed. Reset debounce timer.
//printf("cbar: [debounce] %s going towards %d\r\n", config->name, input);
line->debounce.value = input;
line->debounce.timer = 0;
} else if (line->debounce.value != line->value) {
// Line state is stabilizing. Bump debounce timer.
//printf("cbar: [debounce] %s clocked %d vs %d\r\n", config->name, line->debounce.timer, timeout);
line->debounce.timer += delay;
}
if (line->debounce.value != line->value && line->debounce.timer >= timeout) {
// Line just stabilized. Register the change.
//printf("cbar: [debounce] %s stable at %d\r\n", config->name, input);
line->value = line->debounce.value;
}
} break;
case CBAR_REQUEST: {
} break;
case CBAR_CALCULATED: {
line->value = config->calculated.get(cbar);
} break;
case CBAR_MONITOR: {
int input = cbar->lines[config->monitor.input].value;
if (input != line->monitor.previous) {
//printf("cbar: [monitor] %s changed to %d\r\n", config->name, input);
line->value = true;
line->monitor.previous = input;
}
} break;
case CBAR_PERIODIC: {
line->periodic.elapsed += delay;
if (line->periodic.elapsed >= config->periodic.period) {
line->periodic.elapsed = 0;
line->value = 1;
}
} break;
}
}
pthread_mutex_unlock(&cbar->mutex);
}
void cbar_input(struct cbar *cbar, int id, int value)
{
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
assert(config->type == CBAR_INPUT);
//printf("cbar: [input] %s set to %d\r\n", config->name, value);
pthread_mutex_lock(&cbar->mutex);
line->input.input_value = value;
pthread_mutex_unlock(&cbar->mutex);
}
void cbar_post(struct cbar *cbar, int id)
{
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
assert(config->type == CBAR_REQUEST);
//printf("cbar: [request] %s posted\r\n", config->name);
pthread_mutex_lock(&cbar->mutex);
line->value = 1;
pthread_mutex_unlock(&cbar->mutex);
}
int cbar_value(struct cbar *cbar, int id)
{
struct cbar_line *line = &cbar->lines[id];
// Don't acquire the mutex as we will get called in the calculate function.
return line->value;
}
bool cbar_pending(struct cbar *cbar, int id)
{
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
assert(config->type == CBAR_REQUEST ||
config->type == CBAR_MONITOR ||
config->type == CBAR_PERIODIC);
pthread_mutex_lock(&cbar->mutex);
int value = line->value;
line->value = 0;
pthread_mutex_unlock(&cbar->mutex);
//if (value)
// printf("cbar: [request] %s pended\r\n", config->name);
return value;
}
void cbar_dump(FILE *stream, struct cbar *cbar)
{
for (int id=0; cbar->configs[id].type; id++) {
struct cbar_line *line = &cbar->lines[id];
const struct cbar_line_config *config = &cbar->configs[id];
fprintf(stream, "cbar: %s = %d\r\n", config->name, line->value);
}
}
/* vim: set ts=4 sw=4 et: */