|
| 1 | +/************************************************************************************** |
| 2 | + * INCLUDE |
| 3 | + **************************************************************************************/ |
| 4 | + |
| 5 | +#include <Braccio++.h> |
| 6 | + |
| 7 | +#include "AppState.h" |
| 8 | + |
| 9 | +/************************************************************************************** |
| 10 | + * DEFINES |
| 11 | + **************************************************************************************/ |
| 12 | + |
| 13 | +#define COLOR_TEAL 0x00878F |
| 14 | +#define COLOR_LIGHT_TEAL 0x62AEB2 |
| 15 | +#define COLOR_YELLOW 0xE5AD24 |
| 16 | + |
| 17 | +/************************************************************************************** |
| 18 | + * TYPEDEF |
| 19 | + **************************************************************************************/ |
| 20 | + |
| 21 | +// IDs of the displayed directional UI buttons |
| 22 | +enum BUTTONS { |
| 23 | + BTN_UP = 1, |
| 24 | + BTN_DOWN = 7, |
| 25 | + BTN_LEFT = 3, |
| 26 | + BTN_RIGHT = 5, |
| 27 | +}; |
| 28 | + |
| 29 | +/************************************************************************************** |
| 30 | + * CONSTANTS |
| 31 | + **************************************************************************************/ |
| 32 | + |
| 33 | +static float const HOME_POS[6] = {157.5, 157.5, 157.5, 157.5, 157.5, 90.0}; |
| 34 | +static const char *DIRECTION_BTNM_MAP[] = {" ", LV_SYMBOL_UP, " ", "\n", |
| 35 | + LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, "\n", |
| 36 | + " ", LV_SYMBOL_DOWN, " ", "\0"}; |
| 37 | + |
| 38 | +/************************************************************************************** |
| 39 | + * GLOBAL VARIABLES |
| 40 | + **************************************************************************************/ |
| 41 | + |
| 42 | +lv_obj_t * direction_btnm; |
| 43 | +lv_obj_t * label; |
| 44 | + |
| 45 | +ManualControlApp app; |
| 46 | + |
| 47 | +/************************************************************************************** |
| 48 | + * SETUP/LOOP |
| 49 | + **************************************************************************************/ |
| 50 | + |
| 51 | +void setup() |
| 52 | +{ |
| 53 | + Serial.begin(115200); |
| 54 | + |
| 55 | + if (Braccio.begin(directionScreen)) |
| 56 | + { |
| 57 | + /* Configure Braccio. */ |
| 58 | + Braccio.speed(speed_grade_t(120)/*MEDIUM*/); |
| 59 | + /* Move to home position. */ |
| 60 | + Braccio.moveTo(HOME_POS[0], HOME_POS[1], HOME_POS[2], HOME_POS[3], HOME_POS[4], HOME_POS[5]); |
| 61 | + delay(500); |
| 62 | + /* Init state. */ |
| 63 | + app.update(Button::None); |
| 64 | + /* Enable buttons. */ |
| 65 | + Braccio.lvgl_lock(); |
| 66 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); |
| 67 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, 3, LV_BTNMATRIX_CTRL_DISABLED); |
| 68 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, 5, LV_BTNMATRIX_CTRL_DISABLED); |
| 69 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, 7, LV_BTNMATRIX_CTRL_DISABLED); |
| 70 | + Braccio.lvgl_unlock(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void loop() |
| 75 | +{ |
| 76 | + /* Execute every 10 ms. */ |
| 77 | + { |
| 78 | + static auto prev = millis(); |
| 79 | + auto const now = millis(); |
| 80 | + if ((now - prev) > 10) |
| 81 | + { |
| 82 | + prev = now; |
| 83 | + handle_ButtonPressedReleased(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* Execute every 50 ms. */ |
| 88 | + { |
| 89 | + static auto prev = millis(); |
| 90 | + auto const now = millis(); |
| 91 | + if ((now - prev) > 50) |
| 92 | + { |
| 93 | + prev = now; |
| 94 | + |
| 95 | + if (Braccio.isJoystickPressed_UP()) |
| 96 | + app.update(Button::Up); |
| 97 | + if (Braccio.isJoystickPressed_DOWN()) |
| 98 | + app.update(Button::Down); |
| 99 | + if (Braccio.isJoystickPressed_LEFT()) |
| 100 | + app.update(Button::Left); |
| 101 | + if (Braccio.isJoystickPressed_RIGHT()) |
| 102 | + app.update(Button::Right); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/************************************************************************************** |
| 108 | + * FUNCTIONS |
| 109 | + **************************************************************************************/ |
| 110 | + |
| 111 | +void directionScreen(void) |
| 112 | +{ |
| 113 | + Braccio.lvgl_lock(); |
| 114 | + |
| 115 | + static lv_style_t style_bg; |
| 116 | + lv_style_init(&style_bg); |
| 117 | + lv_style_set_bg_color(&style_bg, lv_color_white()); |
| 118 | + |
| 119 | + static lv_style_t style_btn; |
| 120 | + lv_style_init(&style_btn); |
| 121 | + lv_style_set_bg_color(&style_btn, lv_color_hex(COLOR_LIGHT_TEAL)); |
| 122 | + lv_style_set_text_color(&style_btn, lv_color_white()); |
| 123 | + |
| 124 | + direction_btnm = lv_btnmatrix_create(lv_scr_act()); |
| 125 | + lv_obj_set_size(direction_btnm, 240, 240); |
| 126 | + lv_btnmatrix_set_map(direction_btnm, DIRECTION_BTNM_MAP); |
| 127 | + lv_obj_align(direction_btnm, LV_ALIGN_CENTER, 0, 0); |
| 128 | + |
| 129 | + lv_obj_add_style(direction_btnm, &style_bg, 0); |
| 130 | + lv_obj_add_style(direction_btnm, &style_btn, LV_PART_ITEMS); |
| 131 | + |
| 132 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 0, LV_BTNMATRIX_CTRL_HIDDEN); |
| 133 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 1, LV_BTNMATRIX_CTRL_DISABLED); |
| 134 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 2, LV_BTNMATRIX_CTRL_HIDDEN); |
| 135 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 3, LV_BTNMATRIX_CTRL_DISABLED); |
| 136 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 4, LV_BTNMATRIX_CTRL_HIDDEN); |
| 137 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 5, LV_BTNMATRIX_CTRL_DISABLED); |
| 138 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 6, LV_BTNMATRIX_CTRL_HIDDEN); |
| 139 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 7, LV_BTNMATRIX_CTRL_DISABLED); |
| 140 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, 8, LV_BTNMATRIX_CTRL_HIDDEN); |
| 141 | + |
| 142 | + lv_btnmatrix_set_one_checked(direction_btnm, true); |
| 143 | + lv_btnmatrix_set_selected_btn(direction_btnm, 1); |
| 144 | + |
| 145 | + label = lv_label_create(lv_scr_act()); |
| 146 | + lv_obj_set_width(label, 240); |
| 147 | + lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0); |
| 148 | + lv_obj_align(label, LV_ALIGN_CENTER, 0, 0); |
| 149 | + lv_label_set_text(label, ""); |
| 150 | + |
| 151 | + Braccio.lvgl_unlock(); |
| 152 | +} |
| 153 | + |
| 154 | +void handle_ButtonPressedReleased() |
| 155 | +{ |
| 156 | + /* ENTER */ |
| 157 | + |
| 158 | + static bool prev_joystick_pressed_enter = false; |
| 159 | + bool const curr_joystick_pressed_enter = Braccio.isButtonPressed_ENTER(); |
| 160 | + if (!prev_joystick_pressed_enter && curr_joystick_pressed_enter) { |
| 161 | + app.update(Button::Enter); |
| 162 | + } |
| 163 | + prev_joystick_pressed_enter = curr_joystick_pressed_enter; |
| 164 | + |
| 165 | + /* SELECT */ |
| 166 | + |
| 167 | + static bool prev_joystick_pressed_select = false; |
| 168 | + bool const curr_joystick_pressed_select = Braccio.isJoystickPressed_SELECT(); |
| 169 | + if (!prev_joystick_pressed_select && curr_joystick_pressed_select) { |
| 170 | + app.update(Button::Enter); |
| 171 | + } |
| 172 | + prev_joystick_pressed_select = curr_joystick_pressed_select; |
| 173 | + |
| 174 | + /* DOWN */ |
| 175 | + |
| 176 | + static bool prev_joystick_pressed_down = false; |
| 177 | + bool const curr_joystick_pressed_down = Braccio.isJoystickPressed_DOWN(); |
| 178 | + if (!prev_joystick_pressed_down && curr_joystick_pressed_down) { |
| 179 | + handle_OnButtonDownPressed(); |
| 180 | + } |
| 181 | + if (prev_joystick_pressed_down && !curr_joystick_pressed_down) { |
| 182 | + handle_OnButtonDownReleased(); |
| 183 | + } |
| 184 | + prev_joystick_pressed_down = curr_joystick_pressed_down; |
| 185 | + |
| 186 | + /* UP */ |
| 187 | + |
| 188 | + static bool prev_joystick_pressed_up = false; |
| 189 | + bool const curr_joystick_pressed_up = Braccio.isJoystickPressed_UP(); |
| 190 | + if (!prev_joystick_pressed_up && curr_joystick_pressed_up) { |
| 191 | + handle_OnButtonUpPressed(); |
| 192 | + } |
| 193 | + if (prev_joystick_pressed_up && !curr_joystick_pressed_up) { |
| 194 | + handle_OnButtonUpReleased(); |
| 195 | + } |
| 196 | + prev_joystick_pressed_up = curr_joystick_pressed_up; |
| 197 | + |
| 198 | + /* LEFT */ |
| 199 | + |
| 200 | + static bool prev_joystick_pressed_left = false; |
| 201 | + bool const curr_joystick_pressed_left = Braccio.isJoystickPressed_LEFT(); |
| 202 | + if (!prev_joystick_pressed_left && curr_joystick_pressed_left) { |
| 203 | + handle_OnButtonLeftPressed(); |
| 204 | + } |
| 205 | + if (prev_joystick_pressed_left && !curr_joystick_pressed_left) { |
| 206 | + handle_OnButtonLeftReleased(); |
| 207 | + } |
| 208 | + prev_joystick_pressed_left = curr_joystick_pressed_left; |
| 209 | + |
| 210 | + /* RIGHT */ |
| 211 | + |
| 212 | + static bool prev_joystick_pressed_right = false; |
| 213 | + bool const curr_joystick_pressed_right = Braccio.isJoystickPressed_RIGHT(); |
| 214 | + if (!prev_joystick_pressed_right && curr_joystick_pressed_right) { |
| 215 | + handle_OnButtonRightPressed(); |
| 216 | + } |
| 217 | + if (prev_joystick_pressed_right && !curr_joystick_pressed_right) { |
| 218 | + handle_OnButtonRightReleased(); |
| 219 | + } |
| 220 | + prev_joystick_pressed_right = curr_joystick_pressed_right; |
| 221 | +} |
| 222 | + |
| 223 | +void handle_OnButtonDownPressed() |
| 224 | +{ |
| 225 | + Braccio.lvgl_lock(); |
| 226 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, BTN_DOWN, LV_BTNMATRIX_CTRL_CHECKED); |
| 227 | + Braccio.lvgl_unlock(); |
| 228 | +} |
| 229 | + |
| 230 | +void handle_OnButtonDownReleased() |
| 231 | +{ |
| 232 | + Braccio.lvgl_lock(); |
| 233 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, BTN_DOWN, LV_BTNMATRIX_CTRL_CHECKED); |
| 234 | + Braccio.lvgl_unlock(); |
| 235 | +} |
| 236 | + |
| 237 | +void handle_OnButtonUpPressed() |
| 238 | +{ |
| 239 | + Braccio.lvgl_lock(); |
| 240 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, BTN_UP, LV_BTNMATRIX_CTRL_CHECKED); |
| 241 | + Braccio.lvgl_unlock(); |
| 242 | +} |
| 243 | + |
| 244 | +void handle_OnButtonUpReleased() |
| 245 | +{ |
| 246 | + Braccio.lvgl_lock(); |
| 247 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, BTN_UP, LV_BTNMATRIX_CTRL_CHECKED); |
| 248 | + Braccio.lvgl_unlock(); |
| 249 | +} |
| 250 | + |
| 251 | +void handle_OnButtonLeftPressed() |
| 252 | +{ |
| 253 | + Braccio.lvgl_lock(); |
| 254 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, BTN_LEFT, LV_BTNMATRIX_CTRL_CHECKED); |
| 255 | + Braccio.lvgl_unlock(); |
| 256 | +} |
| 257 | + |
| 258 | +void handle_OnButtonLeftReleased() |
| 259 | +{ |
| 260 | + Braccio.lvgl_lock(); |
| 261 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, BTN_LEFT, LV_BTNMATRIX_CTRL_CHECKED); |
| 262 | + Braccio.lvgl_unlock(); |
| 263 | +} |
| 264 | + |
| 265 | +void handle_OnButtonRightPressed() |
| 266 | +{ |
| 267 | + Braccio.lvgl_lock(); |
| 268 | + lv_btnmatrix_set_btn_ctrl(direction_btnm, BTN_RIGHT, LV_BTNMATRIX_CTRL_CHECKED); |
| 269 | + Braccio.lvgl_unlock(); |
| 270 | +} |
| 271 | + |
| 272 | +void handle_OnButtonRightReleased() |
| 273 | +{ |
| 274 | + Braccio.lvgl_lock(); |
| 275 | + lv_btnmatrix_clear_btn_ctrl(direction_btnm, BTN_RIGHT, LV_BTNMATRIX_CTRL_CHECKED); |
| 276 | + Braccio.lvgl_unlock(); |
| 277 | +} |
0 commit comments