Aruna
ADC.cpp
Go to the documentation of this file.
1 /*
2  * ADC.cpp
3  *
4  * Created on: 30 nov. 2020
5  * Author: noeel
6  */
7 
8 #include "aruna/driver/ADC.h"
9 
10 namespace aruna {
11  namespace driver {
12 
13  err_t ADC::set_resolution(size_t bits) {
14  resolution = bits;
15  return err_t::OK;
16  }
17 
18  err_t ADC::set_reference(uint16_t mV) {
19  reference = mV;
20  return err_t::OK;
21  }
22 
23  size_t ADC::get_resolution() const {
24  return resolution;
25  }
26 
27  int32_t ADC::raw_to_voltage(int32_t raw) {
28  double input, inputLow, inputHigh, outputHigh, outputLow;
29  input = raw;
30  inputLow = 0;
31  inputHigh = INT32_MAX;
32  outputHigh = reference;
33  outputLow = 0;
34 // TODO make convert_range function and replace Actuator::convert_range with this
35 // https://gamedev.stackexchange.com/questions/33441/how-to-convert-a-number-from-one-min-max-set-to-another-min-max-set
36  return ((input - inputLow) / (inputHigh - inputLow)) * (outputHigh - outputLow) + outputLow;
37 
38  }
39 
40  uint16_t ADC::get_reference() const {
41  return reference;
42  }
43 
44  }
45 }
Definition: comm.cpp:14
virtual int32_t raw_to_voltage(int32_t raw)
Convert raw data to voltage.
Definition: ADC.cpp:27
uint16_t get_reference() const
Get the reference voltage in micro Voltage.
Definition: ADC.cpp:40
size_t get_resolution() const
Get the resolution in bits of the ADC.
Definition: ADC.cpp:23
size_t resolution
resolution of the ADC in bits.
Definition: ADC.h:24
Link * driver
stores the driver.
Definition: comm.cpp:45
err_t set_reference(uint16_t mV)
set reference voltage, used to convert raw data to voltage.
Definition: ADC.cpp:18
err_t set_resolution(size_t bits)
Set resolution of ADC.
Definition: ADC.cpp:13
uint16_t reference
ADC voltage refrence, for convertage to voltage.
Definition: ADC.h:29