Utilisation de 2 servomoteurs afin de piloter un portail a l'aide de l'ecran lcd du disco stm32

Dependencies:   BSP_DISCO_F746NG

Committer:
andy92240
Date:
Thu Jun 16 10:18:32 2022 +0000
Revision:
6:e11776bd7e16
Parent:
5:071136c3eefa

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 5:071136c3eefa 1 /**
JohnnyK 5:071136c3eefa 2 * Notebook page https://os.mbed.com/users/JohnnyK/notebook/how-start-with-the-littlevgl/
JohnnyK 5:071136c3eefa 3 */
JohnnyK 5:071136c3eefa 4
JohnnyK 0:10c4b83c458d 5 #include "mbed.h"
JohnnyK 0:10c4b83c458d 6 #include "lvgl/lvgl.h"
JohnnyK 3:4f5dc253eb7b 7 #include "hal_stm_lvgl/tft/tft.h"
JohnnyK 3:4f5dc253eb7b 8 #include "hal_stm_lvgl/touchpad/touchpad.h"
JohnnyK 5:071136c3eefa 9 #include "lv_demo.h" /*Comment/uncomment will switch between LVGL demo and Hello word example*/
JohnnyK 0:10c4b83c458d 10
JohnnyK 3:4f5dc253eb7b 11 #define LVGL_TICK 10 //Time tick value for lvgl in ms (1-10msa)
JohnnyK 3:4f5dc253eb7b 12 #define TICKER_TIME 10ms //modified to miliseconds
JohnnyK 0:10c4b83c458d 13
andy92240 6:e11776bd7e16 14 PwmOut servo1(D9);
andy92240 6:e11776bd7e16 15 PwmOut servo2(D10);
andy92240 6:e11776bd7e16 16 InterruptIn echo(A1); //echo
andy92240 6:e11776bd7e16 17 DigitalOut trigger(A2); // trigger
andy92240 6:e11776bd7e16 18 DigitalOut myled(D8);
JohnnyK 3:4f5dc253eb7b 19 Ticker ticker; //Ticker for lvgl
andy92240 6:e11776bd7e16 20 Timer timer;
andy92240 6:e11776bd7e16 21 int distance = 0;
andy92240 6:e11776bd7e16 22 int correction = 0;
andy92240 6:e11776bd7e16 23
JohnnyK 3:4f5dc253eb7b 24
andy92240 6:e11776bd7e16 25 void startTimer() { // fonction pour commence mesurer echo
andy92240 6:e11776bd7e16 26 timer.start();
andy92240 6:e11776bd7e16 27 }
andy92240 6:e11776bd7e16 28 void stopTimer() { // fonction pour arrete mesurer echo
andy92240 6:e11776bd7e16 29 timer.stop();
andy92240 6:e11776bd7e16 30 }
andy92240 6:e11776bd7e16 31 const int positionMin = 700;
andy92240 6:e11776bd7e16 32 const int positionMax = 1500;
andy92240 6:e11776bd7e16 33 const int positionMin1 = 700;
andy92240 6:e11776bd7e16 34 const int positionMax1 = 1500;
andy92240 6:e11776bd7e16 35
andy92240 6:e11776bd7e16 36
JohnnyK 5:071136c3eefa 37 /*
JohnnyK 5:071136c3eefa 38 * Callback function for lvgl timing.
JohnnyK 5:071136c3eefa 39 * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10).
JohnnyK 5:071136c3eefa 40 * It is required for the internal timing of LittlevGL.
JohnnyK 5:071136c3eefa 41 */
JohnnyK 0:10c4b83c458d 42 void lv_ticker_func(){
JohnnyK 0:10c4b83c458d 43 lv_tick_inc(LVGL_TICK);
JohnnyK 0:10c4b83c458d 44 }
JohnnyK 0:10c4b83c458d 45
JohnnyK 5:071136c3eefa 46 static void event_handler(lv_event_t* event)
JohnnyK 3:4f5dc253eb7b 47 {
JohnnyK 5:071136c3eefa 48 lv_event_code_t code = lv_event_get_code(event);
JohnnyK 5:071136c3eefa 49 if(code == LV_EVENT_CLICKED) {
JohnnyK 3:4f5dc253eb7b 50 printf("Clicked\n");
andy92240 6:e11776bd7e16 51 printf("Clicked\n");
andy92240 6:e11776bd7e16 52
andy92240 6:e11776bd7e16 53 servo1.period_ms(20);
andy92240 6:e11776bd7e16 54 servo2.period_ms(20);
andy92240 6:e11776bd7e16 55 for(int i=positionMin1; i<positionMax1; i += 10) {
andy92240 6:e11776bd7e16 56 servo1.pulsewidth_us(i);
andy92240 6:e11776bd7e16 57 servo2.pulsewidth_us(i);
andy92240 6:e11776bd7e16 58
andy92240 6:e11776bd7e16 59 wait_us(3000); }
andy92240 6:e11776bd7e16 60 }
JohnnyK 5:071136c3eefa 61 else if(code == LV_EVENT_VALUE_CHANGED) {
JohnnyK 3:4f5dc253eb7b 62 printf("Toggled\n");
JohnnyK 0:10c4b83c458d 63 }
JohnnyK 1:627f26953c53 64 }
andy92240 6:e11776bd7e16 65
andy92240 6:e11776bd7e16 66 static void my_event_cb(lv_event_t* event)
andy92240 6:e11776bd7e16 67 {
andy92240 6:e11776bd7e16 68
andy92240 6:e11776bd7e16 69 lv_event_code_t code = lv_event_get_code(event);
andy92240 6:e11776bd7e16 70 if(code == LV_EVENT_CLICKED) {
andy92240 6:e11776bd7e16 71 printf("Clicked\n");
andy92240 6:e11776bd7e16 72 servo1.period_ms(20);
andy92240 6:e11776bd7e16 73 servo2.period_ms(20);
andy92240 6:e11776bd7e16 74 //for(int i=positionMin; i<positionMax; i += 10) {
andy92240 6:e11776bd7e16 75 servo1.pulsewidth_us(2500);
andy92240 6:e11776bd7e16 76 wait_us(40000);
andy92240 6:e11776bd7e16 77 servo2.pulsewidth_us(500);
andy92240 6:e11776bd7e16 78 wait_us(6000);
andy92240 6:e11776bd7e16 79 //lv_label_set_text(label3, "Ouvert");
andy92240 6:e11776bd7e16 80
andy92240 6:e11776bd7e16 81 }
andy92240 6:e11776bd7e16 82
andy92240 6:e11776bd7e16 83 else if(code == LV_EVENT_VALUE_CHANGED) {
andy92240 6:e11776bd7e16 84 printf("Toggled\n");
andy92240 6:e11776bd7e16 85 }
andy92240 6:e11776bd7e16 86 }
andy92240 6:e11776bd7e16 87
JohnnyK 3:4f5dc253eb7b 88 // main() runs in its own thread in the OS
JohnnyK 0:10c4b83c458d 89 int main()
JohnnyK 0:10c4b83c458d 90 {
andy92240 6:e11776bd7e16 91 // echo.rise(&startTimer); //On commence compter le temps quand on a un niveau montant
andy92240 6:e11776bd7e16 92 // echo.fall(&stopTimer); //On arrete compter le temps quand on a un niveau decendant
andy92240 6:e11776bd7e16 93
JohnnyK 3:4f5dc253eb7b 94 printf("LVGL-");
JohnnyK 3:4f5dc253eb7b 95 lv_init(); //Initialize the LVGL
JohnnyK 3:4f5dc253eb7b 96 tft_init(); //Initialize diplay
JohnnyK 3:4f5dc253eb7b 97 touchpad_init(); //Initialize touchpad
JohnnyK 3:4f5dc253eb7b 98 ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker
JohnnyK 3:4f5dc253eb7b 99
andy92240 6:e11776bd7e16 100
JohnnyK 5:071136c3eefa 101 lv_obj_t * label1 = lv_label_create(lv_scr_act());
JohnnyK 5:071136c3eefa 102 lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP); /*Break the long lines*/
JohnnyK 3:4f5dc253eb7b 103 lv_label_set_recolor(label1, true); /*Enable re-coloring by commands in the text*/
andy92240 6:e11776bd7e16 104 lv_label_set_text(label1, "Projet portail");
JohnnyK 3:4f5dc253eb7b 105 lv_obj_set_width(label1, 150);
andy92240 6:e11776bd7e16 106 lv_obj_align(label1, LV_ALIGN_TOP_MID, 25, 0);
JohnnyK 0:10c4b83c458d 107
JohnnyK 0:10c4b83c458d 108 lv_obj_t * label;
JohnnyK 5:071136c3eefa 109 lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
andy92240 6:e11776bd7e16 110
andy92240 6:e11776bd7e16 111 lv_obj_add_event_cb(btn1, my_event_cb, LV_EVENT_ALL, NULL);
andy92240 6:e11776bd7e16 112 lv_obj_align(btn1, LV_ALIGN_CENTER, -50, 50);
JohnnyK 5:071136c3eefa 113 label = lv_label_create(btn1);
andy92240 6:e11776bd7e16 114 lv_label_set_text(label, "Fermer");
andy92240 6:e11776bd7e16 115
andy92240 6:e11776bd7e16 116
JohnnyK 5:071136c3eefa 117 lv_obj_t * btn2 = lv_btn_create(lv_scr_act());
JohnnyK 5:071136c3eefa 118 lv_obj_add_event_cb(btn2, event_handler,LV_EVENT_ALL, NULL);
andy92240 6:e11776bd7e16 119 lv_obj_align(btn2, LV_ALIGN_CENTER, -50, -25);
JohnnyK 5:071136c3eefa 120 label = lv_label_create(btn2);
andy92240 6:e11776bd7e16 121 lv_label_set_text(label, "Ouvrir");
andy92240 6:e11776bd7e16 122
andy92240 6:e11776bd7e16 123 //continue ici//
andy92240 6:e11776bd7e16 124
andy92240 6:e11776bd7e16 125 static lv_style_t style;
andy92240 6:e11776bd7e16 126 lv_style_init(&style);
andy92240 6:e11776bd7e16 127 lv_style_set_radius(&style, 5);
andy92240 6:e11776bd7e16 128
andy92240 6:e11776bd7e16 129 /*Make a gradient*/
andy92240 6:e11776bd7e16 130 lv_style_set_width(&style, 150);
andy92240 6:e11776bd7e16 131 lv_style_set_height(&style, LV_SIZE_CONTENT);
andy92240 6:e11776bd7e16 132
andy92240 6:e11776bd7e16 133 lv_style_set_pad_ver(&style, 20);
andy92240 6:e11776bd7e16 134 lv_style_set_pad_left(&style, 5);
andy92240 6:e11776bd7e16 135
andy92240 6:e11776bd7e16 136 lv_style_set_x(&style, lv_pct(50));
andy92240 6:e11776bd7e16 137 lv_style_set_y(&style, 80);
andy92240 6:e11776bd7e16 138
andy92240 6:e11776bd7e16 139 /*Create an object with the new style*/
andy92240 6:e11776bd7e16 140 lv_obj_t * obj = lv_obj_create(lv_scr_act());
andy92240 6:e11776bd7e16 141 lv_obj_add_style(obj, &style, 0);
andy92240 6:e11776bd7e16 142
andy92240 6:e11776bd7e16 143 lv_obj_t * label3 = lv_label_create(obj);
andy92240 6:e11776bd7e16 144
andy92240 6:e11776bd7e16 145 lv_obj_align(label3, LV_ALIGN_TOP_MID, 0, 50);
andy92240 6:e11776bd7e16 146 lv_obj_t * label4 = lv_label_create(obj);
andy92240 6:e11776bd7e16 147
JohnnyK 0:10c4b83c458d 148
JohnnyK 3:4f5dc253eb7b 149 while (true){
andy92240 6:e11776bd7e16 150 float Tar;
JohnnyK 3:4f5dc253eb7b 151 lv_task_handler();
JohnnyK 3:4f5dc253eb7b 152 //Call lv_task_handler() periodically every few milliseconds.
JohnnyK 3:4f5dc253eb7b 153 //It will redraw the screen if required, handle input devices etc.
JohnnyK 3:4f5dc253eb7b 154 thread_sleep_for(LVGL_TICK);
andy92240 6:e11776bd7e16 155 /** On commence le trigger et arrete avec un period de 10us */
andy92240 6:e11776bd7e16 156 trigger = 1; //Mettre trigger en niveau haut
andy92240 6:e11776bd7e16 157 wait_us(10);//Period 1 trigger
andy92240 6:e11776bd7e16 158
andy92240 6:e11776bd7e16 159 Tar = timer.read()*1e6; //Temps aller retour on a recu s=>u s donc il faut multiplier par 1e6
andy92240 6:e11776bd7e16 160 float distance = Tar*343/2*1e-3/10; //Formule calculer la distance en cm (On divise par 10 pour mm>cm)
andy92240 6:e11776bd7e16 161 timer.reset(); // reset le temp aller retour a 0
andy92240 6:e11776bd7e16 162 wait_us(1000);
andy92240 6:e11776bd7e16 163 lv_label_set_text_fmt(label3, "distance: %0.2f cm \r\n",distance);
andy92240 6:e11776bd7e16 164
andy92240 6:e11776bd7e16 165 if(abs(distance>2))
andy92240 6:e11776bd7e16 166 { //Valeur absolute si superieur a 2cm
andy92240 6:e11776bd7e16 167 lv_label_set_text(label4,"Obstacle");
andy92240 6:e11776bd7e16 168 //lv_btn_set_checkable(btn1, true)
andy92240 6:e11776bd7e16 169 myled = 0;
andy92240 6:e11776bd7e16 170
JohnnyK 0:10c4b83c458d 171 }
andy92240 6:e11776bd7e16 172 else
andy92240 6:e11776bd7e16 173 {
andy92240 6:e11776bd7e16 174 lv_label_set_text(label4,"Aucun obstacle");
andy92240 6:e11776bd7e16 175
andy92240 6:e11776bd7e16 176 myled = 1;
andy92240 6:e11776bd7e16 177 }
andy92240 6:e11776bd7e16 178 }
JohnnyK 3:4f5dc253eb7b 179 }