Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TFT_eSPI disables second SPI device #3400

Open
dani9084 opened this issue Jul 9, 2024 · 0 comments
Open

TFT_eSPI disables second SPI device #3400

dani9084 opened this issue Jul 9, 2024 · 0 comments

Comments

@dani9084
Copy link

dani9084 commented Jul 9, 2024

Hello everyone! I am making project, that uses a ST7789 display through SPI. My problem is that if I try to use a MAX6675 thermocoupler, which also communicates through SPI, it doesn't work. But if I comment out the tft.begin() command it works.

Example output:

 #include <Wire.h>
#include <TFT_eSPI.h>
#include "max6675.h"
#include <TinyGPSPlus.h>
#include "Untitled-1.h"
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite Graf = TFT_eSprite(&tft);

int thermoDO = 19;  // Data Out
int thermoCS = 5;   // Chip Select
int thermoCLK = 18; // Clock
int ertek = 0;
int elozoertek = 0;
unsigned long ms_lastmeres = 0;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

// KIJELZŐ //
unsigned long fasz = 2000; // Meddig marad a kijelzőn az értesítés (ms)
unsigned long idotartam = 0;
int d_cs = 15; // Display CS Pin
// MENÜ //
int menumegnyitva = 0; // Ha menü megnyitva: 1
int valasztottelem = 1; // Hanyadik elem a menüben van bekeretezve / kiválasztva, alapból az első
int jelenlegvalasztott = 1; // Az a menü opció ami már ki van választva. Csak akkor rajzolja újra a keretet, ha valasztott elem != jelenlegvalasztott
int menuelemek = 3; // Menü opciók: Tempomat, Motorbeállítások, Rendszerbeállítások
int tempokielemek = 3; // Tempomat memü, kikapcsolva: T bekapcsolása mostani sebesség, T bekapcsolása választaható sebesség, T gyorsulási mód: Komfort, Aggresszív
int tempobeelemek = 3; // Tempomat kikapcsolása, Sebesség csökkentés, Sebesség növelés
int rbeallelemek = 3; // Rendszer színe, Magas kontraszt, Összes beállítás alaphelyzetbe állítása
int mbeallelemek = 5; // Motor melegítő program bekapcsolása, MMP hőfok beállítása, Gyorsváltó, Fényszóró index, Átlagfogyasztás beállítása, (REV-LIMITER, még nem biztos) 

int menuselect = 12; // Menü megnyitó, kiválasztó (select) gomb
int fel = 13; // FEL gomb
int le = 14; // LE gomb

// Gombok lebegésének kezelése
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // 50ms debounce delay
unsigned long lastActivityTime = 0; // Utolsó gombnyomás ideje
unsigned long timeoutDuration = 7000; // 7 másodperc inaktivitási idő

bool debounce(int pin) {
  if (digitalRead(pin) == LOW) {
    if ((millis() - lastDebounceTime) > debounceDelay) {
      lastDebounceTime = millis();
      return true;
    }
  }
  return false;
}

void setup() {
  pinMode(menuselect, INPUT_PULLUP);
  pinMode(fel, INPUT_PULLUP);
  pinMode(le, INPUT_PULLUP);
  Serial.begin(115200);
  delay(500);

  // Initialize the display
  tft.begin();
  digitalWrite(d_cs, LOW);
  digitalWrite(thermoCS, HIGH);
  tft.setRotation(0);
  tft.setSwapBytes(true);
  tft.fillScreen(TFT_BLACK);
  tft.drawFastHLine(0, 275, 239, TFT_DARKGREY);
  tft.drawFastHLine(0, 276, 239, TFT_LIGHTGREY);
  tft.drawFastHLine(0, 277, 239, TFT_WHITE);
  tft.drawFastHLine(0, 278, 239, TFT_WHITE);
  tft.drawFastHLine(0, 279, 239, TFT_WHITE);
  tft.drawFastHLine(0, 280, 239, TFT_LIGHTGREY);
  tft.drawFastHLine(0, 281, 239, TFT_DARKGREY);
    digitalWrite(d_cs, HIGH);
  digitalWrite(thermoCS, LOW);
  delay(1000);
  
}
void homeres() {
  if (millis() - ms_lastmeres > 500) {
      digitalWrite(d_cs, HIGH);
    digitalWrite(thermoCS, LOW); // Select the MAX6675
    ertek = thermocouple.readCelsius();
    digitalWrite(thermoCS, HIGH); // Deselect the MAX6675
    digitalWrite(d_cs, LOW);

    Serial.print("Temperature: ");
    Serial.println(ertek);

    // Display temperature
    tft.fillRect(100, 175, 40, 40, TFT_BLACK); // Clear the previous temperature display
    tft.setCursor(100, 175);
    tft.setTextSize(3);
    tft.print(ertek);

    ms_lastmeres = millis();
  }
}

void resetToStartingScreen() {
  menumegnyitva = 0;
  valasztottelem = 1;
  jelenlegvalasztott = valasztottelem;
  tft.fillScreen(TFT_BLACK);
  tft.drawFastHLine(0, 275, 239, TFT_DARKGREY);
  tft.drawFastHLine(0, 276, 239, TFT_LIGHTGREY);
  tft.drawFastHLine(0, 277, 239, TFT_WHITE);
  tft.drawFastHLine(0, 278, 239, TFT_WHITE);
  tft.drawFastHLine(0, 279, 239, TFT_WHITE);
  tft.drawFastHLine(0, 280, 239, TFT_LIGHTGREY);
  tft.drawFastHLine(0, 281, 239, TFT_DARKGREY);
}

void menu() {
  digitalWrite(d_cs, LOW);
  digitalWrite(thermoCS, HIGH);
  bool activityDetected = false;
  if (debounce(menuselect)) {
    activityDetected = true;
    if (menumegnyitva == 0) {
      menumegnyitva = 1;
      valasztottelem = 1;
      jelenlegvalasztott = valasztottelem;
      tft.fillScreen(TFT_BLACK);
      tft.pushImage(9, 10, 74, 68, tikon);
      tft.pushImage(89, 29, 142, 39, tiras);
      tft.pushImage(9, 99, 75, 75, rikon);
      tft.pushImage(89, 118, 142, 39, riras);
      tft.pushImage(13, 199, 86, 55, mikon);
      tft.pushImage(103, 207, 128, 39, miras);

      tft.fillRect(0, 4, 240, 5, TFT_WHITE);
      tft.fillRect(0, 9, 5, 79, TFT_WHITE);
      tft.fillRect(0, 88, 240, 5, TFT_WHITE);
      tft.fillRect(236, 9, 5, 79, TFT_WHITE);
    }
  }
  if (menumegnyitva == 1) {
    if (debounce(le)) {
      activityDetected = true;
      if (menuelemek > valasztottelem) {
        valasztottelem++;
      } else if (valasztottelem >= menuelemek) {
        valasztottelem = 1;
      }
    }
    if (debounce(fel)) {
      activityDetected = true;
      if (valasztottelem > 1) {
        valasztottelem--;
      } else if (1 >= valasztottelem) {
        valasztottelem = menuelemek;
      }
    }
    if (valasztottelem != jelenlegvalasztott) {
      tft.fillRect(0, 4 + ((jelenlegvalasztott - 1) * 89), 240, 5, TFT_BLACK);
      tft.fillRect(0, 9 + ((jelenlegvalasztott - 1) * 89), 5, 79, TFT_BLACK);
      tft.fillRect(0, 88 + ((jelenlegvalasztott - 1) * 89), 240, 5, TFT_BLACK);
      tft.fillRect(236, 9 + ((jelenlegvalasztott - 1) * 89), 5, 79, TFT_BLACK);

      tft.fillRect(0, 4 + ((valasztottelem - 1) * 89), 240, 5, TFT_WHITE);
      tft.fillRect(0, 9 + ((valasztottelem - 1) * 89), 5, 79, TFT_WHITE);
      tft.fillRect(0, 88 + ((valasztottelem - 1) * 89), 240, 5, TFT_WHITE);
      tft.fillRect(236, 9 + ((valasztottelem - 1) * 89), 5, 79, TFT_WHITE);
    }
  }

  if (activityDetected) {
    lastActivityTime = millis(); // Reset the inactivity timer
  }
    digitalWrite(d_cs, HIGH);
  digitalWrite(thermoCS, LOW);
}

void loop() {
  homeres(); // Ensure homeres() is called regularly

  // Check for inactivity timeout
  if (menumegnyitva == 1 && (millis() - lastActivityTime > timeoutDuration)) {
    resetToStartingScreen();
  }

  menu();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant