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:
Pat Hickey 2011-11-13 14:14:55 +11:00
parent 017f121566
commit 0c0a1b1dcc
6 changed files with 74 additions and 0 deletions

View 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__

View 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;
}

View 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__

View File

@ -0,0 +1,8 @@
#include "wiring.h"
#include "AP_AnalogSource_Arduino.h"
int AP_AnalogSource_Arduino::read(void)
{
return analogRead(_pin);
}

View 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__

View 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__