YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
adc_interface.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// This file defines a generic and abstract C++
15// interface for a ADC channel. The channel is
16// specified by a uint8_t, so the maximum number
17// of channels is limited to 256. The ADC raw data
18// uses a uint16_t, so the maximum ADC resolution
19// is limited to 16 bits, which should be sufficient
20// for most applications.
21//
22
23#ifndef _ADC_INTERFACE_H_
24#define _ADC_INTERFACE_H_
25
26#include <stdint.h>
27
28typedef uint16_t adc_mode_t;
29
30namespace ADC {
31// ADC modes and resolutions
32const adc_mode_t ADC_8_BIT = 0x0001;
33const adc_mode_t ADC_10_BIT = 0x0002;
34const adc_mode_t ADC_12_BIT = 0x0004;
35const adc_mode_t ADC_14_BIT = 0x0008;
36}
37
39public:
40 // Prepare a ADC channel for reading. On many
41 // systems, the mode of a single pin needs to
42 // be configured by setting the pin mode.
43 // The second parameter specifies the mode
44 // (currently the ADC resolution).
45 virtual void adcMode(uint8_t channel, adc_mode_t mode) = 0;
46
47 // Read the current resolution of a channel
48 virtual adc_mode_t getMode(uint8_t channel) = 0;
49
50 // Read a single raw (binary) ADC value.
51 virtual uint16_t adcReadRaw(uint8_t channel) = 0;
52
53 // Read a single voltage ADC value.
54 virtual float adcReadVoltage(uint8_t channel) = 0;
55
56 // Convert a raw value to a voltage value.
57 virtual float rawToVoltage(uint8_t channel, uint16_t raw) = 0;
58
59protected:
60 virtual ~adc_interface() = default;
61};
62
63// This small wrapper class provides ADC
64// functionality for a single channel.
65
67public:
69 : _interf(interf), _channel(0) { }
70
71 adc_channel(adc_interface & interf, uint8_t c)
72 : _interf(interf), _channel(c) { }
73
74 inline void setChannel(uint8_t c) {
75 _channel = c;
76 }
77 inline void adcMode(adc_mode_t mode) {
78 _interf.adcMode(_channel, mode);
79 }
80 inline adc_mode_t getMode() {
81 return _interf.getMode(_channel);
82 }
83 inline uint16_t adcReadRaw() {
84 return _interf.adcReadRaw(_channel);
85 }
86 inline float adcReadVoltage() {
87 return _interf.adcReadVoltage(_channel);
88 }
89 inline float rawToVoltage(uint16_t raw) {
90 return _interf.rawToVoltage(_channel, raw);
91 }
92
93protected:
94 adc_interface & _interf;
95 uint8_t _channel;
96};
97
98#endif // _ADC_INTERFACE_H_