diff options
Diffstat (limited to 'libraries/TFT/examples')
15 files changed, 1086 insertions, 0 deletions
diff --git a/libraries/TFT/examples/Arduino/TFTBitmapLogo/TFTBitmapLogo.ino b/libraries/TFT/examples/Arduino/TFTBitmapLogo/TFTBitmapLogo.ino new file mode 100644 index 0000000..da7a94d --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTBitmapLogo/TFTBitmapLogo.ino @@ -0,0 +1,108 @@ +/* + + Arduino TFT Bitmap Logo example + + This example reads an image file from a micro-SD card + and draws it on the screen, at random locations. + + In this sketch, the Arduino logo is read from a micro-SD card. + There is a .bmp file included with this sketch. + - open the sketch folder (Ctrl-K or Cmd-K) + - copy the "arduino.bmp" file to a micro-SD + - put the SD into the SD slot of the Arduino TFT module. + + This example code is in the public domain. + + Created 19 April 2013 by Enrico Gueli + + http://arduino.cc/en/Tutorial/TFTBitmapLogo + + */ + +// include the necessary libraries +#include <SPI.h> +#include <SD.h> +#include <TFT.h> // Arduino LCD library + +// pin definition for the Uno +#define sd_cs 4 +#define lcd_cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +//#define sd_cs 8 +//#define lcd_cs 7 +//#define dc 0 +//#define rst 1 + +TFT TFTscreen = TFT(lcd_cs, dc, rst); + +// this variable represents the image to be drawn on screen +PImage logo; + + +void setup() { + // initialize the GLCD and show a message + // asking the user to open the serial line + TFTscreen.begin(); + TFTscreen.background(255, 255, 255); + + TFTscreen.stroke(0, 0, 255); + TFTscreen.println(); + TFTscreen.println("Arduino TFT Bitmap Example"); + TFTscreen.stroke(0, 0, 0); + TFTscreen.println("Open serial monitor"); + TFTscreen.println("to run the sketch"); + + // initialize the serial port: it will be used to + // print some diagnostic info + Serial.begin(9600); + while (!Serial) { + // wait for serial line to be ready + } + + // clear the GLCD screen before starting + TFTscreen.background(255, 255, 255); + + // try to access the SD card. If that fails (e.g. + // no card present), the setup process will stop. + Serial.print("Initializing SD card..."); + if (!SD.begin(sd_cs)) { + Serial.println("failed!"); + return; + } + Serial.println("OK!"); + + // initialize and clear the GLCD screen + TFTscreen.begin(); + TFTscreen.background(255, 255, 255); + + // now that the SD card can be access, try to load the + // image file. + logo = TFTscreen.loadImage("arduino.bmp"); + if (!logo.isValid()) { + Serial.println("error while loading arduino.bmp"); + } +} + +void loop() { + // don't do anything if the image wasn't loaded correctly. + if (logo.isValid() == false) { + return; + } + + Serial.println("drawing image"); + + // get a random location where to draw the image. + // To avoid the image to be draw outside the screen, + // take into account the image size. + int x = random(TFTscreen.width() - logo.width()); + int y = random(TFTscreen.height() - logo.height()); + + // draw the image to the screen + TFTscreen.image(logo, x, y); + + // wait a little bit before drawing again + delay(1500); +} diff --git a/libraries/TFT/examples/Arduino/TFTBitmapLogo/arduino.bmp b/libraries/TFT/examples/Arduino/TFTBitmapLogo/arduino.bmp Binary files differnew file mode 100644 index 0000000..09c670a --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTBitmapLogo/arduino.bmp diff --git a/libraries/TFT/examples/Arduino/TFTColorPicker/TFTColorPicker.ino b/libraries/TFT/examples/Arduino/TFTColorPicker/TFTColorPicker.ino new file mode 100644 index 0000000..74fc176 --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTColorPicker/TFTColorPicker.ino @@ -0,0 +1,67 @@ +/* + + TFT Color Picker + + This example for the Arduino screen reads the input of + potentiometers or analog sensors attached to A0, A1, + and A2 and uses the values to change the screen's color. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTColorPicker + + */ + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +TFT TFTscreen = TFT(cs, dc, rst); + +void setup() { + // begin serial communication + Serial.begin(9600); + + // initialize the display + TFTscreen.begin(); + + // set the background to white + TFTscreen.background(255, 255, 255); + +} + +void loop() { + + // read the values from your sensors and scale them to 0-255 + int redVal = map(analogRead(A0), 0, 1023, 0, 255); + int greenVal = map(analogRead(A1), 0, 1023, 0, 255); + int blueVal = map(analogRead(A2), 0, 1023, 0, 255); + + // draw the background based on the mapped values + TFTscreen.background(redVal, greenVal, blueVal); + + // send the values to the serial monitor + Serial.print("background("); + Serial.print(redVal); + Serial.print(" , "); + Serial.print(greenVal); + Serial.print(" , "); + Serial.print(blueVal); + Serial.println(")"); + + // wait for a moment + delay(33); + +} + diff --git a/libraries/TFT/examples/Arduino/TFTDisplayText/TFTDisplayText.ino b/libraries/TFT/examples/Arduino/TFTDisplayText/TFTDisplayText.ino new file mode 100644 index 0000000..f482bd1 --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTDisplayText/TFTDisplayText.ino @@ -0,0 +1,74 @@ +/* + Arduino TFT text example + + This example demonstrates how to draw text on the + TFT with an Arduino. The Arduino reads the value + of an analog sensor attached to pin A0, and writes + the value to the LCD screen, updating every + quarter second. + + This example code is in the public domain + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTDisplayText + + */ + +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +// create an instance of the library +TFT TFTscreen = TFT(cs, dc, rst); + +// char array to print to the screen +char sensorPrintout[4]; + +void setup() { + + // Put this line at the beginning of every sketch that uses the GLCD: + TFTscreen.begin(); + + // clear the screen with a black background + TFTscreen.background(0, 0, 0); + + // write the static text to the screen + // set the font color to white + TFTscreen.stroke(255,255,255); + // set the font size + TFTscreen.setTextSize(2); + // write the text to the top left corner of the screen + TFTscreen.text("Sensor Value :\n ",0,0); + // ste the font size very large for the loop + TFTscreen.setTextSize(5); +} + +void loop() { + + // Read the value of the sensor on A0 + String sensorVal = String(analogRead(A0)); + + // convert the reading to a char array + sensorVal.toCharArray(sensorPrintout, 4); + + // set the font color + TFTscreen.stroke(255,255,255); + // print the sensor value + TFTscreen.text(sensorPrintout, 0, 20); + // wait for a moment + delay(250); + // erase the text you just wrote + TFTscreen.stroke(0,0,0); + TFTscreen.text(sensorPrintout, 0, 20); +} + diff --git a/libraries/TFT/examples/Arduino/TFTEtchASketch/TFTEtchASketch.ino b/libraries/TFT/examples/Arduino/TFTEtchASketch/TFTEtchASketch.ino new file mode 100644 index 0000000..29e3483 --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTEtchASketch/TFTEtchASketch.ino @@ -0,0 +1,84 @@ +/* + + TFT EtchASketch + + This example for the Arduino screen draws a white point + on the GLCD based on the values of 2 potentiometers. + To clear the screen, press a button attached to pin 2. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTEtchASketch + + */ + +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +TFT TFTscreen = TFT(cs, dc, rst); + +// initial position of the cursor +int xPos = TFTscreen.width()/2; +int yPos = TFTscreen.height()/2; + +// pin the erase switch is connected to +int erasePin = 2; + +void setup() { + // declare inputs + pinMode(erasePin, INPUT); + // initialize the screen + TFTscreen.begin(); + // make the background black + TFTscreen.background(0,0,0); +} + +void loop() +{ + // read the potentiometers on A0 and A1 + int xValue = analogRead(A0); + int yValue = analogRead(A1); + + // map the values and update the position + xPos = xPos + (map(xValue, 0, 1023, 2, -2)); + yPos = yPos + (map(yValue, 0, 1023, -2, 2)); + +// don't let the point go past the screen edges + if(xPos > 159){ + (xPos = 159); + } + + if(xPos < 0){ + (xPos = 0); + } + if(yPos > 127){ + (yPos = 127); + } + + if(yPos < 0){ + (yPos = 0); + } + + // draw the point + TFTscreen.stroke(255,255,255); + TFTscreen.point(xPos,yPos); + + // read the value of the pin, and erase the screen if pressed + if(digitalRead(erasePin) == HIGH){ + TFTscreen.background(0,0,0); + } + + delay(33); +} diff --git a/libraries/TFT/examples/Arduino/TFTGraph/TFTGraph.ino b/libraries/TFT/examples/Arduino/TFTGraph/TFTGraph.ino new file mode 100644 index 0000000..39ae49b --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTGraph/TFTGraph.ino @@ -0,0 +1,71 @@ +/* + + TFT Graph + + This example for an Arduino screen reads + the value of an analog sensor on A0, and + graphs the values on the screen. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTGraph + + */ + +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + + // pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +TFT TFTscreen = TFT(cs, dc, rst); + +// position of the line on screen +int xPos = 0; + +void setup(){ + // initialize the serial port + Serial.begin(9600); + + // initialize the display + TFTscreen.begin(); + + // clear the screen with a pretty color + TFTscreen.background(250,16,200); +} + +void loop(){ + // read the sensor and map it to the screen height + int sensor = analogRead(A0); + int drawHeight = map(sensor,0,1023,0,TFTscreen.height()); + + // print out the height to the serial monitor + Serial.println(drawHeight); + + // draw a line in a nice color + TFTscreen.stroke(250,180,10); + TFTscreen.line(xPos, TFTscreen.height()-drawHeight, xPos, TFTscreen.height()); + + // if the graph has reached the screen edge + // erase the screen and start again + if (xPos >= 160) { + xPos = 0; + TFTscreen.background(250,16,200); + } + else { + // increment the horizontal position: + xPos++; + } + + delay(16); +} + diff --git a/libraries/TFT/examples/Arduino/TFTPong/TFTPong.ino b/libraries/TFT/examples/Arduino/TFTPong/TFTPong.ino new file mode 100644 index 0000000..02ea11c --- /dev/null +++ b/libraries/TFT/examples/Arduino/TFTPong/TFTPong.ino @@ -0,0 +1,135 @@ +/* + + TFT Pong + + This example for the Arduino screen reads the values + of 2 potentiometers to move a rectangular platform + on the x and y axes. The platform can intersect + with a ball causing it to bounce. + + This example code is in the public domain. + + Created by Tom Igoe December 2012 + Modified 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTPong + + */ + +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// pin definition for the Uno +#define cs 10 +#define dc 9 +#define rst 8 + +// pin definition for the Leonardo +// #define cs 7 +// #define dc 0 +// #define rst 1 + +TFT TFTscreen = TFT(cs, dc, rst); + +// variables for the position of the ball and paddle +int paddleX = 0; +int paddleY = 0; +int oldPaddleX, oldPaddleY; +int ballDirectionX = 1; +int ballDirectionY = 1; + +int ballSpeed = 10; // lower numbers are faster + +int ballX, ballY, oldBallX, oldBallY; + +void setup() { + // initialize the display + TFTscreen.begin(); + // black background + TFTscreen.background(0,0,0); +} + +void loop() { + + // save the width and height of the screen + int myWidth = TFTscreen.width(); + int myHeight = TFTscreen.height(); + + // map the paddle's location to the position of the potentiometers + paddleX = map(analogRead(A0), 512, -512, 0, myWidth) - 20/2; + paddleY = map(analogRead(A1), 512, -512, 0, myHeight) - 5/2; + + // set the fill color to black and erase the previous + // position of the paddle if different from present + TFTscreen.fill(0,0,0); + + if (oldPaddleX != paddleX || oldPaddleY != paddleY) { + TFTscreen.rect(oldPaddleX, oldPaddleY, 20, 5); + } + + // draw the paddle on screen, save the current position + // as the previous. + TFTscreen.fill(255,255,255); + + TFTscreen.rect(paddleX, paddleY, 20, 5); + oldPaddleX = paddleX; + oldPaddleY = paddleY; + + // update the ball's position and draw it on screen + if (millis() % ballSpeed < 2) { + moveBall(); + } +} + +// this function determines the ball's position on screen +void moveBall() { + // if the ball goes offscreen, reverse the direction: + if (ballX > TFTscreen.width() || ballX < 0) { + ballDirectionX = -ballDirectionX; + } + + if (ballY > TFTscreen.height() || ballY < 0) { + ballDirectionY = -ballDirectionY; + } + + // check if the ball and the paddle occupy the same space on screen + if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5)) { + ballDirectionX = -ballDirectionX; + ballDirectionY = -ballDirectionY; + } + + // update the ball's position + ballX += ballDirectionX; + ballY += ballDirectionY; + +// erase the ball's previous position + TFTscreen.fill(0,0,0); + + if (oldBallX != ballX || oldBallY != ballY) { + TFTscreen.rect(oldBallX, oldBallY, 5, 5); + } + + + // draw the ball's current position + TFTscreen.fill(255,255,255); + TFTscreen.rect(ballX, ballY, 5, 5); + + oldBallX = ballX; + oldBallY = ballY; + +} + +// this function checks the position of the ball +// to see if it intersects with the paddle +boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) { + boolean result = false; + + if ((x >= rectX && x <= (rectX + rectWidth)) && + (y >= rectY && y <= (rectY + rectHeight))) { + result = true; + } + +return result; +} + + diff --git a/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/EsploraTFTBitmapLogo.ino b/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/EsploraTFTBitmapLogo.ino new file mode 100644 index 0000000..3d3f230 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/EsploraTFTBitmapLogo.ino @@ -0,0 +1,101 @@ +/* + + Esplora TFT Bitmap Logos + + This example for the Arduino TFT screen is for use + with an Arduino Esplora. + + This example reads an image file from a micro-SD card + and draws it on the screen, at random locations. + + There is a .bmp file included with this sketch. + - open the sketch folder (Ctrl-K or Cmd-K) + - copy the "arduino.bmp" file to a micro-SD + - put the SD into the SD slot of the Arduino LCD module. + + This example code is in the public domain. + + Created 19 April 2013 by Enrico Gueli + + http://arduino.cc/en/Tutorial/EsploraTFTBitmapLogo + + */ + +// include the necessary libraries +#include <Esplora.h> +#include <SPI.h> +#include <SD.h> +#include <TFT.h> // Arduino LCD library + +// the Esplora pin connected to the chip select line for SD card +#define SD_CS 8 + +// this variable represents the image to be drawn on screen +PImage logo; + +void setup() { + // initialize the GLCD and show a message + // asking the user to open the serial line + EsploraTFT.begin(); + EsploraTFT.background(255, 255, 255); + + EsploraTFT.stroke(0, 0, 255); + EsploraTFT.println(); + EsploraTFT.println("Arduino LCD Bitmap Example"); + EsploraTFT.stroke(0, 0, 0); + EsploraTFT.println("Open serial monitor"); + EsploraTFT.println("to run the sketch"); + + // initialize the serial port: it will be used to + // print some diagnostic info + Serial.begin(9600); + while (!Serial) { + // wait for serial monitor to be open + } + + // try to access the SD card. If that fails (e.g. + // no card present), the Esplora's LED will turn red. + Serial.print("Initializing SD card..."); + if (!SD.begin(SD_CS)) { + Serial.println("failed!"); + Esplora.writeRed(255); + return; + } + Serial.println("OK!"); + + // clear the GLCD screen before starting + EsploraTFT.background(255, 255, 255); + + // now that the SD card can be access, try to load the + // image file. The Esplora LED will turn green or red if + // the loading went OK or not. + Esplora.writeRGB(0, 0, 0); + logo = EsploraTFT.loadImage("arduino.bmp"); + if (logo.isValid()) { + Esplora.writeGreen(255); + } + else + Esplora.writeRed(255); + +} + +void loop() { + // don't do anything if the image wasn't loaded correctly. + if (logo.isValid() == false) { + return; + } + + Serial.println("drawing image"); + + // get a random location where to draw the image. + // To avoid the image to be draw outside the screen, + // take into account the image size. + int x = random(EsploraTFT.width() - logo.width()); + int y = random(EsploraTFT.height() - logo.height()); + + // draw the image to the screen + EsploraTFT.image(logo, x, y); + + // wait a little bit before drawing again + delay(1500); +} diff --git a/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/arduino.bmp b/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/arduino.bmp Binary files differnew file mode 100644 index 0000000..09c670a --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTBitmapLogo/arduino.bmp diff --git a/libraries/TFT/examples/Esplora/EsploraTFTColorPicker/EsploraTFTColorPicker.ino b/libraries/TFT/examples/Esplora/EsploraTFTColorPicker/EsploraTFTColorPicker.ino new file mode 100644 index 0000000..63a0ee2 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTColorPicker/EsploraTFTColorPicker.ino @@ -0,0 +1,54 @@ +/* + + Esplora TFT Color Picker + + This example for the Esplora with an Arduino TFT reads + the input of the joystick and slider, using the values + to change the screen's color. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/TFTColorPicker + + */ + +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +void setup() { + Serial.begin(9600); + + // initialize the LCD + EsploraTFT.begin(); + + // start out with a white screen + EsploraTFT.background(255, 255, 255); + +} + +void loop() { + + // map the values from sensors + int xValue = map(Esplora.readJoystickX(), -512, 512, 0, 255); // read the joystick's X position + int yValue = map(Esplora.readJoystickY(), -512, 512, 0, 255); // read the joystick's Y position + int slider = map(Esplora.readSlider(), 0, 1023, 0, 255); // read the slider's position + + // change the background color based on the mapped values + EsploraTFT.background(xValue, yValue, slider); + + // print the mapped values to the Serial monitor + Serial.print("background("); + Serial.print(xValue); + Serial.print(" , "); + Serial.print(yValue); + Serial.print(" , "); + Serial.print(slider); + Serial.println(")"); + + delay(33); + +} + diff --git a/libraries/TFT/examples/Esplora/EsploraTFTEtchASketch/EsploraTFTEtchASketch.ino b/libraries/TFT/examples/Esplora/EsploraTFTEtchASketch/EsploraTFTEtchASketch.ino new file mode 100644 index 0000000..a1430d3 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTEtchASketch/EsploraTFTEtchASketch.ino @@ -0,0 +1,83 @@ +/* + + Esplora TFT EtchASketch + + This example for the Arduino TFT and Esplora draws + a white line on the screen, based on the position + of the joystick. To clear the screen, shake the + Esplora, using the values from the accelerometer. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/EsploraTFTEtchASketch + + */ + +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// initial position of the cursor +int xPos = EsploraTFT.width()/2; +int yPos = EsploraTFT.height()/2; + +void setup() { + // initialize the display + EsploraTFT.begin(); + + // clear the background + EsploraTFT.background(0,0,0); +} + +void loop() +{ + + int xAxis = Esplora.readJoystickX(); // read the X axis + int yAxis = Esplora.readJoystickY(); // read the Y axis + + // update the position of the line + // depending on the position of the joystick + if (xAxis<10 && xAxis>-10){ + xPos=xPos; + } + else{ + xPos = xPos + (map(xAxis, -512, 512, 2, -2)); + } + if (yAxis<10 && yAxis>-10){ + yAxis=yAxis; + } + else{ + yPos = yPos + (map(yAxis, -512, 512, -2, 2)); + } + +// don't let the point go past the screen edges + if(xPos > 159){ + (xPos = 159); + } + + if(xPos < 0){ + (xPos = 0); + } + if(yPos > 127){ + (yPos = 127); + } + + if(yPos < 0){ + (yPos = 0); + } + + // draw the point + EsploraTFT.stroke(255,255,255); + EsploraTFT.point(xPos,yPos); + + // check the accelerometer values and clear + // the screen if it is being shaken + if(abs(Esplora.readAccelerometer(X_AXIS))>200 || abs(Esplora.readAccelerometer(Y_AXIS))>200){ + EsploraTFT.background(0,0,0); + } + + delay(33); +} + diff --git a/libraries/TFT/examples/Esplora/EsploraTFTGraph/EsploraTFTGraph.ino b/libraries/TFT/examples/Esplora/EsploraTFTGraph/EsploraTFTGraph.ino new file mode 100644 index 0000000..7f2a427 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTGraph/EsploraTFTGraph.ino @@ -0,0 +1,56 @@ +/* + + Esplora TFT Graph + + This example for the Esplora with an Arduino TFT reads + the value of the light sensor, and graphs the values on + the screen. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/EsploraTFTGraph + + */ + +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// position of the line on screen +int xPos = 0; + +void setup(){ + + // initialize the screen + EsploraTFT.begin(); + + // clear the screen with a nice color + EsploraTFT.background(250,16,200); +} + +void loop(){ + + // read the sensor value + int sensor = Esplora.readLightSensor(); + // map the sensor value to the height of the screen + int graphHeight = map(sensor,0,1023,0,EsploraTFT.height()); + + // draw the line in a pretty color + EsploraTFT.stroke(250,180,10); + EsploraTFT.line(xPos, EsploraTFT.height() - graphHeight, xPos, EsploraTFT.height()); + + // if the graph reaches the edge of the screen + // erase it and start over from the other side + if (xPos >= 160) { + xPos = 0; + EsploraTFT.background(250,16,200); + } + else { + // increment the horizontal position: + xPos++; + } + + delay(16); +} diff --git a/libraries/TFT/examples/Esplora/EsploraTFTHorizon/EsploraTFTHorizon.ino b/libraries/TFT/examples/Esplora/EsploraTFTHorizon/EsploraTFTHorizon.ino new file mode 100644 index 0000000..a7945c9 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTHorizon/EsploraTFTHorizon.ino @@ -0,0 +1,63 @@ +/* + + Esplora TFT Horizon + + This example for the Arduino TFT and Esplora draws + a line on the screen that stays level with the ground + as you tile the Esplora side to side + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/EsploraTFTHorizon + + */ + +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// horizontal start and end positions +int yStart = EsploraTFT.height()/2; +int yEnd = EsploraTFT.height()/2; + +// previous start and end positions +int oldEndY; +int oldStartY; + +void setup() { + // initialize the display + EsploraTFT.begin(); + // make the background black + EsploraTFT.background(0,0,0); +} + +void loop() +{ + // read the x-axis of te accelerometer + int tilt = Esplora.readAccelerometer(X_AXIS); + + // the values are 100 when tilted to the left + // and -100 when tilted to the right + // map these values to the start and end points + yStart = map(tilt,-100,100,EsploraTFT.height(),0); + yEnd = map(tilt,-100,100,0,EsploraTFT.height()); + + // if the previous values are different than the current values + // erase the previous line + if (oldStartY != yStart || oldEndY != yEnd) { + EsploraTFT.stroke(0,0,0); + EsploraTFT.line(0, oldStartY, EsploraTFT.width(), oldEndY); + } + + // draw the line in magenta + EsploraTFT.stroke(255,0,255); + EsploraTFT.line(0,yStart,EsploraTFT.width(),yEnd); + + // save the current start and end points + // to compare int he next loop + oldStartY= yStart; + oldEndY = yEnd; + delay(10); +} diff --git a/libraries/TFT/examples/Esplora/EsploraTFTPong/EsploraTFTPong.ino b/libraries/TFT/examples/Esplora/EsploraTFTPong/EsploraTFTPong.ino new file mode 100644 index 0000000..e3422d4 --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTPong/EsploraTFTPong.ino @@ -0,0 +1,126 @@ +/* + + Esplora TFT Pong + + This example for the Esplora with an Arduino TFT screen reads + the value of the joystick to move a rectangular platform + on the x and y axes. The platform can intersect with a ball + causing it to bounce. The Esplora's slider adjusts the speed + of the ball. + + This example code is in the public domain. + + Created by Tom Igoe December 2012 + Modified 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/EsploraTFTPong + + */ + +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +// variables for the position of the ball and paddle +int paddleX = 0; +int paddleY = 0; +int oldPaddleX, oldPaddleY; +int ballDirectionX = 1; +int ballDirectionY = 1; + +int ballX, ballY, oldBallX, oldBallY; + +void setup() { + + Serial.begin(9600); + + // initialize the display + EsploraTFT.begin(); + // set the background the black + EsploraTFT.background(0,0,0); +} + +void loop() { + // save the width and height of the screen + int myWidth = EsploraTFT.width(); + int myHeight = EsploraTFT.height(); + + // map the paddle's location to the joystick's position + paddleX = map(Esplora.readJoystickX(), 512, -512, 0, myWidth) - 20/2; + paddleY = map(Esplora.readJoystickY(), -512, 512, 0, myHeight) - 5/2; + Serial.print(paddleX); + Serial.print(" "); + Serial.println(paddleY); + + // set the fill color to black and erase the previous + // position of the paddle if different from present + EsploraTFT.fill(0,0,0); + + if (oldPaddleX != paddleX || oldPaddleY != paddleY) { + EsploraTFT.rect(oldPaddleX, oldPaddleY, 20, 5); + } + + // draw the paddle on screen, save the current position + // as the previous. + EsploraTFT.fill(255,255,255); + EsploraTFT.rect(paddleX, paddleY, 20, 5); + oldPaddleX = paddleX; + oldPaddleY = paddleY; + + // read the slider to determinde the speed of the ball + int ballSpeed = map(Esplora.readSlider(), 0, 1023, 0, 80)+1; + if (millis() % ballSpeed < 2) { + moveBall(); + } +} + + +// this function determines the ball's position on screen +void moveBall() { + // if the ball goes offscreen, reverse the direction: + if (ballX > EsploraTFT.width() || ballX < 0) { + ballDirectionX = -ballDirectionX; + } + + if (ballY > EsploraTFT.height() || ballY < 0) { + ballDirectionY = -ballDirectionY; + } + + // check if the ball and the paddle occupy the same space on screen + if (inPaddle(ballX, ballY, paddleX, paddleY, 20, 5)) { + ballDirectionY = -ballDirectionY; + } + + // update the ball's position + ballX += ballDirectionX; + ballY += ballDirectionY; + + // erase the ball's previous position + EsploraTFT.fill(0,0,0); + + if (oldBallX != ballX || oldBallY != ballY) { + EsploraTFT.rect(oldBallX, oldBallY, 5, 5); + } + + // draw the ball's current position + EsploraTFT.fill(255,255,255); + + EsploraTFT.rect(ballX, ballY, 5, 5); + + oldBallX = ballX; + oldBallY = ballY; + +} + +// this function checks the position of the ball +// to see if it intersects with the paddle +boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) { + boolean result = false; + + if ((x >= rectX && x <= (rectX + rectWidth)) && + (y >= rectY && y <= (rectY + rectHeight))) { + result = true; + } + + return result; +} diff --git a/libraries/TFT/examples/Esplora/EsploraTFTTemp/EsploraTFTTemp.ino b/libraries/TFT/examples/Esplora/EsploraTFTTemp/EsploraTFTTemp.ino new file mode 100644 index 0000000..b475d2d --- /dev/null +++ b/libraries/TFT/examples/Esplora/EsploraTFTTemp/EsploraTFTTemp.ino @@ -0,0 +1,64 @@ +/* + + Esplora TFT Temperature Display + + This example for the Arduino TFT screen is for use + with an Arduino Esplora. + + This example reads the temperature of the Esplora's + on board thermisistor and displays it on an attached + LCD screen, updating every second. + + This example code is in the public domain. + + Created 15 April 2013 by Scott Fitzgerald + + http://arduino.cc/en/Tutorial/EsploraTFTTemp + + */ + +// include the necessary libraries +#include <Esplora.h> +#include <TFT.h> // Arduino LCD library +#include <SPI.h> + +char tempPrintout[3]; // array to hold the temperature data + +void setup() { + + // Put this line at the beginning of every sketch that uses the GLCD + EsploraTFT.begin(); + + // clear the screen with a black background + EsploraTFT.background(0,0,0); + + // set the text color to magenta + EsploraTFT.stroke(200,20,180); + // set the text to size 2 + EsploraTFT.setTextSize(2); + // start the text at the top left of the screen + // this text is going to remain static + EsploraTFT.text("Degrees in C :\n ",0,0); + + // set the text in the loop to size 5 + EsploraTFT.setTextSize(5); +} + +void loop() { + + // read the temperature in Celcius and store it in a String + String temperature = String(Esplora.readTemperature(DEGREES_C)); + + // convert the string to a char array + temperature.toCharArray(tempPrintout, 3); + + // set the text color to white + EsploraTFT.stroke(255,255,255); + // print the temperature one line below the static text + EsploraTFT.text(tempPrintout, 0, 30); + + delay(1000); + // erase the text for the next loop + EsploraTFT.stroke(0,0,0); + EsploraTFT.text(tempPrintout, 0, 30); +} |