FINAL VERSION

Dependencies:   BSP_DISCO_F746NG

Committer:
samdcr
Date:
Thu Jun 16 09:08:19 2022 +0000
Revision:
6:22fd049f313b
Parent:
5:071136c3eefa
FINAL VERSION

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*/
samdcr 6:22fd049f313b 10 #include <iostream>
samdcr 6:22fd049f313b 11 #include <string>
JohnnyK 0:10c4b83c458d 12
JohnnyK 3:4f5dc253eb7b 13 #define LVGL_TICK 10 //Time tick value for lvgl in ms (1-10msa)
JohnnyK 3:4f5dc253eb7b 14 #define TICKER_TIME 10ms //modified to miliseconds
JohnnyK 0:10c4b83c458d 15
JohnnyK 3:4f5dc253eb7b 16 Ticker ticker; //Ticker for lvgl
samdcr 6:22fd049f313b 17 float soilHumidity = 0.0;
samdcr 6:22fd049f313b 18 float soilPH = 0.0;
samdcr 6:22fd049f313b 19 float waterPH = 0.0;
samdcr 6:22fd049f313b 20
samdcr 6:22fd049f313b 21 AnalogIn ain(A0);
samdcr 6:22fd049f313b 22 AnalogIn deux(A1);
samdcr 6:22fd049f313b 23 AnalogIn trois(A2);
samdcr 6:22fd049f313b 24
samdcr 6:22fd049f313b 25 lv_obj_t * scr1;
samdcr 6:22fd049f313b 26 lv_obj_t * scr2;
JohnnyK 3:4f5dc253eb7b 27
JohnnyK 5:071136c3eefa 28 /*
JohnnyK 5:071136c3eefa 29 * Callback function for lvgl timing.
JohnnyK 5:071136c3eefa 30 * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10).
JohnnyK 5:071136c3eefa 31 * It is required for the internal timing of LittlevGL.
JohnnyK 5:071136c3eefa 32 */
JohnnyK 0:10c4b83c458d 33 void lv_ticker_func(){
JohnnyK 0:10c4b83c458d 34 lv_tick_inc(LVGL_TICK);
JohnnyK 0:10c4b83c458d 35 }
JohnnyK 0:10c4b83c458d 36
JohnnyK 5:071136c3eefa 37 static void event_handler(lv_event_t* event)
JohnnyK 3:4f5dc253eb7b 38 {
JohnnyK 5:071136c3eefa 39 lv_event_code_t code = lv_event_get_code(event);
JohnnyK 5:071136c3eefa 40 if(code == LV_EVENT_CLICKED) {
samdcr 6:22fd049f313b 41 //soilHumidity++;
samdcr 6:22fd049f313b 42 soilPH++;
samdcr 6:22fd049f313b 43 waterPH++;
JohnnyK 3:4f5dc253eb7b 44 printf("Clicked\n");
samdcr 6:22fd049f313b 45 lv_scr_load(scr2);
JohnnyK 3:4f5dc253eb7b 46 }
JohnnyK 5:071136c3eefa 47 else if(code == LV_EVENT_VALUE_CHANGED) {
JohnnyK 3:4f5dc253eb7b 48 printf("Toggled\n");
JohnnyK 0:10c4b83c458d 49 }
JohnnyK 1:627f26953c53 50 }
samdcr 6:22fd049f313b 51
samdcr 6:22fd049f313b 52 static void my_event_cb(lv_event_t * event)
samdcr 6:22fd049f313b 53 {
samdcr 6:22fd049f313b 54 printf("Clicked\n");
samdcr 6:22fd049f313b 55 lv_scr_load(scr1);
samdcr 6:22fd049f313b 56 }
samdcr 6:22fd049f313b 57
JohnnyK 3:4f5dc253eb7b 58 // main() runs in its own thread in the OS
JohnnyK 0:10c4b83c458d 59 int main()
JohnnyK 0:10c4b83c458d 60 {
JohnnyK 3:4f5dc253eb7b 61 printf("LVGL-");
JohnnyK 3:4f5dc253eb7b 62 lv_init(); //Initialize the LVGL
JohnnyK 3:4f5dc253eb7b 63 tft_init(); //Initialize diplay
JohnnyK 3:4f5dc253eb7b 64 touchpad_init(); //Initialize touchpad
JohnnyK 3:4f5dc253eb7b 65 ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker
samdcr 6:22fd049f313b 66
samdcr 6:22fd049f313b 67
samdcr 6:22fd049f313b 68 scr1 = lv_obj_create(NULL);
samdcr 6:22fd049f313b 69 scr2 = lv_obj_create(NULL);
samdcr 6:22fd049f313b 70
samdcr 6:22fd049f313b 71 lv_scr_load(scr1);
samdcr 6:22fd049f313b 72
samdcr 6:22fd049f313b 73 LV_IMG_DECLARE(myplant);
samdcr 6:22fd049f313b 74 lv_obj_t * imgplant = lv_img_create(scr1);
samdcr 6:22fd049f313b 75 lv_img_set_src(imgplant, &myplant);
samdcr 6:22fd049f313b 76 lv_obj_align(imgplant, LV_ALIGN_TOP_LEFT, 0,0);
samdcr 6:22fd049f313b 77 lv_obj_set_size(imgplant, 480, 85);
samdcr 6:22fd049f313b 78
samdcr 6:22fd049f313b 79 LV_IMG_DECLARE(showdata);
samdcr 6:22fd049f313b 80 lv_obj_t * mydata = lv_img_create(scr2);
samdcr 6:22fd049f313b 81 lv_img_set_src(mydata, &showdata);
samdcr 6:22fd049f313b 82 lv_obj_align(mydata, LV_ALIGN_TOP_LEFT, 0,0);
samdcr 6:22fd049f313b 83 lv_obj_set_size(mydata, 480, 85);
JohnnyK 0:10c4b83c458d 84
JohnnyK 0:10c4b83c458d 85 lv_obj_t * label;
samdcr 6:22fd049f313b 86 lv_obj_t * btn1 = lv_btn_create(scr1);
JohnnyK 5:071136c3eefa 87 lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
samdcr 6:22fd049f313b 88 lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 20, 180);
samdcr 6:22fd049f313b 89 lv_obj_set_size(btn1, 200, 80);
JohnnyK 5:071136c3eefa 90 label = lv_label_create(btn1);
samdcr 6:22fd049f313b 91 lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
samdcr 6:22fd049f313b 92 lv_label_set_text(label, "Vue detaillee");
samdcr 6:22fd049f313b 93
samdcr 6:22fd049f313b 94
samdcr 6:22fd049f313b 95
samdcr 6:22fd049f313b 96 lv_obj_t * label1;
samdcr 6:22fd049f313b 97 lv_obj_t * btn2 = lv_btn_create(scr2);
samdcr 6:22fd049f313b 98 lv_obj_add_event_cb(btn2, my_event_cb, LV_EVENT_CLICKED, NULL);
samdcr 6:22fd049f313b 99 lv_obj_align(btn2, LV_ALIGN_TOP_LEFT, 20, 90);
samdcr 6:22fd049f313b 100 lv_obj_set_size(btn2, 120, 80);
samdcr 6:22fd049f313b 101 label1 = lv_label_create(btn2);
samdcr 6:22fd049f313b 102 lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);
samdcr 6:22fd049f313b 103 lv_label_set_text(label1, "Retour");
samdcr 6:22fd049f313b 104
samdcr 6:22fd049f313b 105 static lv_style_t style;
samdcr 6:22fd049f313b 106 lv_style_init(&style);
samdcr 6:22fd049f313b 107 lv_style_set_radius(&style, 5);
samdcr 6:22fd049f313b 108
samdcr 6:22fd049f313b 109 /*Make a gradient*/
samdcr 6:22fd049f313b 110 lv_style_set_width(&style, 210);
samdcr 6:22fd049f313b 111 lv_style_set_height(&style, 125);
samdcr 6:22fd049f313b 112
samdcr 6:22fd049f313b 113 lv_style_set_pad_ver(&style, 20);
samdcr 6:22fd049f313b 114 lv_style_set_pad_left(&style, 5);
samdcr 6:22fd049f313b 115
samdcr 6:22fd049f313b 116 lv_style_set_x(&style, lv_pct(50));
samdcr 6:22fd049f313b 117 lv_style_set_y(&style, 100);
samdcr 6:22fd049f313b 118
samdcr 6:22fd049f313b 119 /*Create an object with the new style*/
samdcr 6:22fd049f313b 120 lv_obj_t * obj = lv_obj_create(lv_scr_act());
samdcr 6:22fd049f313b 121 lv_obj_add_style(obj, &style, 0);
samdcr 6:22fd049f313b 122
samdcr 6:22fd049f313b 123 lv_obj_t * label22 = lv_label_create(obj);
samdcr 6:22fd049f313b 124
samdcr 6:22fd049f313b 125 static lv_style_t style1;
samdcr 6:22fd049f313b 126 lv_style_init(&style1);
samdcr 6:22fd049f313b 127 lv_style_set_radius(&style1, 5);
samdcr 6:22fd049f313b 128
samdcr 6:22fd049f313b 129 /*Make a gradient*/
samdcr 6:22fd049f313b 130 lv_style_set_width(&style1, 125);
samdcr 6:22fd049f313b 131 lv_style_set_height(&style1, 80);
samdcr 6:22fd049f313b 132
samdcr 6:22fd049f313b 133 lv_style_set_pad_ver(&style1, 20);
samdcr 6:22fd049f313b 134 lv_style_set_pad_left(&style1, 5);
samdcr 6:22fd049f313b 135
samdcr 6:22fd049f313b 136 lv_style_set_x(&style1, lv_pct(12));
samdcr 6:22fd049f313b 137 lv_style_set_y(&style1, 92);
samdcr 6:22fd049f313b 138
samdcr 6:22fd049f313b 139 /*Create an object with the new style*/
samdcr 6:22fd049f313b 140 lv_obj_t * obj2 = lv_obj_create(lv_scr_act());
samdcr 6:22fd049f313b 141 lv_obj_add_style(obj2, &style1, 0);
samdcr 6:22fd049f313b 142
samdcr 6:22fd049f313b 143 lv_obj_t * label23 = lv_label_create(obj2);
samdcr 6:22fd049f313b 144
samdcr 6:22fd049f313b 145 lv_obj_t * chart;
samdcr 6:22fd049f313b 146 chart = lv_chart_create(scr2);
samdcr 6:22fd049f313b 147 lv_obj_set_size(chart, 200, 150);
samdcr 6:22fd049f313b 148 lv_obj_align(chart, LV_ALIGN_CENTER, 20, 40);
samdcr 6:22fd049f313b 149 lv_chart_set_type(chart, LV_CHART_TYPE_LINE);
samdcr 6:22fd049f313b 150 lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);
samdcr 6:22fd049f313b 151 lv_chart_set_point_count(chart, 100);
samdcr 6:22fd049f313b 152 lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 14);
samdcr 6:22fd049f313b 153
samdcr 6:22fd049f313b 154 lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_PRIMARY_X);
samdcr 6:22fd049f313b 155 lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_PRIMARY_X);
samdcr 6:22fd049f313b 156 //lv_chart_series_t * ser3 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_PRIMARY_X);
samdcr 6:22fd049f313b 157
samdcr 6:22fd049f313b 158 lv_obj_t * label31 = lv_label_create(scr2);
samdcr 6:22fd049f313b 159 lv_obj_set_width(label31, 300);
samdcr 6:22fd049f313b 160 lv_label_set_recolor(label31, true);
samdcr 6:22fd049f313b 161 lv_obj_align(label31, LV_ALIGN_CENTER, 285, 40);
samdcr 6:22fd049f313b 162
samdcr 6:22fd049f313b 163 lv_obj_t * label32 = lv_label_create(scr2);
samdcr 6:22fd049f313b 164 lv_obj_set_width(label32, 300);
samdcr 6:22fd049f313b 165 lv_label_set_recolor(label32, true);
samdcr 6:22fd049f313b 166 lv_obj_align(label32, LV_ALIGN_CENTER, 285, 60);
JohnnyK 0:10c4b83c458d 167
JohnnyK 3:4f5dc253eb7b 168 while (true){
samdcr 6:22fd049f313b 169 lv_task_handler();
JohnnyK 3:4f5dc253eb7b 170 //Call lv_task_handler() periodically every few milliseconds.
JohnnyK 3:4f5dc253eb7b 171 //It will redraw the screen if required, handle input devices etc.
samdcr 6:22fd049f313b 172 waterPH = ain * 10;
samdcr 6:22fd049f313b 173 soilPH = deux * 10;
samdcr 6:22fd049f313b 174 soilHumidity = trois * 1000;
samdcr 6:22fd049f313b 175 lv_label_set_text_fmt(label22, "pH de l'eau : %0.1f \n\npH du sol : %0.1f\n\nHumidite du sol : %0.1f", waterPH, soilPH, soilHumidity);
samdcr 6:22fd049f313b 176
samdcr 6:22fd049f313b 177 if((soilHumidity > 500 && soilPH > 7)){
samdcr 6:22fd049f313b 178 lv_label_set_text(label23, "Aroser : OUI \npH : ALCALIN");
samdcr 6:22fd049f313b 179 }
samdcr 6:22fd049f313b 180 else if((soilHumidity > 500 && soilPH < 7)){
samdcr 6:22fd049f313b 181 lv_label_set_text(label23, "Aroser : OUI \npH : ACIDE OK");
samdcr 6:22fd049f313b 182 }
samdcr 6:22fd049f313b 183 else if((soilHumidity < 500 && soilPH < 7)){
samdcr 6:22fd049f313b 184 lv_label_set_text(label23, "Aroser : NON \npH : ACIDE OK");
samdcr 6:22fd049f313b 185 }
samdcr 6:22fd049f313b 186 else if((soilHumidity < 500 && soilPH > 7)){
samdcr 6:22fd049f313b 187 lv_label_set_text(label23, "Aroser : NON \npH : ALCALIN");
samdcr 6:22fd049f313b 188 }
samdcr 6:22fd049f313b 189
samdcr 6:22fd049f313b 190 //lv_chart_set_next_value(chart, ser1, soilHumidity);
samdcr 6:22fd049f313b 191 lv_chart_set_next_value(chart, ser1, soilPH);
samdcr 6:22fd049f313b 192 lv_chart_set_next_value(chart, ser2, waterPH);
samdcr 6:22fd049f313b 193
samdcr 6:22fd049f313b 194 lv_label_set_text_fmt(label31, "#0000ff phEAU : %0.1f#", waterPH);
samdcr 6:22fd049f313b 195 lv_label_set_text_fmt(label32, "#00ff00 phSOL : %0.1f#", soilPH);
samdcr 6:22fd049f313b 196
samdcr 6:22fd049f313b 197
samdcr 6:22fd049f313b 198
JohnnyK 3:4f5dc253eb7b 199 thread_sleep_for(LVGL_TICK);
JohnnyK 0:10c4b83c458d 200 }
JohnnyK 3:4f5dc253eb7b 201 }