diff --git a/libraries/AP_HAL/AP_HAL_Namespace.h b/libraries/AP_HAL/AP_HAL_Namespace.h index 1315e5b45c..bf562f78fd 100644 --- a/libraries/AP_HAL/AP_HAL_Namespace.h +++ b/libraries/AP_HAL/AP_HAL_Namespace.h @@ -18,6 +18,7 @@ namespace AP_HAL { class Storage; class Dataflash; class ConsoleDriver; + class DigitalSource; class GPIO; class RCInput; class RCOutput; diff --git a/libraries/AP_HAL/GPIO.h b/libraries/AP_HAL/GPIO.h index 32fe6f5876..04caad420c 100644 --- a/libraries/AP_HAL/GPIO.h +++ b/libraries/AP_HAL/GPIO.h @@ -9,6 +9,13 @@ #define GPIO_INPUT 0 #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 { public: GPIO() {} @@ -16,6 +23,8 @@ public: virtual void pinMode(uint8_t pin, uint8_t output) = 0; virtual uint8_t read(uint8_t pin) = 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__ diff --git a/libraries/AP_HAL_AVR/AP_HAL_AVR_Namespace.h b/libraries/AP_HAL_AVR/AP_HAL_AVR_Namespace.h index dcc8a78caa..98772b2712 100644 --- a/libraries/AP_HAL_AVR/AP_HAL_AVR_Namespace.h +++ b/libraries/AP_HAL_AVR/AP_HAL_AVR_Namespace.h @@ -18,6 +18,7 @@ namespace AP_HAL_AVR { class APM2Dataflash; class AVRConsoleDriver; class ArduinoGPIO; + class ArduinoDigitalSource; class APM1RCInput; class APM2RCInput; class APM1RCOutput; diff --git a/libraries/AP_HAL_AVR/GPIO.cpp b/libraries/AP_HAL_AVR/GPIO.cpp index 4a980364bc..df479a679d 100644 --- a/libraries/AP_HAL_AVR/GPIO.cpp +++ b/libraries/AP_HAL_AVR/GPIO.cpp @@ -6,6 +6,7 @@ #include "GPIO.h" using namespace AP_HAL_AVR; +ArduinoGPIO* ArduinoDigitalSource::parent; // 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. @@ -81,3 +82,21 @@ void ArduinoGPIO::write(uint8_t pin, uint8_t value) { 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); +} diff --git a/libraries/AP_HAL_AVR/GPIO.h b/libraries/AP_HAL_AVR/GPIO.h index d5643fc416..7ed7c0ca5f 100644 --- a/libraries/AP_HAL_AVR/GPIO.h +++ b/libraries/AP_HAL_AVR/GPIO.h @@ -5,6 +5,18 @@ #include #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 { public: ArduinoGPIO() {} @@ -12,6 +24,7 @@ public: void pinMode(uint8_t pin, uint8_t output); uint8_t read(uint8_t pin); void write(uint8_t pin, uint8_t value); + AP_HAL::DigitalSource* channel(int); }; #endif // __AP_HAL_AVR_GPIO_H__