JayzonvanOeve
Published

2D-Pager: write, draw, play in real time

Send handwritten or drawn messages in real time with this device. This is a 2D-Pager with 4" TFT touch screen.

IntermediateProtip3 hours125
2D-Pager: write, draw, play in real time

Story

Read more

Code

2D-Pager

C/C++
Code
#include "FS.h"
#include <SPI.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();

//#include <SoftwareSerial.h>

//SoftwareSerial mySerial(16, 17); // RX, TX


#define CALIBRATION_FILE "/calibrationData1"

void setup(void) {
  uint16_t calibrationData[5];
  uint8_t calDataOK = 0;

  Serial.begin(115200);
  Serial2.begin(115200);

// mySerial.begin(38400);

  
  Serial.println("starting");

  tft.init();
  
//  SPI.begin();
//  SPI.setFrequency(1000000);

  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);

  tft.setCursor(20, 0, 2);
  tft.setTextColor(TFT_BLACK, TFT_WHITE);  tft.setTextSize(1);
  tft.println("calibration run");


  // check file system
  if (!SPIFFS.begin()) {
    Serial.println("formating file system");

    SPIFFS.format();
    SPIFFS.begin();
  }

  // check if calibration file exists
  if (SPIFFS.exists(CALIBRATION_FILE)) {
    File f = SPIFFS.open(CALIBRATION_FILE, "r");
    if (f) {
      if (f.readBytes((char *)calibrationData, 14) == 14)
        calDataOK = 1;
      f.close();
    }
  }
  if (calDataOK) {
    // calibration data valid
    tft.setTouch(calibrationData);
  } else {
    // data not valid. recalibrate
    tft.calibrateTouch(calibrationData, TFT_WHITE, TFT_RED, 15);
    // store data
    File f = SPIFFS.open(CALIBRATION_FILE, "w");
    if (f) {
      f.write((const unsigned char *)calibrationData, 14);
      f.close();
    }
  }

  tft.fillScreen(TFT_WHITE);

}


  static uint16_t color;
  uint16_t x, y, xold, yold, r;
  int inputcounter = 0, xin, yin, oldxin, oldyin;
  int touched_status=0, touched_latch=0, rtouched_status=0, rtouched_latch=0, reciever_loop_counter=0;
  
  char message[] = "",message_byte, message_segment[] = "";
    
void loop() 
{




      
  if (Serial2.available()) 
  {                                                         // If anything comes in Serial1 (pins 0 & 1)
      message_byte=char(Serial2.read());                    // input-Format:   T x208 y214 N
      Serial.print(message_byte);  Serial.print("");                  
      if(message_byte =='x')xin=Serial2.parseInt();         ////// Erst Nachricht einlesen, dann auswerten...           
      if(message_byte =='y')yin=Serial2.parseInt();         // jeder 2. Loopdurchgang werden er x und y aktuell sein
          
        if(message_byte =='N')
      {
        rtouched_status=0;            Serial.println("  rtouched_status=0  ");
        reciever_loop_counter=0;
        rtouched_latch=0;
      }  
         
      if(message_byte =='T')
      {    
        rtouched_status=1;             Serial.println("  rtouched_status=1  ");
        rtouched_latch=0;             Serial.println("  rtouched_latch=0  ");
      }      

      if(reciever_loop_counter>2 && reciever_loop_counter%2==0)           // jeder 2. Loopdurchgang werden er x und y aktuell sein, darum %2
      {                    
        if (rtouched_status==1)                   
        {
          if(rtouched_latch==0)
          {
              oldxin=xin;
              oldyin=yin;
              rtouched_latch=1;
          }
            tft.drawLine(oldxin*2, oldyin*2, xin*2, yin*2, TFT_RED);        Serial.println("  draw Line  "); 
        }    
        if (rtouched_status==0)
        { 
            tft.drawPixel(xin*2, yin*2, TFT_RED);       Serial.print("  draw Pixel  ");           
        }   
        oldxin=xin;
        oldyin=yin;                          

      }   

         
    reciever_loop_counter++;      
  }


/*
  if (tft.getTouch(&x, &y)) 
  {    
    if(touched_status==1)  
      { 
        tft.drawLine(xold, yold, x, y, TFT_BLACK);                
      } 
       
    if(touched_status==0)    
      {
        if(touched_latch==0)
        {  
          touched_latch=1;
        }
        tft.drawPixel(x, y, TFT_RED);
        touched_status=1;    
      }      
        xold = x;
        yold = y;
  }
  else 
  {
    touched_status=0;
    
    if(touched_latch==1)
    {  
      touched_latch=0;
    }
  }
*/



















  

       //intx = atoi(123\0)
/*
      for (int i = 0; i < message.length(); i++)
      {
      Serial.print(message[i]);
      }  
*/  

  if (tft.getTouch(&x, &y)) 
  {
//       tft_draw();
        
       inputcounter++;
     
    if(touched_status==1)  
      { 
        tft.drawLine(xold, yold, x, y, TFT_BLUE);
                      
      } 
       
    if(touched_status==0)    
      {
        if(touched_latch==0)
        {  
          Serial.print(" T");
          Serial2.write("T"); 
          touched_latch=1;
        }
        tft.drawPixel(x, y, TFT_BLUE);
        touched_status=1;    
      }  
        
//        Serial.print(" x");Serial.print(lowByte(x));Serial.print(" y");Serial.print(lowByte(y));
//        delay(10);
        Serial2.write("x"); 
        Serial2.print(lowByte(x/2)); Serial2.print("\0"); 
        Serial2.write("y"); 
        Serial2.print(lowByte(y/2)); Serial2.print("\0"); 
              
        xold = x;
        yold = y;
        
        color += 200;
  }
  else 
  {
    touched_status=0;
    if(touched_latch==1)
    {  
      Serial.println(" N");
      Serial2.write("N\n"); 
      touched_latch=0;



 /*      
      Serial.print("Länge der nachricht: ");Serial.print(message.length());     
      for (int i = 0; i < message.length(); i++)
      {
      Serial.print(message[i]);
      }
      
      Serial.println();
    
      for (int i = 0; i < message.length(); i++)
      {
        Serial2.write(message[i]);   // Push each char 1 by 1 on each loop pass
        delay(5);
      }

      message="";
      Serial.print("Länge der nachricht: ");Serial.print(message.length()); 
 */  
    }
/*      
      Serial.print("Länge der nachricht: ");Serial.println(message.length()); 
      for (int i = 0; i < message.length(); i++)
        {
          Serial2.write(message[i]);
          Serial.write(message[i]);Serial.write(" ");
        }
      message="";
 */   
  }


}


void tft_draw()
{
  tft.setCursor(5, 5, 2);
  tft.printf("x: %i     ", x);
  tft.setCursor(5, 20, 2);
  tft.printf("y: %i    ", y);
}

Credits

JayzonvanOeve

JayzonvanOeve

3 projects • 4 followers

Comments