diff --git a/libraries/AP_AnalogSource/AP_AnalogSource.h b/libraries/AP_AnalogSource/AP_AnalogSource.h new file mode 100644 index 0000000000..3530e356bc --- /dev/null +++ b/libraries/AP_AnalogSource/AP_AnalogSource.h @@ -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__ diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp new file mode 100644 index 0000000000..a400c07ce4 --- /dev/null +++ b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp @@ -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; +} diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h new file mode 100644 index 0000000000..a438f00f40 --- /dev/null +++ b/libraries/AP_AnalogSource/AP_AnalogSource_ADC.h @@ -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__ diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp new file mode 100644 index 0000000000..d8f389dcf0 --- /dev/null +++ b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp @@ -0,0 +1,8 @@ + +#include "wiring.h" +#include "AP_AnalogSource_Arduino.h" + +int AP_AnalogSource_Arduino::read(void) +{ + return analogRead(_pin); +} diff --git a/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h new file mode 100644 index 0000000000..ba17f50fd6 --- /dev/null +++ b/libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h @@ -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__ diff --git a/libraries/AP_AnalogSource/AnalogSource.h b/libraries/AP_AnalogSource/AnalogSource.h new file mode 100644 index 0000000000..1a5ad7e6f3 --- /dev/null +++ b/libraries/AP_AnalogSource/AnalogSource.h @@ -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__