Programme affichant la température et le taux de CO2 sur graphique. Bibliothèque LVGL + I2C Capteur Telaire T30-22 / Capteur temp/Hum Si7021 by Simon M

Dependencies:   BSP_DISCO_F746NG

main.cpp

Committer:
simon11111
Date:
2022-06-16
Revision:
6:61d9f0dbdc21
Parent:
5:071136c3eefa

File content as of revision 6:61d9f0dbdc21:

#include "mbed.h"
#include "lvgl/lvgl.h"
#include "lvgl/src/misc/lv_color.h"
#include "lvgl/src/misc/lv_style.h"
#include "lvgl/src/misc/lv_style_gen.h"
#include "lvgl/examples/lv_examples.h"
#include "hal_stm_lvgl/tft/tft.h"
#include "hal_stm_lvgl/touchpad/touchpad.h"

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

//Variable globale liaison I2C capteur Temp/HUM.
#define READ_TEMP        0xE0
#define READ_RH          0xE5
#define READ_ID1_1       0xFA
#define READ_ID1_2       0x0F
#define READ_ID2_1       0xFC
#define READ_ID2_2       0xC9
#define READ_FWREV_1     0x84
#define READ_FWREV_2     0xB8
#define ADDR    0x80
#define FREQ    100000
#define DEVICE_ID 0x15


char x_event;
Ticker ticker;                                       //Ticker for lvgl

//création des objets des deux écrans
lv_obj_t * scr1;
lv_obj_t * scr2;


I2C i2c(PB_9,PB_8); // initialisation des broches I2C


/*
 * 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);
}
//
//  Bouton ON/OFF
//
static void eventActivAlarme(lv_event_t * e)//event du bouton ON/OFF non utilisé actuellement
{
    LV_LOG_USER("btnActivAlarme was Clicked");

    lv_obj_t * btnActivAlarme = lv_event_get_target(e);
    lv_obj_t * label = lv_obj_get_child(btnActivAlarme, 0);

    if (x_event==1) {
        lv_label_set_text(label, "ON");

        x_event=0;
    } else {
        lv_label_set_text(label, "OFF");
        x_event=1;
    }
}


//
//  Bouton Graph
//
static void eventGraph(lv_event_t * event)  //event du bouton Graph pour charger scr2
{

    lv_scr_load(scr2);

    lv_obj_t * btnGraph = lv_event_get_target(event);
    lv_obj_t * label = lv_obj_get_child(btnGraph, 0);

}
//
//  Bouton MENU
//
static void event_MENU(lv_event_t * e)   //event du bouton Menu pour charger scr1
{

    lv_scr_load(scr1);

    lv_obj_t * btn_ALR = lv_event_get_target(e);
    lv_obj_t * label = lv_obj_get_child(btn_ALR, 0);

}

// main() runs in its own thread in the OS
int main()
{

    //init variable I2C capteur CO2
    char readdata[6] = {};
    char writedata[6] = {};
    uint8_t adresse = 42; // adresse du capteur CO2
    char bufvalue[12] = {};
    int MSB = 0; // most significant bit
    int LSB = 0; // less signifiicant bit
    int ppm = 0;
    // variable I2C capteur Temp/Hum
    uint8_t  rx_buff[8];
    uint8_t  tx_buff[2];
    uint32_t rhData;    //valeur Hum
    int32_t  tData;     //valeur Temp

    i2c.frequency(FREQ); // FREQ de communication imposee par le maître 100 000

    //Initialisation des différents objets à l'écran
    lv_init();                                              //Initialize the LVGL
    tft_init();                                             //Initialize diplay
    touchpad_init();
    //Initialize touchpad
    ticker.attach(callback(&lv_ticker_func),TICKER_TIME);
    lv_obj_t * label;   //Attach callback to ticker
    lv_obj_t * labe2;   //Attach callback to ticker
    lv_obj_t * labe3;   //Attach callback to ticker
    lv_obj_t * labe4;   //Attach callback to ticker
    lv_obj_t * labe5;   //Attach callback to ticker

    scr1 = lv_obj_create(NULL); //création scr1
    scr2 = lv_obj_create(NULL); //création scr2

    lv_scr_load(scr1);          //Utilisation scr1 par défault

    //Texte appartenant au scr1
    //******************************************************************************//
    lv_obj_t * label1 = lv_label_create(scr1);
    lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP);     /*Break the long lines*/
    lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_text(label1, "#ff0000 Boitier domotique Temp Hum & CO2#");
    lv_obj_set_width(label1, 150);
    lv_obj_align(label1, LV_ALIGN_TOP_LEFT, 0, 0);

    lv_obj_t * label2 = lv_label_create(scr1);
    lv_obj_set_width(label2, 300);
    lv_label_set_text(label2, "Project by Simon Marchiano");
    lv_obj_align(label2, LV_ALIGN_BOTTOM_LEFT, 0, 0);

    lv_obj_t * label31 = lv_label_create(scr1);
    lv_obj_set_width(label31, 300);
    lv_label_set_recolor(label31, true);                      /*Enable re-coloring by commands in the text*/
    lv_obj_align(label31, LV_ALIGN_CENTER, 50, -50);

    lv_obj_t * label32 = lv_label_create(scr1);
    lv_obj_set_width(label32, 300);
    lv_label_set_recolor(label32, true);                      /*Enable re-coloring by commands in the text*/
    lv_obj_align(label32, LV_ALIGN_CENTER, 50, 0);

    lv_obj_t * label33 = lv_label_create(scr1);
    lv_obj_set_width(label33, 300);
    lv_label_set_recolor(label33, true);                      /*Enable re-coloring by commands in the text*/
    lv_obj_align(label33, LV_ALIGN_CENTER, 50, 50);
    //******************************************************************************//

    //Texte appartenant au scr2
    //******************************************************************************//
    lv_obj_t * label3 = lv_label_create(scr2);
    lv_obj_set_width(label3, 300);
    lv_label_set_recolor(label3, true);                      /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(label3, "Temperature : %d", tData);
    lv_obj_align(label3, LV_ALIGN_TOP_MID, 50, 30);

    lv_obj_t * label4 = lv_label_create(scr2);
    lv_obj_set_width(label4, 300);
    lv_label_set_recolor(label4, true);                      /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(label4, "Humidite : %d", rhData);
    lv_obj_align(label4, LV_ALIGN_TOP_MID, 50, 45);

    lv_obj_t * label5 = lv_label_create(scr2);
    lv_obj_set_width(label5, 300);
    lv_label_set_recolor(label5, true);                      /*Enable re-coloring by commands in the text*/
    //lv_label_set_text_fmt(label5, "CO2 : %d", ppm);
    lv_obj_align(label5, LV_ALIGN_TOP_MID, 50, 15);

    lv_obj_t * label20 = lv_label_create(scr2);
    lv_obj_set_width(label20, 300);
    lv_label_set_text_fmt(label20, "Graphique ppm ");
    lv_obj_align(label20, LV_ALIGN_BOTTOM_MID, 90, -35);
//******************************************************************************//

    //Bouton appartenant au scr1
    //******************************************************************************//
    //  Bouton Graph
    lv_obj_t * btnGraph = lv_btn_create(scr1);
    lv_obj_add_event_cb(btnGraph, eventGraph, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btnGraph, LV_ALIGN_LEFT_MID, 10, 0);
    label = lv_label_create(btnGraph);
    lv_label_set_text(label, "Graph");

    //  Style Bouton ON/OFF
    static lv_style_t style_btn_green;
    lv_style_init(&style_btn_green);
    lv_style_set_bg_color(&style_btn_green, lv_color_make(0,225,0));
    lv_style_set_bg_grad_color(&style_btn_green, lv_color_make(0,200,0));
    lv_style_set_border_color(&style_btn_green, lv_color_make(0,255,0));
    lv_style_set_text_color(&style_btn_green, lv_color_make(255,255,255));

    //  Bouton ON/OFF
    lv_obj_t * btnActivAlarme = lv_btn_create(scr1);
    lv_obj_add_style(btnActivAlarme, &style_btn_green,0);

    lv_obj_add_event_cb(btnActivAlarme, eventActivAlarme, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btnActivAlarme, LV_ALIGN_LEFT_MID, 10, 50);
    lv_obj_add_flag(btnActivAlarme, LV_OBJ_FLAG_CHECKABLE);
    label = lv_label_create(btnActivAlarme);
    lv_label_set_text(label, "ON");
    //******************************************************************************//
    //Bouton et graphique appartenant au scr2
    //******************************************************************************//
    //  Bouton ALARMES
    lv_obj_t * btn_ALR = lv_btn_create(scr2);
    lv_obj_add_event_cb(btn_ALR, event_MENU, LV_EVENT_CLICKED, NULL);
    lv_obj_align(btn_ALR, LV_ALIGN_LEFT_MID, 10, -50);
    //lv_disp_get_scr_act(scr1);
    label = lv_label_create(btn_ALR);
    lv_label_set_text(label, "Menu");

    /*Create a chart*/
    lv_obj_t * chart;
    chart = lv_chart_create(scr2);
    lv_obj_set_size(chart, 250, 155);
    lv_obj_align(chart, LV_ALIGN_CENTER, 0, 0);
    lv_chart_set_type(chart, LV_CHART_TYPE_LINE);   /*Show lines and points too*/
    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, 1400);

    /*Add 3 data series*/
    lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), 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);

    while (true) {

        //Init capteur CO2
        writedata[0] = 0x04;
        writedata[1] = 0x13;
        writedata[2] = 0x8B;
        writedata[3] = 0x00;
        writedata[4] = 0x01;

        i2c.write(adresse, writedata, 6);  // requête pour obtenir les mesures du capteur
        HAL_Delay(7);
        i2c.read(adresse, readdata, 5); // stock les mesures dans le tableau readdata
        HAL_Delay(10);

        MSB = (int)readdata[2];                     // affecte la valeur du MSB
        LSB = (int)readdata[3];                     // affecte la valeur du LSB
        ppm = MSB*256+LSB;                          // calcul pour obtenir la valeur en ppm
        sprintf(bufvalue,"CO2 : %d ppm",ppm);       // conversion d'un nombre en caractère

        tx_buff[0] = READ_RH;
        i2c.write(ADDR, (char*)tx_buff, 1);
        i2c.read(ADDR, (char*)rx_buff, 2);

        rhData = ((((uint32_t)rx_buff[0] << 8 | rx_buff[1]) * 125 ) / 65536)-6;

        tx_buff[0] = READ_TEMP;
        i2c.write(ADDR, (char*)tx_buff, 1);
        i2c.read(ADDR, (char*)rx_buff, 2);

        tData = ((((uint32_t)rx_buff[0] << 8 | rx_buff[1]) * 175.72) / 65536) - 46.85;

        //Affichage valeurs scr1
        lv_label_set_text_fmt(label5,"#ff0000 %s#",bufvalue);
        lv_label_set_text_fmt(label3, "#0000ff Temperature : %d C°#", tData);
        lv_label_set_text_fmt(label4, "#008000 Humidite : %d %%#", rhData);
        //Affichage valeurs scr2
        lv_label_set_text_fmt(label31,"#ff0000 %s#",bufvalue);
        lv_label_set_text_fmt(label32, "#0000ff Temperature : %d C°#", tData);
        lv_label_set_text_fmt(label33, "#008000 Humidite : %d %%#", rhData);
        //Mise à jour valeur graphique
        lv_chart_set_next_value(chart, ser1, ppm);
        lv_chart_set_next_value(chart, ser2, rhData);
        lv_chart_set_next_value(chart, ser3, tData);

        lv_task_handler();
        //Call lv_task_handler() periodically every few milliseconds.
        //It will redraw the screen if required, handle input devices etc.
        thread_sleep_for(LVGL_TICK);
    }
}