Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added hPa and dewpoint, fixed typos #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,16 @@ UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
UNITEMP_DEBUG("Sensor %s update status %d", sensor->name, sensor->status);
}

if(app->settings.humidity_unit == UT_HUMIDITY_DEWPOINT && app->settings.temp_unit == UT_TEMP_CELSIUS && sensor->status == UT_SENSORSTATUS_OK) {
unitemp_rhToDewpointC(sensor);
}

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

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

if(sensor->status == UT_SENSORSTATUS_OK) {
Expand All @@ -639,6 +647,8 @@ UnitempStatus unitemp_sensor_updateData(Sensor* sensor) {
unitemp_pascalToInHg(sensor);
} else if(app->settings.pressure_unit == UT_PRESSURE_KPA) {
unitemp_pascalToKPa(sensor);
} else if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
unitemp_pascalToHPa(sensor);
}
}
return sensor->status;
Expand Down
4 changes: 2 additions & 2 deletions interfaces/OneWireSensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static uint8_t onewire_enum[8] = {0};
// последний нулевой бит, где была неоднозначность (нумеруя с единицы)
static uint8_t onewire_enum_fork_bit = 65;

OneWireBus* uintemp_onewire_bus_alloc(const GPIO* gpio) {
OneWireBus* unitemp_onewire_bus_alloc(const GPIO* gpio) {
if(gpio == NULL) {
return NULL;
}
Expand Down Expand Up @@ -334,7 +334,7 @@ bool unitemp_onewire_sensor_alloc(Sensor* sensor, char* args) {

instance->familyCode = instance->deviceID[0];

instance->bus = uintemp_onewire_bus_alloc(unitemp_gpio_getFromInt(gpio));
instance->bus = unitemp_onewire_bus_alloc(unitemp_gpio_getFromInt(gpio));

if(instance != NULL) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion interfaces/OneWireSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ UnitempStatus unitemp_onewire_sensor_update(Sensor* sensor);
* @param gpio Порт на котором необходимо создать шину
* @return При успехе возвращает указатель на шину one wire
*/
OneWireBus* uintemp_onewire_bus_alloc(const GPIO* gpio);
OneWireBus* unitemp_onewire_bus_alloc(const GPIO* gpio);

/**
* @brief Инициализация шины one wire
Expand Down
30 changes: 29 additions & 1 deletion unitemp.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,45 @@
#include "interfaces/SingleWireSensor.h"
#include "Sensors.h"
#include "./views/UnitempViews.h"
#include "math.h"

#include <furi_hal_power.h>

/* Переменные */
//Данные приложения
Unitemp* app;

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

float calculateDewPoint(float temperature, float relativeHumidity);

void unitemp_rhToDewpointC(Sensor* sensor) {
sensor->hum = calculateDewPoint(sensor->temp, sensor->hum);
}

void unitemp_rhToDewpointF(Sensor* sensor) {
sensor->hum = calculateDewPoint(sensor->temp, sensor->hum) * (9.0 / 5.0) + 32;
}

float calculateDewPoint(float temperature, float relativeHumidity) {
float a = 17.27f;
float b = 237.7f;
float tempCalc = (a * temperature) / (b + temperature) + (float)log(relativeHumidity / 100.0f);
float dewPoint = (b * tempCalc) / (a - tempCalc);
return dewPoint;
}

void unitemp_pascalToMmHg(Sensor* sensor) {
sensor->pressure = sensor->pressure * 0.007500638;
}
void unitemp_pascalToKPa(Sensor* sensor) {
sensor->pressure = sensor->pressure / 1000.0f;
}
void unitemp_pascalToHPa(Sensor* sensor) {
sensor->pressure = sensor->pressure / 100.0f;
}
void unitemp_pascalToInHg(Sensor* sensor) {
sensor->pressure = sensor->pressure * 0.0002953007;
}
Expand Down Expand Up @@ -67,6 +89,7 @@ bool unitemp_saveSettings(void) {
stream_write_format(
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, "HUMIDITY_UNIT %d\n", app->settings.humidity_unit);
stream_write_format(app->file_stream, "PRESSURE_UNIT %d\n", app->settings.pressure_unit);

//Закрытие потока и освобождение памяти
Expand Down Expand Up @@ -158,6 +181,10 @@ bool unitemp_loadSettings(void) {
int p = 0;
sscanf(((char*)(file_buf + line_end)), "\nTEMP_UNIT %d", &p);
app->settings.temp_unit = p;
} else if(!strcmp(buff, "HUMIDITY_UNIT")) {
int p = 9;
sscanf(((char*)(file_buf + line_end)), "\nHUMIDITY_UNIT %d", &p);
app->settings.humidity_unit = p;
} else if(!strcmp(buff, "PRESSURE_UNIT")) {
//Чтение значения параметра
int p = 0;
Expand Down Expand Up @@ -199,6 +226,7 @@ static bool unitemp_alloc(void) {
//Установка значений по умолчанию
app->settings.infinityBacklight = true; //Подсветка горит всегда
app->settings.temp_unit = UT_TEMP_CELSIUS; //Единица измерения температуры - градусы Цельсия
app->settings.humidity_unit = UT_HUMIDITY_RELATIVE;
app->settings.pressure_unit = UT_PRESSURE_MM_HG; //Единица измерения давления - мм рт. ст.

app->gui = furi_record_open(RECORD_GUI);
Expand Down
31 changes: 30 additions & 1 deletion unitemp.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,25 @@ typedef enum {
UT_PRESSURE_MM_HG,
UT_PRESSURE_IN_HG,
UT_PRESSURE_KPA,
UT_PRESSURE_HPA,

UT_PRESSURE_COUNT
} pressureMeasureUnit;
// Humidity units
typedef enum {
UT_HUMIDITY_RELATIVE, // Relative humidity
UT_HUMIDITY_DEWPOINT, // Dewpoint
UT_HUMIDITY_COUNT // Number of humidity modes
} humidityUnit;
/* Объявление структур */
//Настройки плагина
typedef struct {
//Бесконечная работа подсветки
bool infinityBacklight;
//Единица измерения температуры
tempMeasureUnit temp_unit;
// Humidity units
humidityUnit humidity_unit;
//Единица измерения давления
pressureMeasureUnit pressure_unit;
//Последнее состояние OTG
Expand Down Expand Up @@ -115,7 +124,21 @@ typedef struct {
*
* @param sensor Указатель на датчик
*/
void uintemp_celsiumToFarengate(Sensor* sensor);
void unitemp_celsiusToFahrenheit(Sensor* sensor);

/**
* @brief Calculate dewpoint in C from relative humidity
*
* @param sensor Pointer to sensor
*/
void unitemp_rhToDewpointC(Sensor* sensor);

/**
* @brief Calculate dewpoint in F from relative humidity
*
* @param sensor Pointer to sensor
*/
void unitemp_rhToDewpointF(Sensor* sensor);

/**
* @brief Конвертация давления из паскалей в мм рт.ст.
Expand All @@ -130,6 +153,12 @@ void unitemp_pascalToMmHg(Sensor* sensor);
* @param sensor Указатель на датчик
*/
void unitemp_pascalToKPa(Sensor* sensor);
/**
* @brief Convert pressure from Pa to hPa
*
* @param sensor Pointer to sensor
*/
void unitemp_pascalToHPa(Sensor* sensor);
/**
* @brief Конвертация давления из паскалей в дюйм рт.ст.
*
Expand Down
47 changes: 31 additions & 16 deletions views/General_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,41 @@ static void _draw_temperature(Canvas* canvas, Sensor* sensor, uint8_t x, uint8_t
}

static void _draw_humidity(Canvas* canvas, Sensor* sensor, const uint8_t pos[2]) {
//Рисование рамки
// Drawing the frame
canvas_draw_rframe(canvas, pos[0], pos[1], 54, 20, 3);
canvas_draw_rframe(canvas, pos[0], pos[1], 54, 19, 3);

//Рисование иконки
// Drawing the icon
canvas_draw_icon(canvas, pos[0] + 3, pos[1] + 2, &I_hum_9x15);

//Целая часть влажности
snprintf(app->buff, BUFF_SIZE, "%d", (uint8_t)sensor->hum);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, pos[0] + 27, pos[1] + 10, AlignCenter, AlignCenter, app->buff);
uint8_t int_len = canvas_string_width(canvas, app->buff);
//Единица измерения
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 4, pos[1] + 10 + 7, "%");
if(app->settings.humidity_unit == UT_HUMIDITY_RELATIVE) {
// Relative humidity
snprintf(app->buff, BUFF_SIZE, "%d", (uint8_t)sensor->hum);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, pos[0] + 27, pos[1] + 10, AlignCenter, AlignCenter, app->buff);
uint8_t int_len = canvas_string_width(canvas, app->buff);
// Adding '%' for relative humidity
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 4, pos[1] + 10 + 7, "%");
} else if(app->settings.humidity_unit == UT_HUMIDITY_DEWPOINT) {
// Dewpoint with a decimal
int humidity_dec = abs((int16_t)(sensor->hum * 10) % 10);
snprintf(app->buff, BUFF_SIZE, "%d", (int16_t)sensor->hum);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(canvas, pos[0] + 27, pos[1] + 10, AlignCenter, AlignCenter, app->buff);
uint8_t int_len = canvas_string_width(canvas, app->buff);
// Printing the decimal part similar to temperature display
snprintf(app->buff, BUFF_SIZE, ".%d", humidity_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;
//Рисование рамки
canvas_draw_rframe(canvas, x, y, 69, 20, 3);
canvas_draw_rframe(canvas, x, y, 69, 19, 3);
canvas_draw_rframe(canvas, x, y, 76, 20, 3);
canvas_draw_rframe(canvas, x, y, 76, 19, 3);

//Рисование иконки
canvas_draw_icon(canvas, x + 3, y + 4, &I_pressure_7x13);
Expand All @@ -129,7 +142,7 @@ static void _draw_pressure(Canvas* canvas, Sensor* sensor) {
snprintf(app->buff, BUFF_SIZE, "%d", press_int);
canvas_set_font(canvas, FontBigNumbers);
canvas_draw_str_aligned(
canvas, x + 27 + ((press_int > 99) ? 5 : 0), y + 10, AlignCenter, AlignCenter, app->buff);
canvas, x + 28 + ((press_int > 99) ? 5 : 0), y + 10, AlignCenter, AlignCenter, app->buff);
//Печать дробной части давления в диапазоне от 0 до 99 (когда два знака в числе)
if(press_int <= 99) {
uint8_t int_len = canvas_string_width(canvas, app->buff);
Expand All @@ -140,11 +153,13 @@ static void _draw_pressure(Canvas* canvas, Sensor* sensor) {
canvas_set_font(canvas, FontSecondary);
//Единица измерения
if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) {
canvas_draw_icon(canvas, x + 50, y + 2, &I_mm_hg_15x15);
canvas_draw_icon(canvas, x + 56, y + 3, &I_mm_hg_15x15);
} else if(app->settings.pressure_unit == UT_PRESSURE_IN_HG) {
canvas_draw_icon(canvas, x + 50, y + 2, &I_in_hg_15x15);
canvas_draw_icon(canvas, x + 56, y + 3, &I_in_hg_15x15);
} else if(app->settings.pressure_unit == UT_PRESSURE_KPA) {
canvas_draw_str(canvas, x + 52, y + 13, "kPa");
canvas_draw_str(canvas, x + 57, y + 13, "kPa");
} else if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
canvas_draw_str(canvas, x + 58, y + 13, "hPa");
}
}

Expand Down
19 changes: 18 additions & 1 deletion views/Settings_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ 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"};
static const char humidity_units[UT_HUMIDITY_COUNT][12] = {"Relative", "Dewpoint"};
static const char pressure_units[UT_PRESSURE_COUNT][6] = {"mmHg", "inHg", "kPa", "hPa"};

//Элемент списка - бесконечная подсветка
VariableItem* infinity_backlight_item;
//Единица измерения температуры
VariableItem* temperature_unit_item;
// Humidity unit
VariableItem* humidity_unit_item;
//Единица измерения давления
VariableItem* pressure_unit_item;
#define VIEW_ID UnitempViewSettings
Expand All @@ -56,6 +59,7 @@ static uint32_t _exit_callback(void* context) {
app->settings.infinityBacklight =
(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.humidity_unit = variable_item_get_current_value_index(humidity_unit_item);
app->settings.pressure_unit = variable_item_get_current_value_index(pressure_unit_item);
unitemp_saveSettings();
unitemp_loadSettings();
Expand Down Expand Up @@ -85,6 +89,11 @@ static void _setting_change_callback(VariableItem* item) {
temperature_unit_item,
temp_units[variable_item_get_current_value_index(temperature_unit_item)]);
}
if(item == humidity_unit_item) {
variable_item_set_current_value_text(
humidity_unit_item,
humidity_units[variable_item_get_current_value_index(humidity_unit_item)]);
}
if(item == pressure_unit_item) {
variable_item_set_current_value_text(
pressure_unit_item,
Expand All @@ -104,6 +113,8 @@ void unitemp_Settings_alloc(void) {
variable_item_list, "Backlight time", UT_TEMP_COUNT, _setting_change_callback, app);
temperature_unit_item =
variable_item_list_add(variable_item_list, "Temp. unit", 2, _setting_change_callback, app);
humidity_unit_item = variable_item_list_add(
variable_item_list, "Humidity unit", UT_HUMIDITY_COUNT, _setting_change_callback, app);
pressure_unit_item = variable_item_list_add(
variable_item_list, "Press. unit", UT_PRESSURE_COUNT, _setting_change_callback, app);

Expand Down Expand Up @@ -133,6 +144,12 @@ void unitemp_Settings_switch(void) {
temperature_unit_item,
temp_units[variable_item_get_current_value_index(temperature_unit_item)]);

variable_item_set_current_value_index(
humidity_unit_item, app->settings.humidity_unit);
variable_item_set_current_value_text(
humidity_unit_item,
humidity_units[variable_item_get_current_value_index(humidity_unit_item)]);

variable_item_set_current_value_index(
pressure_unit_item, (uint8_t)app->settings.pressure_unit);
variable_item_set_current_value_text(
Expand Down