Commit | Line | Data |
---|---|---|
df27a6a3 | 1 | /* |
3c132bd0 AW |
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. | |
df27a6a3 | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
3c132bd0 AW |
6 | */ |
7 | ||
8 | ||
9 | ||
10 | #ifndef ADC_H | |
11 | #define ADC_H | |
12 | ||
da3a10b9 | 13 | #include "PinNames.h" // mbed.h lib |
7af0714f | 14 | |
c0c44ac0 JM |
15 | #include <cmath> |
16 | ||
7af0714f | 17 | class Pin; |
232593a2 JM |
18 | namespace mbed { |
19 | class ADC; | |
20 | } | |
3c132bd0 | 21 | |
c0c44ac0 JM |
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 | ||
232593a2 JM |
26 | class Adc |
27 | { | |
28 | public: | |
29 | Adc(); | |
30 | void enable_pin(Pin *pin); | |
31 | unsigned int read(Pin *pin); | |
3c132bd0 | 32 | |
232593a2 JM |
33 | static Adc *instance; |
34 | void new_sample(int chan, uint32_t value); | |
c0c44ac0 JM |
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 | |
232593a2 JM |
41 | |
42 | private: | |
43 | PinName _pin_to_pinname(Pin *pin); | |
44 | mbed::ADC *adc; | |
3c132bd0 | 45 | |
232593a2 | 46 | static const int num_channels= 6; |
c0c44ac0 JM |
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 | |
232593a2 | 51 | static const int num_samples= 8; |
c0c44ac0 | 52 | #endif |
232593a2 JM |
53 | // buffers storing the last num_samples readings for each channel |
54 | uint16_t sample_buffers[num_channels][num_samples]; | |
55 | }; | |
3c132bd0 AW |
56 | |
57 | #endif |