phydat.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <sys/types.h>
40 
41 #include "modules.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
58 #define PHYDAT_DIM (3U)
59 
63 #define PHYDAT_SCALE_STR_MAXLEN (sizeof("*E-128\0"))
64 
76 enum {
77  /* generic values */
80  /* temperature */
84  /* illuminance */
86  /* dimension */
90  /* kinetic */
94  /* weight */
95  UNIT_GRAM,
98  /* electricity */
109  /* electrochemical */
111  /* pressure */
114  /* light */
116  /* logical */
123  /* aggregate values */
126  /* mass concentration */
128  /* number concentration */
129  UNIT_CPM3
130  /* extend this list as needed */
131 };
132 
154 typedef struct {
155  int16_t val[PHYDAT_DIM];
156  uint8_t unit;
157  int8_t scale;
158 } phydat_t;
159 
163 #define PHYDAT_MIN (INT16_MIN)
164 
168 #define PHYDAT_MAX (INT16_MAX)
169 
176 void phydat_dump(phydat_t *data, uint8_t dim);
177 
183 void phydat_unit_print(uint8_t unit);
184 
201 ssize_t phydat_unit_write(char *dest, size_t max_size, uint8_t unit);
202 
214 char phydat_prefix_from_scale(int8_t scale);
215 
246 void phydat_fit(phydat_t *dat, const int32_t *values, unsigned int dim);
247 
291 size_t phydat_to_json(const phydat_t *data, size_t dim, char *buf);
292 
303 int64_t phydat_date_time_to_unix(phydat_t *date, phydat_t *time, int32_t offset_seconds);
304 
318 int64_t phydat_unix(int16_t year, int16_t month, int16_t day,
319  int16_t hour, int16_t minute, int16_t second,
320  int32_t offset);
321 
322 #ifdef __cplusplus
323 }
324 #endif
325 
void phydat_unit_print(uint8_t unit)
Print a unit.
#define PHYDAT_DIM
The fixed number of dimensions we work with.
Definition: phydat.h:58
char phydat_prefix_from_scale(int8_t scale)
Convert the given scale factor to an SI prefix.
int64_t phydat_date_time_to_unix(phydat_t *date, phydat_t *time, int32_t offset_seconds)
Convert a date and time contained in phydat structs to a Unix timestamp.
int64_t phydat_unix(int16_t year, int16_t month, int16_t day, int16_t hour, int16_t minute, int16_t second, int32_t offset)
Convert a date and time (per ISO8601) to a Unix timestamp (seconds since 1970).
size_t phydat_to_json(const phydat_t *data, size_t dim, char *buf)
Convert the given phydat_t structure into a JSON string.
ssize_t phydat_unit_write(char *dest, size_t max_size, uint8_t unit)
Write the string representation of the given unit into the given buffer.
void phydat_fit(phydat_t *dat, const int32_t *values, unsigned int dim)
Scale integer value(s) to fit into a phydat_t.
void phydat_dump(phydat_t *data, uint8_t dim)
Dump the given data container to STDIO.
@ UNIT_M2
square meters
Definition: phydat.h:88
@ UNIT_TEMP_C
degree Celsius
Definition: phydat.h:81
@ UNIT_G_FORCE
gravitational force equivalent
Definition: phydat.h:91
@ UNIT_W
Watt.
Definition: phydat.h:101
@ UNIT_COULOMB
coulomb
Definition: phydat.h:106
@ UNIT_PA
Pascal.
Definition: phydat.h:113
@ UNIT_A
Ampere.
Definition: phydat.h:99
@ UNIT_PERCENT
out of 100
Definition: phydat.h:119
@ UNIT_TEMP_K
Kelvin.
Definition: phydat.h:83
@ UNIT_NONE
data has no physical unit
Definition: phydat.h:79
@ UNIT_UNDEF
unit undefined
Definition: phydat.h:78
@ UNIT_TEMP_F
degree Fahrenheit
Definition: phydat.h:82
@ UNIT_GS
Definition: phydat.h:103
@ UNIT_PPB
part per billion
Definition: phydat.h:122
@ UNIT_GAUSS
gauss
Definition: phydat.h:102
@ UNIT_DATE
the 3 dimensions contain days, months and years
Definition: phydat.h:125
@ UNIT_G
Definition: phydat.h:92
@ UNIT_PH
pH
Definition: phydat.h:110
@ UNIT_CD
Candela.
Definition: phydat.h:115
@ UNIT_DPS
degree per second
Definition: phydat.h:93
@ UNIT_T
Tesla.
Definition: phydat.h:104
@ UNIT_V
Volts.
Definition: phydat.h:100
@ UNIT_PPM
part per million
Definition: phydat.h:121
@ UNIT_GPM3
grams per cubic meter
Definition: phydat.h:127
@ UNIT_DBM
decibel-milliwatts
Definition: phydat.h:105
@ UNIT_PERMILL
out of 1000
Definition: phydat.h:120
@ UNIT_GR
Definition: phydat.h:97
@ UNIT_M3
cubic meters
Definition: phydat.h:89
@ UNIT_TIME
the three dimensions contain sec, min, and hours
Definition: phydat.h:124
@ UNIT_BOOL
boolean value [0|1]
Definition: phydat.h:117
@ UNIT_CPM3
count per cubic meter
Definition: phydat.h:129
@ UNIT_CTS
counts
Definition: phydat.h:118
@ UNIT_LUX
Lux (lx)
Definition: phydat.h:85
@ UNIT_F
Farad.
Definition: phydat.h:107
@ UNIT_GRAM
grams - not using the SI unit (kg) here to make scale handling simpler
Definition: phydat.h:95
@ UNIT_BAR
Beer?
Definition: phydat.h:112
@ UNIT_OHM
Ohm.
Definition: phydat.h:108
@ UNIT_M
meters
Definition: phydat.h:87
Common macros and compiler attributes/pragmas configuration.
Generic data structure for expressing physical values.
Definition: phydat.h:154
int8_t scale
the scale factor, 10^*scale*
Definition: phydat.h:157
uint8_t unit
the (physical) unit of the data
Definition: phydat.h:156