From 2ec148925bea6f10010b112e25eadbd0f7ef3690 Mon Sep 17 00:00:00 2001 From: Andrew Brownbill Date: Wed, 1 Jan 2020 19:16:55 -0500 Subject: [PATCH 1/2] - Switch unique_ptrs to shared_pointers - Some minor interface cleanup. --- firmware/focuser_state.cpp | 33 +++++++++++++++------------------ firmware/focuser_state.h | 12 ++++++------ firmware/main.cpp | 12 ++++++------ firmware/net_esp8266.cpp | 12 +++++++++++- firmware/net_esp8266.h | 13 ++++--------- firmware/net_interface.h | 4 +--- firmware_sim/main.cpp | 4 ---- unit_tests/test_mock_net.h | 7 ------- 8 files changed, 43 insertions(+), 54 deletions(-) diff --git a/firmware/focuser_state.cpp b/firmware/focuser_state.cpp index 54519c5..4c27821 100644 --- a/firmware/focuser_state.cpp +++ b/firmware/focuser_state.cpp @@ -48,29 +48,27 @@ using namespace FS; ///////////////////////////////////////////////////////////////////////// Focuser::Focuser( - std::unique_ptr netArg, - std::unique_ptr hardwareArg, - std::unique_ptr debugArg, - const BuildParams params -) : buildParams{ params } + std::shared_ptr netArg, + std::shared_ptr hardwareArg, + std::shared_ptr debugArg, + const BuildParams params +) : net { netArg }, + hardware { hardwareArg }, + debugLog { debugArg }, + buildParams { params }, + dir { Dir::FORWARD }, + motorState { MotorState::OFF}, + focuserPosition { 0 }, + isSynched { false }, + time { 0 }, + uSecRemainder { 0 }, + timeLastInterruptingCommandOccured { 0 } { - focuserPosition = 0; - isSynched = false; - time = 0; - uSecRemainder = 0; - timeLastInterruptingCommandOccured = 0; - motorState = MotorState::OFF; - - std::swap( net, netArg ); - std::swap( hardware, hardwareArg ); - std::swap( debugLog, debugArg ); - DebugInterface& dlog = *debugLog; dlog << "Bringing up net interface\n"; // Bring up the interface to the controlling computer - net->setup( dlog ); WifiDebugOstream log( debugLog.get(), net.get() ); // @@ -86,7 +84,6 @@ Focuser::Focuser( // setMotor( log, MotorState::ON ); - dir = Dir::FORWARD; hardware->DigitalWrite( HWI::Pin::DIR, HWI::PinState::DIR_FORWARD); hardware->DigitalWrite( HWI::Pin::STEP, HWI::PinState::STEP_INACTIVE ); diff --git a/firmware/focuser_state.h b/firmware/focuser_state.h index 5c4e0f5..98f7c67 100644 --- a/firmware/focuser_state.h +++ b/firmware/focuser_state.h @@ -321,9 +321,9 @@ class Focuser /// @param[in] params - Hardware Parameters /// Focuser( - std::unique_ptr netArg, - std::unique_ptr hardwareArg, - std::unique_ptr debugArg, + std::shared_ptr netArg, + std::shared_ptr hardwareArg, + std::shared_ptr debugArg, const BuildParams params ); @@ -395,9 +395,9 @@ class Focuser void doDebugOff( CommandParser::CommandPacket ); void doError( CommandParser::CommandPacket ); - std::unique_ptr net; - std::unique_ptr hardware; - std::unique_ptr debugLog; + std::shared_ptr net; + std::shared_ptr hardware; + std::shared_ptr debugLog; const BuildParams buildParams; diff --git a/firmware/main.cpp b/firmware/main.cpp index c53baa2..1f945b1 100644 --- a/firmware/main.cpp +++ b/firmware/main.cpp @@ -19,15 +19,15 @@ void loop() { } void setup() { - std::unique_ptr wifi( new WifiInterfaceEthernet ); - std::unique_ptr hardware( new HardwareESP8266 ); - std::unique_ptr debug( new DebugESP8266 ); + auto debug = std::make_shared(); + auto wifi = std::make_shared( *(debug.get()) ); + auto hardware = std::make_shared(); FS::BuildParams params( FS::Build::LOW_POWER_HYPERSTAR_FOCUSER ); focuser = std::unique_ptr( new FS::Focuser( - std::move(wifi), - std::move(hardware), - std::move(debug), + wifi, + hardware, + debug, params ) ); } diff --git a/firmware/net_esp8266.cpp b/firmware/net_esp8266.cpp index c40bd14..d8236a1 100644 --- a/firmware/net_esp8266.cpp +++ b/firmware/net_esp8266.cpp @@ -3,7 +3,12 @@ #include "wifi_ostream.h" #include "wifi_debug_ostream.h" -void WifiInterfaceEthernet::setup( DebugInterface& log ) { +WifiInterfaceEthernet::WifiInterfaceEthernet( DebugInterface& log ) + : m_lastSlotAllocated{0}, + m_kickout{0}, + m_nextToKick{m_connections.begin()} +{ + reset(); delay(10); log << "Init Wifi\n"; @@ -39,6 +44,11 @@ void WifiInterfaceEthernet::setup( DebugInterface& log ) { //wifi_set_sleep_type(LIGHT_SLEEP_T); } +WifiInterfaceEthernet::~WifiInterfaceEthernet() +{ + reset(); +} + bool WifiInterfaceEthernet::getString( WifiDebugOstream& log, std::string& string ) { handleNewConnections( log ); diff --git a/firmware/net_esp8266.h b/firmware/net_esp8266.h index 571547d..a6b3a26 100644 --- a/firmware/net_esp8266.h +++ b/firmware/net_esp8266.h @@ -68,17 +68,12 @@ class WifiConnectionEthernet: public NetConnection { class WifiInterfaceEthernet: public NetInterface { public: - WifiInterfaceEthernet() : m_lastSlotAllocated{0}, m_kickout{0}, m_nextToKick{m_connections.begin()} - { - reset(); - } - ~WifiInterfaceEthernet() - { - reset(); - } + WifiInterfaceEthernet( DebugInterface& log); + WifiInterfaceEthernet() = delete; + WifiInterfaceEthernet(const WifiInterfaceEthernet& ) = delete; + ~WifiInterfaceEthernet(); void reset( void ); - void setup( DebugInterface& debugLog ) override; bool getString( WifiDebugOstream &log, std::string& string ) override; std::streamsize write( const char_type* s, std::streamsize n ) override; diff --git a/firmware/net_interface.h b/firmware/net_interface.h index 2810ead..7bed7d0 100644 --- a/firmware/net_interface.h +++ b/firmware/net_interface.h @@ -18,15 +18,13 @@ class NetInterface { struct category : public beefocus_tag {}; using char_type = char; - NetInterface() + NetInterface( void ) { } virtual ~NetInterface() { } - virtual void setup( DebugInterface &debugLog ) = 0; - virtual bool getString( WifiDebugOstream &log, std::string& string ) = 0; virtual std::streamsize write( const char_type* s, std::streamsize n ) = 0; virtual void flush() = 0; diff --git a/firmware_sim/main.cpp b/firmware_sim/main.cpp index 396943d..02d57b1 100644 --- a/firmware_sim/main.cpp +++ b/firmware_sim/main.cpp @@ -14,10 +14,6 @@ class NetInterfaceSim: public NetInterface { struct category: virtual beefocus_tag {}; using char_type = char; - void setup( DebugInterface& debugLog ) override - { - debugLog << "Simulator Net Interface Init\n"; - } bool getString( WifiDebugOstream& log, std::string& input ) override { fd_set readfds; diff --git a/unit_tests/test_mock_net.h b/unit_tests/test_mock_net.h index a5b783d..7062f1d 100644 --- a/unit_tests/test_mock_net.h +++ b/unit_tests/test_mock_net.h @@ -78,13 +78,6 @@ class NetMockSimpleTimed: public NetInterface NetMockSimpleTimed( const HWMockTimed& ) = delete; NetMockSimpleTimed& operator=( const NetMockSimpleTimed& ) = delete; - /// - /// @brief Implement setup required by NetInterface. Does nothing. - /// - void setup( DebugInterface& debugLog ) override - { - } - /// /// @brief Get input from the net interface /// @param[in] log - Debug Log (ignored) From 74be433ac29e79cb1e81e55afd6f7d5d868541bc Mon Sep 17 00:00:00 2001 From: Andrew Brownbill Date: Wed, 1 Jan 2020 19:25:31 -0500 Subject: [PATCH 2/2] Fix a compile bug that showed up on CI --- unit_tests/test_mock_net.h | 1 + 1 file changed, 1 insertion(+) diff --git a/unit_tests/test_mock_net.h b/unit_tests/test_mock_net.h index 7062f1d..179073c 100644 --- a/unit_tests/test_mock_net.h +++ b/unit_tests/test_mock_net.h @@ -5,6 +5,7 @@ #ifndef __TEST_MOCK_NET_H__ #define __TEST_MOCK_NET_H__ +#include // for std::copy_if #include "net_interface.h" #include "test_mock_event.h"