Skip to content

Commit 7ceb29c

Browse files
committed
temp save.
1 parent 5ff8dde commit 7ceb29c

File tree

3 files changed

+419
-23
lines changed

3 files changed

+419
-23
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/**************************************************************************************
2+
* INCLUDE
3+
**************************************************************************************/
4+
5+
#include "AppState.h"
6+
7+
#include <Braccio++.h>
8+
9+
extern lv_obj_t * label;
10+
extern lv_obj_t * direction_btnm;
11+
12+
enum BUTTONS {
13+
BTN_UP = 1,
14+
BTN_DOWN = 7,
15+
BTN_LEFT = 3,
16+
BTN_RIGHT = 5,
17+
};
18+
19+
/**************************************************************************************
20+
* State
21+
**************************************************************************************/
22+
23+
State * State::handle_OnButtonDownPressed()
24+
{
25+
Braccio.lvgl_lock();
26+
lv_btnmatrix_set_btn_ctrl(direction_btnm, BTN_DOWN, LV_BTNMATRIX_CTRL_CHECKED);
27+
Braccio.lvgl_unlock();
28+
return this;
29+
}
30+
31+
State * State::handle_OnButtonDownReleased()
32+
{
33+
Braccio.lvgl_lock();
34+
lv_btnmatrix_clear_btn_ctrl(direction_btnm, BTN_DOWN, LV_BTNMATRIX_CTRL_CHECKED);
35+
Braccio.lvgl_unlock();
36+
return this;
37+
}
38+
39+
/**************************************************************************************
40+
* ShoulderState
41+
**************************************************************************************/
42+
43+
void ShoulderState::onEnter()
44+
{
45+
Braccio.lvgl_lock();
46+
lv_label_set_text(label, "Shoulder");
47+
Braccio.lvgl_unlock();
48+
}
49+
50+
void ShoulderState::onExit()
51+
{
52+
53+
}
54+
55+
State * ShoulderState::handle_OnButtonDownPressed()
56+
{
57+
State::handle_OnButtonDownPressed();
58+
return this;
59+
}
60+
61+
State * ShoulderState::handle_OnButtonDownReleased()
62+
{
63+
State::handle_OnButtonDownReleased();
64+
return this;
65+
}
66+
67+
State * ShoulderState::handle_OnButtonUp()
68+
{
69+
return this;
70+
}
71+
72+
State * ShoulderState::handle_OnButtonLeft()
73+
{
74+
return this;
75+
}
76+
77+
State * ShoulderState::handle_OnButtonRight()
78+
{
79+
return this;
80+
}
81+
82+
State * ShoulderState::handle_OnButtonEnter()
83+
{
84+
return new ElbowState();
85+
}
86+
87+
/**************************************************************************************
88+
* ElbowState
89+
**************************************************************************************/
90+
91+
void ElbowState::onEnter()
92+
{
93+
Braccio.lvgl_lock();
94+
lv_label_set_text(label, "Elbow");
95+
Braccio.lvgl_unlock();
96+
}
97+
98+
void ElbowState::onExit()
99+
{
100+
101+
}
102+
103+
State * ElbowState::handle_OnButtonDownPressed()
104+
{
105+
State::handle_OnButtonDownPressed();
106+
return this;
107+
}
108+
109+
State * ElbowState::handle_OnButtonDownReleased()
110+
{
111+
State::handle_OnButtonDownReleased();
112+
return this;
113+
}
114+
115+
State * ElbowState::handle_OnButtonUp()
116+
{
117+
return new WristState();
118+
}
119+
120+
State * ElbowState::handle_OnButtonEnter()
121+
{
122+
return new WristState();
123+
}
124+
125+
/**************************************************************************************
126+
* WristState
127+
**************************************************************************************/
128+
129+
void WristState::onEnter()
130+
{
131+
Braccio.lvgl_lock();
132+
lv_label_set_text(label, "Wrist");
133+
Braccio.lvgl_unlock();
134+
}
135+
136+
void WristState::onExit()
137+
{
138+
139+
}
140+
141+
State * WristState::handle_OnButtonDownPressed()
142+
{
143+
State::handle_OnButtonDownPressed();
144+
return this;
145+
}
146+
147+
State * WristState::handle_OnButtonDownReleased()
148+
{
149+
State::handle_OnButtonDownReleased();
150+
return this;
151+
}
152+
153+
State * WristState::handle_OnButtonUp()
154+
{
155+
return this;
156+
}
157+
158+
State * WristState::handle_OnButtonLeft()
159+
{
160+
return this;
161+
}
162+
163+
State * WristState::handle_OnButtonRight()
164+
{
165+
return this;
166+
}
167+
168+
State * WristState::handle_OnButtonEnter()
169+
{
170+
return new PinchState();
171+
}
172+
173+
/**************************************************************************************
174+
* PinchState
175+
**************************************************************************************/
176+
177+
void PinchState::onEnter()
178+
{
179+
Braccio.lvgl_lock();
180+
lv_label_set_text(label, "Pinch");
181+
Braccio.lvgl_unlock();
182+
}
183+
184+
void PinchState::onExit()
185+
{
186+
187+
}
188+
189+
State * PinchState::handle_OnButtonLeft()
190+
{
191+
return this;
192+
}
193+
194+
State * PinchState::handle_OnButtonRight()
195+
{
196+
return this;
197+
}
198+
199+
State * PinchState::handle_OnButtonEnter()
200+
{
201+
return new ShoulderState();
202+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#ifndef APP_STATE_H_
2+
#define APP_STATE_H_
3+
4+
/**************************************************************************************
5+
* TYPEDEF
6+
**************************************************************************************/
7+
8+
enum class EventSource
9+
{
10+
Button_DownPressed, Button_DownReleased, Button_Up, Button_Left, Button_Right, Button_Enter
11+
};
12+
13+
enum class StateName
14+
{
15+
Shoulder, Elbow, Wrist, Pinch
16+
};
17+
18+
/**************************************************************************************
19+
* CLASS DECLARATION
20+
**************************************************************************************/
21+
22+
class State
23+
{
24+
public:
25+
virtual ~State() { }
26+
virtual void onEnter() { }
27+
virtual void onExit() { }
28+
virtual StateName name() = 0;
29+
State * update(EventSource const evt_src)
30+
{
31+
State * next_state = this;
32+
switch (evt_src)
33+
{
34+
case EventSource::Button_DownPressed: next_state = handle_OnButtonDownPressed(); break;
35+
case EventSource::Button_DownReleased: next_state = handle_OnButtonDownReleased(); break;
36+
37+
case EventSource::Button_Up: next_state = handle_OnButtonUp(); break;
38+
case EventSource::Button_Left: next_state = handle_OnButtonLeft(); break;
39+
case EventSource::Button_Right: next_state = handle_OnButtonRight(); break;
40+
case EventSource::Button_Enter: next_state = handle_OnButtonEnter(); break;
41+
}
42+
return next_state;
43+
}
44+
45+
protected:
46+
virtual State * handle_OnButtonDownPressed();
47+
virtual State * handle_OnButtonDownReleased();
48+
virtual State * handle_OnButtonUp() { return this; }
49+
virtual State * handle_OnButtonLeft() { return this; }
50+
virtual State * handle_OnButtonRight() { return this; }
51+
virtual State * handle_OnButtonEnter() { return this; }
52+
};
53+
54+
class ShoulderState : public State
55+
{
56+
public:
57+
virtual ~ShoulderState() { }
58+
virtual StateName name() override { return StateName::Shoulder; }
59+
virtual void onEnter() override;
60+
virtual void onExit() override;
61+
62+
protected:
63+
virtual State * handle_OnButtonDownPressed() override;
64+
virtual State * handle_OnButtonDownReleased() override;
65+
virtual State * handle_OnButtonUp() override;
66+
virtual State * handle_OnButtonLeft() override;
67+
virtual State * handle_OnButtonRight() override;
68+
virtual State * handle_OnButtonEnter() override;
69+
};
70+
71+
class ElbowState : public State
72+
{
73+
public:
74+
virtual ~ElbowState() { }
75+
virtual StateName name() override { return StateName::Elbow; }
76+
virtual void onEnter() override;
77+
virtual void onExit() override;
78+
79+
protected:
80+
virtual State * handle_OnButtonDownPressed() override;
81+
virtual State * handle_OnButtonDownReleased() override;
82+
virtual State * handle_OnButtonUp() override;
83+
virtual State * handle_OnButtonEnter() override;
84+
};
85+
86+
class WristState : public State
87+
{
88+
public:
89+
virtual ~WristState() { }
90+
virtual StateName name() override { return StateName::Wrist; }
91+
virtual void onEnter() override;
92+
virtual void onExit() override;
93+
94+
protected:
95+
virtual State * handle_OnButtonDownPressed() override;
96+
virtual State * handle_OnButtonDownReleased() override;
97+
virtual State * handle_OnButtonUp() override;
98+
virtual State * handle_OnButtonLeft() override;
99+
virtual State * handle_OnButtonRight() override;
100+
virtual State * handle_OnButtonEnter() override;
101+
};
102+
103+
class PinchState : public State
104+
{
105+
public:
106+
virtual ~PinchState() { }
107+
virtual StateName name() override { return StateName::Pinch; }
108+
virtual void onEnter() override;
109+
virtual void onExit() override;
110+
111+
protected:
112+
virtual State * handle_OnButtonLeft() override;
113+
virtual State * handle_OnButtonRight() override;
114+
virtual State * handle_OnButtonEnter() override;
115+
};
116+
117+
class ControlApp
118+
{
119+
public:
120+
ControlApp()
121+
: _state{nullptr}
122+
{ }
123+
124+
void update(EventSource const evt_src)
125+
{
126+
if (!_state)
127+
{
128+
_state = new ShoulderState();
129+
_state->onEnter();
130+
}
131+
132+
State * next_state = _state->update(evt_src);
133+
134+
if (next_state->name() != _state->name())
135+
{
136+
_state->onExit();
137+
delete _state;
138+
_state = next_state;
139+
_state->onEnter();
140+
}
141+
}
142+
143+
private:
144+
State * _state;
145+
};
146+
147+
#endif /* APP_STATE_H_ */

0 commit comments

Comments
 (0)