Humidistat
Arduino firmware for a humidistat (humidity controller)
Loading...
Searching...
No Matches
ConfigPar.cpp
Go to the documentation of this file.
1#include <stdint.h>
2#include <math.h>
3
4#include "ConfigPar.h"
5#include "asprintf.h"
6#include "imath.h"
7
8void ConfigPar::adjust(int16_t delta) const {
9 switch (var.type) {
11 *var.ui8 += delta;
12 return;
14 *var.ui16 += delta;
15 return;
17 *var.d += static_cast<double>(delta) / ipow(10, NUM_DECIMALS);
18 return;
19 }
20}
21
22char *ConfigPar::asprint() const {
23 switch (var.type) {
25 return asprintf("%-8s % " XSTR(WIDTH) "u", label, *var.ui8);
27 return asprintf("%-8s % " XSTR(WIDTH) "u", label, *var.ui16);
29 return asprintf("%-8s % " XSTR(WIDTH) "." XSTR(NUM_DECIMALS) "f", label, *var.d);
30 }
31}
32
33uint8_t ConfigPar::magnitude() const {
34 auto mag = [](auto n) -> uint8_t {return floor(ilog10(floor(abs(n))));};
35
36 switch (var.type) {
38 return mag(*var.ui8);
40 return mag(*var.ui16);
42 return mag(*var.d);
43 }
44}
#define XSTR(s)
Definition ConfigPar.h:4
#define WIDTH
Definition ConfigPar.h:6
#define NUM_DECIMALS
Definition ConfigPar.h:8
char * asprintf(const char *fmt, T... args)
Print formatted data to string. Automatically allocates string on the heap. Make sure to delete it im...
Definition asprintf.h:13
uint8_t magnitude() const
Get magnitude (number of digits before the decimal separator) of variable.
Definition ConfigPar.cpp:33
char * asprint() const
Print "label: value" to string. Automatically allocates string on the heap. Make sure to delete it im...
Definition ConfigPar.cpp:22
void adjust(int16_t delta) const
Add delta to the variable.
Definition ConfigPar.cpp:8
struct ConfigPar::Var var
char label[10]
Definition ConfigPar.h:40
constexpr unsigned int ilog10(int n)
Constexpr integer base-10 logarithm.
Definition imath.h:17
constexpr T ipow(T base, unsigned int pow)
Constexpr function for computing integer power.
Definition imath.h:10
double *const d
Definition ConfigPar.h:25
const ConfigParType type
Definition ConfigPar.h:21
uint16_t *const ui16
Definition ConfigPar.h:24
uint8_t *const ui8
Definition ConfigPar.h:23