Skip to content

Commit

Permalink
Heat index
Browse files Browse the repository at this point in the history
by ClementGre
  • Loading branch information
xMasterX committed Jun 24, 2023
1 parent 34ff4c7 commit 35f9533
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 11 deletions.
13 changes: 9 additions & 4 deletions applications/external/unitemp/Sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,16 @@ UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
UNITEMP_DEBUG("Sensor %s update status %d", sensor->name, sensor->status);
}

if(app->settings.temp_unit == UT_TEMP_FAHRENHEIT && sensor->status == UT_SENSORSTATUS_OK) {
uintemp_celsiumToFarengate(sensor);
}

if(sensor->status == UT_SENSORSTATUS_OK) {
if(app->settings.heat_index &&
((sensor->type->datatype & (UT_TEMPERATURE | UT_HUMIDITY)) ==
(UT_TEMPERATURE | UT_HUMIDITY))) {
unitemp_calculate_heat_index(sensor);
}
if(app->settings.temp_unit == UT_TEMP_FAHRENHEIT) {
uintemp_celsiumToFarengate(sensor);
}

sensor->temp += sensor->temp_offset / 10.f;
if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) {
unitemp_pascalToMmHg(sensor);
Expand Down
1 change: 1 addition & 0 deletions applications/external/unitemp/Sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ typedef struct Sensor {
char* name;
//Температура
float temp;
float heat_index;
//Относительная влажность
float hum;
//Атмосферное давление
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions applications/external/unitemp/unitemp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,31 @@ Unitemp* app;

void uintemp_celsiumToFarengate(Sensor* sensor) {
sensor->temp = sensor->temp * (9.0 / 5.0) + 32;
sensor->heat_index = sensor->heat_index * (9.0 / 5.0) + 32;
}

static float heat_index_consts[9] = {
-42.379f,
2.04901523f,
10.14333127f,
-0.22475541f,
-0.00683783f,
-0.05481717f,
0.00122874f,
0.00085282f,
-0.00000199f};
void unitemp_calculate_heat_index(Sensor* sensor) {
// temp should be in Celsius, heat index will be in Celsius
float temp = sensor->temp * (9.0 / 5.0) + 32.0f;
float hum = sensor->hum;
sensor->heat_index =
(heat_index_consts[0] + heat_index_consts[1] * temp + heat_index_consts[2] * hum +
heat_index_consts[3] * temp * hum + heat_index_consts[4] * temp * temp +
heat_index_consts[5] * hum * hum + heat_index_consts[6] * temp * temp * hum +
heat_index_consts[7] * temp * hum * hum + heat_index_consts[8] * temp * temp * hum * hum -
32.0f) *
(5.0 / 9.0);
}
void unitemp_pascalToMmHg(Sensor* sensor) {
sensor->pressure = sensor->pressure * 0.007500638;
}
Expand Down Expand Up @@ -71,6 +94,7 @@ bool unitemp_saveSettings(void) {
app->file_stream, "INFINITY_BACKLIGHT %d\n", app->settings.infinityBacklight);
stream_write_format(app->file_stream, "TEMP_UNIT %d\n", app->settings.temp_unit);
stream_write_format(app->file_stream, "PRESSURE_UNIT %d\n", app->settings.pressure_unit);
stream_write_format(app->file_stream, "HEAT_INDEX %d\n", app->settings.heat_index);

//Закрытие потока и освобождение памяти
file_stream_close(app->file_stream);
Expand Down Expand Up @@ -166,6 +190,11 @@ bool unitemp_loadSettings(void) {
int p = 0;
sscanf(((char*)(file_buf + line_end)), "\nPRESSURE_UNIT %d", &p);
app->settings.pressure_unit = p;
} else if(!strcmp(buff, "HEAT_INDEX")) {
//Чтение значения параметра
int p = 0;
sscanf(((char*)(file_buf + line_end)), "\nHEAT_INDEX %d", &p);
app->settings.heat_index = p;
} else {
FURI_LOG_W(APP_NAME, "Unknown settings parameter: %s", buff);
}
Expand Down Expand Up @@ -203,6 +232,7 @@ static bool unitemp_alloc(void) {
app->settings.infinityBacklight = true; //Подсветка горит всегда
app->settings.temp_unit = UT_TEMP_CELSIUS; //Единица измерения температуры - градусы Цельсия
app->settings.pressure_unit = UT_PRESSURE_MM_HG; //Единица измерения давления - мм рт. ст.
app->settings.heat_index = false;

app->gui = furi_record_open(RECORD_GUI);
//Диспетчер окон
Expand Down
11 changes: 10 additions & 1 deletion applications/external/unitemp/unitemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
//Имя приложения
#define APP_NAME "Unitemp"
//Версия приложения
#define UNITEMP_APP_VER "1.3"
#define UNITEMP_APP_VER "1.4"
//Путь хранения файлов плагина
#define APP_PATH_FOLDER "/ext/unitemp"
//Имя файла с настройками
Expand Down Expand Up @@ -80,6 +80,8 @@ typedef struct {
tempMeasureUnit temp_unit;
//Единица измерения давления
pressureMeasureUnit pressure_unit;
// Do calculate and show heat index
bool heat_index;
//Последнее состояние OTG
bool lastOTGState;
} UnitempSettings;
Expand Down Expand Up @@ -111,6 +113,13 @@ typedef struct {

/* Объявление прототипов функций */

/**
* @brief Calculates the heat index in Celsius from the temperature and humidity and stores it in the sensor heat_index field
*
* @param sensor The sensor struct, with temperature in Celcius and humidity in percent
*/
void unitemp_calculate_heat_index(Sensor* sensor);

/**
* @brief Перевод значения температуры датчика из Цельсия в Фаренгейты
*
Expand Down
50 changes: 44 additions & 6 deletions applications/external/unitemp/views/General_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,33 @@ static void _draw_humidity(Canvas* canvas, Sensor* sensor, const uint8_t pos[2])
canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 4, pos[1] + 10 + 7, "%");
}

static void _draw_heat_index(Canvas* canvas, Sensor* sensor, const uint8_t pos[2]) {
canvas_draw_rframe(canvas, pos[0], pos[1], 54, 20, 3);
canvas_draw_rframe(canvas, pos[0], pos[1], 54, 19, 3);

canvas_draw_icon(canvas, pos[0] + 3, pos[1] + 3, &I_heat_index_11x14);

int16_t heat_index_int = sensor->heat_index;
int8_t heat_index_dec = abs((int16_t)(sensor->heat_index * 10) % 10);

snprintf(app->buff, BUFF_SIZE, "%d", heat_index_int);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(
canvas,
pos[0] + 27 + ((sensor->heat_index <= -10 || sensor->heat_index > 99) ? 5 : 0),
pos[1] + 10,
AlignCenter,
AlignCenter,
app->buff);

if(heat_index_int <= 99) {
uint8_t int_len = canvas_string_width(canvas, app->buff);
snprintf(app->buff, BUFF_SIZE, ".%d", heat_index_dec);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 2, pos[1] + 10 + 7, app->buff);
}
}

static void _draw_pressure(Canvas* canvas, Sensor* sensor) {
const uint8_t x = 29, y = 39;
//Рисование рамки
Expand Down Expand Up @@ -320,12 +347,23 @@ static void _draw_carousel_values(Canvas* canvas) {
ColorWhite);
break;
case UT_DATA_TYPE_TEMP_HUM:
_draw_temperature(
canvas,
unitemp_sensor_getActive(generalview_sensor_index),
temp_positions[1][0],
temp_positions[1][1],
ColorWhite);
if(!app->settings.heat_index) {
_draw_temperature(
canvas,
unitemp_sensor_getActive(generalview_sensor_index),
temp_positions[1][0],
temp_positions[1][1],
ColorWhite);
} else {
_draw_temperature(
canvas,
unitemp_sensor_getActive(generalview_sensor_index),
temp_positions[2][0],
temp_positions[2][1],
ColorWhite);
_draw_heat_index(
canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[1]);
}
_draw_humidity(
canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[0]);
break;
Expand Down
15 changes: 15 additions & 0 deletions applications/external/unitemp/views/Settings_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ static VariableItemList* variable_item_list;
static const char states[2][9] = {"Auto", "Infinity"};
static const char temp_units[UT_TEMP_COUNT][3] = {"*C", "*F"};
static const char pressure_units[UT_PRESSURE_COUNT][6] = {"mm Hg", "in Hg", "kPa", "hPA"};
static const char heat_index_bool[2][4] = {"OFF", "ON"};

//Элемент списка - бесконечная подсветка
VariableItem* infinity_backlight_item;
//Единица измерения температуры
VariableItem* temperature_unit_item;
//Единица измерения давления
VariableItem* pressure_unit_item;

VariableItem* heat_index_item;
#define VIEW_ID UnitempViewSettings

/**
Expand All @@ -57,6 +60,7 @@ static uint32_t _exit_callback(void* context) {
(bool)variable_item_get_current_value_index(infinity_backlight_item);
app->settings.temp_unit = variable_item_get_current_value_index(temperature_unit_item);
app->settings.pressure_unit = variable_item_get_current_value_index(pressure_unit_item);
app->settings.heat_index = variable_item_get_current_value_index(heat_index_item);
unitemp_saveSettings();
unitemp_loadSettings();

Expand Down Expand Up @@ -90,6 +94,11 @@ static void _setting_change_callback(VariableItem* item) {
pressure_unit_item,
pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);
}
if(item == heat_index_item) {
variable_item_set_current_value_text(
heat_index_item,
heat_index_bool[variable_item_get_current_value_index(heat_index_item)]);
}
}

/**
Expand All @@ -106,6 +115,8 @@ void unitemp_Settings_alloc(void) {
variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app);
pressure_unit_item = variable_item_list_add(
variable_item_list, "Press. unit", UT_PRESSURE_COUNT, _setting_change_callback, app);
heat_index_item = variable_item_list_add(
variable_item_list, "Calc. heat index", 2, _setting_change_callback, app);

//Добавление колбека на нажатие средней кнопки
variable_item_list_set_enter_callback(variable_item_list, _enter_callback, app);
Expand Down Expand Up @@ -139,6 +150,10 @@ void unitemp_Settings_switch(void) {
pressure_unit_item,
pressure_units[variable_item_get_current_value_index(pressure_unit_item)]);

variable_item_set_current_value_index(heat_index_item, (uint8_t)app->settings.heat_index);
variable_item_set_current_value_text(
heat_index_item, heat_index_bool[variable_item_get_current_value_index(heat_index_item)]);

view_dispatcher_switch_to_view(app->view_dispatcher, VIEW_ID);
}

Expand Down

0 comments on commit 35f9533

Please sign in to comment.