Skip to content

Commit

Permalink
Allocate class instances statically
Browse files Browse the repository at this point in the history
Avoiding dynamic allocation saves some bookkeeping, but mainly
it lets Platform.IO output a more faithful evaluation of the RAM
requirements for the firmware.
  • Loading branch information
jonathanperret authored and dl1com committed Jul 23, 2024
1 parent 72f113d commit 9c8648f
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/ayab/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,25 @@
#include "solenoids.h"
#include "tester.h"

// Global definitions: references elsewhere must use `extern`.
// Each of the following is a pointer to a singleton class
// containing static methods.
constexpr GlobalBeeper *beeper;
constexpr GlobalCom *com;
constexpr GlobalEncoders *encoders;
constexpr GlobalFsm *fsm;
constexpr GlobalKnitter *knitter;
constexpr GlobalSolenoids *solenoids;
constexpr GlobalTester *tester;

// Initialize static members.
// Each singleton class contains a pointer to a static instance
// that implements a public interface. When testing, a pointer
// to an instance of a mock class can be substituted.
BeeperInterface *GlobalBeeper::m_instance = new Beeper();
ComInterface *GlobalCom::m_instance = new Com();
EncodersInterface *GlobalEncoders::m_instance = new Encoders();
FsmInterface *GlobalFsm::m_instance = new Fsm();
KnitterInterface *GlobalKnitter::m_instance = new Knitter();
SolenoidsInterface *GlobalSolenoids::m_instance = new Solenoids();
TesterInterface *GlobalTester::m_instance = new Tester();
Beeper _Beeper;
Com _Com;
Encoders _Encoders;
Fsm _Fsm;
Knitter _Knitter;
Solenoids _Solenoids;
Tester _Tester;

BeeperInterface *GlobalBeeper::m_instance = &_Beeper;
ComInterface *GlobalCom::m_instance = &_Com;
EncodersInterface *GlobalEncoders::m_instance = &_Encoders;
FsmInterface *GlobalFsm::m_instance = &_Fsm;
KnitterInterface *GlobalKnitter::m_instance = &_Knitter;
SolenoidsInterface *GlobalSolenoids::m_instance = &_Solenoids;
TesterInterface *GlobalTester::m_instance = &_Tester;

/*!
* Setup - do once before going to the main loop.
Expand Down

0 comments on commit 9c8648f

Please sign in to comment.