From 2a9d5db1791e8d429a50c8b1e36df1756f57397a Mon Sep 17 00:00:00 2001 From: Christoph Schiessl Date: Mon, 23 Apr 2018 20:24:02 +0200 Subject: [PATCH] feat(battery): implement animation-discharging --- include/modules/battery.hpp | 2 ++ src/modules/battery.cpp | 20 +++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/modules/battery.hpp b/include/modules/battery.hpp index 57dce08e..94036e32 100644 --- a/include/modules/battery.hpp +++ b/include/modules/battery.hpp @@ -67,6 +67,7 @@ namespace modules { static constexpr const char* FORMAT_FULL{"format-full"}; static constexpr const char* TAG_ANIMATION_CHARGING{""}; + static constexpr const char* TAG_ANIMATION_DISCHARGING{""}; static constexpr const char* TAG_BAR_CAPACITY{""}; static constexpr const char* TAG_RAMP_CAPACITY{""}; static constexpr const char* TAG_LABEL_CHARGING{""}; @@ -84,6 +85,7 @@ namespace modules { label_t m_label_discharging; label_t m_label_full; animation_t m_animation_charging; + animation_t m_animation_discharging; progressbar_t m_bar_capacity; ramp_t m_ramp_capacity; diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp index d9f4b44d..067e37e3 100644 --- a/src/modules/battery.cpp +++ b/src/modules/battery.cpp @@ -114,13 +114,16 @@ namespace modules { // Add formats and elements m_formatter->add(FORMAT_CHARGING, TAG_LABEL_CHARGING, {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_ANIMATION_CHARGING, TAG_LABEL_CHARGING}); - m_formatter->add( - FORMAT_DISCHARGING, TAG_LABEL_DISCHARGING, {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_LABEL_DISCHARGING}); + m_formatter->add(FORMAT_DISCHARGING, TAG_LABEL_DISCHARGING, + {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_ANIMATION_DISCHARGING, TAG_LABEL_DISCHARGING}); m_formatter->add(FORMAT_FULL, TAG_LABEL_FULL, {TAG_BAR_CAPACITY, TAG_RAMP_CAPACITY, TAG_LABEL_FULL}); if (m_formatter->has(TAG_ANIMATION_CHARGING, FORMAT_CHARGING)) { m_animation_charging = load_animation(m_conf, name(), TAG_ANIMATION_CHARGING); } + if (m_formatter->has(TAG_ANIMATION_DISCHARGING, FORMAT_DISCHARGING)) { + m_animation_discharging = load_animation(m_conf, name(), TAG_ANIMATION_DISCHARGING); + } if (m_formatter->has(TAG_BAR_CAPACITY)) { m_bar_capacity = load_progressbar(m_bar, m_conf, name(), TAG_BAR_CAPACITY); } @@ -256,6 +259,8 @@ namespace modules { bool battery_module::build(builder* builder, const string& tag) const { if (tag == TAG_ANIMATION_CHARGING) { builder->node(m_animation_charging->get()); + } else if (tag == TAG_ANIMATION_DISCHARGING) { + builder->node(m_animation_discharging->get()); } else if (tag == TAG_BAR_CAPACITY) { builder->node(m_bar_capacity->output(m_percentage)); } else if (tag == TAG_RAMP_CAPACITY) { @@ -327,21 +332,26 @@ namespace modules { } /** - * Subthread runner that emit update events - * to refresh in case it is used. + * Subthread runner that emit update events to refresh + * or in case they are used. Note, that it is ok to + * use a single thread, because the two animations are never shown at the + * same time. */ void battery_module::subthread() { chrono::duration dur{0.0}; if (m_animation_charging) { dur += chrono::milliseconds{m_animation_charging->framerate()}; + } else if (m_animation_discharging) { + dur += chrono::milliseconds{m_animation_discharging->framerate()}; } else { dur += 1s; } while (running()) { for (int i = 0; running() && i < dur.count(); ++i) { - if (m_state == battery_module::state::CHARGING) { + if (m_state == battery_module::state::CHARGING || + m_state == battery_module::state::DISCHARGING) { broadcast(); } sleep(dur);