Repository navigation
Expand file tree
/
Copy pathwfplug-ups.cpp
More file actions
500 lines (436 loc) · 14.2 KB
/
Copy pathwfplug-ups.cpp
File metadata and controls
500 lines (436 loc) · 14.2 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <algorithm>
#include <cerrno>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <gtkmm.h>
#include <linux/i2c-dev.h>
#include <wf-panel-pi/gtk-utils.hpp>
#include <wf-panel-pi/widget.hpp>
static constexpr int INA219_ADDRS[] = {0x41, 0x40};
static constexpr int REG_CONFIG = 0x00;
static constexpr int REG_BUS_VOLTAGE = 0x02;
static constexpr int REG_POWER = 0x03;
static constexpr int REG_CURRENT = 0x04;
static constexpr int REG_CALIBRATION = 0x05;
static constexpr int CAL_VALUE = 26868;
static constexpr double CURRENT_LSB = 0.1524;
static constexpr double POWER_LSB = 0.003048;
static constexpr double EMPTY_VOLTAGE = 9.3;
static constexpr double FULL_VOLTAGE = 12.3;
static constexpr double DISCHARGE_THRESHOLD_MA = -120.0;
static constexpr const char *SHELL_COLOR = "#4A4A4A";
static constexpr const char *DETAIL_COLOR = "#E6E6E6";
static constexpr const char *FILL_HIGH_COLOR = "#24D353";
static constexpr const char *FILL_MEDIUM_COLOR = "#FFCC00";
static constexpr const char *FILL_LOW_COLOR = "#FF6B35";
static constexpr const char *FILL_CRITICAL_COLOR = "#FF3B30";
static std::string color_to_markup_hex(const std::string& color)
{
Gdk::RGBA rgba;
rgba.set(color);
auto channel = [](double value) {
return std::clamp(static_cast<int>(std::lround(value * 255.0)), 0, 255);
};
char hex[8];
snprintf(hex, sizeof(hex), "#%02X%02X%02X",
channel(rgba.get_red()), channel(rgba.get_green()), channel(rgba.get_blue()));
return hex;
}
struct UpsStatus
{
bool available = false;
int percent = 0;
double voltage = 0.0;
double current_ma = 0.0;
double power_w = 0.0;
bool plugged = false;
std::string error;
};
class Ina219Reader
{
public:
~Ina219Reader()
{
close_fd();
}
UpsStatus read_status()
{
if (fd < 0)
{
open_device();
configure();
}
double voltage = read_bus_voltage();
double current = read_current();
double power = read_power();
double raw_percent = (voltage - EMPTY_VOLTAGE) / (FULL_VOLTAGE - EMPTY_VOLTAGE) * 100.0;
int percent = std::clamp(static_cast<int>(std::lround(raw_percent)), 0, 100);
UpsStatus status;
status.available = true;
status.percent = percent;
status.voltage = voltage;
status.current_ma = current;
status.power_w = power;
status.plugged = current > DISCHARGE_THRESHOLD_MA;
return status;
}
void reset()
{
close_fd();
}
private:
int fd = -1;
int addr = 0;
void close_fd()
{
if (fd >= 0)
{
close(fd);
fd = -1;
}
}
void open_device()
{
for (int bus = 1; bus <= 8; ++bus)
{
std::string path = "/dev/i2c-" + std::to_string(bus);
int candidate = open(path.c_str(), O_RDWR);
if (candidate < 0)
{
continue;
}
for (int candidate_addr : INA219_ADDRS)
{
if (ioctl(candidate, I2C_SLAVE, candidate_addr) == 0)
{
fd = candidate;
addr = candidate_addr;
return;
}
}
close(candidate);
}
throw std::runtime_error("INA219 not found");
}
void write_register(uint8_t reg, uint16_t value)
{
uint8_t data[3] = {reg, static_cast<uint8_t>((value >> 8) & 0xff), static_cast<uint8_t>(value & 0xff)};
if (write(fd, data, sizeof(data)) != static_cast<ssize_t>(sizeof(data)))
{
throw std::runtime_error("I2C write failed");
}
}
uint16_t read_register(uint8_t reg)
{
if (write(fd, ®, 1) != 1)
{
throw std::runtime_error("I2C select register failed");
}
uint8_t data[2] = {0, 0};
if (read(fd, data, sizeof(data)) != static_cast<ssize_t>(sizeof(data)))
{
throw std::runtime_error("I2C read failed");
}
return (static_cast<uint16_t>(data[0]) << 8) | data[1];
}
static int16_t signed_register(uint16_t value)
{
return static_cast<int16_t>(value);
}
void configure()
{
uint16_t config = (0x00 << 13) | (0x01 << 11) | (0x0D << 7) | (0x0D << 3) | 0x07;
write_register(REG_CALIBRATION, CAL_VALUE);
write_register(REG_CONFIG, config);
}
double read_bus_voltage()
{
write_register(REG_CALIBRATION, CAL_VALUE);
read_register(REG_BUS_VOLTAGE);
uint16_t value = read_register(REG_BUS_VOLTAGE);
return (value >> 3) * 0.004;
}
double read_current()
{
write_register(REG_CALIBRATION, CAL_VALUE);
return signed_register(read_register(REG_CURRENT)) * CURRENT_LSB;
}
double read_power()
{
write_register(REG_CALIBRATION, CAL_VALUE);
return signed_register(read_register(REG_POWER)) * POWER_LSB;
}
};
class BatteryIcon : public Gtk::DrawingArea
{
public:
BatteryIcon()
{
set_size_request(28, 18);
}
void set_status(int new_percent, bool new_plugged, bool new_available)
{
percent = std::clamp(new_percent, 0, 100);
plugged = new_plugged;
available = new_available;
queue_draw();
}
void set_colors(const std::string& new_shell_color, const std::string& new_bolt_color,
const std::string& new_high_color, const std::string& new_medium_color,
const std::string& new_low_color, const std::string& new_critical_color)
{
shell_color = new_shell_color;
bolt_color = new_bolt_color;
high_color = new_high_color;
medium_color = new_medium_color;
low_color = new_low_color;
critical_color = new_critical_color;
queue_draw();
}
protected:
bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) override
{
const double x = 1.0;
const double body_w = 22.0;
const double body_h = 12.0;
const double y = std::max(0.0, (get_allocated_height() - body_h) / 2.0);
const double cap_w = 2.2;
const double cap_h = 5.0;
const double radius = 3.8;
const std::string color = available ? color_for(percent) : "#AEB6C2";
cr->set_line_width(1.35);
set_source(cr, shell_color);
rounded_rect(cr, x, y, body_w, body_h, radius);
cr->stroke();
rounded_rect(cr, x + body_w + 1.0, y + (body_h - cap_h) / 2.0, cap_w, cap_h, 1.4);
cr->fill();
if (available)
{
const double inset = 2.4;
const double fill_w = std::max(1.2, (body_w - inset * 2.0) * percent / 100.0);
set_source(cr, color);
rounded_rect(cr, x + inset, y + inset, fill_w, body_h - inset * 2.0, 2.2);
cr->fill();
}
if (available && plugged)
{
draw_bolt(cr, x + body_w / 2.0, y + body_h / 2.0);
}
return true;
}
private:
int percent = 0;
bool plugged = false;
bool available = false;
std::string shell_color = SHELL_COLOR;
std::string bolt_color = DETAIL_COLOR;
std::string high_color = FILL_HIGH_COLOR;
std::string medium_color = FILL_MEDIUM_COLOR;
std::string low_color = FILL_LOW_COLOR;
std::string critical_color = FILL_CRITICAL_COLOR;
std::string color_for(int percent) const
{
if (percent > 70) return high_color;
if (percent > 40) return medium_color;
if (percent > 30) return low_color;
return critical_color;
}
static void set_source(const Cairo::RefPtr<Cairo::Context>& cr, const std::string& color)
{
Gdk::RGBA rgba;
rgba.set(color);
cr->set_source_rgba(rgba.get_red(), rgba.get_green(), rgba.get_blue(), rgba.get_alpha());
}
static void rounded_rect(const Cairo::RefPtr<Cairo::Context>& cr,
double x, double y, double w, double h, double r)
{
cr->begin_new_sub_path();
cr->arc(x + w - r, y + r, r, -M_PI / 2.0, 0.0);
cr->arc(x + w - r, y + h - r, r, 0.0, M_PI / 2.0);
cr->arc(x + r, y + h - r, r, M_PI / 2.0, M_PI);
cr->arc(x + r, y + r, r, M_PI, 3.0 * M_PI / 2.0);
cr->close_path();
}
void draw_bolt(const Cairo::RefPtr<Cairo::Context>& cr, double cx, double cy)
{
set_source(cr, bolt_color);
cr->move_to(cx + 1.0, cy - 5.0);
cr->line_to(cx - 3.0, cy + 0.4);
cr->line_to(cx - 0.6, cy + 0.4);
cr->line_to(cx - 1.6, cy + 5.0);
cr->line_to(cx + 3.2, cy - 1.0);
cr->line_to(cx + 0.6, cy - 1.0);
cr->close_path();
cr->fill();
}
};
class WayfireUPS : public WayfireWidget
{
public:
void init(Gtk::HBox *container) override
{
event_box.set_name("ups");
event_box.set_visible_window(false);
event_box.add(box);
add_longpress_default(event_box);
connect_option_callbacks();
apply_style_options();
box.set_spacing(3);
box.set_margin_start(4);
box.set_margin_end(4);
box.pack_start(icon, false, false);
box.pack_start(label, false, false);
label.set_xalign(0.5);
label.set_yalign(0.5);
container->pack_start(event_box, false, false);
refresh_display();
timer = g_timeout_add_seconds(2, [](gpointer data) -> gboolean {
static_cast<WayfireUPS *>(data)->refresh_display();
return G_SOURCE_CONTINUE;
}, this);
event_box.show_all();
}
~WayfireUPS() override
{
if (timer)
{
g_source_remove(timer);
timer = 0;
}
}
void handle_config_reload() override
{
apply_style_options();
refresh_display();
}
private:
Gtk::EventBox event_box;
Gtk::HBox box;
BatteryIcon icon;
Gtk::Label label;
Ina219Reader reader;
WfOption<std::string> shell_color{"panel/ups_shell_color"};
WfOption<std::string> bolt_color{"panel/ups_bolt_color"};
WfOption<std::string> text_color{"panel/ups_text_color"};
WfOption<bool> text_bold{"panel/ups_text_bold"};
WfOption<std::string> high_color{"panel/ups_high_color"};
WfOption<std::string> medium_color{"panel/ups_medium_color"};
WfOption<std::string> low_color{"panel/ups_low_color"};
WfOption<std::string> critical_color{"panel/ups_critical_color"};
guint timer = 0;
void connect_option_callbacks()
{
auto on_update = [this]() {
apply_style_options();
refresh_display();
};
shell_color.set_callback(on_update);
bolt_color.set_callback(on_update);
text_color.set_callback(on_update);
text_bold.set_callback(on_update);
high_color.set_callback(on_update);
medium_color.set_callback(on_update);
low_color.set_callback(on_update);
critical_color.set_callback(on_update);
}
void apply_style_options()
{
icon.set_colors(shell_color.value(), bolt_color.value(),
high_color.value(), medium_color.value(),
low_color.value(), critical_color.value());
}
std::string text_weight() const
{
return text_bold.value() ? "bold" : "normal";
}
void refresh_display()
{
try
{
UpsStatus status = reader.read_status();
icon.set_status(status.percent, status.plugged, true);
std::ostringstream markup;
markup << "<span foreground=\"" << color_to_markup_hex(text_color.value())
<< "\" weight=\"" << text_weight() << "\">" << status.percent << "%</span>";
label.set_markup(markup.str());
char tip[160];
snprintf(tip, sizeof(tip), "%.2fV / %.0fmA / %.3fW / %s",
status.voltage, status.current_ma, status.power_w,
status.plugged ? "external power" : "discharging");
event_box.set_tooltip_text(tip);
}
catch (const std::exception &exc)
{
reader.reset();
icon.set_status(0, false, false);
label.set_markup("<span foreground=\"#AEB6C2\" weight=\"bold\">--</span>");
event_box.set_tooltip_text(exc.what());
}
}
#if 0
void update_display()
{
try
{
UpsStatus status = reader.read_status();
std::string bolt = status.plugged ? "⚡" : "";
std::ostringstream markup;
markup << "<span foreground=\"" << color_for(status.percent)
<< "\" weight=\"bold\">UPS " << status.percent << "%" << bolt << "</span>";
label.set_markup(markup.str());
char tip[160];
snprintf(tip, sizeof(tip), "%.2fV · %.0fmA · %.3fW · %s",
status.voltage, status.current_ma, status.power_w,
status.plugged ? "external power" : "discharging");
label.set_tooltip_text(tip);
}
catch (const std::exception &exc)
{
reader.reset();
label.set_markup("<span foreground=\"#AEB6C2\" weight=\"bold\">UPS --</span>");
label.set_tooltip_text(exc.what());
}
}
#endif
};
extern "C"
{
static conf_table_t conf_table[] = {
{CONF_TYPE_BOOL, "show_label", "Show label", nullptr},
{CONF_TYPE_COLOUR, "shell_color", "Battery shell color", nullptr},
{CONF_TYPE_COLOUR, "bolt_color", "Charging bolt color", nullptr},
{CONF_TYPE_COLOUR, "text_color", "Text color", nullptr},
{CONF_TYPE_BOOL, "text_bold", "Bold text", nullptr},
{CONF_TYPE_COLOUR, "high_color", "High battery color", nullptr},
{CONF_TYPE_COLOUR, "medium_color", "Medium battery color", nullptr},
{CONF_TYPE_COLOUR, "low_color", "Low battery color", nullptr},
{CONF_TYPE_COLOUR, "critical_color", "Critical battery color", nullptr},
{CONF_TYPE_NONE, nullptr, nullptr, nullptr},
};
WayfireWidget *create()
{
return new WayfireUPS();
}
void destroy(WayfireWidget *widget)
{
delete widget;
}
const char *display_name()
{
return "UPS";
}
const char *package_name()
{
return "wfplug_ups";
}
const conf_table_t *config_params()
{
return conf_table;
}
}