Skip to content

Commit

Permalink
Nextion TFT touch screen (MarlinFirmware#21324)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <[email protected]>
  • Loading branch information
Skorpi08 and thinkyhead authored Mar 19, 2021
1 parent 1f52112 commit b51aed8
Show file tree
Hide file tree
Showing 12 changed files with 1,235 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2396,6 +2396,14 @@
//#define ANYCUBIC_LCD_DEBUG
#endif

//
// 320x240 Nextion 2.8" serial TFT Resistive Touch Screen NX3224T028
//
//#define NEXTION_TFT
#if ENABLED(NEXTION_TFT)
#define LCD_SERIAL_PORT 1 // Default is 1 for Nextion
#endif

//
// Third-party or vendor-customized controller interfaces.
// Sources should be installed in 'src/lcd/extui'.
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@
#endif

// Extensible UI serial touch screens. (See src/lcd/extui)
#if ANY(HAS_DGUS_LCD, MALYAN_LCD, TOUCH_UI_FTDI_EVE, ANYCUBIC_LCD_I3MEGA, ANYCUBIC_LCD_CHIRON)
#if ANY(HAS_DGUS_LCD, MALYAN_LCD, TOUCH_UI_FTDI_EVE, ANYCUBIC_LCD_I3MEGA, ANYCUBIC_LCD_CHIRON, NEXTION_TFT)
#define IS_EXTUI 1
#define EXTENSIBLE_UI
#endif
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -2353,6 +2353,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal
+ ENABLED(MAKEBOARD_MINI_2_LINE_DISPLAY_1602) \
+ ENABLED(MAKRPANEL) \
+ ENABLED(MALYAN_LCD) \
+ ENABLED(NEXTION_TFT) \
+ ENABLED(MKS_LCD12864) \
+ ENABLED(OLED_PANEL_TINYBOY2) \
+ ENABLED(OVERLORD_OLED) \
Expand Down
174 changes: 174 additions & 0 deletions Marlin/src/lcd/extui/lib/nextion/FileNavigator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

/* ****************************************
* lcd/extui/lib/nextion/FileNavigator.cpp
* ****************************************
* Extensible_UI implementation for Nextion
* https://github.com/Skorpi08
* ***************************************/

#include "../../../../inc/MarlinConfigPre.h"

#if ENABLED(NEXTION_TFT)

#include "FileNavigator.h"
#include "nextion_tft.h"

using namespace ExtUI;

#define DEBUG_OUT NEXDEBUGLEVEL
#include "../../../../core/debug_out.h"

FileList FileNavigator::filelist; // Instance of the Marlin file API
char FileNavigator::currentfoldername[MAX_PATH_LEN]; // Current folder path
uint16_t FileNavigator::lastindex;
uint8_t FileNavigator::folderdepth;
uint16_t FileNavigator::currentindex; // override the panel request

FileNavigator filenavigator;

FileNavigator::FileNavigator() { reset(); }

void FileNavigator::reset() {
currentfoldername[0] = '\0';
folderdepth = 0;
currentindex = 0;
lastindex = 0;
// Start at root folder
while (!filelist.isAtRootDir()) filelist.upDir();
refresh();
}

void FileNavigator::refresh() { filelist.refresh(); }

void FileNavigator::getFiles(uint16_t index) {
uint16_t files = 7, fseek = 0, fcnt = 0;
if (index == 0)
currentindex = 0;
else {
// Each time we change folder we reset the file index to 0 and keep track
// of the current position as the TFT panel isn't aware of folder trees.
--currentindex; // go back a file to take account of the .. added to the root.
if (index > lastindex)
currentindex += files + 1;
else if (currentindex >= files)
currentindex -= files - 1;
else
currentindex = 0;
}
lastindex = index;

#if NEXDEBUG(AC_FILE)
DEBUG_ECHOLNPAIR("index=", index, " currentindex=", currentindex);
#endif

if (currentindex == 0 && folderdepth > 0) { // Add a link to go up a folder
nextion.SendtoTFT(PSTR("vis p0,1"));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
SEND_VAL("tmpUP", "0");
files--;
}
else {
nextion.SendtoTFT(PSTR("vis p0,0"));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
}

for (uint16_t seek = currentindex; seek < currentindex + files; seek++) {
if (filelist.seek(seek)) {
nextion.SendtoTFT(PSTR("s"));
LCD_SERIAL.print(fcnt);
nextion.SendtoTFT(PSTR(".txt=\""));
if (filelist.isDir()) {
LCD_SERIAL.print(filelist.shortFilename());
nextion.SendtoTFT(PSTR("/\""));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));

nextion.SendtoTFT(PSTR("l"));
LCD_SERIAL.print(fcnt);
nextion.SendtoTFT(PSTR(".txt=\""));
LCD_SERIAL.print(filelist.filename());
nextion.SendtoTFT(PSTR("\""));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
SEND_PCO2("l", fcnt, "1055");
}
else {
LCD_SERIAL.print(currentfoldername);
LCD_SERIAL.print(filelist.shortFilename());
nextion.SendtoTFT(PSTR("\""));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));

nextion.SendtoTFT(PSTR("l"));
LCD_SERIAL.print(fcnt);
nextion.SendtoTFT(PSTR(".txt=\""));
LCD_SERIAL.print(filelist.longFilename());
nextion.SendtoTFT(PSTR("\""));
nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
}
fcnt++;
fseek = seek;
#if NEXDEBUG(AC_FILE)
DEBUG_ECHOLNPAIR("-", seek, " '", filelist.longFilename(), "' '", currentfoldername, "", filelist.shortFilename(), "'\n");
#endif
}
}
SEND_VAL("n0", filelist.count());
SEND_VAL("n1", fseek + 1);
}

void FileNavigator::changeDIR(char *folder) {
#if NEXDEBUG(AC_FILE)
DEBUG_ECHOLNPAIR("currentfolder: ", currentfoldername, " New: ", folder);
#endif
if (folderdepth >= MAX_FOLDER_DEPTH) return; // limit the folder depth
strcat(currentfoldername, folder);
strcat(currentfoldername, "/");
filelist.changeDir(folder);
refresh();
folderdepth++;
currentindex = 0;
}

void FileNavigator::upDIR() {
filelist.upDir();
refresh();
folderdepth--;
currentindex = 0;
// Remove the last child folder from the stored path
if (folderdepth == 0) {
currentfoldername[0] = '\0';
reset();
}
else {
char *pos = nullptr;
for (uint8_t f = 0; f < folderdepth; f++)
pos = strchr(currentfoldername, '/');
pos[1] = '\0';
}
#if NEXDEBUG(AC_FILE)
DEBUG_ECHOLNPAIR("depth: ", folderdepth, " currentfoldername: ", currentfoldername);
#endif
}

char* FileNavigator::getCurrentFolderName() { return currentfoldername; }

#endif // NEXTION_TFT
53 changes: 53 additions & 0 deletions Marlin/src/lcd/extui/lib/nextion/FileNavigator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once

/* ****************************************
* lcd/extui/lib/nextion/FileNavigator.cpp
* ****************************************
* Extensible_UI implementation for Nextion
* https://github.com/Skorpi08
* ***************************************/

#include "nextion_tft_defs.h" // for MAX_PATH_LEN
#include "../../ui_api.h"

using namespace ExtUI;

class FileNavigator {
public:
FileNavigator();
static void reset();
static void getFiles(uint16_t);
static void upDIR();
static void changeDIR(char *);
static void refresh();
static char* getCurrentFolderName();
private:
static FileList filelist;
static char currentfoldername[MAX_PATH_LEN];
static uint16_t lastindex;
static uint8_t folderdepth;
static uint16_t currentindex;
};

extern FileNavigator filenavigator;
Loading

0 comments on commit b51aed8

Please sign in to comment.