From 0caf351c32cb8fd5cea942c9de6b903dc0120fc9 Mon Sep 17 00:00:00 2001 From: Pat Hickey Date: Sun, 13 Nov 2011 14:14:55 +1100 Subject: [PATCH] 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() --- libraries/AP_AnalogSource/AP_AnalogSource.h | 8 +++++++ .../AP_AnalogSource/AP_AnalogSource_ADC.cpp | 9 ++++++++ .../AP_AnalogSource/AP_AnalogSource_ADC.h | 21 +++++++++++++++++++ .../AP_AnalogSource_Arduino.cpp | 8 +++++++ .../AP_AnalogSource/AP_AnalogSource_Arduino.h | 17 +++++++++++++++ libraries/AP_AnalogSource/AnalogSource.h | 11 ++++++++++ 6 files changed, 74 insertions(+) create mode 100644 libraries/AP_AnalogSource/AP_AnalogSource.h create mode 100644 libraries/AP_AnalogSource/AP_AnalogSource_ADC.cpp create mode 100644 libraries/AP_AnalogSource/AP_AnalogSource_ADC.h create mode 100644 libraries/AP_AnalogSource/AP_AnalogSource_Arduino.cpp create mode 100644 libraries/AP_AnalogSource/AP_AnalogSource_Arduino.h create mode 100644 libraries/AP_AnalogSource/AnalogSource.h 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__