2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* temphelper.c: aid in converting temperature units
|
2008-12-07 19:08:29 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Phil Sutter <Phil@nwl.cc>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
*/
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "temphelper.h"
|
2008-12-07 19:08:29 +00:00
|
|
|
#include <sys/types.h>
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cctype>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "config.h"
|
2009-02-18 05:00:23 +00:00
|
|
|
#include "conky.h"
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
template <>
|
2010-08-25 19:26:14 +00:00
|
|
|
conky::lua_traits<TEMP_UNIT>::Map conky::lua_traits<TEMP_UNIT>::map = {
|
2018-05-12 16:03:00 +00:00
|
|
|
{"celsius", TEMP_CELSIUS}, {"fahrenheit", TEMP_FAHRENHEIT}};
|
2010-08-25 19:26:14 +00:00
|
|
|
|
|
|
|
static conky::simple_config_setting<TEMP_UNIT> output_unit("temperature_unit",
|
2018-05-12 16:03:00 +00:00
|
|
|
TEMP_CELSIUS, true);
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static double fahrenheit_to_celsius(double n) { return ((n - 32) * 5 / 9); }
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static double celsius_to_fahrenheit(double n) { return ((n * 9 / 5) + 32); }
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
static double convert_temp_output(double n, enum TEMP_UNIT input_unit) {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (input_unit == output_unit.get(*state)) {
|
|
|
|
return n;
|
|
|
|
}
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
switch (output_unit.get(*state)) {
|
|
|
|
case TEMP_CELSIUS:
|
|
|
|
return fahrenheit_to_celsius(n);
|
|
|
|
case TEMP_FAHRENHEIT:
|
|
|
|
return celsius_to_fahrenheit(n);
|
|
|
|
}
|
|
|
|
/* NOT REACHED */
|
|
|
|
return 0.0;
|
2008-12-07 19:08:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
int temp_print(char *p, size_t p_max_size, double n,
|
|
|
|
enum TEMP_UNIT input_unit) {
|
|
|
|
int out;
|
|
|
|
size_t plen;
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
out = round_to_int_temp(convert_temp_output(n, input_unit));
|
|
|
|
plen = spaced_print(p, p_max_size, "%d", 3, out);
|
2008-12-07 19:08:29 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
return static_cast<int>(!(plen >= p_max_size));
|
2008-12-07 19:08:29 +00:00
|
|
|
}
|