Friday, June 17, 2016

Arduino Mega read string from Serial, display on 3.2" 480 x 320 TFT LCD Shield


Arduino example run on Mega 2560, read string from Serial port, and display on 3.2" 480 x 320 TFT LCD Shield usin UTFT library. The sting is limited to 30 characters on each line.


Mega_UTFT_SerialRead.ino
/*
 * Example run on Arduino Mega 2560 to read string from Serial,
 * then display on 3.2" 480 x 320 TFT LCD Shield
 * 
 * http://arduino-er.blogspot.com/search/label/3.2%22%20480%20x%20320%20TFT%20LCD%20Shield
*/

#include <UTFT.h>

extern uint8_t BigFont[];

UTFT myGLCD(CTE32HR,38,39,40,41);

const int NumOfRow = 20;
const int HeightOfRow = 16;
const int CharPerRow = 30;

String buffer[NumOfRow];

void setup()
{
  Serial.begin(57600);
  myGLCD.InitLCD();
  myGLCD.clrScr();

  myGLCD.setColor(255, 255, 255);
  myGLCD.setBackColor(0, 0, 0);

  myGLCD.setFont(BigFont);

  myGLCD.print("Open Serial Monitor,", LEFT, 0);
  myGLCD.print("to enter something.", LEFT, HeightOfRow);
  
  initBuffer();
}

void loop()
{
  String stringIn = Serial.readStringUntil('\n');
  Serial.print(".");
  if(!stringIn.equals("")){
    Serial.print("*");
    String stringToIns = stringIn.substring(0, CharPerRow);
    insert(stringToIns);
    Serial.println(stringToIns);
    printBuffer();
  }
  
}

void initBuffer(){
  for(int i=0; i<NumOfRow; i++){
    buffer[i] = "";
  }
}

void insert(String ins){
  for(int i=0; i<NumOfRow-1; i++){
    buffer[i] = buffer[i+1];
  }
  buffer[NumOfRow-1] = ins;
}

void printBuffer(){
  myGLCD.clrScr();
  for(int i=0; i<NumOfRow; i++){
    myGLCD.print(buffer[i], LEFT, i*HeightOfRow);
  }
}



No comments:

Post a Comment