Merge pull request #442 from raidzero/master

Add battery usage/charge in watts token %consumption%
This commit is contained in:
Michael Carlberg 2017-02-26 14:56:56 +01:00 committed by GitHub
commit 3c3ebc92cc
2 changed files with 27 additions and 1 deletions

View File

@ -42,6 +42,7 @@ namespace modules {
using state_reader = mutex_wrapper<value_reader<bool /* is_charging */>>;
using capacity_reader = mutex_wrapper<value_reader<int /* percentage */>>;
using rate_reader = mutex_wrapper<value_reader<unsigned long /* seconds */>>;
using consumption_reader = mutex_wrapper<value_reader<string /* watts */>>;
public:
explicit battery_module(const bar_settings&, string);
@ -57,6 +58,7 @@ namespace modules {
state current_state();
int current_percentage(state state);
string current_time();
string current_consumption();
void subthread();
private:
@ -76,6 +78,7 @@ namespace modules {
unique_ptr<state_reader> m_state_reader;
unique_ptr<capacity_reader> m_capacity_reader;
unique_ptr<rate_reader> m_rate_reader;
unique_ptr<consumption_reader> m_consumption_reader;
label_t m_label_charging;
label_t m_label_discharging;

View File

@ -81,6 +81,21 @@ namespace modules {
return 0UL;
});
// Make consumption reader
m_consumption_reader = make_unique<consumption_reader>([this] {
unsigned long current{std::strtoul(file_util::contents(m_frate).c_str(), nullptr, 10)};
unsigned long voltage{std::strtoul(file_util::contents(m_fvoltage).c_str(), nullptr, 10)};
float consumption = ((voltage / 1000.0) * (current / 1000.0)) / 1e6;
// convert to string with 2 decimmal places
string rtn(16, '\0'); // 16 should be plenty big. Cant see it needing more than 6/7..
auto written = std::snprintf(&rtn[0], rtn.size(), "%.2f", consumption);
rtn.resize(written);
return rtn;
});
// Load state and capacity level
m_state = current_state();
m_percentage = current_percentage(m_state);
@ -201,6 +216,7 @@ namespace modules {
if (label) {
label->reset_tokens();
label->replace_token("%percentage%", to_string(m_percentage));
label->replace_token("%consumption%", current_consumption());
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
label->replace_token("%time%", current_time());
@ -270,6 +286,13 @@ namespace modules {
return percentage;
}
/**
* Get the current power consumption
*/
string battery_module::current_consumption() {
return read(*m_consumption_reader);
}
/**
* Get estimate of remaining time until fully dis-/charged
*/