DIY Logging Volt/Ampmeter
conversion.h
Go to the documentation of this file.
1 /***************************************************************************//**
2  * @file conversion.h
3  * @brief Conversion and settings-related methods and structs
4  * for high-precision logging voltage/current meter.
5  * @version 1.0
6  * @author Brecht Van Eeckhoudt
7  *
8  * ******************************************************************************
9  *
10  * @section Versions
11  *
12  * @li v1.0: Initial version.
13  *
14  * ******************************************************************************
15  *
16  * @section License
17  *
18  * **Copyright (C) 2021 - Brecht Van Eeckhoudt**
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the **GNU General Public License** as published by
22  * the Free Software Foundation, either **version 3** of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU General Public License for more details.
29  *
30  * *A copy of the GNU General Public License can be found in the `LICENSE`
31  * file along with this source code.*
32  *
33  ******************************************************************************/
34 
35 
36 /* Include guards prevent multiple inclusions of the same header */
37 #ifndef __CONVERSION_H_
38 #define __CONVERSION_H_
39 
40 
41 /* Includes necessary for this header file */
42 #include <stdint.h> /* (u)intXX_t */
43 
44 
45 /* Public definitions */
46 
47 /** Rounding macro */
48 #define FLOAT_TO_INT(x) ((x)>=0?(uint32_t)((x)+0.5):(uint32_t)((x)-0.5))
49 
50 /** Struct to store the gathered measurements */
51 extern struct Data_t
52 {
53  uint8_t voltReceived; /* 1 = Voltage value received */
54  float voltage; /* [V] */
55  float oldVolt; /* [V], necessary for optimal char conversion logic */
56  uint8_t firstVolt; /* Necessary for MIN logic */
57  uint8_t newVolt; /* 1 = Value needs to be converted to char array */
58  float minVoltage; /* [V] */
59  uint8_t newMinVolt; /* 1 = Value needs to be converted to char array */
60  float maxVoltage; /* [V] */
61  uint8_t newMaxVolt; /* 1 = Value needs to be converted to char array */
62 
63  uint8_t currReceived; /* 1 = Current value received */
64  float current; /* [A] */
65  float oldCurr; /* [A], necessary for optimal char conversion logic */
66  uint8_t firstCurr; /* Necessary for MIN logic */
67  uint8_t newCurr; /* 1 = Value needs to be converted to char array */
68  float minCurrent; /* [A] */
69  uint8_t newMinCurr; /* 1 = Value needs to be converted to char array */
70  float maxCurrent; /* [A] */
71  uint8_t newMaxCurr; /* 1 = Value needs to be converted to char array */
72 
73  float power; /* [W] */
74  float oldPower; /* [W], necessary for optimal char conversion logic */
75  uint8_t newPow; /* 1 = Value needs to be converted to char array */
76  float ah; /* [Ah] */
77  uint8_t newAh; /* 1 = Value needs to be converted to char array */
78  float wh; /* [Wh] */
79  uint8_t newWh; /* 1 = Value needs to be converted to char array */
80 
81  uint8_t runHours; /* [hour] */
82  uint8_t newRunHours; /* 1 = Value needs to be converted to char array */
83  uint8_t runMins; /* [min] */
84  uint8_t newRunMins; /* 1 = Value needs to be converted to char array */
85  uint8_t runSecs; /* [sec] */
86  uint8_t newRunSecs; /* 1 = Value needs to be converted to char array */
87 
88  uint32_t msCounter0; /* 24-bit millisecond tick value (Max "16 777 215" or "0xFFFFFF" ) */
89  uint32_t msCounter1; /* 24-bit millisecond tick value (Max "16 777 215" or "0xFFFFFF" ) */
90  uint8_t latestMScounter; /* Index of most recent counter */
91 } data;
92 
93 /** Struct to store the converted char-data */
94 extern struct cData_t
95 {
96  char vLeft [3]; /* Value won't exceed 50 (2 chars + NULL termination = 3) */
97  char vRight [4]; /* Value won't exceed 999 (3 chars + NULL termination = 4) */
98  uint8_t newVolt; /* 1 = Value needs to be updated on the display */
99  char vMaxLeft[3]; /* Value won't exceed 50 (2 chars + NULL termination = 3) */
100  char vMaxRight[4]; /* Value won't exceed 999 (3 chars + NULL termination = 4) */
101  uint8_t newMaxVolt; /* 1 = Value needs to be updated on the display */
102  char vMinLeft[3]; /* Value won't exceed 50 (2 chars + NULL termination = 3) */
103  char vMinRight[4]; /* Value won't exceed 999 (3 chars + NULL termination = 4) */
104  uint8_t newMinVolt; /* 1 = Value needs to be updated on the display */
105 
106  char aLeft [2]; /* Value won't exceed 5 (1 char + NULL termination = 2) */
107  char aRight [5]; /* Value won't exceed 9999 (4 chars + NULL termination = 5) */
108  uint8_t newCurr; /* 1 = Value needs to be updated on the display */
109  char aMaxLeft[2]; /* Value won't exceed 5 (1 char + NULL termination = 2) */
110  char aMaxRight[5]; /* Value won't exceed 9999 (4 chars + NULL termination = 5) */
111  uint8_t newMaxCurr; /* 1 = Value needs to be updated on the display */
112  char aMinLeft[2]; /* Value won't exceed 5 (1 char + NULL termination = 2) */
113  char aMinRight[5]; /* Value won't exceed 9999 (4 chars + NULL termination = 5) */
114  uint8_t newMinCurr; /* 1 = Value needs to be updated on the display */
115 
116  char pLeft [4]; /* Value won't exceed 250 (3 chars + NULL termination = 4) */
117  char pRight [4]; /* Value won't exceed 999 (3 chars + NULL termination = 4) */
118  uint8_t newPow; /* 1 = Value needs to be updated on the display */
119 
120  char ahLeft [5]; /* Value won't be able to exceed 9999 (4 chars + NULL termination = 5) */
121  char ahRight[4]; /* Value won't be able to exceed 999 (3 chars + NULL termination = 4) */
122  uint8_t newAh; /* 1 = Value needs to be updated on the display */
123  char whLeft [5]; /* Value won't be able to exceed 9999 (4 chars + NULL termination = 5) */
124  char whRight[4]; /* Value won't be able to exceed 999 (3 chars + NULL termination = 4) */
125  uint8_t newWh; /* 1 = Value needs to be updated on the display */
126 
127  char charRunHours[3]; /* Value won't exceed 99 (2 chars + NULL termination = 3) */
128  uint8_t newRunHours; /* 1 = Value needs to be updated on the display */
129  char charRunMins[3]; /* Value won't exceed 59 (2 chars + NULL termination = 3) */
130  uint8_t newRunMins; /* 1 = Value needs to be updated on the display */
131  char charRunSecs[3]; /* Value won't exceed 59 (2 chars + NULL termination = 3) */
132  uint8_t newRunSecs; /* 1 = Value needs to be updated on the display */
133 } cData;
134 
135 /** Struct to store data (~settings) that change less frequently */
136 extern struct Settings_t
137 {
138  /* Volatile because it's modified by an interrupt service routine (@RAM) */
139  volatile float vbat; /* Internal battery voltage */
140  volatile uint8_t newVbat; /* 1 = Value needs to be converted to char array */
141  char vbatLeft[2]; /* Max 1 char + NULL termination = 2 */
142  char vbatRight[3]; /* Max 2 chars + NULL termination = 3 */
143  uint8_t newCharVbat; /* 1 = Value needs to be updated on the display */
144  uint8_t vbatCounter; /* Countdown timer between battery voltage measurements */
145 
146  uint8_t mode; /* 0 = STOP, 1 = RUN */
147  uint8_t newMode; /* 1 = Value needs to be updated on the display */
148  uint8_t selectedDisplay; /* 1 = Left display selected, 2 = Right display selected */
149  uint8_t newSelDisplay; /* 1 = Display selection line needs to be updated */
150  uint8_t leftPage; /* 0 = Vmax/min, 1 = Amax/min, 2 = Ah/Wh */
151  uint8_t rightPage; /* 0 = Date/time, 1 = Period, 2 = Runtime */
152 
153  uint8_t measPeriodIndex; /* Index of LUT for measurement periods */
154  uint16_t remMeasPeriodS; /* Remaining amount of seconds before the next saved measurement */
155  uint16_t measPeriodS; /* Selected measurement period */
156  uint8_t changePeriod; /* 1 = Changing period */
157  uint8_t periodChanged; /* 1 = Value needs to be updated on the display */
158  uint8_t changeDateTime; /* 1 = Changing day, 2 = Month, 3 = Year, 4 = Hours, 5 = Minutes, 6 = Seconds */
159  uint8_t dateTimeChanged; /* 1 = Value needs to be updated on the display */
160 
161  uint8_t headerSaved; /* 1 = Log-file header is saved (~UART logger is initialized) */
162  uint8_t forceConv; /* 1 = Convert all values not on screens (yet) to chars */
163  uint16_t oldTIM3; /* Encoder rotation logic */
164  volatile uint8_t secPassed; /* Interrupt signaling of seconds */
165  uint8_t newSec; /* 1 = Value needs to be converted to char array */
166  uint8_t loopAsDelay; /* >1 = we need to pass a certain amount of loops before other logic is triggered */
167  volatile uint8_t loggerReady; /* 1 = UART logger has started up (startup message detected) */
168  uint16_t msWaiting; /* Waiting-time functionality */
169  uint32_t msWaitingStart; /* Starting value of the waiting-time-counter */
170 } settings;
171 
172 
173 /* Public prototypes */
174 void initDataStruct(void);
175 void initSettingsStruct(void);
176 void forceConversion(void);
177 void convertValues(void);
178 void setSecondPassed(uint8_t value);
179 uint8_t getSecondPassed(void);
180 void incSecondPassed(void);
181 uint8_t decSecondPassed(void);
182 
183 
184 #endif /* __CONVERSION_H_ */
Settings_t::headerSaved
uint8_t headerSaved
Definition: conversion.h:161
Settings_t::loopAsDelay
uint8_t loopAsDelay
Definition: conversion.h:166
Data_t::runSecs
uint8_t runSecs
Definition: conversion.h:85
cData_t::newCurr
uint8_t newCurr
Definition: conversion.h:108
Settings_t::rightPage
uint8_t rightPage
Definition: conversion.h:151
Settings_t::selectedDisplay
uint8_t selectedDisplay
Definition: conversion.h:148
Data_t::oldVolt
float oldVolt
Definition: conversion.h:55
settings
struct Settings_t settings
Data_t::current
float current
Definition: conversion.h:64
cData_t::aMaxLeft
char aMaxLeft[2]
Definition: conversion.h:109
cData_t
Definition: conversion.h:94
cData_t::aMaxRight
char aMaxRight[5]
Definition: conversion.h:110
cData_t::newMaxCurr
uint8_t newMaxCurr
Definition: conversion.h:111
cData_t::newRunHours
uint8_t newRunHours
Definition: conversion.h:128
Settings_t::msWaiting
uint16_t msWaiting
Definition: conversion.h:168
Data_t::newRunHours
uint8_t newRunHours
Definition: conversion.h:82
cData_t::ahLeft
char ahLeft[5]
Definition: conversion.h:120
Settings_t::leftPage
uint8_t leftPage
Definition: conversion.h:150
Data_t::newMaxVolt
uint8_t newMaxVolt
Definition: conversion.h:61
cData_t::newPow
uint8_t newPow
Definition: conversion.h:118
Data_t::maxCurrent
float maxCurrent
Definition: conversion.h:70
Data_t::maxVoltage
float maxVoltage
Definition: conversion.h:60
Data_t::newAh
uint8_t newAh
Definition: conversion.h:77
setSecondPassed
void setSecondPassed(uint8_t value)
cData_t::charRunHours
char charRunHours[3]
Definition: conversion.h:127
Data_t::runHours
uint8_t runHours
Definition: conversion.h:81
cData_t::aRight
char aRight[5]
Definition: conversion.h:107
cData
struct cData_t cData
Data_t::newRunSecs
uint8_t newRunSecs
Definition: conversion.h:86
data
struct Data_t data
Settings_t::loggerReady
volatile uint8_t loggerReady
Definition: conversion.h:167
Data_t::wh
float wh
Definition: conversion.h:78
Settings_t::changePeriod
uint8_t changePeriod
Definition: conversion.h:156
Data_t::newMinVolt
uint8_t newMinVolt
Definition: conversion.h:59
Data_t::currReceived
uint8_t currReceived
Definition: conversion.h:63
cData_t::vLeft
char vLeft[3]
Definition: conversion.h:96
Data_t::msCounter0
uint32_t msCounter0
Definition: conversion.h:88
Settings_t::newMode
uint8_t newMode
Definition: conversion.h:147
cData_t::whLeft
char whLeft[5]
Definition: conversion.h:123
cData_t::whRight
char whRight[4]
Definition: conversion.h:124
cData_t::pLeft
char pLeft[4]
Definition: conversion.h:116
cData_t::charRunMins
char charRunMins[3]
Definition: conversion.h:129
cData_t::pRight
char pRight[4]
Definition: conversion.h:117
cData_t::newMaxVolt
uint8_t newMaxVolt
Definition: conversion.h:101
Settings_t::vbatRight
char vbatRight[3]
Definition: conversion.h:142
Settings_t::measPeriodIndex
uint8_t measPeriodIndex
Definition: conversion.h:153
cData_t::newAh
uint8_t newAh
Definition: conversion.h:122
cData_t::aLeft
char aLeft[2]
Definition: conversion.h:106
Data_t::voltage
float voltage
Definition: conversion.h:54
convertValues
void convertValues(void)
Function to convert the measurements to char-arrays.
Definition: conversion.c:172
Data_t::newVolt
uint8_t newVolt
Definition: conversion.h:57
Data_t::oldPower
float oldPower
Definition: conversion.h:74
Settings_t::periodChanged
uint8_t periodChanged
Definition: conversion.h:157
cData_t::vRight
char vRight[4]
Definition: conversion.h:97
Data_t::power
float power
Definition: conversion.h:73
cData_t::aMinRight
char aMinRight[5]
Definition: conversion.h:113
Settings_t::dateTimeChanged
uint8_t dateTimeChanged
Definition: conversion.h:159
Data_t::newMinCurr
uint8_t newMinCurr
Definition: conversion.h:69
Settings_t::measPeriodS
uint16_t measPeriodS
Definition: conversion.h:155
Data_t
Definition: conversion.h:51
cData_t::charRunSecs
char charRunSecs[3]
Definition: conversion.h:131
Settings_t::vbatCounter
uint8_t vbatCounter
Definition: conversion.h:144
initDataStruct
void initDataStruct(void)
Function to initialize/reset the values in the data-struct.
Definition: conversion.c:69
cData_t::newMinCurr
uint8_t newMinCurr
Definition: conversion.h:114
Settings_t
Definition: conversion.h:136
Data_t::newCurr
uint8_t newCurr
Definition: conversion.h:67
cData_t::ahRight
char ahRight[4]
Definition: conversion.h:121
Data_t::msCounter1
uint32_t msCounter1
Definition: conversion.h:89
Settings_t::newCharVbat
uint8_t newCharVbat
Definition: conversion.h:143
incSecondPassed
void incSecondPassed(void)
Function to increase the value of secondPassed.
Definition: conversion.c:433
forceConversion
void forceConversion(void)
Function to force a conversion of all values not (yet) displayed on the OLED screens.
Definition: conversion.c:155
Data_t::firstVolt
uint8_t firstVolt
Definition: conversion.h:56
Settings_t::newSelDisplay
uint8_t newSelDisplay
Definition: conversion.h:149
getSecondPassed
uint8_t getSecondPassed(void)
cData_t::newMinVolt
uint8_t newMinVolt
Definition: conversion.h:104
Settings_t::changeDateTime
uint8_t changeDateTime
Definition: conversion.h:158
Data_t::minVoltage
float minVoltage
Definition: conversion.h:58
Data_t::newMaxCurr
uint8_t newMaxCurr
Definition: conversion.h:71
cData_t::vMaxRight
char vMaxRight[4]
Definition: conversion.h:100
Data_t::ah
float ah
Definition: conversion.h:76
cData_t::aMinLeft
char aMinLeft[2]
Definition: conversion.h:112
Settings_t::msWaitingStart
uint32_t msWaitingStart
Definition: conversion.h:169
Data_t::voltReceived
uint8_t voltReceived
Definition: conversion.h:53
Data_t::newWh
uint8_t newWh
Definition: conversion.h:79
cData_t::vMaxLeft
char vMaxLeft[3]
Definition: conversion.h:99
Data_t::firstCurr
uint8_t firstCurr
Definition: conversion.h:66
decSecondPassed
uint8_t decSecondPassed(void)
Function to decrease the value of secondPassed.
Definition: conversion.c:447
Data_t::minCurrent
float minCurrent
Definition: conversion.h:68
cData_t::newVolt
uint8_t newVolt
Definition: conversion.h:98
cData_t::vMinRight
char vMinRight[4]
Definition: conversion.h:103
Settings_t::remMeasPeriodS
uint16_t remMeasPeriodS
Definition: conversion.h:154
Settings_t::oldTIM3
uint16_t oldTIM3
Definition: conversion.h:163
cData_t::newRunMins
uint8_t newRunMins
Definition: conversion.h:130
cData_t::newRunSecs
uint8_t newRunSecs
Definition: conversion.h:132
Settings_t::vbatLeft
char vbatLeft[2]
Definition: conversion.h:141
Data_t::oldCurr
float oldCurr
Definition: conversion.h:65
cData_t::newWh
uint8_t newWh
Definition: conversion.h:125
initSettingsStruct
void initSettingsStruct(void)
Function to initialize/reset the values in the settings-struct.
Definition: conversion.c:119
Settings_t::secPassed
volatile uint8_t secPassed
Definition: conversion.h:164
Data_t::runMins
uint8_t runMins
Definition: conversion.h:83
Settings_t::newSec
uint8_t newSec
Definition: conversion.h:165
Settings_t::vbat
volatile float vbat
Definition: conversion.h:139
Settings_t::forceConv
uint8_t forceConv
Definition: conversion.h:162
Data_t::latestMScounter
uint8_t latestMScounter
Definition: conversion.h:90
cData_t::vMinLeft
char vMinLeft[3]
Definition: conversion.h:102
Settings_t::mode
uint8_t mode
Definition: conversion.h:146
Settings_t::newVbat
volatile uint8_t newVbat
Definition: conversion.h:140
Data_t::newPow
uint8_t newPow
Definition: conversion.h:75
Data_t::newRunMins
uint8_t newRunMins
Definition: conversion.h:84