Exemple de graphique LVGL en mise à jour en direct ST7735R pour PlatformIO

**Important :**Dans les versions plus récentes de PlatformIO, la plupart des cartes ESP32 planteront avec cet exemple à moins que vous ne créiez un fichier de config lv_conf.h personnalisé spécifiant d’utiliser la mémoire interne au lieu de PSRAM. Voir Comment corriger le crash / redémarrage ESP32 LVGL dans lv_tlsf_create() / lv_mem_init() pour des instructions sur comment corriger cela.

Dans notre exemple précédent Exemple minimal de graphique LVGL ST7735R pour PlatformIO

Voir Exemple minimal d’affichage TFT PlatformIO ESP32 utilisant Adafruit ST7735 pour la configuration matérielle recommandée.

Utilisant un ESP32 avec un affichage ST7735R 128x160px, ce code atteint un taux de mise à jour d’environ 3Hz. Notez que cet exemple peut facilement être modifié pour fonctionner avec d’autres contrôleurs LCD ou affichages.

Affichage TFT ST7735R montrant le graphique LVGL avec onde sinusoïdale en mise à jour en direct avec label Température

st7735r_lvgl_live_chart.cpp
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <Adafruit_LvGL_Glue.h>
#include <lvgl.h>
#include <string>

constexpr int Pin_LCD_CS = 27;
constexpr int Pin_LCD_DC = 23;
constexpr int Pin_LCD_RST = 22;
constexpr int Pin_LCD_SCLK = 14;
constexpr int Pin_LCD_MISO = 12;
constexpr int Pin_LCD_MOSI = 13;

Adafruit_ST7735 lcd(Pin_LCD_CS, Pin_LCD_DC, Pin_LCD_MOSI, Pin_LCD_SCLK,
                                 Pin_LCD_RST);
Adafruit_LvGL_Glue glue;

lv_obj_t* chart;
lv_chart_series_t * ser1;

void lvgl_chart_setup() {
    /*Crée un graphique*/
    chart = lv_chart_create(lv_scr_act());
    lv_obj_set_size(chart, 160, 100);
    lv_obj_align(chart, LV_ALIGN_BOTTOM_MID, 0, -3);
    lv_chart_set_type(chart, LV_CHART_TYPE_LINE);   /*Affiche aussi les lignes et points*/

    /*Ajoute deux séries de données*/
    ser1 = lv_chart_add_series(chart, lv_palette_main(LV_PALETTE_RED), LV_CHART_AXIS_PRIMARY_Y);

    /*Définit les points suivants sur 'ser1'*/
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 10);
    lv_chart_set_next_value(chart, ser1, 30);
    lv_chart_set_next_value(chart, ser1, 70);
    lv_chart_set_next_value(chart, ser1, 90);

    lv_chart_refresh(chart);
}

void lvgl_setup(void) {
  // Crée un label simple centré sur l'écran
  lv_obj_t *label = lv_label_create(lv_scr_act());
  lv_label_set_text(label, "Temperature");
  lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 2);

  lvgl_chart_setup();
}

void setup() {
  Serial.begin(115200);
  lcd.initR(INITR_BLACKTAB);      // Init la puce ST7735S, onglet noir
  lcd.setRotation(1);

  LvGLStatus status = glue.begin(&lcd);
  if(status != LVGL_OK) {
    Serial.printf("Glue error %d\r\n", (int)status);
    ESP.restart();
  }

  lvgl_setup(); // Appelle la fonction de construction d'UI ci-dessus
}

void UpdateChart() {
  // Remplit les données du graphique avec des données d'onde sinusoïdale
  float sineOffset = millis() / 1000.0;
  int16_t maxValue = 50;
  size_t count = lv_chart_get_point_count(chart);

  for (size_t i = 0; i < count; i++) {
    lv_chart_set_value_by_id(chart, ser1,  i, maxValue * (1 + sin((sineOffset + i) * 0.7)));
  }

  lv_chart_refresh(chart);
}

void loop() {
  // Met à jour l'affichage
  UpdateChart();
  lv_task_handler();
  delay(5);
}
platformio_ini_env_esp32dev.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
lib_deps =
    adafruit/Adafruit GFX Library@^1.11.5
    adafruit/Adafruit ST7735 and ST7789 Library@^1.10.0
    adafruit/Adafruit LittlevGL Glue Library@^2.1.4
    adafruit/SdFat - Adafruit Fork@^2.2.1
    lvgl/lvgl@^8.3.7

Consultez les articles similaires par catégorie : Arduino LVGL PlatformIO