Add battery usage/charge in watts token %consumption%

This commit is contained in:
raidzero 2017-02-16 14:35:43 -07:00
parent bd8e748399
commit 93c425fdfe
2 changed files with 31 additions and 5 deletions

View File

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

View File

@ -81,6 +81,21 @@ namespace modules {
return 0UL; 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 // Load state and capacity level
m_state = current_state(); m_state = current_state();
m_percentage = current_percentage(m_state); m_percentage = current_percentage(m_state);
@ -201,6 +216,7 @@ namespace modules {
if (label) { if (label) {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%percentage%", to_string(m_percentage)); label->replace_token("%percentage%", to_string(m_percentage));
label->replace_token("%consumption%", current_consumption());
if (m_state != battery_module::state::FULL && !m_timeformat.empty()) { if (m_state != battery_module::state::FULL && !m_timeformat.empty()) {
label->replace_token("%time%", current_time()); label->replace_token("%time%", current_time());
@ -270,6 +286,13 @@ namespace modules {
return percentage; 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 * Get estimate of remaining time until fully dis-/charged
*/ */