|
| 1 | +// |
| 2 | +// JPEGDisplay class demo |
| 3 | +// |
| 4 | +// This sketch shows how to use the new helper class, JPEGDisplay to more easily |
| 5 | +// display JPEG images on displays supported by my bb_spi_lcd library |
| 6 | +// There are only two overloaded methods exposed by the library: loadJPEG(), getJPEGInfo() |
| 7 | +// It allows you to pass JPEG image data as a pointer or a filename on a uSD card |
| 8 | +// loadJPEG requires a x,y position for where to draw the image. This code doesn't |
| 9 | +// currently support clipping, so attempts to draw off the edge of the display |
| 10 | +// will return with an error. |
| 11 | +// |
| 12 | +#include <JPEGDisplay.h> |
| 13 | +#include <bb_spi_lcd.h> |
| 14 | +#include <SPI.h> |
| 15 | +#include <SD.h> |
| 16 | +#include "octocat_small.h" |
| 17 | + |
| 18 | +BB_SPI_LCD lcd, sprite; // one instance for the display and another for a 'sprite' |
| 19 | +JPEGDisplay jd; // only one instance of this class is needed |
| 20 | +SPIClass SD_SPI; |
| 21 | +bool bSD = false; |
| 22 | +// These GPIOs are for the uSD card slot on the JC4827W543 "Cheap Yellow Display" |
| 23 | +#define SD_CS 10 |
| 24 | +#define SD_MOSI 11 |
| 25 | +#define SD_SCK 12 |
| 26 | +#define SD_MISO 13 |
| 27 | + |
| 28 | +void setup() { |
| 29 | + int x, y, w, h, bpp; |
| 30 | + lcd.begin(DISPLAY_CYD_543); |
| 31 | + lcd.fillScreen(TFT_BLACK); |
| 32 | + lcd.setTextColor(TFT_GREEN); |
| 33 | + lcd.setFont(FONT_12x16); |
| 34 | + |
| 35 | + SD_SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS); |
| 36 | + if (!SD.begin(SD_CS, SD_SPI, 10000000)) { // Faster than 10MHz seems to fail on the CYDs |
| 37 | + lcd.println("Card Mount Failed"); |
| 38 | + } else { |
| 39 | + lcd.println("Card Mount Succeeded"); |
| 40 | + bSD = true; |
| 41 | + } |
| 42 | +// Load a PNG from the uSD card |
| 43 | + if (bSD) { |
| 44 | + // The PNG can be directly decoded to the LCD |
| 45 | + if (jd.getJPEGInfo(&w, &h, &bpp, "/tulips_320x213.jpg")) { // get info |
| 46 | + // center it on the LCD |
| 47 | + x = (lcd.width() - w)/2; |
| 48 | + y = (lcd.height() - h)/2; |
| 49 | + if (x < 0) x = 0; |
| 50 | + if (y < 0) y = 0; |
| 51 | + jd.loadJPEG(&lcd, x, y, "/tulips_320x213.jpg"); // load this image from the root dir of the SD card |
| 52 | + delay(5000); |
| 53 | + } |
| 54 | + } |
| 55 | + delay(3000); |
| 56 | + // |
| 57 | + // Load and display the PNG image all over the display by using a 'sprite' |
| 58 | + // First, create a sprite instance of BB_SPI_LCD with the createVirtual() method |
| 59 | + // Next, decode a PNG image directly into the sprite memory |
| 60 | + // And finally, draw it in multiple places on the LCD |
| 61 | + // |
| 62 | + lcd.fillScreen(TFT_BLACK); |
| 63 | + // You can request the dimensions and bit depth of the image BEFORE decoding it |
| 64 | + if (jd.getJPEGInfo(&w, &h, &bpp, octocat_small, sizeof(octocat_small))) { |
| 65 | + sprite.createVirtual(w, h); // create a sprite of the PNG image size |
| 66 | + // The PNG image can be decoded directly into the sprite instance |
| 67 | + jd.loadJPEG(&sprite, 0, 0, octocat_small, sizeof(octocat_small)); |
| 68 | + for (int y = 0; y < lcd.height(); y += h) { // now draw it all over the LCD |
| 69 | + for (int x = 0; x < lcd.width(); x += w) { |
| 70 | + lcd.drawSprite(x, y, &sprite, 0xffffffff); // 0xffffffff = no transparent color |
| 71 | + } // for x |
| 72 | + } // for y |
| 73 | + sprite.freeVirtual(); // free the sprite memory |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | +} |
0 commit comments