From f74ad276a930fd10a59a42aa91302d342e42f90b Mon Sep 17 00:00:00 2001 From: Joshua Henderson Date: Sun, 4 Sep 2022 03:15:49 -0400 Subject: [PATCH] AP_EFI: add EFI scripting driver --- libraries/AP_EFI/AP_EFI.cpp | 22 +++++++++++++++++++++- libraries/AP_EFI/AP_EFI.h | 10 ++++++++-- libraries/AP_EFI/AP_EFI_Backend.h | 4 ++++ libraries/AP_EFI/AP_EFI_Scripting.cpp | 18 ++++++++++++++++++ libraries/AP_EFI/AP_EFI_Scripting.h | 20 ++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 libraries/AP_EFI/AP_EFI_Scripting.cpp create mode 100644 libraries/AP_EFI/AP_EFI_Scripting.h diff --git a/libraries/AP_EFI/AP_EFI.cpp b/libraries/AP_EFI/AP_EFI.cpp index 7a8a05e6b5..c94dc871c2 100644 --- a/libraries/AP_EFI/AP_EFI.cpp +++ b/libraries/AP_EFI/AP_EFI.cpp @@ -22,6 +22,8 @@ #include "AP_EFI_NWPMU.h" #include "AP_EFI_DroneCAN.h" #include "AP_EFI_Currawong_ECU.h" +#include "AP_EFI_Scripting.h" + #include #include @@ -36,7 +38,7 @@ const AP_Param::GroupInfo AP_EFI::var_info[] = { // @Param: _TYPE // @DisplayName: EFI communication type // @Description: What method of communication is used for EFI #1 - // @Values: 0:None,1:Serial-MS,2:NWPMU,3:Serial-Lutan,5:DroneCAN,6:Currawong-ECU + // @Values: 0:None,1:Serial-MS,2:NWPMU,3:Serial-Lutan,5:DroneCAN,6:Currawong-ECU,7:Scripting // @User: Advanced // @RebootRequired: True AP_GROUPINFO_FLAGS("_TYPE", 1, AP_EFI, type, 0, AP_PARAM_FLAG_ENABLE), @@ -104,6 +106,11 @@ void AP_EFI::init(void) case Type::CurrawongECU: #if HAL_EFI_CURRAWONG_ECU_ENABLED backend = new AP_EFI_Currawong_ECU(*this); +#endif + break; + case Type::SCRIPTING: +#if AP_EFI_SCRIPTING_ENABLED + backend = new AP_EFI_Scripting(*this); #endif break; default: @@ -272,6 +279,19 @@ void AP_EFI::get_state(EFI_State &_state) _state = state; } +#if AP_EFI_SCRIPTING_ENABLED && AP_SCRIPTING_ENABLED + +void AP_EFI::handle_scripting(const EFI_State &efi_state) +{ + if (!backend || (Type(type.get()) != Type::SCRIPTING)) { + return; + } + + backend->handle_scripting(efi_state); +} + +#endif + namespace AP { AP_EFI *EFI() { diff --git a/libraries/AP_EFI/AP_EFI.h b/libraries/AP_EFI/AP_EFI.h index cce0566e73..b4e4ac5050 100644 --- a/libraries/AP_EFI/AP_EFI.h +++ b/libraries/AP_EFI/AP_EFI.h @@ -78,11 +78,12 @@ public: enum class Type : uint8_t { NONE = 0, MegaSquirt = 1, - NWPMU = 2, - Lutan = 3, + NWPMU = 2, + Lutan = 3, // LOWEHEISER = 4, DroneCAN = 5, CurrawongECU = 6, + SCRIPTING = 7, }; static AP_EFI *get_singleton(void) { @@ -92,6 +93,11 @@ public: // send EFI_STATUS void send_mavlink_status(mavlink_channel_t chan); +#if AP_SCRIPTING_ENABLED + // Ingest EFI_State from scripting driver + void handle_scripting(const EFI_State &efi_state); +#endif + protected: // Back end Parameters diff --git a/libraries/AP_EFI/AP_EFI_Backend.h b/libraries/AP_EFI/AP_EFI_Backend.h index 2e6b7e48c0..3b3e631691 100644 --- a/libraries/AP_EFI/AP_EFI_Backend.h +++ b/libraries/AP_EFI/AP_EFI_Backend.h @@ -31,6 +31,10 @@ public: // Update the state structure virtual void update() = 0; +#if AP_SCRIPTING_ENABLED + virtual void handle_scripting(const EFI_State &efi_state) { return; } +#endif + protected: // Copies internal state to the frontend state void copy_to_frontend(); diff --git a/libraries/AP_EFI/AP_EFI_Scripting.cpp b/libraries/AP_EFI/AP_EFI_Scripting.cpp new file mode 100644 index 0000000000..2467a3002b --- /dev/null +++ b/libraries/AP_EFI/AP_EFI_Scripting.cpp @@ -0,0 +1,18 @@ +#include "AP_EFI_Scripting.h" + +#if AP_EFI_SCRIPTING_ENABLED + +// Called from frontend to update with the readings received by handler +void AP_EFI_Scripting::update() +{ + // Nothing to do here +} + +// handle EFI message from scripting +void AP_EFI_Scripting::handle_scripting(const EFI_State &efi_state) +{ + internal_state = efi_state; + copy_to_frontend(); +} + +#endif // AP_EFI_SCRIPTING_ENABLED diff --git a/libraries/AP_EFI/AP_EFI_Scripting.h b/libraries/AP_EFI/AP_EFI_Scripting.h new file mode 100644 index 0000000000..78a542ed1b --- /dev/null +++ b/libraries/AP_EFI/AP_EFI_Scripting.h @@ -0,0 +1,20 @@ +#pragma once + +#include "AP_EFI.h" +#include "AP_EFI_Backend.h" + +#ifndef AP_EFI_SCRIPTING_ENABLED +#define AP_EFI_SCRIPTING_ENABLED (HAL_EFI_ENABLED && AP_SCRIPTING_ENABLED) +#endif + +#if AP_EFI_SCRIPTING_ENABLED + +class AP_EFI_Scripting : public AP_EFI_Backend { +public: + using AP_EFI_Backend::AP_EFI_Backend; + + void update() override; + + void handle_scripting(const EFI_State &efi_state) override; +}; +#endif // AP_EFI_SCRIPTING_ENABLED