Replies: 1 comment 1 reply
-
I think I solved by using : class Display : public TFT_eSPI |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hai
I am try to use the TFT libray in combination with the task sceduler and a Class . Parts of the screen are updated in the sceduled function. But it seems that the tft functions losing track of the (init) context when I do a function call to a method in the Class in the sceduled task.
When I use the Class in a Arduino EDE loop is is working fine. But when I use the Class in a sceduled function it seems that the context is not available. I need some help how to use TFT in a Class and in a sceduled function. I include the Class becouse I think the problem can be found in my Class. I use the Class to replace a small Oled display using a Adafruit_SSD1327.h. That is working ok but when I use the Class in a sceduled function to update parts of the screen the Class is losing track of the TFT paramaters
First I need too be shure of the correct use of the TFT library in a Class
Thanks in advantage
John
#ifndef DISPLAY_h
#define DISPLAY_h
#include <TFT_eSPI.h>
class Display
{
public:
// void display();// activate
void clearDisplay();//
bool getTouch(uint16_t &x , uint16_t &y);
int getIframe();
void frame_kolrow_to_XY (int kol , int row, int length, int Char);
private:
uint16_t FRAME_X; uint16_t FRAME_Y; int16_t FRAME_W; int16_t FRAME_H;
uint16_t TXT_X; uint16_t TXT_Y; uint16_t TXT_W; uint16_t TXT_H;//hight
uint16_t PIJL_X; uint16_t PIJL_Y; uint16_t PIJL_W; uint16_t PIJL_H;//hight
uint16_t x; uint16_t y;
uint16_t prev_x=0;uint16_t prev_y=0;
uint16_t x1; uint16_t y1;
//uint16_t Frames [ 2 ][ 4 ];
TFT_eSPI tft;
};
#endif
#include "DISPLAY.h"
#include <TFT_eSPI.h>
Display::Display( TFT_eSPI tft, uint16_t X_[], uint16_t Y_[], uint16_t W_[], uint16_t H_[], uint16_t Type_[], uint16_t Touch_[], bool ToutchStatus_[] ) {
X = X_; Y = Y_; W = W_; H = H_; Type = Type_; Touch = Touch_; ToutchStatus = ToutchStatus_;
};
// Switch position and size
void Display::settxt_Hight(int Hight) {
txt_Hight = Hight;
}
int Display::gettxt_Hight() {
return txt_Hight;
}
void Display::setchr(int chrx) {
chr = chrx;
}
int Display::getchr() {
return chr;
}
void Display :: Prep () {
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLUE);
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setTextDatum(MC_DATUM);
}
//validated
void Display:: setCursor ( uint16_t x, uint16_t y) {
x1 = x * chr;
y1 = y * txt_Hight;
}
//validated
void Display:: setCursorPixels ( uint16_t x, uint16_t y) {
x1 = x;
y1 = y;
}
//validated
void Display:: print (String txt) {
int txt_With = txt.length() * chr;
tft.fillRect(x1, y1, txt_With, txt_Hight, TFT_BLACK);
tft.drawRect(x1, y1, txt_With, txt_Hight, TFT_BLACK);
tft.setTextSize(2);
tft.setTextDatum(TL_DATUM);
tft.drawString(txt, x1, y1);
}
//validated
void Display:: print (String txt, int length) {
int txt_With = length * chr;
tft.fillRect(x1, y1, txt_With, txt_Hight, TFT_BLACK);
tft.drawRect(x1, y1, txt_With, txt_Hight, TFT_BLACK);
tft.setTextSize(2);
tft.setTextDatum(TL_DATUM);
tft.drawString(txt, x1, y1);
}
//validated
void Display::drawXYBlackSpot_V1 (uint16_t &x, uint16_t &y) {
int row = 14; int kol = 15;
if (getTouch(x, y)) {
if (!(prev_x == 0)) {
drawBlueSpot(prev_x, prev_y);
}
drawBlackSpot(x, y);
tft.fillCircle(x, y, 2, TFT_BLACK);
String tekstx = String( x); String teksty = String( y);
setCursor ( kol, 14); print("x="); setCursor (kol + 3, 14); print(tekstx, 3);
setCursor (kol + 10, 14); print("y="); setCursor (kol + 13, 14); print(teksty, 3);
prev_x = x; prev_y = y;
}
}
//validated
void Display:: clearDisplay () {
tft.fillScreen(TFT_BLUE);
}
//validated
bool Display:: getTouch(uint16_t &x , uint16_t &y) {
bool touched = tft.getTouch(&x1, &y1);
x = x1; y = y1;
return touched;
}
void Display::setFrame (int i, uint16_t x, uint16_t y, uint16_t w, uint16_t h , uint16_t type) {
if (i > i_max) {
i_max = i;
};
X[i] = x ; Y[i] = y; W[i] = w; H [i] = h ; Type [i] = type ;
}
void Display::setFrame_V1 ( uint16_t x, uint16_t y, uint16_t w, uint16_t h , uint16_t type) {
X[i_current] = x ; Y[i_current] = y; W[i_current] = w; H [i_current] = h ; Type [i_current] = type ;
if (i_current > i_max) {
i_max = i_current;
};
i_current = i_current + 1;
}
void Display :: touchFrames () {
uint16_t x; uint16_t y;
bool touched = getTouch(x, y);// nu
for (int i_touch = 0; i_touch <= i_max; i_touch++) {
if (touched) {
if (((x > X[i_touch]) && (x < (X[i_touch] + W[i_touch]))) && ((y > Y[i_touch]) && (y <= (Y[i_touch] + H[i_touch])))) {
if (Type[i_touch] == 0) {
Touch[i_touch] = 1;
i_touch_save = i_touch;
String tekstx = String( x); String teksty = String( y);
int row = 14; int kol = 1;
setCursor ( kol, 14); print("x="); setCursor (kol + 3, 14); print(tekstx, 3);
setCursor (kol + 10, 14); print("y="); setCursor (kol + 13, 14); print(teksty, 3);
setCursor (kol + 16, 14); print("i="); setCursor (kol + 18, 14); print(String(i_touch), 3);
setCursor (kol + 22, 14); print("type="); setCursor (kol + 28, 14); print(String(Type[i_touch]), 3);
} else {
Touch[i_touch] = 1; ToutchStatus[Type[i_touch]] = Touch[i_touch];
}
}
}
}
}
bool Display::ifButton(uint16_t type) {
if (ToutchStatus[type] == true) {
ToutchStatus[type] = false;
return true;
}
}
int Display :: getIframe() {
return i_touch_save;
}
void Display::drawXYBlackSpot_V2 (uint16_t &x, uint16_t &y) {
//uint16_t x; uint16_t y;
int row = 14; int kol = 20;
if (getTouch(x, y)) {
if (!(prev_x == 0)) {
drawBlueSpot(prev_x, prev_y);
}
drawBlackSpot(x, y);
tft.fillCircle(x, y, 2, TFT_BLACK);
String tekstx = String( x); String teksty = String( y);
setCursor ( kol, 14); print("x="); setCursor (kol + 3, 14); print(tekstx, 3);
setCursor (kol + 10, 14); print("y="); setCursor (kol + 13, 14); print(teksty, 3);
prev_x = x; prev_y = y;
}
}
void getFrameConfirm (uint16_t &X, uint16_t &Y, uint16_t &W, uint16_t &H) {
uint16_t Frame_Confirm_X = X; uint16_t Frame_Confirm_Y = Y; uint16_t Frame_Confirm_W = W; uint16_t Frame_Confirm_H = H;
}
void Display:: drawFrame (uint16_t X, uint16_t Y, uint16_t W, uint16_t H) {
tft.fillRect(X, Y, 20 * chr, txt_Hight, TFT_BLACK);
tft.drawRect(X, Y, 20 * chr, txt_Hight, TFT_BLACK);
//tft.fillRect(X, Y, W, H, TFT_BLACK);
// tft.drawRect(X, Y, W, H, TFT_BLACK);
}
void Display:: drawBlackSpot (uint16_t x , uint16_t y) {
tft.fillCircle(x, y, 2, TFT_BLACK);
}
void Display:: drawBlueSpot(uint16_t x , uint16_t y) {
tft.fillCircle(x, y, 2, TFT_BLUE);
}
The test main loop
#include <TFT_eSPI.h> // Hardware-specific library
//// https://www.youtube.com/watch?v=WFVjsxFMbSM
#include "FS.h"
#include <SPI.h>
#include "Display.h"
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#include "PARAMETERS.h"
#define TYPE_FIELD 0
#define TYPE_BUTTON_ONE 1 //frame touched, button one, set a flag red button to touch
#define TYPE_BUTTON_TWO 2 //frame confirm, butten two, confirms the input
#define TYPE_BUTTON_THREE 3 //frame back button tree, errase the input
#define TYPE_BUTTON_FOUR 4 //fram execute button four, execute the input
//
int current_Status = 0;
//Total frame of one row
int row ; int kol; int length = 20;
long int ts1 = 0;
uint16_t X[100]; uint16_t Y[100]; uint16_t W[100]; uint16_t H[100 ]; uint16_t Type[100]; uint16_t Touch[100]; bool ToutchStatus[] = {false, false, false, false, false};
Display display(tft, X, Y, W, H, Type, Touch, ToutchStatus);
uint16_t FRAME_X; uint16_t FRAME_Y; uint16_t FRAME_W; uint16_t FRAME_H; uint16_t FRAME_Type; uint16_t FRAME_Touch;
int txt_Hight = display.gettxt_Hight();
int chr = display.getchr();
uint16_t prev_x = 0; uint16_t prev_y = 0;
void drawScreen() {
// put your main code here, to run repeatedly:
for ( int i = 0; i <= Menue_Size; ++i ) {
row = i; kol = 1;
String tekstx = String( i); String teksty = String( i);
display.setCursor ( kol, row); display.print("x="); display.setCursor (kol + 2, row); display.print(tekstx, 3);
display.setCursor (kol + 5, row); display.print("y="); display.setCursor (kol + 8, row); display.print(teksty, 3);
FRAME_X = 1 ; FRAME_Y = row * txt_Hight ; FRAME_W = chr * length; FRAME_H = txt_Hight; FRAME_Type = TYPE_FIELD;
display.setFrame_V1 ( FRAME_X, FRAME_Y, FRAME_W, FRAME_H, FRAME_Type);
}
drawButtons();
for ( int i = 0; i <= 12; ++i ) {
row = i;
FRAME_X = X[row] ; FRAME_Y = Y[row]; FRAME_W = W[row]; FRAME_H = H[row]; FRAME_Type = Type[row];
display.setCursor (20, row); display.print(String(FRAME_X), 3);
display.setCursor (24, row); display.print(String(FRAME_Y), 3);
display.setCursor (28, row); display.print(String(FRAME_W), 3);
display.setCursor (32, row); display.print(String(FRAME_H), 3);
display.setCursor (36, row); display.print(String(FRAME_Type), 1);
display.setCursor (38, row); display.print(String(FRAME_Touch), 1);
}
}
void drawButtons() {
kol = 0; row = 13; String txt;
display.setCursor ( kol, row); txt = "pic"; display.print(txt, txt.length());
FRAME_X = kol * chr ; FRAME_Y = row * txt_Hight ; FRAME_W = chr * txt.length(); FRAME_H = txt_Hight; FRAME_Type = TYPE_BUTTON_ONE;
display.setFrame_V1 ( FRAME_X, FRAME_Y, FRAME_W, FRAME_H, FRAME_Type);
kol = kol + txt.length() + 1; row = 13;
display.setCursor ( kol, row); txt = "conf"; display.print(txt, txt.length());
FRAME_X = kol * chr ; FRAME_Y = row * txt_Hight ; FRAME_W = chr * txt.length(); FRAME_H = txt_Hight; FRAME_Type = TYPE_BUTTON_TWO;
display.setFrame_V1 ( FRAME_X, FRAME_Y, FRAME_W, FRAME_H, FRAME_Type);
kol = kol + txt.length() + 1; row = 13;
display.setCursor ( kol, row); txt = "back"; display.print(txt, txt.length());
FRAME_X = kol * chr ; FRAME_Y = row * txt_Hight ; FRAME_W = chr * txt.length(); FRAME_H = txt_Hight; FRAME_Type = TYPE_BUTTON_THREE;
display.setFrame_V1 ( FRAME_X, FRAME_Y, FRAME_W, FRAME_H, FRAME_Type);
kol = kol + txt.length() + 1; row = 13;
display.setCursor ( kol, row); txt = "exe"; display.print(txt, txt.length());
FRAME_X = kol * chr; FRAME_Y = row * txt_Hight ; FRAME_W = chr * txt.length(); FRAME_H = txt_Hight; FRAME_Type = TYPE_BUTTON_FOUR;
display.setFrame_V1 ( FRAME_X, FRAME_Y, FRAME_W, FRAME_H, FRAME_Type);
}
int returnmkol (int i, int MiOne[][2]) {
return MiOne [i][0];
}
int returnmrow (int i, int MiOne[][2]) {
return MiOne [i][1];
}
int returnakol (int i, int AiOne[][2]) {
return AiOne [i][0];
}
int returnarow (int i, int AiOne[][2]) {
return AiOne [i][1];
}
void setup() {
Serial.begin(115200);
display.Prep();
row = 0; kol = 0;
}
void loop() {
uint16_t x, y; int j; int i_frame;
ts1 = micros(); //time stamp
switch (current_Status) {
case 0: {
drawScreen();
}
}
Beta Was this translation helpful? Give feedback.
All reactions