/
snis_strip_chart.c
270 lines (235 loc) · 6.33 KB
/
snis_strip_chart.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "snis_font.h"
#include "snis_typeface.h"
#include "snis_graph.h"
#include "snis_strip_chart.h"
#include "string-utils.h"
struct strip_chart {
int x, y, width, height, color, warn_color, font, history_size, needle;
char label[100];
char warning_msg[100];
uint8_t *history;
uint8_t warning_on, warning_level;
};
struct scaling_strip_chart {
int x, y, width, height, color, warn_color, font, history_size, needle;
char label[100];
char warning_msg[100];
float warning_on;
float *history;
float warning_level;
float bottom, top;
};
struct strip_chart *snis_strip_chart_init(int x, int y, int width, int height, char *label, char *warning_msg,
int color, int warn_color, uint8_t warning_level, int font, int history_size)
{
struct strip_chart *sc = NULL;
if (history_size > 1000)
return NULL;
sc = calloc(sizeof(*sc), 1);
sc->history = calloc(sizeof(*sc->history), history_size);
sc->x = x;
sc->y = y;
sc->width = width;
sc->height = height;
sc->color = color;
sc->warn_color = warn_color;
sc->font = font;
sc->history_size = history_size;
strlcpy(sc->label, label, sizeof(sc->label));
strlcpy(sc->warning_msg, warning_msg, sizeof(sc->warning_msg));
sc->needle = 0;
sc->warning_on = 0;
sc->warning_level = warning_level;
return sc;
}
void snis_strip_chart_draw(struct strip_chart *sc)
{
int i, index;
float x1, y1, x2, y2, w, h, ox, oy;
sng_set_foreground(sc->color);
w = sc->width;
h = sc->height;
ox = sc->x;
oy = sc->y;
sng_current_draw_rectangle(0, ox, oy, w, h);
x2 = ox;
y2 = oy;
for (i = 0; i < sc->history_size; i++) {
index = (sc->needle + i) % sc->history_size;
x1 = x2;
y1 = y2;
x2 = (w * i) / sc->history_size + ox;
y2 = oy + h * (255.0 - sc->history[index]) / 255.0;
sng_current_draw_line(x1, y1, x2, y2);
}
sng_abs_xy_draw_string(sc->label, sc->font, ox, oy + h + snis_font_lineheight(sc->font));
if (sc->warning_on) {
sng_set_foreground(sc->warn_color);
sng_abs_xy_draw_string(sc->warning_msg, sc->font, ox + 10, oy + 0.5 * h);
}
}
void snis_strip_chart_update(struct strip_chart *sc, uint8_t value)
{
sc->history[sc->needle] = value;
if (value > sc->warning_level)
sc->warning_on = 1;
else
sc->warning_on = 0;
sc->needle = (sc->needle + 1) % sc->history_size;
sc->history[sc->needle] = 0;
}
int snis_strip_chart_inside(struct strip_chart *sc, int x, int y)
{
x = sng_pixelx_to_screenx(x);
y = sng_pixelx_to_screenx(y);
if (x < sc->x)
return 0;
if (x > sc->x + sc->width)
return 0;
if (y < sc->y)
return 0;
if (y > sc->y + sc->height)
return 0;
return 1;
}
void snis_strip_chart_update_position(struct strip_chart *sc, int x, int y)
{
sc->x = x;
sc->y = y;
}
int snis_strip_chart_get_x(struct strip_chart *sc)
{
return sc->x;
}
int snis_strip_chart_get_y(struct strip_chart *sc)
{
return sc->y;
}
struct scaling_strip_chart *snis_scaling_strip_chart_init(int x, int y,
int width, int height, char *label, char *warning_msg,
int color, int warn_color, float warning_level, int font, int history_size)
{
struct scaling_strip_chart *sc = NULL;
if (history_size > 1000)
return NULL;
sc = calloc(sizeof(*sc), 1);
sc->history = calloc(sizeof(*sc->history), history_size);
sc->bottom = 0.0;
sc->top = 1.0;
sc->x = x;
sc->y = y;
sc->width = width;
sc->height = height;
sc->color = color;
sc->warn_color = warn_color;
sc->font = font;
sc->history_size = history_size;
strlcpy(sc->label, label, sizeof(sc->label));
strlcpy(sc->warning_msg, warning_msg, sizeof(sc->warning_msg));
sc->needle = 0;
sc->warning_on = 0;
sc->warning_level = warning_level;
return sc;
}
static const char *format_string(float value)
{
if (fabsf(value) == 0.0)
return "%.0f";
if (fabsf(value) < 0.1)
return "%f";
if (fabsf(value) < 100.0)
return "%1.1f";
return "%.0f";
}
void snis_scaling_strip_chart_draw(struct scaling_strip_chart *sc)
{
int i, index;
float x1, y1, x2, y2, w, h, ox, oy;
char toplabel[20], bottomlabel[20];
sng_set_foreground(sc->color);
w = sc->width;
h = sc->height;
ox = sc->x;
oy = sc->y;
sng_current_draw_rectangle(0, ox, oy, w, h);
char valuelabel[100];
x2 = ox;
y2 = oy;
sprintf(toplabel, format_string(sc->top), sc->top);
sprintf(bottomlabel, format_string(sc->bottom), sc->bottom);
for (i = 0; i < sc->history_size; i++) {
index = (sc->needle + i) % sc->history_size;
if (sc->history[index] > sc->top)
sc->top = sc->history[index];
if (sc->history[index] < sc->bottom)
sc->bottom = sc->history[index];
x1 = x2;
y1 = y2;
x2 = (w * i) / sc->history_size + ox;
y2 = oy + h * (sc->top - sc->history[index] + sc->bottom) / (sc->top - sc->bottom);
sng_current_draw_line(x1, y1, x2, y2);
if (i == sc->history_size - 1) {
sprintf(valuelabel, format_string(sc->history[index]), sc->history[index]);
sng_set_foreground(sc->warn_color);
sng_abs_xy_draw_string(valuelabel, sc->font, ox + w + 5,
y2 + 0.5 * snis_font_lineheight(sc->font));
}
}
sng_set_foreground(sc->color);
sng_abs_xy_draw_string(sc->label, sc->font, ox, oy + h + snis_font_lineheight(sc->font));
sng_abs_xy_draw_string(toplabel, sc->font, ox + w + 5, oy);
sng_abs_xy_draw_string(bottomlabel, sc->font, ox + w + 5, oy + h);
if (sc->warning_on) {
sng_set_foreground(sc->warn_color);
sng_abs_xy_draw_string(sc->warning_msg, sc->font, ox + 10, oy + 0.5 * h);
}
}
void snis_scaling_strip_chart_update(struct scaling_strip_chart *sc, float value)
{
sc->history[sc->needle] = value;
if (value > sc->warning_level)
sc->warning_on = 1;
else
sc->warning_on = 0;
sc->needle = (sc->needle + 1) % sc->history_size;
sc->history[sc->needle] = 0;
}
int snis_scaling_strip_chart_inside(struct scaling_strip_chart *sc, int x, int y)
{
x = sng_pixelx_to_screenx(x);
y = sng_pixelx_to_screenx(y);
if (x < sc->x)
return 0;
if (x > sc->x + sc->width)
return 0;
if (y < sc->y)
return 0;
if (y > sc->y + sc->height)
return 0;
return 1;
}
void snis_scaling_strip_chart_update_position(struct scaling_strip_chart *sc, int x, int y)
{
sc->x = x;
sc->y = y;
}
int snis_scaling_strip_chart_get_x(struct scaling_strip_chart *sc)
{
return sc->x;
}
int snis_scaling_strip_chart_get_y(struct scaling_strip_chart *sc)
{
return sc->y;
}
char *snis_strip_chart_get_label(struct strip_chart *sc)
{
return sc->label;
}
char *snis_scaling_strip_chart_get_label(struct scaling_strip_chart *sc)
{
return sc->label;
}