fix(battery): animation framerate
This commit is contained in:
parent
d3844c40b6
commit
e6e010e45b
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#include "common.hpp"
|
#include "common.hpp"
|
||||||
@ -14,32 +15,33 @@ namespace chrono = std::chrono;
|
|||||||
namespace drawtypes {
|
namespace drawtypes {
|
||||||
class animation : public non_copyable_mixin<animation> {
|
class animation : public non_copyable_mixin<animation> {
|
||||||
public:
|
public:
|
||||||
explicit animation(int framerate_ms) : m_framerate_ms(framerate_ms) {}
|
explicit animation(unsigned int framerate_ms) : m_framerate_ms(framerate_ms) {}
|
||||||
explicit animation(vector<label_t>&& frames, int framerate_ms)
|
explicit animation(vector<label_t>&& frames, int framerate_ms)
|
||||||
: m_frames(forward<decltype(frames)>(frames))
|
: m_frames(move(frames))
|
||||||
, m_framerate_ms(framerate_ms)
|
, m_framerate_ms(framerate_ms)
|
||||||
, m_framecount(m_frames.size())
|
, m_framecount(m_frames.size())
|
||||||
, m_lastupdate(chrono::system_clock::now()) {}
|
, m_frame(m_frames.size() - 1) {}
|
||||||
|
|
||||||
void add(label_t&& frame);
|
void add(label_t&& frame);
|
||||||
label_t get();
|
void increment();
|
||||||
int framerate();
|
|
||||||
operator bool();
|
label_t get() const;
|
||||||
|
unsigned int framerate() const;
|
||||||
|
|
||||||
|
explicit operator bool() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void tick();
|
|
||||||
|
|
||||||
vector<label_t> m_frames;
|
vector<label_t> m_frames;
|
||||||
int m_framerate_ms = 1000;
|
|
||||||
int m_frame = 0;
|
unsigned int m_framerate_ms = 1000;
|
||||||
int m_framecount = 0;
|
size_t m_framecount = 0;
|
||||||
chrono::system_clock::time_point m_lastupdate;
|
std::atomic_size_t m_frame{0_z};
|
||||||
};
|
};
|
||||||
|
|
||||||
using animation_t = shared_ptr<animation>;
|
using animation_t = shared_ptr<animation>;
|
||||||
|
|
||||||
animation_t load_animation(
|
animation_t load_animation(
|
||||||
const config& conf, const string& section, string name = "animation", bool required = true);
|
const config& conf, const string& section, string name = "animation", bool required = true);
|
||||||
}
|
} // namespace drawtypes
|
||||||
|
|
||||||
POLYBAR_NS_END
|
POLYBAR_NS_END
|
||||||
|
@ -8,33 +8,27 @@ namespace drawtypes {
|
|||||||
void animation::add(label_t&& frame) {
|
void animation::add(label_t&& frame) {
|
||||||
m_frames.emplace_back(forward<decltype(frame)>(frame));
|
m_frames.emplace_back(forward<decltype(frame)>(frame));
|
||||||
m_framecount = m_frames.size();
|
m_framecount = m_frames.size();
|
||||||
|
m_frame = m_framecount - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
label_t animation::get() {
|
label_t animation::get() const {
|
||||||
tick();
|
|
||||||
return m_frames[m_frame];
|
return m_frames[m_frame];
|
||||||
}
|
}
|
||||||
|
|
||||||
int animation::framerate() {
|
unsigned int animation::framerate() const {
|
||||||
return m_framerate_ms;
|
return m_framerate_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
animation::operator bool() {
|
animation::operator bool() const {
|
||||||
return !m_frames.empty();
|
return !m_frames.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void animation::tick() {
|
void animation::increment() {
|
||||||
auto now = chrono::system_clock::now();
|
auto tmp = m_frame.load();
|
||||||
auto diff = chrono::duration_cast<chrono::milliseconds>(now - m_lastupdate);
|
++tmp;
|
||||||
|
tmp %= m_framecount;
|
||||||
|
|
||||||
if (diff.count() < m_framerate_ms) {
|
m_frame = tmp;
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (++m_frame >= m_framecount) {
|
|
||||||
m_frame = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_lastupdate = now;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,6 +58,6 @@ namespace drawtypes {
|
|||||||
|
|
||||||
return factory_util::shared<animation>(move(vec), framerate);
|
return factory_util::shared<animation>(move(vec), framerate);
|
||||||
}
|
}
|
||||||
}
|
} // namespace drawtypes
|
||||||
|
|
||||||
POLYBAR_NS_END
|
POLYBAR_NS_END
|
||||||
|
@ -345,19 +345,25 @@ namespace modules {
|
|||||||
m_log.trace("%s: Start of subthread", name());
|
m_log.trace("%s: Start of subthread", name());
|
||||||
|
|
||||||
while (running()) {
|
while (running()) {
|
||||||
int framerate = 1000; // milliseconds
|
auto now = chrono::system_clock::now();
|
||||||
|
auto framerate = 1000U; // milliseconds
|
||||||
if (m_state == battery_module::state::CHARGING) {
|
if (m_state == battery_module::state::CHARGING) {
|
||||||
|
m_animation_charging->increment();
|
||||||
broadcast();
|
broadcast();
|
||||||
framerate = m_animation_charging->framerate();
|
framerate = m_animation_charging->framerate();
|
||||||
} else if (m_state == battery_module::state::DISCHARGING) {
|
} else if (m_state == battery_module::state::DISCHARGING) {
|
||||||
|
m_animation_discharging->increment();
|
||||||
broadcast();
|
broadcast();
|
||||||
framerate = m_animation_discharging->framerate();
|
framerate = m_animation_discharging->framerate();
|
||||||
}
|
}
|
||||||
this_thread::sleep_for(std::chrono::milliseconds(framerate));
|
|
||||||
|
// We don't count the the first part of the loop to be as close as possible to the framerate.
|
||||||
|
now += chrono::milliseconds(framerate);
|
||||||
|
this_thread::sleep_until(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_log.trace("%s: End of subthread", name());
|
m_log.trace("%s: End of subthread", name());
|
||||||
}
|
}
|
||||||
}
|
} // namespace modules
|
||||||
|
|
||||||
POLYBAR_NS_END
|
POLYBAR_NS_END
|
||||||
|
@ -174,13 +174,16 @@ namespace modules {
|
|||||||
|
|
||||||
void network_module::subthread_routine() {
|
void network_module::subthread_routine() {
|
||||||
const chrono::milliseconds framerate{m_animation_packetloss->framerate()};
|
const chrono::milliseconds framerate{m_animation_packetloss->framerate()};
|
||||||
const auto dur = chrono::duration<double>(framerate);
|
|
||||||
|
|
||||||
while (running()) {
|
while (running()) {
|
||||||
|
auto now = chrono::system_clock::now();
|
||||||
if (m_connected && m_packetloss) {
|
if (m_connected && m_packetloss) {
|
||||||
|
m_animation_packetloss->increment();
|
||||||
broadcast();
|
broadcast();
|
||||||
}
|
}
|
||||||
sleep(dur);
|
|
||||||
|
now += framerate;
|
||||||
|
this_thread::sleep_until(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_log.trace("%s: Reached end of network subthread", name());
|
m_log.trace("%s: Reached end of network subthread", name());
|
||||||
|
Loading…
Reference in New Issue
Block a user