-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcbar.h
165 lines (145 loc) · 4.08 KB
/
cbar.h
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
/*
* Copyright © 2014 Kosma Moczek <[email protected]>
* 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.
*/
#ifndef CBAR_H
#define CBAR_H
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
enum cbar_line_type {
CBAR_INPUT = 1,
CBAR_EXTERNAL,
CBAR_THRESHOLD,
CBAR_DEBOUNCE,
CBAR_REQUEST,
CBAR_CALCULATED,
CBAR_MONITOR,
CBAR_PERIODIC,
};
struct cbar;
struct cbar_line_config {
const char *name; /**< Line name. Use NULL to terminate config block. */
enum cbar_line_type type; /**< Line type. */
union {
struct {
} input;
struct {
int (*get)(intptr_t); /**< Callback for retrieving input state. */
intptr_t priv; /**< Callback argument. */
bool invert; /**< True if the input is active-low. */
} external;
struct {
int input; /**< Input line ID. */
int threshold_up; /**< Threshold for low->high transition. */
int threshold_down; /**< Threshold for high->low transition. */
} threshold;
struct {
int input; /**< Input line ID. */
int timeout_up; /**< Timeout for low->high transition. */
int timeout_down; /**< Timeout for high->low transition. */
} debounce;
struct {
} request;
struct {
int (*get)(struct cbar *cbar); /**< Callback for calculating line state. */
} calculated;
struct {
int input; /**< Input line ID. */
} monitor;
struct {
int period; /**< Timer period in miliseconds. */
} periodic;
};
};
/**
* @internal
*/
struct cbar_line {
int value;
union {
struct {
int input_value;
} input;
struct {
int value;
int timer;
} debounce;
struct {
int previous;
} monitor;
struct {
int elapsed;
} periodic;
};
};
/**
* @internal
*/
struct cbar {
pthread_mutex_t mutex;
struct cbar_line *lines;
const struct cbar_line_config *configs;
};
/**
* Declare a cbar instance. Allocates memory for state storage as well.
*/
#define CBAR_DECLARE(VAR, CONFIGS) \
struct cbar VAR; \
struct cbar_line VAR ## _lines[sizeof(CONFIGS)/sizeof(CONFIGS[0])-1];
/**
* Initialize a cbar instance.
* @param VAR Variable name (NOTE: not a pointer).
* @param CONFIGS Configs variable name.
*/
#define CBAR_INIT(VAR, CONFIGS) \
cbar_init(&VAR, CONFIGS, VAR ## _lines)
/**
* @internal
*/
void cbar_init(struct cbar *cbar, const struct cbar_line_config *configs, struct cbar_line *lines);
/**
* Perform one round of debouncing/calculation of states.
*
* @param delay Delay since last call, in miliseconds.
*/
void cbar_recalculate(struct cbar *cbar, int delay);
/**
* Set cbar input line value.
*
* @param cbar Initialized cbar instance.
* @param id Line ID.
* @param value New value.
*/
void cbar_input(struct cbar *cbar, int id, int value);
/**
* Read cbar line value.
* @param cbar Initialized cbar instance.
* @param id Line ID.
* @returns Current line value.
*/
int cbar_value(struct cbar *cbar, int id);
/**
* Post a request.
* @param cbar Initialized cbar instance.
* @param id Line ID.
*/
void cbar_post(struct cbar *cbar, int id);
/**
* Read and clear a "request pending" line.
* @param cbar Initialized cbar instance.
* @param id Line ID. Must be one of request, monitor, or periodic.
* @returns true if request was pending, false otherwise.
*/
bool cbar_pending(struct cbar *cbar, int id);
/**
* Dump the cbar state.
* @param cbar Initialized cbar instance.
*/
void cbar_dump(FILE *stream, struct cbar *cbar);
#endif
/* vim: set ts=4 sw=4 et: */