Simon Marchiano / Mbed OS DISCO-F746NG_MbedOs_LvGL_CO2_Temp

Dependencies:   BSP_DISCO_F746NG

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "lvgl/lvgl.h"
00003 #include "lvgl/src/misc/lv_color.h"
00004 #include "lvgl/src/misc/lv_style.h"
00005 #include "lvgl/src/misc/lv_style_gen.h"
00006 #include "lvgl/examples/lv_examples.h"
00007 #include "hal_stm_lvgl/tft/tft.h "
00008 #include "hal_stm_lvgl/touchpad/touchpad.h"
00009 
00010 #define LVGL_TICK   10                               //Time tick value for lvgl in ms (1-10msa)
00011 #define TICKER_TIME 10ms                             //modified to miliseconds
00012 
00013 //Variable globale liaison I2C capteur Temp/HUM.
00014 #define READ_TEMP        0xE0
00015 #define READ_RH          0xE5
00016 #define READ_ID1_1       0xFA
00017 #define READ_ID1_2       0x0F
00018 #define READ_ID2_1       0xFC
00019 #define READ_ID2_2       0xC9
00020 #define READ_FWREV_1     0x84
00021 #define READ_FWREV_2     0xB8
00022 #define ADDR    0x80
00023 #define FREQ    100000
00024 #define DEVICE_ID 0x15
00025 
00026 
00027 char x_event;
00028 Ticker ticker;                                       //Ticker for lvgl
00029 
00030 //création des objets des deux écrans
00031 lv_obj_t * scr1;
00032 lv_obj_t * scr2;
00033 
00034 
00035 I2C i2c(PB_9,PB_8); // initialisation des broches I2C
00036 
00037 
00038 /*
00039  * Callback function for lvgl timing.
00040  * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10).
00041  * It is required for the internal timing of LittlevGL.
00042  */
00043 void lv_ticker_func()
00044 {
00045     lv_tick_inc(LVGL_TICK);
00046 }
00047 //
00048 //  Bouton ON/OFF
00049 //
00050 static void eventActivAlarme(lv_event_t * e)//event du bouton ON/OFF non utilisé actuellement
00051 {
00052     LV_LOG_USER("btnActivAlarme was Clicked");
00053 
00054     lv_obj_t * btnActivAlarme = lv_event_get_target(e);
00055     lv_obj_t * label = lv_obj_get_child(btnActivAlarme, 0);
00056 
00057     if (x_event==1) {
00058         lv_label_set_text(label, "ON");
00059 
00060         x_event=0;
00061     } else {
00062         lv_label_set_text(label, "OFF");
00063         x_event=1;
00064     }
00065 }
00066 
00067 
00068 //
00069 //  Bouton Graph
00070 //
00071 static void eventGraph(lv_event_t * event)  //event du bouton Graph pour charger scr2
00072 {
00073 
00074     lv_scr_load(scr2);
00075 
00076     lv_obj_t * btnGraph = lv_event_get_target(event);
00077     lv_obj_t * label = lv_obj_get_child(btnGraph, 0);
00078 
00079 }
00080 //
00081 //  Bouton MENU
00082 //
00083 static void event_MENU(lv_event_t * e)   //event du bouton Menu pour charger scr1
00084 {
00085 
00086     lv_scr_load(scr1);
00087 
00088     lv_obj_t * btn_ALR = lv_event_get_target(e);
00089     lv_obj_t * label = lv_obj_get_child(btn_ALR, 0);
00090 
00091 }
00092 
00093 // main() runs in its own thread in the OS
00094 int main()
00095 {
00096 
00097     //init variable I2C capteur CO2
00098     char readdata[6] = {};
00099     char writedata[6] = {};
00100     uint8_t adresse = 42; // adresse du capteur CO2
00101     char bufvalue[12] = {};
00102     int MSB = 0; // most significant bit
00103     int LSB = 0; // less signifiicant bit
00104     int ppm = 0;
00105     // variable I2C capteur Temp/Hum
00106     uint8_t  rx_buff[8];
00107     uint8_t  tx_buff[2];
00108     uint32_t rhData;    //valeur Hum
00109     int32_t  tData;     //valeur Temp
00110 
00111     i2c.frequency(FREQ); // FREQ de communication imposee par le maître 100 000
00112 
00113     //Initialisation des différents objets à l'écran
00114     lv_init();                                              //Initialize the LVGL
00115     tft_init();                                             //Initialize diplay
00116     touchpad_init();
00117     //Initialize touchpad
00118     ticker.attach(callback(&lv_ticker_func),TICKER_TIME);
00119     lv_obj_t * label;   //Attach callback to ticker
00120     lv_obj_t * labe2;   //Attach callback to ticker
00121     lv_obj_t * labe3;   //Attach callback to ticker
00122     lv_obj_t * labe4;   //Attach callback to ticker
00123     lv_obj_t * labe5;   //Attach callback to ticker
00124 
00125     scr1 = lv_obj_create(NULL); //création scr1
00126     scr2 = lv_obj_create(NULL); //création scr2
00127 
00128     lv_scr_load(scr1);          //Utilisation scr1 par défault
00129 
00130     //Texte appartenant au scr1
00131     //******************************************************************************//
00132     lv_obj_t * label1 = lv_label_create(scr1);
00133     lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP);     /*Break the long lines*/
00134     lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
00135     lv_label_set_text(label1, "#ff0000 Boitier domotique Temp Hum & CO2#");
00136     lv_obj_set_width(label1, 150);
00137     lv_obj_align(label1, LV_ALIGN_TOP_LEFT, 0, 0);
00138 
00139     lv_obj_t * label2 = lv_label_create(scr1);
00140     lv_obj_set_width(label2, 300);
00141     lv_label_set_text(label2, "Project by Simon Marchiano");
00142     lv_obj_align(label2, LV_ALIGN_BOTTOM_LEFT, 0, 0);
00143 
00144     lv_obj_t * label31 = lv_label_create(scr1);
00145     lv_obj_set_width(label31, 300);
00146     lv_label_set_recolor(label31, true);                      /*Enable re-coloring by commands in the text*/
00147     lv_obj_align(label31, LV_ALIGN_CENTER, 50, -50);
00148 
00149     lv_obj_t * label32 = lv_label_create(scr1);
00150     lv_obj_set_width(label32, 300);
00151     lv_label_set_recolor(label32, true);                      /*Enable re-coloring by commands in the text*/
00152     lv_obj_align(label32, LV_ALIGN_CENTER, 50, 0);
00153 
00154     lv_obj_t * label33 = lv_label_create(scr1);
00155     lv_obj_set_width(label33, 300);
00156     lv_label_set_recolor(label33, true);                      /*Enable re-coloring by commands in the text*/
00157     lv_obj_align(label33, LV_ALIGN_CENTER, 50, 50);
00158     //******************************************************************************//
00159 
00160     //Texte appartenant au scr2
00161     //******************************************************************************//
00162     lv_obj_t * label3 = lv_label_create(scr2);
00163     lv_obj_set_width(label3, 300);
00164     lv_label_set_recolor(label3, true);                      /*Enable re-coloring by commands in the text*/
00165     //lv_label_set_text_fmt(label3, "Temperature : %d", tData);
00166     lv_obj_align(label3, LV_ALIGN_TOP_MID, 50, 30);
00167 
00168     lv_obj_t * label4 = lv_label_create(scr2);
00169     lv_obj_set_width(label4, 300);
00170     lv_label_set_recolor(label4, true);                      /*Enable re-coloring by commands in the text*/
00171     //lv_label_set_text_fmt(label4, "Humidite : %d", rhData);
00172     lv_obj_align(label4, LV_ALIGN_TOP_MID, 50, 45);
00173 
00174     lv_obj_t * label5 = lv_label_create(scr2);
00175     lv_obj_set_width(label5, 300);
00176     lv_label_set_recolor(label5, true);                      /*Enable re-coloring by commands in the text*/
00177     //lv_label_set_text_fmt(label5, "CO2 : %d", ppm);
00178     lv_obj_align(label5, LV_ALIGN_TOP_MID, 50, 15);
00179 
00180     lv_obj_t * label20 = lv_label_create(scr2);
00181     lv_obj_set_width(label20, 300);
00182     lv_label_set_text_fmt(label20, "Graphique ppm ");
00183     lv_obj_align(label20, LV_ALIGN_BOTTOM_MID, 90, -35);
00184 //******************************************************************************//
00185 
00186     //Bouton appartenant au scr1
00187     //******************************************************************************//
00188     //  Bouton Graph
00189     lv_obj_t * btnGraph = lv_btn_create(scr1);
00190     lv_obj_add_event_cb(btnGraph, eventGraph, LV_EVENT_CLICKED, NULL);
00191     lv_obj_align(btnGraph, LV_ALIGN_LEFT_MID, 10, 0);
00192     label = lv_label_create(btnGraph);
00193     lv_label_set_text(label, "Graph");
00194 
00195     //  Style Bouton ON/OFF
00196     static lv_style_t style_btn_green;
00197     lv_style_init(&style_btn_green);
00198     lv_style_set_bg_color(&style_btn_green, lv_color_make(0,225,0));
00199     lv_style_set_bg_grad_color(&style_btn_green, lv_color_make(0,200,0));
00200     lv_style_set_border_color(&style_btn_green, lv_color_make(0,255,0));
00201     lv_style_set_text_color(&style_btn_green, lv_color_make(255,255,255));
00202 
00203     //  Bouton ON/OFF
00204     lv_obj_t * btnActivAlarme = lv_btn_create(scr1);
00205     lv_obj_add_style(btnActivAlarme, &style_btn_green,0);
00206 
00207     lv_obj_add_event_cb(btnActivAlarme, eventActivAlarme, LV_EVENT_CLICKED, NULL);
00208     lv_obj_align(btnActivAlarme, LV_ALIGN_LEFT_MID, 10, 50);
00209     lv_obj_add_flag(btnActivAlarme, LV_OBJ_FLAG_CHECKABLE);
00210     label = lv_label_create(btnActivAlarme);
00211     lv_label_set_text(label, "ON");
00212     //******************************************************************************//
00213     //Bouton et graphique appartenant au scr2
00214     //******************************************************************************//
00215     //  Bouton ALARMES
00216     lv_obj_t * btn_ALR = lv_btn_create(scr2);
00217     lv_obj_add_event_cb(btn_ALR, event_MENU, LV_EVENT_CLICKED, NULL);
00218     lv_obj_align(btn_ALR, LV_ALIGN_LEFT_MID, 10, -50);
00219     //lv_disp_get_scr_act(scr1);
00220     label = lv_label_create(btn_ALR);
00221     lv_label_set_text(label, "Menu");
00222 
00223     /*Create a chart*/
00224     lv_obj_t * chart;
00225     chart = lv_chart_create(scr2);
00226     lv_obj_set_size(chart, 250, 155);
00227     lv_obj_align(chart, LV_ALIGN_CENTER, 0, 0);
00228     lv_chart_set_type(chart, LV_CHART_TYPE_LINE);   /*Show lines and points too*/
00229     lv_chart_set_update_mode(chart, LV_CHART_UPDATE_MODE_SHIFT);
00230     lv_chart_set_point_count(chart, 100);
00231     lv_chart_set_range(chart, LV_CHART_AXIS_PRIMARY_Y, 0, 1400);
00232 
00233     /*Add 3 data series*/
00234     lv_chart_series_t * ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_X);
00235     lv_chart_series_t * ser2 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_GREEN), LV_CHART_AXIS_PRIMARY_X);
00236     lv_chart_series_t * ser3 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_BLUE), LV_CHART_AXIS_PRIMARY_X);
00237 
00238     while (true) {
00239 
00240         //Init capteur CO2
00241         writedata[0] = 0x04;
00242         writedata[1] = 0x13;
00243         writedata[2] = 0x8B;
00244         writedata[3] = 0x00;
00245         writedata[4] = 0x01;
00246 
00247         i2c.write(adresse, writedata, 6);  // requête pour obtenir les mesures du capteur
00248         HAL_Delay(7);
00249         i2c.read(adresse, readdata, 5); // stock les mesures dans le tableau readdata
00250         HAL_Delay(10);
00251 
00252         MSB = (int)readdata[2];                     // affecte la valeur du MSB
00253         LSB = (int)readdata[3];                     // affecte la valeur du LSB
00254         ppm = MSB*256+LSB;                          // calcul pour obtenir la valeur en ppm
00255         sprintf(bufvalue,"CO2 : %d ppm",ppm);       // conversion d'un nombre en caractère
00256 
00257         tx_buff[0] = READ_RH;
00258         i2c.write(ADDR, (char*)tx_buff, 1);
00259         i2c.read(ADDR, (char*)rx_buff, 2);
00260 
00261         rhData = ((((uint32_t)rx_buff[0] << 8 | rx_buff[1]) * 125 ) / 65536)-6;
00262 
00263         tx_buff[0] = READ_TEMP;
00264         i2c.write(ADDR, (char*)tx_buff, 1);
00265         i2c.read(ADDR, (char*)rx_buff, 2);
00266 
00267         tData = ((((uint32_t)rx_buff[0] << 8 | rx_buff[1]) * 175.72) / 65536) - 46.85;
00268 
00269         //Affichage valeurs scr1
00270         lv_label_set_text_fmt(label5,"#ff0000 %s#",bufvalue);
00271         lv_label_set_text_fmt(label3, "#0000ff Temperature : %d C°#", tData);
00272         lv_label_set_text_fmt(label4, "#008000 Humidite : %d %%#", rhData);
00273         //Affichage valeurs scr2
00274         lv_label_set_text_fmt(label31,"#ff0000 %s#",bufvalue);
00275         lv_label_set_text_fmt(label32, "#0000ff Temperature : %d C°#", tData);
00276         lv_label_set_text_fmt(label33, "#008000 Humidite : %d %%#", rhData);
00277         //Mise à jour valeur graphique
00278         lv_chart_set_next_value(chart, ser1, ppm);
00279         lv_chart_set_next_value(chart, ser2, rhData);
00280         lv_chart_set_next_value(chart, ser3, tData);
00281 
00282         lv_task_handler();
00283         //Call lv_task_handler() periodically every few milliseconds.
00284         //It will redraw the screen if required, handle input devices etc.
00285         thread_sleep_for(LVGL_TICK);
00286     }
00287 }