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