|
| 1 | +/************************************************************************************** |
| 2 | + * INCLUDE |
| 3 | + **************************************************************************************/ |
| 4 | + |
| 5 | +#include "AppState.h" |
| 6 | + |
| 7 | +/************************************************************************************** |
| 8 | + * CONSTANT |
| 9 | + **************************************************************************************/ |
| 10 | + |
| 11 | +static int const SAMPLE_BUF_SIZE = 6*100*2; /* 20 seconds. */ |
| 12 | +static float const HOME_POS[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0}; |
| 13 | + |
| 14 | + |
| 15 | +/************************************************************************************** |
| 16 | + * GLOBAL VARIABLES |
| 17 | + **************************************************************************************/ |
| 18 | + |
| 19 | +extern const char * btnm_map[]; |
| 20 | +extern lv_obj_t * counter; |
| 21 | +extern lv_obj_t * btnm; |
| 22 | + |
| 23 | +static float sample_buf[SAMPLE_BUF_SIZE]; |
| 24 | +static int sample_cnt; |
| 25 | + |
| 26 | +IdleState LearnAndRepeatApp::_idle_state; |
| 27 | +RecordState LearnAndRepeatApp::_record_state; |
| 28 | +ReplayState LearnAndRepeatApp::_replay_state; |
| 29 | +ZeroState LearnAndRepeatApp::_zero_state; |
| 30 | + |
| 31 | +/************************************************************************************** |
| 32 | + * State |
| 33 | + **************************************************************************************/ |
| 34 | + |
| 35 | +State * State::handle_OnZeroPosition() |
| 36 | +{ |
| 37 | + return &LearnAndRepeatApp::_zero_state; |
| 38 | +} |
| 39 | + |
| 40 | +/************************************************************************************** |
| 41 | + * IdleState |
| 42 | + **************************************************************************************/ |
| 43 | + |
| 44 | +void IdleState::onEnter() |
| 45 | +{ |
| 46 | + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); |
| 47 | +} |
| 48 | + |
| 49 | +void IdleState::onExit() |
| 50 | +{ |
| 51 | + lv_btnmatrix_clear_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); |
| 52 | +} |
| 53 | + |
| 54 | +State * IdleState::handle_OnRecord() |
| 55 | +{ |
| 56 | + return &LearnAndRepeatApp::_record_state; |
| 57 | +} |
| 58 | + |
| 59 | +State * IdleState::handle_OnReplay() |
| 60 | +{ |
| 61 | + return &LearnAndRepeatApp::_replay_state; |
| 62 | +} |
| 63 | + |
| 64 | +/************************************************************************************** |
| 65 | + * RecordState |
| 66 | + **************************************************************************************/ |
| 67 | + |
| 68 | +void RecordState::onEnter() |
| 69 | +{ |
| 70 | + btnm_map[0] = "STOP"; |
| 71 | + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED); |
| 72 | + |
| 73 | + Braccio.disengage(); |
| 74 | + sample_cnt = 0; |
| 75 | +} |
| 76 | + |
| 77 | +void RecordState::onExit() |
| 78 | +{ |
| 79 | + btnm_map[0] = "RECORD"; |
| 80 | + lv_btnmatrix_clear_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_CHECKED); |
| 81 | + lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); |
| 82 | + lv_label_set_text_fmt(counter, "Counter: %d" , 0); |
| 83 | + |
| 84 | + Braccio.engage(); |
| 85 | +} |
| 86 | + |
| 87 | +State * RecordState::handle_OnRecord() |
| 88 | +{ |
| 89 | + return &LearnAndRepeatApp::_idle_state; |
| 90 | +} |
| 91 | + |
| 92 | +State * RecordState::handle_OnTimerTick() |
| 93 | +{ |
| 94 | + /* The sample buffer is full. */ |
| 95 | + if (sample_cnt >= SAMPLE_BUF_SIZE) { |
| 96 | + return &LearnAndRepeatApp::_idle_state; |
| 97 | + } |
| 98 | + |
| 99 | + /* We still have space, let's sample some data. */ |
| 100 | + Braccio.positions(sample_buf + sample_cnt); |
| 101 | + sample_cnt += 6; |
| 102 | + |
| 103 | + /* Update sample counter. */ |
| 104 | + lv_label_set_text_fmt(counter, "Counter: %d" , (sample_cnt / 6)); |
| 105 | + |
| 106 | + return this; |
| 107 | +} |
| 108 | + |
| 109 | +/************************************************************************************** |
| 110 | + * ReplayState |
| 111 | + **************************************************************************************/ |
| 112 | + |
| 113 | +void ReplayState::onEnter() |
| 114 | +{ |
| 115 | + _replay_cnt = 0; |
| 116 | + |
| 117 | + btnm_map[2] = "STOP"; |
| 118 | + lv_btnmatrix_set_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED); |
| 119 | + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED); |
| 120 | +} |
| 121 | + |
| 122 | +void ReplayState::onExit() |
| 123 | +{ |
| 124 | + btnm_map[2] = "REPLAY"; |
| 125 | + lv_btnmatrix_clear_btn_ctrl(btnm, 0, LV_BTNMATRIX_CTRL_DISABLED); |
| 126 | + lv_btnmatrix_clear_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_CHECKED); |
| 127 | + lv_label_set_text_fmt(counter, "Counter: %d" , 0); |
| 128 | +} |
| 129 | + |
| 130 | +State * ReplayState::handle_OnReplay() |
| 131 | +{ |
| 132 | + return &LearnAndRepeatApp::_idle_state; |
| 133 | +} |
| 134 | + |
| 135 | +State * ReplayState::handle_OnTimerTick() |
| 136 | +{ |
| 137 | + /* All samples have been replayed. */ |
| 138 | + if (_replay_cnt >= sample_cnt) { |
| 139 | + return &LearnAndRepeatApp::_idle_state; |
| 140 | + } |
| 141 | + |
| 142 | + /* Replay recorded movements. */ |
| 143 | + Braccio.moveTo(sample_buf[_replay_cnt + 0], |
| 144 | + sample_buf[_replay_cnt + 1], |
| 145 | + sample_buf[_replay_cnt + 2], |
| 146 | + sample_buf[_replay_cnt + 3], |
| 147 | + sample_buf[_replay_cnt + 4], |
| 148 | + sample_buf[_replay_cnt + 5]); |
| 149 | + _replay_cnt += 6; |
| 150 | + |
| 151 | + lv_label_set_text_fmt(counter, "Counter: %d" , (_replay_cnt / 6)); |
| 152 | + |
| 153 | + return this; |
| 154 | +} |
| 155 | + |
| 156 | +/************************************************************************************** |
| 157 | + * ReplayState |
| 158 | + **************************************************************************************/ |
| 159 | + |
| 160 | +State * ZeroState::handle_OnTimerTick() |
| 161 | +{ |
| 162 | + return &LearnAndRepeatApp::_idle_state; |
| 163 | +} |
| 164 | + |
| 165 | +void ZeroState::onEnter() |
| 166 | +{ |
| 167 | + lv_btnmatrix_set_btn_ctrl(btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); |
| 168 | + lv_btnmatrix_set_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); |
| 169 | + |
| 170 | + Braccio.engage(); |
| 171 | + delay(100); |
| 172 | + Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]); |
| 173 | + delay(500); |
| 174 | +} |
| 175 | + |
| 176 | +void ZeroState::onExit() |
| 177 | +{ |
| 178 | + lv_btnmatrix_clear_btn_ctrl(btnm, 2, LV_BTNMATRIX_CTRL_CHECKED); |
| 179 | +} |
0 commit comments