sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.65k stars 630 forks source link

TFT_eSPI crashes after a while #417

Open j45p41 opened 3 years ago

j45p41 commented 3 years ago

Hi,

carrying on from: https://github.com/sandeepmistry/arduino-LoRa/issues/290.

I have managed to implement code that works but crashes within 24 hours. Did anybody find a fix for this issue?

I am using lgvl but I am sure the issue is related to SPI.

Here is the code that works for while and then crashes:

#include <lvgl.h>
#include <TFT_eSPI.h>

#include <SPI.h>
#include <LoRa.h>
// #include "NotoSansBold15.h"

//define the pins used by the transceiver module
#define ss 33
#define rst 12
#define dio0 27

#define MY_CS       33
#define MY_SCLK     25
#define MY_MISO     27
#define MY_MOSI     26

#define CE_PIN   12
#define CSN_PIN 33

TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
static lv_disp_buf_t disp_buf;
static lv_color_t buf[LV_HOR_RES_MAX * 10];
int counter = 0;

// TFT_eSPI tft = TFT_eSPI(135, 240); // Invoke custom library

SPIClass spiLoRA(HSPI);
  LoRaClass LoRa2;

#if USE_LV_LOG != 0
/* Serial debugging */
void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
{

    Serial.printf("%s@%d->%s\r\n", file, line, dsc);
    Serial.flush();
}
#endif
void lv_ex_led_1(void)
{
    /*Create a LED and switch it OFF*/
    lv_obj_t * led1  = lv_led_create(lv_scr_act(), NULL);
    lv_obj_align(led1, NULL, LV_ALIGN_CENTER, -80, 0);
    lv_led_off(led1);

    /*Copy the previous LED and set a brightness*/
    lv_obj_t * led2  = lv_led_create(lv_scr_act(), led1);
    lv_obj_align(led2, NULL, LV_ALIGN_CENTER, 0, 0);
    lv_led_set_bright(led2, 190);

    /*Copy the previous LED and switch it ON*/
    lv_obj_t * led3  = lv_led_create(lv_scr_act(), led1);
    lv_obj_align(led3, NULL, LV_ALIGN_CENTER, 80, 0);
    lv_led_on(led3);
}

/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors(&color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp);
}

/* Reading input device (simulated encoder here) */
bool read_encoder(lv_indev_drv_t * indev, lv_indev_data_t * data)
{
    static int32_t last_diff = 0;
    int32_t diff = 0; /* Dummy - no movement */
    int btn_state = LV_INDEV_STATE_REL; /* Dummy - no press */

    data->enc_diff = diff - last_diff;;
    data->state = btn_state;

    last_diff = diff;

    return false;
}

void setup()
{

  pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);

    Serial.begin(115200); /* prepare for possible serial debug */
    while (!Serial);
    Serial.println("LoRa Receiver");

    lv_init();

#if USE_LV_LOG != 0
    lv_log_register_print_cb(my_print); /* register print function for debugging */
#endif

    tft.begin(); /* TFT init */
    tft.setRotation(1); /* Landscape orientation */

    lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);

    /*Initialize the display*/
    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.hor_res = 480;
    disp_drv.ver_res = 320;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.buffer = &disp_buf;
    lv_disp_drv_register(&disp_drv);

    /*Initialize the (dummy) input device driver*/
    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_ENCODER;
    indev_drv.read_cb = read_encoder;
    lv_indev_drv_register(&indev_drv);

    // /* Create simple label */
    // lv_obj_t *label = lv_label_create(lv_scr_act(), NULL);
    // lv_label_set_text(label, "Hello Arduino! (V7.0.X)");
    // lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);

    spiLoRA.begin(MY_SCLK, MY_MISO, MY_MOSI);
    LoRa2.setSPI(spiLoRA);
    LoRa2.setPins(CSN_PIN, rst, dio0);

      while (!LoRa2.begin(433E6)) {
        Serial.println("[LoRa 2] Starting LoRa failed!");
        // LORA2_Status="FAILED";
        delay(1000);
      }

      LoRa2.setSyncWord(0xF3);
      Serial.println("LoRa Initializing OK!");
      tft.println("LoRa Initializing OK!");

// CREATE INITIAL screen

// // Background Image  | Image de fond
// lv_obj_t * img1 = lv_img_create(lvglpage, NULL);
// lv_img_set_src(img1, &WALLPAPER_1_IMG);
// lv_obj_align(img1, NULL, LV_ALIGN_CENTER, 0, 0);

// Button in center of the screen | Bouton au centre de l'écran
lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(btn1, event_handler);
lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, 0);

// Display a circuar scrolling welcome message
// Affiche un message défilant de bienvenue
lv_obj_t * welcomemessage;
welcomemessage = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(welcomemessage, LV_LABEL_LONG_SROLL_CIRC);     /*Circular scroll*/
lv_obj_set_width(welcomemessage, lv_disp_get_hor_res( NULL ));
lv_label_set_text(welcomemessage, "GARAGE DOOR STATE");
lv_obj_align(welcomemessage, btn1, LV_ALIGN_CENTER, 0, -60);

// lv_demo_printer();

}

void loop()
{

    lv_task_handler(); /* let the GUI do its work */

    int packetSize = LoRa2.parsePacket();
    if (packetSize) {
      // received a packet
      Serial.print("Received packet '");
      // tft.print("Received packet '");

      // read packet
      while (LoRa2.available()) {
        String LoRaData = LoRa2.readString();
        Serial.println(LoRaData);

        const char* conv_my_str = LoRaData.c_str();

        if(LoRaData == "L02I"){

          lv_obj_t * led1  = lv_led_create(lv_scr_act(), NULL);
          lv_obj_align(led1, NULL, LV_ALIGN_CENTER, -80, 0);
          lv_led_off(led1);

          /*Copy the previous LED and set a brightness*/
          lv_obj_t * led2  = lv_led_create(lv_scr_act(), led1);
          lv_obj_align(led2, NULL, LV_ALIGN_CENTER, 0, 0);
          lv_led_set_bright(led2, 190);

          /*Copy the previous LED and switch it ON*/
          lv_obj_t * led3  = lv_led_create(lv_scr_act(), led1);
          lv_obj_align(led3, NULL, LV_ALIGN_CENTER, 80, 0);
          lv_led_on(led3);

        }
        else if(LoRaData == "O02I"){

          lv_obj_t * led1  = lv_led_create(lv_scr_act(), NULL);
          lv_obj_align(led1, NULL, LV_ALIGN_CENTER, -80, 0);
          lv_led_on(led1);

          /*Copy the previous LED and set a brightness*/
          lv_obj_t * led2  = lv_led_create(lv_scr_act(), led1);
          lv_obj_align(led2, NULL, LV_ALIGN_CENTER, 0, 0);
          lv_led_set_bright(led2, 190);

          /*Copy the previous LED and switch it ON*/
          lv_obj_t * led3  = lv_led_create(lv_scr_act(), led1);
          lv_obj_align(led3, NULL, LV_ALIGN_CENTER, 80, 0);
          lv_led_off(led3);

        }

        /* Create simple label */
        lv_obj_t *label = lv_label_create(lv_scr_act(), NULL);
        lv_label_set_text(label, conv_my_str);
        lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, 0);
        // tft.println(LoRaData);
            // counter = counter+10;
      }

}

yield();
}