AP_HAL: add run() method

Add run method, that encapsulate any mainloop logic on behalf of the
client code. The setup/loop functions are passed via a HAL::Callbacks
interface. The AP_HAL_MAIN() macro should be kept as trivial as
possible.

This interface should be implemented by the existing vehicle objects. To
make easy for the examples (that don't have the equivalent of vehicle
objects), a FunCallbacks was added to bridge to the functions directly.
This commit is contained in:
Caio Marcelo de Oliveira Filho 2015-10-19 12:13:39 -02:00 committed by Andrew Tridgell
parent ec52df991c
commit 72cd5ef185
2 changed files with 34 additions and 0 deletions

15
libraries/AP_HAL/HAL.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <assert.h>
#include "HAL.h"
namespace AP_HAL {
HAL::FunCallbacks::FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void))
: _setup(setup_fun)
, _loop(loop_fun)
{
assert(setup_fun);
assert(loop_fun);
}
}

View File

@ -51,8 +51,27 @@ public:
util(_util)
{}
struct Callbacks {
virtual void setup() = 0;
virtual void loop() = 0;
};
struct FunCallbacks : public Callbacks {
FunCallbacks(void (*setup_fun)(void), void (*loop_fun)(void));
void setup() override { _setup(); }
void loop() override { _loop(); }
private:
void (*_setup)(void);
void (*_loop)(void);
};
virtual void init(int argc, char * const argv[]) const = 0;
// TODO: Make it pure virtual once all the boards implement it.
virtual void run(int argc, char * const argv[], Callbacks* callbacks) const {};
AP_HAL::UARTDriver* uartA;
AP_HAL::UARTDriver* uartB;
AP_HAL::UARTDriver* uartC;