-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
199 lines (166 loc) · 3.83 KB
/
utils.c
File metadata and controls
199 lines (166 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
EspressoGuide
Copyright (C) 2015 Leonid Lezner
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
#include <stdio.h>
/**
* Resets the time structure
*/
void initTime(TIME *t)
{
t->seconds = 0;
t->hours = 0;
t->minutes = 0;
}
/**
* Formats the time as a string for the top bar
*/
void formatTime(TIME *t, char *buffer)
{
/* Show the hours only when greater than 0 */
if (t->hours > 0)
{
sprintf(buffer, "%d:%02d:%02d", t->hours, t->minutes, t->seconds);
}
else
{
sprintf(buffer, "%02d:%02d", t->minutes, t->seconds);
}
}
/**
* Increments the seconds of a time structure. If the seconds count is greater than
* 59, minutes will be counted (and also hours)
*/
void incrementSeconds(TIME *t)
{
t->seconds++;
if (t->seconds >= 60)
{
t->minutes++;
t->seconds = 0;
}
if (t->minutes >= 60)
{
t->hours++;
t->minutes = 0;
}
if (t->hours >= 10)
{
t->hours = 0;
t->minutes = 0;
t->seconds = 0;
}
}
/**
* Converts a time structure to seconds
*/
unsigned long timeInSeconds(TIME *t)
{
return t->seconds + t->minutes * 60 + t->hours * 3600;
}
/*
* Resets a time counter
*/
void initCounter(ACTION_COUNTER *counter, unsigned int interval, ACTION_COUNTER_CALLBACK callback)
{
counter->elapsed = 0;
counter->interval = interval;
counter->count = 0;
counter->callback = callback;
}
/***
* Adding the next measured value and calculatong the linear regression
* for determining the trend.
*/
void addNextValue(MEAS_DATA *data, double value)
{
unsigned int i = 0;
double x_average = 0;
double sum1 = 0;
double sum2 = 0;
double x_temp;
if (data->length < MEAS_DATA_MAXLEN)
{
data->length++;
}
/* Clculating the average value */
data->average = (data->last_sum + value) / (data->length);
x_average = (data->length - 1) / 2.0;
data->last_sum = 0;
for (i = 0; i < data->length; i++)
{
/* Shift the values to the left */
if (i < (data->length - 1) && data->full)
{
data->values[i] = data->values[i + 1];
}
else
{
data->values[data->length - 1] = value;
}
/* Store the sum without the first element */
if (i > 0 || data->length < MEAS_DATA_MAXLEN)
{
data->last_sum += data->values[i];
}
/* Calculating the least squares */
x_temp = (i - x_average);
sum1 += x_temp * (data->values[i] - data->average);
sum2 += x_temp * x_temp;
}
/* Calculate the slope */
if (sum2 > 0)
{
data->slope = sum1 / sum2;
}
else
{
data->slope = 0;
}
if (!data->full)
{
data->full = data->length == MEAS_DATA_MAXLEN;
}
}
void initMeasData(MEAS_DATA *data)
{
data->length = 0;
data->full = 0;
data->average = 0;
data->last_sum = 0;
data->slope = 0;
}
uint8_t getNumberDigits(uint16_t number, uint8_t *digits)
{
uint16_t delimiter[] = {10000, 1000, 100, 10, 1};
uint8_t index;
uint8_t count = 0;
uint16_t temp = number;
for (index = 0; index < sizeof(delimiter) / sizeof(uint16_t); index++)
{
digits[count] = (uint8_t)(temp / delimiter[index]);
temp -= digits[count] * delimiter[index];
if (digits[count] != 0 || count != 0)
{
count++;
}
}
if (number < 10)
{
count = 2;
digits[1] = digits[0];
digits[0] = 0;
}
return count;
}