rpi4b-temp-humidity/ths.h

66 lines
1.6 KiB
C

/*
* ths.h - Utilities for reading sysctl based temp. and humidity sensors
* Copyright (C) 2024 Alexander Rosenberg
*
* This program 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 3 of the License, or (at your option) any later
* version. See the LICENSE file for more information.
*/
#ifndef INCLUDED_THS_H
#define INCLUDED_THS_H
#include <stdint.h>
#include <stddef.h>
#define THS_MIB_BUF_SIZE 8
typedef struct {
size_t temp_mib_len;
int temp_mib[THS_MIB_BUF_SIZE];
size_t humid_mib_len;
int humid_mib[THS_MIB_BUF_SIZE];
size_t fail_mib_len;
int fail_mib[THS_MIB_BUF_SIZE];
} THS;
/*
* Convert deciKelvin to degrees Celsius
*/
#define DK_TO_C(k) ((k) / 10.0f - 273.15f)
/*
* Convert deciKelvin to degrees Fahrenheit
*/
#define DK_TO_F(k) (((DK_TO_C((k))) * 1.8f) + 32.0f)
/*
* Create a new THS from the given sysctl keys. Any of the keys can be null.
* Return: the new THS, or NULL if an error occurred.
*/
THS *ths_open(const char *temp_key, const char *humid_key, const char *fail_key);
/*
* Close THS.
*/
void ths_close(THS *ths);
/*
* Read the fail cound of THS.
* Return: the value read, or -1 on error
*/
int32_t ths_read_fails(const THS *ths);
/*
* Read the temperature of THS in deciKelvin.
* Return: the value read, or -1 on error
*/
int32_t ths_read_temp(const THS *ths);
/*
* Read the relative humidity of THS as an integer percentage.
* Return: the value read, or -1 on error
*/
int32_t ths_read_humid(const THS *ths);
#endif