Skip to content

Commit 7765ec2

Browse files
committed
Added scroll example
1 parent a77e4ae commit 7765ec2

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

examples/scrolltest/scrolltest.ino

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* This sketch demonstrates how to use the hardware scrolling on a portion of the screen.
3+
* It performs a basic WiFi scan to generate some terminal-like animation.
4+
* Courtesy of tobozo (Dec.2018)
5+
*
6+
*/
7+
8+
9+
#include <WiFi.h>
10+
#include <Adafruit_GFX.h> // Core graphics library
11+
#include "WROVER_KIT_LCD.h"
12+
WROVER_KIT_LCD tft;
13+
14+
// this only works in portrait mode (orientation=0 or 3)
15+
uint16_t height = tft.height();// (=320)
16+
uint16_t width = tft.width();// (=240)
17+
18+
// scroll control variables
19+
uint16_t scrollTopFixedArea = 40; // top fixed area
20+
uint16_t scrollBottomFixedArea = 40; // bottom fixed area
21+
uint16_t yStart = scrollTopFixedArea;
22+
uint16_t yArea = height - scrollTopFixedArea - scrollBottomFixedArea;
23+
uint16_t w_tmp, h_tmp;
24+
int16_t x1_tmp, y1_tmp;
25+
int scrollPosY = scrollTopFixedArea; // keeps track of the cursor position
26+
int scrollPosX = -1;
27+
28+
String output;
29+
30+
31+
int scrollText(const char* str) {
32+
if (scrollPosY == -1) {
33+
scrollPosY = tft.getCursorY();
34+
}
35+
scrollPosX = tft.getCursorX();
36+
if (scrollPosY >= (height - scrollBottomFixedArea)) {
37+
scrollPosY = (scrollPosY % (height - scrollBottomFixedArea)) + scrollTopFixedArea;
38+
}
39+
tft.getTextBounds(str, scrollPosX, scrollPosY, &x1_tmp, &y1_tmp, &w_tmp, &h_tmp);
40+
if (scrollPosX == 0) {
41+
tft.fillRect(0, scrollPosY, width, h_tmp, WROVER_BLACK);
42+
} else { // fill the horizontal gap
43+
tft.fillRect(0, scrollPosY, w_tmp, h_tmp, WROVER_BLACK);
44+
}
45+
tft.setCursor(scrollPosX, scrollPosY);
46+
doScroll(h_tmp, 5); // Scroll lines, 5ms per line
47+
tft.print(str);
48+
scrollPosY = tft.getCursorY();
49+
return h_tmp;
50+
}
51+
52+
53+
int doScroll(int lines, int wait) {
54+
int yTemp = yStart;
55+
scrollPosY = -1;
56+
for (int i = 0; i < lines; i++) {
57+
yStart++;
58+
if (yStart == height - scrollBottomFixedArea) yStart = scrollTopFixedArea;
59+
tft.scrollTo(yStart);
60+
delay(wait);
61+
}
62+
return yTemp;
63+
}
64+
65+
66+
void setup() {
67+
Serial.begin(115200);
68+
tft.begin();
69+
tft.setRotation( 0 ); // portrait mode is required
70+
tft.setTextColor(WROVER_YELLOW);
71+
tft.fillScreen(WROVER_DARKGREY);
72+
tft.setCursor(100, 10);
73+
tft.print("HEADER");
74+
tft.setCursor(100, 300);
75+
tft.print("FOOTER");
76+
tft.setCursor(0, scrollTopFixedArea);
77+
// setup the scrollable area
78+
tft.setupScrollArea(scrollTopFixedArea, scrollBottomFixedArea);
79+
// clear it
80+
tft.fillRect(0, scrollTopFixedArea, width, yArea, WROVER_BLACK);
81+
82+
WiFi.mode(WIFI_STA);
83+
WiFi.disconnect();
84+
delay(100);
85+
Serial.println("Setup done");
86+
}
87+
88+
void loop() {
89+
90+
scrollText("WiFi Scan Start\n");
91+
int n = WiFi.scanNetworks();
92+
scrollText("WiFi Scan done\n");
93+
94+
if (n == 0) {
95+
scrollText("no networks found\n");
96+
} else {
97+
output = String(n) + " networks found\n";
98+
scrollText(output.c_str());
99+
for (int i = 0; i < n; ++i) {
100+
output = String(i+1) + ":" + WiFi.SSID(i) + " (" +WiFi.RSSI(i)+") " + ((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*") + "\n";
101+
scrollText(output.c_str());
102+
Serial.println(output);
103+
delay(10);
104+
}
105+
}
106+
107+
delay(5000);
108+
}

0 commit comments

Comments
 (0)