-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.c
73 lines (62 loc) · 2.14 KB
/
conversion.c
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
/* Copyright (C) 2016, Marc-Andre Guimond <[email protected]>.
*
* 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/>.
*
* This file is encoded in UTF-8.
*/
// Standard includes.
#include <stdint.h>
#include <stdlib.h>
// Common includes.
#include "conversion.h"
#include "utils.h"
// --------------------------------------------------------------------------------------------------------------
uint16_t ConversionDecode(uint16_t inData, uint16_t inMultiplier, uint8_t inScale, int8_t inSign)
{
uint32_t temp_product;
uint32_t roundup;
uint32_t result;
if (!inScale)
{
return 0;
}
temp_product = inData * inMultiplier;
result = temp_product >> inScale;
// For negatives voltages monitoring.
(inSign < 0)? result = abs(result - 31516): result;
roundup = temp_product & (1 << (inScale - 1));
if (result > UINT16_MAX)
{
return UINT16_MAX;
}
if (roundup && result != UINT16_MAX)
{
// Fract part >= 0.5, round up.
result += 1;
}
return (uint16_t)result;
}
// --------------------------------------------------------------------------------------------------------------
uint16_t ComputeAmplitude(uint16_t inDataTable[], uint16_t inSize, uint16_t* outMin, uint16_t* outMax)
{
// Initialize min & max to opposites.
*outMax = 0;
*outMin = UINT16_MAX;
for (int tableIdx = 0; tableIdx < inSize; tableIdx ++)
{
*outMin = min(*outMin, inDataTable[tableIdx]);
*outMax = max(*outMax, inDataTable[tableIdx]);
}
return *outMax - *outMin;
}