Merge branch 'upstreamedge' into hack/interim-feed-hold
[clinton/Smoothieware.git] / src / libs / Adc.h
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie 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.
4 Smoothie 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8
9
10 #ifndef ADC_H
11 #define ADC_H
12
13 #include "PinNames.h" // mbed.h lib
14
15 #include <cmath>
16
17 class Pin;
18 namespace mbed {
19 class ADC;
20 }
21
22 // define how many bits of extra resolution required
23 // 2 bits means the 12bit ADC is 14 bits of resolution
24 #define OVERSAMPLE 2
25
26 class Adc
27 {
28 public:
29 Adc();
30 void enable_pin(Pin *pin);
31 unsigned int read(Pin *pin);
32
33 static Adc *instance;
34 void new_sample(int chan, uint32_t value);
35 // return the maximum ADC value, base is 12bits 4095.
36 #ifdef OVERSAMPLE
37 int get_max_value() const { return 4095 << OVERSAMPLE;}
38 #else
39 int get_max_value() const { return 4095;}
40 #endif
41
42 private:
43 PinName _pin_to_pinname(Pin *pin);
44 mbed::ADC *adc;
45
46 static const int num_channels= 6;
47 #ifdef OVERSAMPLE
48 // we need 4^n sample to oversample and we get double that to filter out spikes
49 static const int num_samples= powf(4, OVERSAMPLE)*2;
50 #else
51 static const int num_samples= 8;
52 #endif
53 // buffers storing the last num_samples readings for each channel
54 uint16_t sample_buffers[num_channels][num_samples];
55 };
56
57 #endif