mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-11 02:18:29 -04:00
purple: Added AnalogSource library
this library abstracts out the way of getting an analog value. If the ADC library is being used then it calls the ADC Ch() method, otherwise it calls analogRead()
This commit is contained in:
parent
6d876bc54d
commit
0caf351c32
8
libraries/AP_AnalogSource/AP_AnalogSource.h
Normal file
8
libraries/AP_AnalogSource/AP_AnalogSource.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
#ifndef __AP_ANALOG_SOURCE_H__
|
||||||
|
#define __AP_ANALOG_SOURCE_H__
|
||||||
|
|
||||||
|
#include "AP_AnalogSource_Arduino.h"
|
||||||
|
#include "AP_AnalogSource_ADC.h"
|
||||||
|
|
||||||
|
#endif // __AP_ANALOG_SOURCE_H__
|
9
libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp
Normal file
9
libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
#include "AP_AnalogSource_ADC.h"
|
||||||
|
|
||||||
|
int AP_AnalogSource_ADC::read(void)
|
||||||
|
{
|
||||||
|
int fullscale = _adc->Ch(_ch);
|
||||||
|
int scaled = _prescale * fullscale;
|
||||||
|
return scaled;
|
||||||
|
}
|
21
libraries/AP_AnalogSource/AP_AnalogSource_ADC.h
Normal file
21
libraries/AP_AnalogSource/AP_AnalogSource_ADC.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
#ifndef __AP_ANALOG_SOURCE_ADC_H__
|
||||||
|
#define __AP_ANALOG_SOURCE_ADC_H__
|
||||||
|
|
||||||
|
#include "AnalogSource.h"
|
||||||
|
#include "../AP_ADC/AP_ADC.h"
|
||||||
|
|
||||||
|
class AP_AnalogSource_ADC : public AP_AnalogSource
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AP_AnalogSource_ADC( AP_ADC * adc, int ch, float prescale = 1.0 ) :
|
||||||
|
_adc(adc), _ch(ch) {}
|
||||||
|
int read(void);
|
||||||
|
|
||||||
|
private:
|
||||||
|
AP_ADC * _adc;
|
||||||
|
int _ch;
|
||||||
|
float _prescale;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __AP_ANALOG_SOURCE_ADC_H__
|
8
libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp
Normal file
8
libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
#include "wiring.h"
|
||||||
|
#include "AP_AnalogSource_Arduino.h"
|
||||||
|
|
||||||
|
int AP_AnalogSource_Arduino::read(void)
|
||||||
|
{
|
||||||
|
return analogRead(_pin);
|
||||||
|
}
|
17
libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h
Normal file
17
libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
|
||||||
|
#ifndef __AP_ANALOG_SOURCE_ARDUINO_H__
|
||||||
|
#define __AP_ANALOG_SOURCE_ARDUINO_H__
|
||||||
|
|
||||||
|
#include "AnalogSource.h"
|
||||||
|
|
||||||
|
class AP_AnalogSource_Arduino : public AP_AnalogSource
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AP_AnalogSource_Arduino( int pin ) : _pin(pin) {}
|
||||||
|
int read(void);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int _pin;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __AP_ANALOG_SOURCE_ARDUINO_H__
|
11
libraries/AP_AnalogSource/AnalogSource.h
Normal file
11
libraries/AP_AnalogSource/AnalogSource.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
|
||||||
|
#ifndef __ANALOG_SOURCE_H__
|
||||||
|
#define __ANALOG_SOURCE_H__
|
||||||
|
|
||||||
|
class AP_AnalogSource
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual int read(void) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // __ANALOG_SOURCE_H__
|
Loading…
Reference in New Issue
Block a user