ILI9341_t3n.h problem on SPI1

Status
Not open for further replies.

peter247

Member
IMG-0225.jpg

I've been trying to get the ILI9341 and touch screen to work for weeks and finally got it working with a slight problem.
My problem is that redrawing a button corrupts it , this does not happen using default SPI.
I think my problem could be that SPI1 is not using the in-chip pull out resistor and I've been told to set them up which I haven't done so far.



Code:
#include <ILI9341_t3n.h>
#include <SPI.h>
// XPT2046_Touchscreen from git hub 
#include <XPT2046_Touchscreen.h>
#include <Arduino.h>
#include <stdlib.h>

#define use_spi1

#ifdef use_spi1
// SPI1 Defines
#define TOUCH_CS 37 
#define TFT_DC   9
#define TFT_CS   0
#define TFT_MOSI 26
#define TFT_SCLK 27
#define TFT_MISO 39
#define TFT_RST 255
ILI9341_t3n tft = ILI9341_t3n( TFT_CS , TFT_DC, TFT_RST, TFT_MOSI, TFT_SCLK, TFT_MISO );
#else
// SPI default Defines
#define TOUCH_CS 8 
#define TFT_DC   9
#define TFT_CS   10
#define TFT_MOSI 11
#define TFT_SCLK 13
#define TFT_MISO 12
#define TFT_RST 255
ILI9341_t3n tft = ILI9341_t3n( TFT_CS , TFT_DC );
#endif

char keyLabel[15][5] = {"Del", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Ent" };
char Blank[] = {"          "};
uint8_t Screen_no;
boolean Back_light = true;
#define NUM_LEN 12
char numberBuffer[NUM_LEN + 1] = "";
uint8_t numberIndex = 0;
int password = 123456;
boolean Draw_static;

Adafruit_GFX_Button key[15];
XPT2046_Touchscreen ts(TOUCH_CS); 

#define Keypad 0
#define Admin 1

void setup() {
  // put your setup code here, to run once:
    tft.begin();
    #ifdef use_spi1
    ts.begin(SPI1);
    #else 
    ts.begin();
    #endif
    tft.fillScreen(ILI9341_BLACK);
    tft.setRotation(1); 
    Keypad_static();
}

void loop() 
{
  // put your main code here, to run repeatedly:
if ( Screen_no == Keypad && Draw_static == true ) Keypad_static();
if ( Screen_no == Keypad && Back_light == true ) Check_keypad();
}

void Keypad_static(void)
{
     uint8_t xc = 28; // half of width
     uint8_t yc = 22;
     key[0].initButton (&tft,170+xc,180+yc,60,45,ILI9341_WHITE,ILI9341_RED,ILI9341_WHITE,keyLabel[0],2,2);   
     key[11].initButton(&tft,245+xc,180+yc,60,45,ILI9341_WHITE,ILI9341_YELLOW,ILI9341_DARKGREY,keyLabel[11],2,2);    
     key[1].initButton (&tft,20+xc,70+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[1],3,3);
     key[2].initButton (&tft,95+xc,70+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[2],3,3);
     key[3].initButton (&tft,170+xc,70+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[3],3,3);
     key[4].initButton (&tft,245+xc,70+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[4],2,2);
     key[5].initButton (&tft,20+xc,125+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[5],2,2);
     key[6].initButton (&tft,95+xc,125+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[6],2,2);
     key[7].initButton (&tft,170+xc,125+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[7],2,2);
     key[8].initButton (&tft,245+xc,125+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[8],2,2);
     key[9].initButton (&tft,20+xc,180+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[9],2,2);
     key[10].initButton(&tft,95+xc,180+yc,60,45,ILI9341_WHITE,ILI9341_BLUE,ILI9341_WHITE,keyLabel[10],2,2);
 
     for (uint8_t b = 0; b < 12; b++)
      { key[b].drawButton();} 
 
     Display_password(Blank);  
     Draw_static = false; // only draw once
}

void Display_password(char *keytext)
{
      tft.setCursor(70,10);
      tft.setTextSize(3);
      tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK); 
      tft.println(keytext);
}

void Check_keypad(void) 
{
int t_x = 0, t_y = 0;
boolean pressed = Pressed( t_x , t_y );
    
  for (uint8_t b = 0; b < 12; b++) 
  {
     if (pressed && key[b].contains(t_x, t_y)) 
     { key[b].press(true); } // tell the button it is pressed
     else 
     { key[b].press(false); } // tell the button it is NOT pressed
  } // for next

  for (uint8_t b = 0; b < 12; b++)
    {
    if (key[b].justReleased()) 
      key[b].drawButton();     // draw normal
    if (key[b].justPressed())
    {
      key[b].drawButton(true);  // draw invert
   if (( b > 0 ) && (b < 11 )) // Button 0 to 9
    {
          if ( numberIndex < 7 ) 
          {
              numberBuffer[numberIndex] = keyLabel[b][0];
              numberIndex++;
          }
    }
    else if (b == 0) // delete
    {
          if ( numberIndex > 0 )
          {
              numberIndex --;
              numberBuffer[numberIndex]= 0;
              Display_password(Blank); 
          }
    }
    else if (b == 11) //enter
    {
          int num = atoi(numberBuffer);        
          if ( num == password )
          {
            tft.fillScreen(ILI9341_BLACK);
            Screen_no = Admin ;  // should switch to admin after
            Draw_static = true;
            numberIndex = 0;
              for (uint8_t i = 0; i < 8; i++)
              {
                numberBuffer[i] = 0;
              }
          }
    }
        if (Screen_no == Keypad) Display_password(numberBuffer); 
        delay(50);
    }// just pressed
  }
 
}// check_keypad

boolean Pressed(int &x , int &y)
{
boolean pressed;
  if (ts.touched())
  { 
    TS_Point p = ts.getPoint();
    x = ( 3600 - ( p.x - 300 ))/10.9;
    y = ( 3600 - (  p.y - 240 ))/ 15;
    pressed = true;  
  }
  else
  { pressed = false; }
   return pressed ;
}
 
Status
Not open for further replies.
Back
Top