Skip to content

Commit ad7adeb

Browse files
committed
Added examples
1 parent 06ac0e3 commit ad7adeb

File tree

16 files changed

+1116
-1
lines changed

16 files changed

+1116
-1
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@ TFT LCD screen. It simplifies the process for drawing shapes, lines, images, and
44
text to the screen. See also
55
[the original TFT library](https://github.com/arduino-libraries/TFT).
66

7+
The Arduino TFT library extends the Adafruit GFX, and ILI9163 libraries that it
8+
is based on. The GFX library is responsible for the drawing routines, while the
9+
ST7735 library is specific to the screen on the Arduino GTFT. The Arduino
10+
specific additions were designed to work as similarly to the Processing API as
11+
possible.
12+
13+
Onboard the screen is a SD card slot, which can be used through the SD library.
14+
The TFT library relies on the SPI library for communication with the screen and
15+
SD card, and needs to be included in all sketches.
16+
17+
* https://github.com/adafruit/Adafruit-GFX-Library
18+
* https://github.com/adafruit/Adafruit-ST7735-Library
19+
* http://www.arduino.cc/en/Reference/SD
20+
* http://www.arduino.cc/en/Reference/SPI
21+
22+
See also:
23+
* [Adafruit GFX Graphics Library](https://learn.adafruit.com/adafruit-gfx-graphics-library)
24+
Processing subset:
25+
* [PImage](https://processing.org/reference/PImage.html)
26+
* [loadImage()](https://processing.org/reference/loadImage_.html)
27+
* [image()](https://processing.org/reference/image_.html)
28+
* [background()](https://processing.org/reference/background_.html)
29+
* [fill()](http://processing.org/reference/fill_.html)
30+
* [noFill()](http://processing.org/reference/noFill_.html)
31+
* [stroke()](http://processing.org/reference/stroke_.html)
32+
* [noStroke()](http://processing.org/reference/noStroke_.html)
33+
* [text()](https://processing.org/reference/text_.html)
34+
* [textSize()](https://processing.org/reference/textSize_.html)
35+
* circle()
36+
* [point()](https://processing.org/reference/point_.html)
37+
* [line()](https://processing.org/reference/line_.html)
38+
* [quad()](https://processing.org/reference/quad_.html)
39+
* [rect()](https://processing.org/reference/rect_.html)
40+
* [triangle()](https://processing.org/reference/triangle_.html)
41+
742
This library requires at least
843
[Arduino IDE](https://www.arduino.cc/en/Main/Software) v1.6.8, where v1.8.3 or
944
newer is recommended.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
3+
Arduino TFT Bitmap Logo example
4+
5+
This example reads an image file from a micro-SD card
6+
and draws it on the screen, at random locations.
7+
8+
In this sketch, the Arduino logo is read from a micro-SD card.
9+
There is a .bmp file included with this sketch.
10+
- open the sketch folder (Ctrl-K or Cmd-K)
11+
- copy the "arduino.bmp" file to a micro-SD
12+
- put the SD into the SD slot of the Arduino TFT module.
13+
14+
This example code is in the public domain.
15+
16+
Created 19 April 2013 by Enrico Gueli
17+
18+
http://www.arduino.cc/en/Tutorial/TFTBitmapLogo
19+
20+
*/
21+
22+
// include the necessary libraries
23+
#include <SPI.h>
24+
#include <SD.h>
25+
#include <LcdScreen.h> // Arduino LCD library
26+
27+
// pin definition for the Uno
28+
#define sd_cs 4
29+
#define lcd_cs 10
30+
#define dc 9
31+
#define rst 8
32+
33+
// pin definition for the Leonardo
34+
//#define sd_cs 8
35+
//#define lcd_cs 7
36+
//#define dc 0
37+
//#define rst 1
38+
39+
LcdScreen TFTscreen = LcdScreen(lcd_cs, dc, rst);
40+
41+
// this variable represents the image to be drawn on screen
42+
PImage logo;
43+
44+
45+
void setup() {
46+
// initialize the GLCD and show a message
47+
// asking the user to open the serial line
48+
TFTscreen.begin();
49+
TFTscreen.background(255, 255, 255);
50+
51+
TFTscreen.stroke(0, 0, 255);
52+
TFTscreen.println();
53+
TFTscreen.println(F("Arduino TFT Bitmap Example"));
54+
TFTscreen.stroke(0, 0, 0);
55+
TFTscreen.println(F("Open serial monitor"));
56+
TFTscreen.println(F("to run the sketch"));
57+
58+
// initialize the serial port: it will be used to
59+
// print some diagnostic info
60+
Serial.begin(9600);
61+
while (!Serial) {
62+
// wait for serial port to connect. Needed for native USB port only
63+
}
64+
65+
// clear the GLCD screen before starting
66+
TFTscreen.background(255, 255, 255);
67+
68+
// try to access the SD card. If that fails (e.g.
69+
// no card present), the setup process will stop.
70+
Serial.print(F("Initializing SD card..."));
71+
if (!SD.begin(sd_cs)) {
72+
Serial.println(F("failed!"));
73+
return;
74+
}
75+
Serial.println(F("OK!"));
76+
77+
// initialize and clear the GLCD screen
78+
TFTscreen.begin();
79+
TFTscreen.background(255, 255, 255);
80+
81+
// now that the SD card can be access, try to load the
82+
// image file.
83+
logo = TFTscreen.loadImage("arduino.bmp");
84+
if (!logo.isValid()) {
85+
Serial.println(F("error while loading arduino.bmp"));
86+
}
87+
}
88+
89+
void loop() {
90+
// don't do anything if the image wasn't loaded correctly.
91+
if (logo.isValid() == false) {
92+
return;
93+
}
94+
95+
Serial.println(F("drawing image"));
96+
97+
// get a random location where to draw the image.
98+
// To avoid the image to be draw outside the screen,
99+
// take into account the image size.
100+
int x = random(TFTscreen.width() - logo.width());
101+
int y = random(TFTscreen.height() - logo.height());
102+
103+
// draw the image to the screen
104+
TFTscreen.image(logo, x, y);
105+
106+
// wait a little bit before drawing again
107+
delay(1500);
108+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
3+
TFT Color Picker
4+
5+
This example for the Arduino screen reads the input of
6+
potentiometers or analog sensors attached to A0, A1,
7+
and A2 and uses the values to change the screen's color.
8+
9+
This example code is in the public domain.
10+
11+
Created 15 April 2013 by Scott Fitzgerald
12+
13+
http://www.arduino.cc/en/Tutorial/TFTColorPicker
14+
15+
*/
16+
17+
// pin definition for the Uno
18+
#define cs 10
19+
#define dc 9
20+
#define rst 8
21+
22+
// pin definition for the Leonardo
23+
// #define cs 7
24+
// #define dc 0
25+
// #define rst 1
26+
27+
#include <LcdScreen.h> // Arduino LCD library
28+
#include <SPI.h>
29+
30+
LcdScreen TFTscreen = LcdScreen(cs, dc, rst);
31+
32+
void setup() {
33+
// begin serial communication
34+
Serial.begin(9600);
35+
36+
// initialize the display
37+
TFTscreen.begin();
38+
39+
// set the background to white
40+
TFTscreen.background(255, 255, 255);
41+
42+
}
43+
44+
void loop() {
45+
46+
// read the values from your sensors and scale them to 0-255
47+
int redVal = map(analogRead(A0), 0, 1023, 0, 255);
48+
int greenVal = map(analogRead(A1), 0, 1023, 0, 255);
49+
int blueVal = map(analogRead(A2), 0, 1023, 0, 255);
50+
51+
// draw the background based on the mapped values
52+
TFTscreen.background(redVal, greenVal, blueVal);
53+
54+
// send the values to the serial monitor
55+
Serial.print("background(");
56+
Serial.print(redVal);
57+
Serial.print(" , ");
58+
Serial.print(greenVal);
59+
Serial.print(" , ");
60+
Serial.print(blueVal);
61+
Serial.println(")");
62+
63+
// wait for a moment
64+
delay(33);
65+
66+
}
67+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Arduino TFT text example
3+
4+
This example demonstrates how to draw text on the
5+
TFT with an Arduino. The Arduino reads the value
6+
of an analog sensor attached to pin A0, and writes
7+
the value to the LCD screen, updating every
8+
quarter second.
9+
10+
This example code is in the public domain
11+
12+
Created 15 April 2013 by Scott Fitzgerald
13+
14+
http://www.arduino.cc/en/Tutorial/TFTDisplayText
15+
16+
*/
17+
18+
#include <LcdScreen.h> // Arduino LCD library
19+
#include <SPI.h>
20+
21+
// pin definition for the Uno
22+
#define cs 10
23+
#define dc 9
24+
#define rst 8
25+
26+
// pin definition for the Leonardo
27+
// #define cs 7
28+
// #define dc 0
29+
// #define rst 1
30+
31+
// create an instance of the library
32+
LcdScreen TFTscreen = LcdScreen(cs, dc, rst);
33+
34+
// char array to print to the screen
35+
char sensorPrintout[4];
36+
37+
void setup() {
38+
39+
// Put this line at the beginning of every sketch that uses the GLCD:
40+
TFTscreen.begin();
41+
42+
// clear the screen with a black background
43+
TFTscreen.background(0, 0, 0);
44+
45+
// write the static text to the screen
46+
// set the font color to white
47+
TFTscreen.stroke(255, 255, 255);
48+
// set the font size
49+
TFTscreen.setTextSize(2);
50+
// write the text to the top left corner of the screen
51+
TFTscreen.text("Sensor Value :\n ", 0, 0);
52+
// ste the font size very large for the loop
53+
TFTscreen.setTextSize(5);
54+
}
55+
56+
void loop() {
57+
58+
// Read the value of the sensor on A0
59+
String sensorVal = String(analogRead(A0));
60+
61+
// convert the reading to a char array
62+
sensorVal.toCharArray(sensorPrintout, 4);
63+
64+
// set the font color
65+
TFTscreen.stroke(255, 255, 255);
66+
// print the sensor value
67+
TFTscreen.text(sensorPrintout, 0, 20);
68+
// wait for a moment
69+
delay(250);
70+
// erase the text you just wrote
71+
TFTscreen.stroke(0, 0, 0);
72+
TFTscreen.text(sensorPrintout, 0, 20);
73+
}
74+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
3+
TFT EtchASketch
4+
5+
This example for the Arduino screen draws a white point
6+
on the GLCD based on the values of 2 potentiometers.
7+
To clear the screen, press a button attached to pin 2.
8+
9+
This example code is in the public domain.
10+
11+
Created 15 April 2013 by Scott Fitzgerald
12+
13+
http://www.arduino.cc/en/Tutorial/TFTEtchASketch
14+
15+
*/
16+
17+
#include <LcdScreen.h> // Arduino LCD library
18+
#include <SPI.h>
19+
20+
// pin definition for the Uno
21+
#define cs 10
22+
#define dc 9
23+
#define rst 8
24+
25+
// pin definition for the Leonardo
26+
// #define cs 7
27+
// #define dc 0
28+
// #define rst 1
29+
30+
LcdScreen TFTscreen = LcdScreen(cs, dc, rst);
31+
32+
// initial position of the cursor
33+
int xPos = TFTscreen.width() / 2;
34+
int yPos = TFTscreen.height() / 2;
35+
36+
// pin the erase switch is connected to
37+
int erasePin = 2;
38+
39+
void setup() {
40+
// declare inputs
41+
pinMode(erasePin, INPUT);
42+
// initialize the screen
43+
TFTscreen.begin();
44+
// make the background black
45+
TFTscreen.background(0, 0, 0);
46+
}
47+
48+
void loop() {
49+
// read the potentiometers on A0 and A1
50+
int xValue = analogRead(A0);
51+
int yValue = analogRead(A1);
52+
53+
// map the values and update the position
54+
xPos = xPos + (map(xValue, 0, 1023, 2, -2));
55+
yPos = yPos + (map(yValue, 0, 1023, -2, 2));
56+
57+
// don't let the point go past the screen edges
58+
if (xPos > 159) {
59+
(xPos = 159);
60+
}
61+
62+
if (xPos < 0) {
63+
(xPos = 0);
64+
}
65+
if (yPos > 127) {
66+
(yPos = 127);
67+
}
68+
69+
if (yPos < 0) {
70+
(yPos = 0);
71+
}
72+
73+
// draw the point
74+
TFTscreen.stroke(255, 255, 255);
75+
TFTscreen.point(xPos, yPos);
76+
77+
// read the value of the pin, and erase the screen if pressed
78+
if (digitalRead(erasePin) == HIGH) {
79+
TFTscreen.background(0, 0, 0);
80+
}
81+
82+
delay(33);
83+
}

0 commit comments

Comments
 (0)