Skip to content

Commit 0a057b5

Browse files
committed
Replace static states with dynamic allocation (simplifies init code).
1 parent a801ac3 commit 0a057b5

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

examples/Braccio_Learn_and_Repeat/AppState.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ const char * btnm_map[] = { "RECORD", "\n", "REPLAY", "\n", "ZERO_POSITION", "\n
3232
static float sample_buf[SAMPLE_BUF_SIZE];
3333
static int sample_cnt;
3434

35-
IdleState LearnAndRepeatApp::_idle_state;
36-
RecordState LearnAndRepeatApp::_record_state;
37-
ReplayState LearnAndRepeatApp::_replay_state;
38-
ZeroState LearnAndRepeatApp::_zero_state;
39-
4035
extern LearnAndRepeatApp app;
4136

4237
/**************************************************************************************
@@ -107,7 +102,7 @@ void custom_main_menu()
107102

108103
State * State::handle_OnZeroPosition()
109104
{
110-
return &LearnAndRepeatApp::_zero_state;
105+
return new ZeroState();
111106
}
112107

113108
/**************************************************************************************
@@ -126,12 +121,12 @@ void IdleState::onExit()
126121

127122
State * IdleState::handle_OnRecord()
128123
{
129-
return &LearnAndRepeatApp::_record_state;
124+
return new RecordState();
130125
}
131126

132127
State * IdleState::handle_OnReplay()
133128
{
134-
return &LearnAndRepeatApp::_replay_state;
129+
return new ReplayState();
135130
}
136131

137132
/**************************************************************************************
@@ -159,14 +154,14 @@ void RecordState::onExit()
159154

160155
State * RecordState::handle_OnRecord()
161156
{
162-
return &LearnAndRepeatApp::_idle_state;
157+
return new IdleState();
163158
}
164159

165160
State * RecordState::handle_OnTimerTick()
166161
{
167162
/* The sample buffer is full. */
168163
if (sample_cnt >= SAMPLE_BUF_SIZE) {
169-
return &LearnAndRepeatApp::_idle_state;
164+
return new IdleState();
170165
}
171166

172167
/* We still have space, let's sample some data. */
@@ -185,8 +180,6 @@ State * RecordState::handle_OnTimerTick()
185180

186181
void ReplayState::onEnter()
187182
{
188-
_replay_cnt = 0;
189-
190183
btnm_map[2] = "STOP";
191184
lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED);
192185
lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED);
@@ -202,14 +195,14 @@ void ReplayState::onExit()
202195

203196
State * ReplayState::handle_OnReplay()
204197
{
205-
return &LearnAndRepeatApp::_idle_state;
198+
return new IdleState();
206199
}
207200

208201
State * ReplayState::handle_OnTimerTick()
209202
{
210203
/* All samples have been replayed. */
211204
if (_replay_cnt >= sample_cnt) {
212-
return &LearnAndRepeatApp::_idle_state;
205+
return new IdleState();
213206
}
214207

215208
/* Replay recorded movements. */
@@ -232,7 +225,7 @@ State * ReplayState::handle_OnTimerTick()
232225

233226
State * ZeroState::handle_OnTimerTick()
234227
{
235-
return &LearnAndRepeatApp::_idle_state;
228+
return new IdleState();
236229
}
237230

238231
void ZeroState::onEnter()

examples/Braccio_Learn_and_Repeat/AppState.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class RecordState : public State
8787
class ReplayState : public State
8888
{
8989
public:
90+
ReplayState() : _replay_cnt{0} { }
9091
virtual ~ReplayState() { }
9192
virtual StateName name() override { return StateName::Replay; }
9293
virtual void onEnter() override;
@@ -120,11 +121,6 @@ class LearnAndRepeatApp
120121
, _mtx{}
121122
{ }
122123

123-
static IdleState _idle_state;
124-
static RecordState _record_state;
125-
static ReplayState _replay_state;
126-
static ZeroState _zero_state;
127-
128124
void enableButtons();
129125

130126
void update(EventSource const evt_src)
@@ -133,7 +129,7 @@ class LearnAndRepeatApp
133129

134130
if (!_state)
135131
{
136-
_state = &_zero_state;
132+
_state = new ZeroState();
137133
_state->onEnter();
138134
return;
139135
}
@@ -143,6 +139,7 @@ class LearnAndRepeatApp
143139
if (next_state->name() != _state->name())
144140
{
145141
_state->onExit();
142+
delete _state;
146143
_state = next_state;
147144
_state->onEnter();
148145
}

0 commit comments

Comments
 (0)