feat(temperature): Added temp module and examples

This commit is contained in:
NBonaparte 2016-11-09 22:52:11 -08:00
parent ee68aea773
commit a06b38f534
7 changed files with 126 additions and 1 deletions

View File

@ -39,6 +39,8 @@ set(SETTING_PATH_CPU_INFO "/proc/stat"
CACHE STRING "Path to file containing cpu info")
set(SETTING_PATH_MEMORY_INFO "/proc/meminfo"
CACHE STRING "Path to file containing memory info")
set(SETTING_PATH_TEMPERATURE_INFO "/sys/class/thermal/thermal_zone%zone%/temp"
CACHE STRING "Path to file containing the current temperature")
find_package(ALSA QUIET)
if(NOT ALSA_FOUND)

View File

@ -225,6 +225,15 @@ bar-progress-empty-font = 3
label-time-foreground = #77
[module/temperature]
type = internal/temperature
thermal-zone = 0
warn-temperature = 60
label = 🌡 %temperature%
label-warn = 🔥 %temperature%
[module/powermenu]
type = custom/menu

View File

@ -327,6 +327,15 @@ bar-progress-empty-font = 3
label-time-foreground = #77
[module/temperature]
type = internal/temperature
thermal-zone = 0
warn-temperature = 60
label = 🌡 %temperature%
label-warn = 🔥 %temperature%
[module/powermenu]
type = custom/menu

View File

@ -38,6 +38,7 @@
#define BSPWM_STATUS_PREFIX "@SETTING_BSPWM_STATUS_PREFIX@"
#define PATH_CPU_INFO "@SETTING_PATH_CPU_INFO@"
#define PATH_MEMORY_INFO "@SETTING_PATH_MEMORY_INFO@"
#define PATH_TEMPERATURE_INFO "@SETTING_PATH_TEMPERATURE_INFO@"
auto print_build_info = []() {
// clang-format off
@ -59,7 +60,8 @@ auto print_build_info = []() {
<< "PATH_BACKLIGHT_VAL " << PATH_BACKLIGHT_VAL << "\n"
<< "PATH_BATTERY_CAPACITY " << PATH_BATTERY_CAPACITY << "\n"
<< "PATH_CPU_INFO " << PATH_CPU_INFO << "\n"
<< "PATH_MEMORY_INFO " << PATH_MEMORY_INFO << "\n";
<< "PATH_MEMORY_INFO " << PATH_MEMORY_INFO << "\n"
<< "PATH_TEMPERATURE_INFO " << PATH_TEMPERATURE_INFO << "\n";
// clang-format on
};

View File

@ -0,0 +1,40 @@
#pragma once
#include <istream>
#include "config.hpp"
#include "drawtypes/label.hpp"
#include "modules/meta.hpp"
#include "utils/file.hpp"
LEMONBUDDY_NS
namespace modules {
enum class temp_state { NORMAL = 0, WARN };
class temperature_module : public timer_module<temperature_module> {
public:
using timer_module::timer_module;
void setup();
bool update();
string get_format() const;
bool build(builder* builder, string tag) const;
private:
static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_LABEL_WARN = "<label-warn>";
static constexpr auto FORMAT_WARN = "format-warn";
map<temp_state, label_t> m_label;
string m_path;
int m_zone;
int m_tempwarn;
int m_temp = 0;
};
}
LEMONBUDDY_NS_END

View File

@ -13,6 +13,7 @@
#include "modules/menu.hpp"
#include "modules/script.hpp"
#include "modules/text.hpp"
#include "modules/temperature.hpp"
#include "modules/unsupported.hpp"
#include "modules/xbacklight.hpp"
#include "utils/process.hpp"
@ -352,6 +353,8 @@ void controller::bootstrap_modules() {
module.reset(new volume_module(bar, m_log, m_conf, module_name));
else if (type == "internal/network")
module.reset(new network_module(bar, m_log, m_conf, module_name));
else if (type == "internal/temperature")
module.reset(new temperature_module(bar, m_log, m_conf, module_name));
else if (type == "custom/text")
module.reset(new text_module(bar, m_log, m_conf, module_name));
else if (type == "custom/script")

View File

@ -0,0 +1,60 @@
#include "modules/temperature.hpp"
LEMONBUDDY_NS
namespace modules {
void temperature_module::setup() {
m_zone = m_conf.get<int>(name(), "thermal-zone", 0);
m_tempwarn = m_conf.get<int>(name(), "warn-temperature", 80);
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
m_path = string_util::replace(PATH_TEMPERATURE_INFO, "%zone%", to_string(m_zone));
if (!file_util::exists(m_path))
throw module_error("The file '" + m_path + "' does not exist");
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN});
if (m_formatter->has(TAG_LABEL))
m_label[temp_state::NORMAL] = load_optional_label(m_conf, name(), TAG_LABEL, "%temperature%");
if (m_formatter->has(TAG_LABEL_WARN))
m_label[temp_state::WARN] = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%temperature%");
}
bool temperature_module::update() {
m_temp = std::atoi(file_util::get_contents(m_path).c_str()) / 1000.0f + 0.5f;
// replace tokens
const auto replace_tokens = [&](label_t& label) {
label->reset_tokens();
label->replace_token("%temperature%", to_string(m_temp) + "°C");
};
if (m_label[temp_state::NORMAL]) {
replace_tokens(m_label[temp_state::NORMAL]);
}
if (m_label[temp_state::WARN]) {
replace_tokens(m_label[temp_state::WARN]);
}
return true;
}
string temperature_module::get_format() const {
if(m_temp > m_tempwarn)
return FORMAT_WARN;
else
return DEFAULT_FORMAT;
}
bool temperature_module::build(builder* builder, string tag) const {
if (tag == TAG_LABEL)
builder->node(m_label.at(temp_state::NORMAL));
else if (tag == TAG_LABEL_WARN)
builder->node(m_label.at(temp_state::WARN));
else
return false;
return true;
}
}
LEMONBUDDY_NS_END