AP_HAL: implement DigitalSource abstraction

* GPIO's complement to AnalogSource
This commit is contained in:
Pat Hickey 2012-10-01 21:55:52 -07:00 committed by Andrew Tridgell
parent deb96143d3
commit 73ccfaf2d7
5 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,7 @@ namespace AP_HAL {
class Storage; class Storage;
class Dataflash; class Dataflash;
class ConsoleDriver; class ConsoleDriver;
class DigitalSource;
class GPIO; class GPIO;
class RCInput; class RCInput;
class RCOutput; class RCOutput;

View File

@ -9,6 +9,13 @@
#define GPIO_INPUT 0 #define GPIO_INPUT 0
#define GPIO_OUTPUT 1 #define GPIO_OUTPUT 1
class AP_HAL::DigitalSource {
public:
virtual void mode(uint8_t output) = 0;
virtual uint8_t read() = 0;
virtual void write(uint8_t value) = 0;
};
class AP_HAL::GPIO { class AP_HAL::GPIO {
public: public:
GPIO() {} GPIO() {}
@ -16,6 +23,8 @@ public:
virtual void pinMode(uint8_t pin, uint8_t output) = 0; virtual void pinMode(uint8_t pin, uint8_t output) = 0;
virtual uint8_t read(uint8_t pin) = 0; virtual uint8_t read(uint8_t pin) = 0;
virtual void write(uint8_t pin, uint8_t value) = 0; virtual void write(uint8_t pin, uint8_t value) = 0;
/* Alternative interface: */
virtual AP_HAL::DigitalSource* channel(int n) = 0;
}; };
#endif // __AP_HAL_GPIO_H__ #endif // __AP_HAL_GPIO_H__

View File

@ -18,6 +18,7 @@ namespace AP_HAL_AVR {
class APM2Dataflash; class APM2Dataflash;
class AVRConsoleDriver; class AVRConsoleDriver;
class ArduinoGPIO; class ArduinoGPIO;
class ArduinoDigitalSource;
class APM1RCInput; class APM1RCInput;
class APM2RCInput; class APM2RCInput;
class APM1RCOutput; class APM1RCOutput;

View File

@ -6,6 +6,7 @@
#include "GPIO.h" #include "GPIO.h"
using namespace AP_HAL_AVR; using namespace AP_HAL_AVR;
ArduinoGPIO* ArduinoDigitalSource::parent;
// Get the bit location within the hardware port of the given virtual pin. // Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration. // This comes from the pins_*.c file for the active board configuration.
@ -81,3 +82,21 @@ void ArduinoGPIO::write(uint8_t pin, uint8_t value) {
SREG = oldSREG; SREG = oldSREG;
} }
AP_HAL::DigitalSource* ArduinoGPIO::channel(int n) {
if (ArduinoDigitalSource::parent == NULL) {
ArduinoDigitalSource::parent = this;
}
return new ArduinoDigitalSource(n);
}
void ArduinoDigitalSource::mode(uint8_t output) {
parent->pinMode(_pin, output);
}
uint8_t ArduinoDigitalSource::read() {
return parent->read(_pin);
}
void ArduinoDigitalSource::write(uint8_t value) {
parent->write(_pin, value);
}

View File

@ -5,6 +5,18 @@
#include <AP_HAL.h> #include <AP_HAL.h>
#include "AP_HAL_AVR_Namespace.h" #include "AP_HAL_AVR_Namespace.h"
class AP_HAL_AVR::ArduinoDigitalSource : public AP_HAL::DigitalSource {
public:
ArduinoDigitalSource(int pin) : _pin(pin) {}
void mode(uint8_t output);
uint8_t read();
void write(uint8_t value);
static ArduinoGPIO* parent;
private:
int _pin;
};
class AP_HAL_AVR::ArduinoGPIO : public AP_HAL::GPIO { class AP_HAL_AVR::ArduinoGPIO : public AP_HAL::GPIO {
public: public:
ArduinoGPIO() {} ArduinoGPIO() {}
@ -12,6 +24,7 @@ public:
void pinMode(uint8_t pin, uint8_t output); void pinMode(uint8_t pin, uint8_t output);
uint8_t read(uint8_t pin); uint8_t read(uint8_t pin);
void write(uint8_t pin, uint8_t value); void write(uint8_t pin, uint8_t value);
AP_HAL::DigitalSource* channel(int);
}; };
#endif // __AP_HAL_AVR_GPIO_H__ #endif // __AP_HAL_AVR_GPIO_H__