-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubniAnalogTools.cpp
More file actions
92 lines (75 loc) · 2.84 KB
/
SubniAnalogTools.cpp
File metadata and controls
92 lines (75 loc) · 2.84 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
/*
SubniAnalogTools.cpp - SubniAnalogTools library
Copyright (c) 2013 MDPS. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* Includes
******************************************************************************/
#include "SubniAnalogTools.h"
/******************************************************************************
* Definitions
******************************************************************************/
/******************************************************************************
* Constructors & Inicialization
******************************************************************************/
void SubniAnalogTools::init()
{
_mv_per_count = (float)(_vcc / pow(2,10));
_analog_avg_factor = (uint8_t)( log10(SANALOG_NUM_READS) / SLOG_2_10 );
}
/******************************************************************************
* Configuracion
******************************************************************************/
void SubniAnalogTools::setup(uint16_t vcc, uint16_t vref, uint16_t vref_count, uint16_t adc_res)
{
_vcc = vcc;
_vref = vref;
_vref_count = vref_count;
_adc_res = adc_res;
init();
}
void SubniAnalogTools::set_vref_count(uint16_t vref_count)
{
_vref_count = vref_count;
}
/******************************************************************************
* User API
******************************************************************************/
uint16_t SubniAnalogTools::read(uint8_t pin)
{
uint16_t value = 0;
for(int i = 0; i < SANALOG_NUM_READS; i++)
{
value += analogRead(pin);
delay(SANALOG_READS_DELAY);
}
return (uint16_t)(value >> _analog_avg_factor);
}
uint16_t SubniAnalogTools::read_mv(uint8_t pin)
{
return calculate_mv(read(pin));
}
uint16_t SubniAnalogTools::read_mv_compensated(uint8_t pin)
{
return calculate_mv_compensated(read(pin));
}
uint16_t SubniAnalogTools::calculate_mv(uint16_t value)
{
return (uint16_t)round( (value * _mv_per_count) );
}
uint16_t SubniAnalogTools::calculate_mv_compensated(uint16_t value)
{
return (uint16_t)round( ((value * (float)_vref) / (float)_vref_count) );
}
SubniAnalogTools AnalogTools;