|
| 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://arduino.cc/en/Tutorial/TFTBitmapLogo |
| 19 | + |
| 20 | + */ |
| 21 | + |
| 22 | +// include the necessary libraries |
| 23 | +#include <SPI.h> |
| 24 | +#include <SD.h> |
| 25 | +#include <TFT.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 | +TFT TFTscreen = TFT(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("Arduino TFT Bitmap Example"); |
| 54 | + TFTscreen.stroke(0, 0, 0); |
| 55 | + TFTscreen.println("Open serial monitor"); |
| 56 | + TFTscreen.println("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 line to be ready |
| 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("Initializing SD card..."); |
| 71 | + if (!SD.begin(sd_cs)) { |
| 72 | + Serial.println("failed!"); |
| 73 | + return; |
| 74 | + } |
| 75 | + Serial.println("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("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("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 | +} |
0 commit comments