acidportal

- 😈 Worlds smallest Evil Portal on a LilyGo T-QT
git clone git://git.acid.vegas/acidportal.git
Log | Files | Refs | Archive | README | LICENSE

TFT_Terminal.ino (5862B)

      1 
      2 /*************************************************************
      3   This sketch implements a simple serial receive terminal
      4   program for monitoring serial debug messages from another
      5   board.
      6   
      7   Connect GND to target board GND
      8   Connect RX line to TX line of target board
      9   Make sure the target and terminal have the same baud rate
     10   and serial stettings!
     11 
     12   The sketch works with the ILI9341 TFT 240x320 display and
     13   the called up libraries.
     14   
     15   The sketch uses the hardware scrolling feature of the
     16   display. Modification of this sketch may lead to problems
     17   unless the ILI9341 data sheet has been understood!
     18 
     19   Updated by Bodmer 21/12/16 for TFT_eSPI library:
     20   https://github.com/Bodmer/TFT_eSPI
     21   
     22   BSD license applies, all text above must be included in any
     23   redistribution
     24  *************************************************************/
     25 
     26 #include <TFT_eSPI.h> // Hardware-specific library
     27 #include <SPI.h>
     28 
     29 TFT_eSPI tft = TFT_eSPI();       // Invoke custom library
     30 
     31 // The scrolling area must be a integral multiple of TEXT_HEIGHT
     32 #define TEXT_HEIGHT 16 // Height of text to be printed and scrolled
     33 #define BOT_FIXED_AREA 0 // Number of lines in bottom fixed area (lines counted from bottom of screen)
     34 #define TOP_FIXED_AREA 16 // Number of lines in top fixed area (lines counted from top of screen)
     35 #define YMAX 320 // Bottom of screen area
     36 
     37 // The initial y coordinate of the top of the scrolling area
     38 uint16_t yStart = TOP_FIXED_AREA;
     39 // yArea must be a integral multiple of TEXT_HEIGHT
     40 uint16_t yArea = YMAX-TOP_FIXED_AREA-BOT_FIXED_AREA;
     41 // The initial y coordinate of the top of the bottom text line
     42 uint16_t yDraw = YMAX - BOT_FIXED_AREA - TEXT_HEIGHT;
     43 
     44 // Keep track of the drawing x coordinate
     45 uint16_t xPos = 0;
     46 
     47 // For the byte we read from the serial port
     48 byte data = 0;
     49 
     50 // A few test variables used during debugging
     51 bool change_colour = 1;
     52 bool selected = 1;
     53 
     54 // We have to blank the top line each time the display is scrolled, but this takes up to 13 milliseconds
     55 // for a full width line, meanwhile the serial buffer may be filling... and overflowing
     56 // We can speed up scrolling of short text lines by just blanking the character we drew
     57 int blank[19]; // We keep all the strings pixel lengths to optimise the speed of the top line blanking
     58 
     59 void setup() {
     60   // Setup the TFT display
     61   tft.init();
     62   tft.setRotation(0); // Must be setRotation(0) for this sketch to work correctly
     63   tft.fillScreen(TFT_BLACK);
     64   
     65   // Setup baud rate and draw top banner
     66   Serial.begin(9600);
     67   
     68   tft.setTextColor(TFT_WHITE, TFT_BLUE);
     69   tft.fillRect(0,0,240,16, TFT_BLUE);
     70   tft.drawCentreString(" Serial Terminal - 9600 baud ",120,0,2);
     71 
     72   // Change colour for scrolling zone text
     73   tft.setTextColor(TFT_WHITE, TFT_BLACK);
     74 
     75   // Setup scroll area
     76   setupScrollArea(TOP_FIXED_AREA, BOT_FIXED_AREA);
     77 
     78   // Zero the array
     79   for (byte i = 0; i<18; i++) blank[i]=0;
     80 }
     81 
     82 
     83 void loop(void) {
     84   //  These lines change the text colour when the serial buffer is emptied
     85   //  These are test lines to see if we may be losing characters
     86   //  Also uncomment the change_colour line below to try them
     87   //
     88   //  if (change_colour){
     89   //  change_colour = 0;
     90   //  if (selected == 1) {tft.setTextColor(TFT_CYAN, TFT_BLACK); selected = 0;}
     91   //  else {tft.setTextColor(TFT_MAGENTA, TFT_BLACK); selected = 1;}
     92   //}
     93 
     94   while (Serial.available()) {
     95     data = Serial.read();
     96     // If it is a CR or we are near end of line then scroll one line
     97     if (data == '\r' || xPos>231) {
     98       xPos = 0;
     99       yDraw = scroll_line(); // It can take 13ms to scroll and blank 16 pixel lines
    100     }
    101     if (data > 31 && data < 128) {
    102       xPos += tft.drawChar(data,xPos,yDraw,2);
    103       blank[(18+(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT)%19]=xPos; // Keep a record of line lengths
    104     }
    105     //change_colour = 1; // Line to indicate buffer is being emptied
    106   }
    107 }
    108 
    109 // ##############################################################################################
    110 // Call this function to scroll the display one text line
    111 // ##############################################################################################
    112 int scroll_line() {
    113   int yTemp = yStart; // Store the old yStart, this is where we draw the next line
    114   // Use the record of line lengths to optimise the rectangle size we need to erase the top line
    115   tft.fillRect(0,yStart,blank[(yStart-TOP_FIXED_AREA)/TEXT_HEIGHT],TEXT_HEIGHT, TFT_BLACK);
    116 
    117   // Change the top of the scroll area
    118   yStart+=TEXT_HEIGHT;
    119   // The value must wrap around as the screen memory is a circular buffer
    120   if (yStart >= YMAX - BOT_FIXED_AREA) yStart = TOP_FIXED_AREA + (yStart - YMAX + BOT_FIXED_AREA);
    121   // Now we can scroll the display
    122   scrollAddress(yStart);
    123   return  yTemp;
    124 }
    125 
    126 // ##############################################################################################
    127 // Setup a portion of the screen for vertical scrolling
    128 // ##############################################################################################
    129 // We are using a hardware feature of the display, so we can only scroll in portrait orientation
    130 void setupScrollArea(uint16_t tfa, uint16_t bfa) {
    131   tft.writecommand(ILI9341_VSCRDEF); // Vertical scroll definition
    132   tft.writedata(tfa >> 8);           // Top Fixed Area line count
    133   tft.writedata(tfa);
    134   tft.writedata((YMAX-tfa-bfa)>>8);  // Vertical Scrolling Area line count
    135   tft.writedata(YMAX-tfa-bfa);
    136   tft.writedata(bfa >> 8);           // Bottom Fixed Area line count
    137   tft.writedata(bfa);
    138 }
    139 
    140 // ##############################################################################################
    141 // Setup the vertical scrolling start address pointer
    142 // ##############################################################################################
    143 void scrollAddress(uint16_t vsp) {
    144   tft.writecommand(ILI9341_VSCRSADD); // Vertical scrolling pointer
    145   tft.writedata(vsp>>8);
    146   tft.writedata(vsp);
    147 }
    148