FINAL VERSION

Dependencies:   BSP_DISCO_F746NG

main.cpp

Committer:
samdcr
Date:
2022-06-16
Revision:
6:22fd049f313b
Parent:
5:071136c3eefa

File content as of revision 6:22fd049f313b:

/**
 * Notebook page https://os.mbed.com/users/JohnnyK/notebook/how-start-with-the-littlevgl/
 */

#include "mbed.h"
#include "lvgl/lvgl.h"
#include "hal_stm_lvgl/tft/tft.h"
#include "hal_stm_lvgl/touchpad/touchpad.h" 
#include "lv_demo.h"    /*Comment/uncomment will switch between LVGL demo and Hello word example*/
#include <iostream>
#include <string>

#define LVGL_TICK   10                               //Time tick value for lvgl in ms (1-10msa)
#define TICKER_TIME 10ms                             //modified to miliseconds

Ticker ticker;                                       //Ticker for lvgl                    
float soilHumidity = 0.0;
float soilPH = 0.0;
float waterPH = 0.0;

AnalogIn   ain(A0);
AnalogIn   deux(A1);
AnalogIn   trois(A2);

lv_obj_t * scr1;
lv_obj_t * scr2; 
 
/*
 * Callback function for lvgl timing.
 * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10). 
 * It is required for the internal timing of LittlevGL.
 */
void lv_ticker_func(){
    lv_tick_inc(LVGL_TICK); 
}

static void event_handler(lv_event_t* event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if(code == LV_EVENT_CLICKED) {
    //soilHumidity++;
    soilPH++;
    waterPH++;
        printf("Clicked\n");
        lv_scr_load(scr2);
    }
    else if(code == LV_EVENT_VALUE_CHANGED) {
        printf("Toggled\n");
    }
}

static void my_event_cb(lv_event_t * event)
{
    printf("Clicked\n");
    lv_scr_load(scr1);
}

// main() runs in its own thread in the OS
int main()
{
    printf("LVGL-"); 
    lv_init();                                              //Initialize the LVGL
    tft_init();                                             //Initialize diplay 
    touchpad_init();                                        //Initialize touchpad
    ticker.attach(callback(&lv_ticker_func),TICKER_TIME);   //Attach callback to ticker
    
    
    scr1 = lv_obj_create(NULL);
    scr2 = lv_obj_create(NULL);
    
    lv_scr_load(scr1);
    
    LV_IMG_DECLARE(myplant);
    lv_obj_t * imgplant = lv_img_create(scr1);
    lv_img_set_src(imgplant, &myplant);
    lv_obj_align(imgplant, LV_ALIGN_TOP_LEFT, 0,0);
    lv_obj_set_size(imgplant, 480, 85);
    
    LV_IMG_DECLARE(showdata);
    lv_obj_t * mydata = lv_img_create(scr2);
    lv_img_set_src(mydata, &showdata);
    lv_obj_align(mydata, LV_ALIGN_TOP_LEFT, 0,0);
    lv_obj_set_size(mydata, 480, 85);
    
    lv_obj_t * label;
    lv_obj_t * btn1 = lv_btn_create(scr1);
    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_align(btn1, LV_ALIGN_TOP_LEFT, 20, 180);
    lv_obj_set_size(btn1, 200, 80);
    label = lv_label_create(btn1);
    lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(label, "Vue detaillee");
    
    
    
    lv_obj_t * label1;
    lv_obj_t * btn2 = lv_btn_create(scr2);
    lv_obj_add_event_cb(btn2, my_event_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btn2, LV_ALIGN_TOP_LEFT, 20, 90);
    lv_obj_set_size(btn2, 120, 80);
    label1 = lv_label_create(btn2);
    lv_obj_align(label1, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text(label1, "Retour");
    
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, 5);

    /*Make a gradient*/
    lv_style_set_width(&style, 210);
    lv_style_set_height(&style, 125);

    lv_style_set_pad_ver(&style, 20);
    lv_style_set_pad_left(&style, 5);

    lv_style_set_x(&style, lv_pct(50));
    lv_style_set_y(&style, 100);

    /*Create an object with the new style*/
    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj, &style, 0);

    lv_obj_t * label22 = lv_label_create(obj);
    
    static lv_style_t style1;
    lv_style_init(&style1);
    lv_style_set_radius(&style1, 5);

    /*Make a gradient*/
    lv_style_set_width(&style1, 125);
    lv_style_set_height(&style1, 80);

    lv_style_set_pad_ver(&style1, 20);
    lv_style_set_pad_left(&style1, 5);

    lv_style_set_x(&style1, lv_pct(12));
    lv_style_set_y(&style1, 92);

    /*Create an object with the new style*/
    lv_obj_t * obj2 = lv_obj_create(lv_scr_act());
    lv_obj_add_style(obj2, &style1, 0);

    lv_obj_t * label23 = lv_label_create(obj2);
    
    lv_obj_t * chart;
    chart = lv_chart_create(scr2);
    lv_obj_set_size(chart, 200, 150);
    lv_obj_align(chart, LV_ALIGN_CENTER, 20, 40);
    lv_chart_set_type(chart, LV_CHART_TYPE_LINE);
    lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);
    lv_chart_set_point_count(chart, 100);
    lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 14);
    
    lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_PRIMARY_X);
    lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_PRIMARY_X);
    //lv_chart_series_t * ser3 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_PRIMARY_X);
    
    lv_obj_t * label31 = lv_label_create(scr2);
    lv_obj_set_width(label31, 300);
    lv_label_set_recolor(label31, true);
    lv_obj_align(label31, LV_ALIGN_CENTER, 285, 40);
    
    lv_obj_t * label32 = lv_label_create(scr2);
    lv_obj_set_width(label32, 300);
    lv_label_set_recolor(label32, true);
    lv_obj_align(label32, LV_ALIGN_CENTER, 285, 60);

    while (true){
        lv_task_handler();
        //Call lv_task_handler() periodically every few milliseconds. 
        //It will redraw the screen if required, handle input devices etc.
        waterPH = ain * 10;
        soilPH = deux * 10;
        soilHumidity = trois * 1000;
        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);
        
        if((soilHumidity > 500 && soilPH > 7)){
                lv_label_set_text(label23, "Aroser : OUI \npH : ALCALIN");
            }
        else if((soilHumidity > 500 && soilPH < 7)){
                lv_label_set_text(label23, "Aroser : OUI \npH : ACIDE OK");
        }
        else if((soilHumidity < 500 && soilPH < 7)){
                lv_label_set_text(label23, "Aroser : NON \npH : ACIDE OK");
        }
        else if((soilHumidity < 500 && soilPH > 7)){
                lv_label_set_text(label23, "Aroser : NON \npH : ALCALIN");
        }
    
        //lv_chart_set_next_value(chart, ser1, soilHumidity);
        lv_chart_set_next_value(chart, ser1, soilPH);
        lv_chart_set_next_value(chart, ser2, waterPH);
        
        lv_label_set_text_fmt(label31, "#0000ff phEAU : %0.1f#", waterPH);
        lv_label_set_text_fmt(label32, "#00ff00 phSOL : %0.1f#", soilPH);
        
        
        
        thread_sleep_for(LVGL_TICK); 
    }
}