diff --git a/src/displays/tft.cpp b/src/displays/tft.cpp index ae1b061..bb5eb7e 100644 --- a/src/displays/tft.cpp +++ b/src/displays/tft.cpp @@ -1,11 +1,157 @@ #include "tft.h" -extern Adafruit_ST7789 tft; +// LCD display pins +#define TFT_CS 7 +#define TFT_RST 12 +#define TFT_DC 14 +#define MOSI 4 // MOSI +#define SCK 6 // SCK +// Create an instance of the Adafruit ST7789 class using the custom SPI pins -TFT::TFT() +Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST); + +TFT_Display::TFT_Display() { } -TFT::~TFT() +TFT_Display::~TFT_Display() { } + +void TFT_Display::start() +{ + tft.init(240, 320); // Init ST7789 320x240 + tft.setRotation(1); // Set screen orientation +} + +void TFT_Display::init(ReflowProfile *profile) +{ + + clear(); + + tft.setTextColor(ST77XX_WHITE); + tft.setTextSize(2); + TFT_XY xy = getCenteredTextXY(profile->name); + tft.setCursor(xy.x, xy.y); + tft.println(profile->name); + + drawGraph(); +} + +void TFT_Display::clear() +{ + tft.fillScreen(ST77XX_BLACK); +} + +TFT_XY TFT_Display::getCenteredTextXY(char *text) +{ + TFT_XY xy; + + uint8_t textLength = strlen(text); + int16_t x, y; + uint16_t w, h; + w = tft.width(); + h = tft.height(); + + tft.getTextBounds(text, 0, 0, &x, &y, &w, &h); + + xy.x = (w - textLength) / 2; + xy.y = (h - 1) / 2; + + return xy; +} + +TFT_XY TFT_Display::getRightAlignedTextXY(char *text, uint16_t x, uint16_t y) +{ + TFT_XY xy; + + uint8_t textLength = strlen(text); + int16_t x1, y1; + uint16_t w, h; + w = tft.width(); + h = tft.height(); + + tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); + + xy.x = x - textLength - 1; + xy.y = y; + + return xy; +} + +TFT_XY TFT_Display::getLeftAlignedTopTextXY(char *text, uint16_t x, uint16_t y) +{ + TFT_XY xy; + + uint8_t textLength = strlen(text); + int16_t x1, y1; + uint16_t w, h; + w = tft.width(); + h = tft.height(); + + tft.getTextBounds(text, 0, 0, &x1, &y1, &w, &h); + + xy.x = x; + xy.y = y - h - 1; + + return xy; +} + +void TFT_Display::getMaxTempFromProfile(ReflowProfile *profile) +{ + + for (uint8_t i = 0; i < 5; i++) + { + if (profile->steps[i].targetTempAtEnd > maxTemp) + { + maxTemp = profile->steps[i].targetTempAtEnd; + } + } + + if (maxTemp < 200) + { + maxTemp = 200; + } + else if (maxTemp > 200 && maxTemp < 255) + { + if (maxTemp + 20 > 255) + { + maxTemp = 255; + } + else + { + maxTemp += 20; + } + } + else + { + maxTemp = 255; + } +} + +TFT_XY TFT_Display::getXYWithinGraphBounds(uint8_t temp, uint8_t time) +{ + + TFT_XY xy; + + xy.x = graphXY.x + (graphWidth * (time / totalTIme)); + xy.y = graphXY.y - (graphHeight * ((temp - minTemp) / (maxTemp - minTemp))); + + return xy; +} + +void TFT_Display::drawGraph() +{ + + tft.drawPixel(graphXY.x, graphXY.y, ST77XX_WHITE); + + tft.drawFastHLine(graphXY.x, graphXY.y, graphWidth, ST77XX_WHITE); + + tft.drawFastVLine(graphXY.x, graphXY.y - graphHeight, graphHeight, ST77XX_WHITE); + + tft.setTextColor(ST77XX_WHITE); + tft.setTextSize(1); + TFT_XY position = getLeftAlignedTopTextXY("20", graphXY.x, graphXY.y); + tft.setCursor(position.x, position.y); + tft.println("20"); +} \ No newline at end of file diff --git a/src/displays/tft.h b/src/displays/tft.h index 513271d..13910d6 100644 --- a/src/displays/tft.h +++ b/src/displays/tft.h @@ -1,21 +1,44 @@ -#ifndef TFT_H -#define TFT_H +#ifndef TFT_Display_h +#define TFT_Display_h #include #include // Include the ST7789 library +#include -class TFT { +struct TFT_XY +{ + int x; + int y; +}; + +class TFT_Display +{ public: - TFT(); - ~TFT(); + TFT_Display(); + ~TFT_Display(); + void start(); // Add your class methods here + void init(ReflowProfile *profile); void clear(); - private: // Add your private members here + TFT_XY getCenteredTextXY(char *text); + TFT_XY getRightAlignedTextXY(char *text, uint16_t x, uint16_t y); + TFT_XY getLeftAlignedTopTextXY(char *text, uint16_t x, uint16_t y); + void getMaxTempFromProfile(ReflowProfile *profile); + uint8_t graphHeight = 180; + uint16_t graphWidth = 256; + TFT_XY graphXY = {32, 220}; + uint16_t maxTemp = 0; + uint16_t totalTIme ; + uint8_t minTemp = 20; + + TFT_XY getXYWithinGraphBounds(uint8_t temp, uint8_t time); + + void drawGraph(); }; #endif // TFT_H diff --git a/src/globals.cpp b/src/globals.cpp index b69f9cb..20978d9 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -59,7 +59,7 @@ ReflowProfile reflowProfiles[] = { ReflowStep(ReflowProcessState::PREHEAT, 120, 35, EASE_OUT), ReflowStep(ReflowProcessState::SOAK, 90, 155), ReflowStep(ReflowProcessState::REFLOW, 45, 185, EASE_OUT), - ReflowStep(ReflowProcessState::COOL, 45, 155, EASE_IN), + ReflowStep(ReflowProcessState::COOL, 45, 155, EASE_OUT), ReflowStep(ReflowProcessState::DONE, 0, 0)}, "138c Sn42Bi58\0"), diff --git a/src/main.cpp b/src/main.cpp index 1ddc80a..1f145dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,5 @@ #include #include "ArduPID.h" -#include // Include the ST7789 library #include #include #include @@ -9,6 +8,7 @@ #include "leds/leds.h" #include "reflow.h" #include "displays/oled.h" +#include "displays/tft.h" #include "PID/PidController.h" #include "globals.h" #include "EEPROMDataManager.h" @@ -16,17 +16,6 @@ -// LCD display pins -#define TFT_CS 7 -#define TFT_RST 12 -#define TFT_DC 14 -#define MOSI 4 // MOSI -#define SCK 6 // SCK -// Create an instance of the Adafruit ST7789 class using the custom SPI pins -Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, MOSI, SCK, TFT_RST); - - - #define MOSTFET_PIN 17 double currentTemp = 0; @@ -38,12 +27,10 @@ LEDS leds = LEDS(); // Declare the PID ArduPID PID; OledDisplay oled = OledDisplay(); - -TemperatureController temperatureController ; - - +TFT_Display tftDisplay ; +TemperatureController temperatureController; void setup() { @@ -65,6 +52,8 @@ void setup() reflowProcessState.set(USER_INPUT); temperatureController.checkPluggedInThermistors(); + + tftDisplay.start(); } void loop() { @@ -91,16 +80,25 @@ void loop() } ReflowProcessState newState = reflowProcessState.get(); - if (newState != state) { + if (newState != state) + { Serial.println("State changed from " + String(STATE_STR(state)) + " to " + String(STATE_STR(newState))); // State changed from state to newState (user input or wifi input needs to be above here) - if (newState == PREHEAT) { + if (newState == PREHEAT) + { + + // Initalize the TFT + tftDisplay.init(&chosenReflowProfile); + + + //Start the reflow profile after tft to make sure the timer is accurate chosenReflowProfile.start(); + // Start the PID pidController.start(); } } state = newState; - + leds.loop(); oled.loop(); @@ -121,7 +119,6 @@ void loop() pidController.stop(); reflowProcessState.set(USER_INPUT); } - // if (step.state == ReflowProcessState::DONE) { // profile.start(); diff --git a/src/reflow.h b/src/reflow.h index 9dd3379..c5e1fb0 100644 --- a/src/reflow.h +++ b/src/reflow.h @@ -155,6 +155,8 @@ public: return curStep.calcTempAtPercentage(startTemp, percentage); } + + uint8_t getCurrentStepRelativeTime() { uint32_t elapsedTime = timer.elapsed(); uint16_t startTime = startTimes[STEPINDEX(curReflowStep())];