From 6528e6d52891364d19df36549bf8cdcf2b01abf5 Mon Sep 17 00:00:00 2001 From: Fernando Esquirio Torres Date: Fri, 19 Nov 2021 18:38:24 +0100 Subject: [PATCH] chore: add basic examples --- examples/Check_inputs/Check_inputs.ino | 30 ++++++++ examples/LCD_Custom_Menu/LCD_Custom_Menu.ino | 23 ++++++ .../LCD_Menu_Joystick/LCD_Menu_Joystick.ino | 43 +++++++++++ examples/LCD_Motors/LCD_Motors.ino | 71 +++++++++++++++++++ examples/Moving_Motors/Moving_Motors.ino | 22 ++++++ .../Testing_communications.ino | 23 ++++++ 6 files changed, 212 insertions(+) create mode 100644 examples/Check_inputs/Check_inputs.ino create mode 100644 examples/LCD_Custom_Menu/LCD_Custom_Menu.ino create mode 100644 examples/LCD_Menu_Joystick/LCD_Menu_Joystick.ino create mode 100644 examples/LCD_Motors/LCD_Motors.ino create mode 100644 examples/Moving_Motors/Moving_Motors.ino create mode 100644 examples/Testing_communications/Testing_communications.ino diff --git a/examples/Check_inputs/Check_inputs.ino b/examples/Check_inputs/Check_inputs.ino new file mode 100644 index 0000000..5ea9361 --- /dev/null +++ b/examples/Check_inputs/Check_inputs.ino @@ -0,0 +1,30 @@ +#include "Braccio++.h" + +String message = ""; + +String checkInputs(int input){ + String check[] = { "", + "Joystick left was moved!", + "Joystick right was moved!", + "Joystick select button was pressed!", + "Joystick up was moved!", + "Joystick down was moved!", + "Enter button was pressed!"}; + return check[input]; +} + +void setup() { + Serial.begin(115200); + while(!Serial){} + Braccio.begin(); + Serial.println("Press any button or move the joystick."); +} + +void loop() { + message = checkInputs(Braccio.getKey()); + if(message != ""){ + Serial.println(message); + message = ""; + } + delay(500); +} diff --git a/examples/LCD_Custom_Menu/LCD_Custom_Menu.ino b/examples/LCD_Custom_Menu/LCD_Custom_Menu.ino new file mode 100644 index 0000000..2eff457 --- /dev/null +++ b/examples/LCD_Custom_Menu/LCD_Custom_Menu.ino @@ -0,0 +1,23 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +static const char * btnm_map[] = {"Option 1", "\n", + "Option 2", "\n", + "Option 3", "\n", "\0" + }; + +void customMenu(){ + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); +} + +void setup() { + Braccio.begin(customMenu); +} + +void loop() { + +} diff --git a/examples/LCD_Menu_Joystick/LCD_Menu_Joystick.ino b/examples/LCD_Menu_Joystick/LCD_Menu_Joystick.ino new file mode 100644 index 0000000..fba5ae6 --- /dev/null +++ b/examples/LCD_Menu_Joystick/LCD_Menu_Joystick.ino @@ -0,0 +1,43 @@ +#include "Braccio++.h" + +#define MARGIN_LEFT 0 +#define MARGIN_TOP 0 + +static void event_handler(lv_event_t * e){ + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + if (code == LV_EVENT_CLICKED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + LV_LOG_USER("%s was pressed\n", txt); + Serial.println(String(txt) + " was pressed."); + } +} + +static const char * btnm_map[] = {"Option 1", "Option 2", "\n", + "Option 3", "Option 4", "\n", + "Option 5", "Option 6", "\n", "\0" + }; + +void customMenu(){ + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_obj_align(btnm1, LV_ALIGN_CENTER, MARGIN_LEFT, MARGIN_TOP); + lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL); + Braccio.connectJoystickTo(btnm1); +} + +void setup() { + Braccio.begin(customMenu); +} + +void loop() { + +} diff --git a/examples/LCD_Motors/LCD_Motors.ino b/examples/LCD_Motors/LCD_Motors.ino new file mode 100644 index 0000000..98b270e --- /dev/null +++ b/examples/LCD_Motors/LCD_Motors.ino @@ -0,0 +1,71 @@ +#include "Braccio++.h" + +int selected_motor = 0; + +static void event_handler(lv_event_t * e) +{ + lv_event_code_t code = lv_event_get_code(e); + lv_obj_t * obj = lv_event_get_target(e); + if (code == LV_EVENT_CLICKED) { + uint32_t id = lv_btnmatrix_get_selected_btn(obj); + const char * txt = lv_btnmatrix_get_btn_text(obj, id); + + LV_LOG_USER("%s was pressed\n", txt); + Serial.println(txt); + if (strcmp(txt, "Motor 1") == 0) { + selected_motor = 1; + Braccio.ping_allowed = false; + } else if (strcmp(txt, "Motor 2") == 0) { + selected_motor = 2; + Braccio.ping_allowed = false; + } else if (strcmp(txt, "Motor 3") == 0) { + selected_motor = 3; + Braccio.ping_allowed = false; + } else if (strcmp(txt, "Motor 4") == 0) { + selected_motor = 4; + Braccio.ping_allowed = false; + } else if (strcmp(txt, "Motor 5") == 0) { + selected_motor = 5; + Braccio.ping_allowed = false; + } else if (strcmp(txt, "Motor 6") == 0) { + selected_motor = 6; + Braccio.ping_allowed = false; + } else { + Braccio.ping_allowed = true; + selected_motor = 0; + } + } +} + + +static const char * btnm_map[] = {"Motor 1", "Motor 2", "\n", + "Motor 3", "Motor 4", "\n", + "Motor 5", "Motor 6", "\n", "\0" + }; + +void customMenu() { + lv_obj_t * btnm1 = lv_btnmatrix_create(lv_scr_act()); + lv_btnmatrix_set_map(btnm1, btnm_map); + lv_btnmatrix_set_btn_ctrl(btnm1, 0, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 1, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 2, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 3, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 4, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_btnmatrix_set_btn_ctrl(btnm1, 5, LV_BTNMATRIX_CTRL_CHECKABLE); + lv_obj_align(btnm1, LV_ALIGN_CENTER, 0, 0); + lv_obj_add_event_cb(btnm1, event_handler, LV_EVENT_ALL, NULL); + Braccio.connectJoystickTo(btnm1); +} + +void setup() { + // Call Braccio.begin() for default menu or pass a function for custom menu + Braccio.begin(customMenu); + Serial.begin(115200); +} + +void loop() { + for (float angle = 0.0; angle <= 180.0; angle+=10.0){ + Braccio.move(selected_motor).to(angle); + delay(500); + } +} diff --git a/examples/Moving_Motors/Moving_Motors.ino b/examples/Moving_Motors/Moving_Motors.ino new file mode 100644 index 0000000..6d552c4 --- /dev/null +++ b/examples/Moving_Motors/Moving_Motors.ino @@ -0,0 +1,22 @@ +#include "Braccio++.h" + +int selected_motor = 0; + +void setup() { + // Call Braccio.begin() for default menu or pass a function for custom menu + Braccio.begin(); + Serial.begin(115200); + while(!Serial){} + Serial.println("Testing the motor angular moviment!"); +} + +void loop() { + Serial.println("Choose the motor 1 to 6 to test the connection:"); + while((Serial.available() <= 0)){}; + int selected_motor = Serial.parseInt(); + for (float i = 0.0; i <= 180.0; i+=10.0){ + Braccio.move(selected_motor).to(i); + Serial.println("Current angle: " + String(i)); + delay(100); + } +} diff --git a/examples/Testing_communications/Testing_communications.ino b/examples/Testing_communications/Testing_communications.ino new file mode 100644 index 0000000..72b4b7b --- /dev/null +++ b/examples/Testing_communications/Testing_communications.ino @@ -0,0 +1,23 @@ +#include "Braccio++.h" + +int selected_motor = 0; + +void setup() { + Serial.begin(115200); + while(!Serial){} + Serial.println("Testing the motor communication!"); + Braccio.begin(); +} + +void loop() { + Serial.println("Choose the motor 1 to 6 to test the connection:"); + while((Serial.available() <= 0)){}; + int motorID = Serial.parseInt(); + + bool connected = (Braccio.connected(motorID)); + if (connected) + Serial.println("Communcation with motor " + String(motorID) + " successful"); + else + Serial.println("Communcation failure - Please check the motor " + String(motorID) + " connection"); + Serial.println(); +}