Thursday, April 10, 2014

Arduino Esplora example: read bmp from SD Card, display on TFT screen

Example of Arduino Esplora, read bmp from AD Card, and display on TFT screen.

#include <Esplora.h>
#include <SPI.h>
#include <SD.h>
#include <TFT.h>

// SD Chip Select pin
#define SD_CS    8

// this variable represents the image to be drawn on screen
PImage image;
int prvSwitch1, prvSwitch2, prvSwitch3, prvSwitch4;

void setup() {
  Esplora.writeRGB(255, 255, 255);
  
  EsploraTFT.begin();
  EsploraTFT.background(0, 0, 0);
  EsploraTFT.stroke(255,255,255);
  
  // initialize the serial port
  Serial.begin(9600);
  while (!Serial) {
    // wait for serial line to be ready
  }

  // try to access the SD card
  Serial.print("Initializing SD card...");
  EsploraTFT.text("Initializing SD card...", 0, 10);
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    EsploraTFT.text("failed!", 0, 10);
    return;
  }
  Serial.println("OK!");
  EsploraTFT.text("OK!", 0, 10);

  image = EsploraTFT.loadImage("arduino.bmp");

  if (image.isValid() != true) {
    Serial.println("error while loading arduino.bmp");
    EsploraTFT.text("error while loading arduino.bmp", 0, 10);
  }
  
  Esplora.writeRGB(0, 0, 0);
  EsploraTFT.background(0, 0, 0);
  Serial.println("started");
  EsploraTFT.text("started", 0, 10);

}

void loop(){
  int sw = Esplora.readButton(SWITCH_1);
  if(sw != prvSwitch1){
    prvSwitch1 = sw;
    if(sw == LOW){
      Esplora.writeRGB(255, 255, 255);
      Serial.println("Switch1 pressed");
      EsploraTFT.image(image, 0, 0);
      Esplora.writeRGB(0, 0, 0);
    }
  }
  
  sw = Esplora.readButton(SWITCH_2);
  if(sw != prvSwitch2){
    prvSwitch2 = sw;
    if(sw == LOW){
      Esplora.writeRGB(255, 255, 255);
      Serial.println("Switch2 pressed");
      EsploraTFT.background(255, 0, 0);
      Esplora.writeRGB(0, 0, 0);
    }
  }
  
  sw = Esplora.readButton(SWITCH_3);
  if(sw != prvSwitch3){
    prvSwitch3 = sw;
    if(sw == LOW){
      Esplora.writeRGB(255, 255, 255);
      Serial.println("Switch3 pressed");
      EsploraTFT.background(0, 255, 0);
      Esplora.writeRGB(0, 0, 0);
    }
  }
  
  sw = Esplora.readButton(SWITCH_4);
  if(sw != prvSwitch4){
    prvSwitch4 = sw;
    if(sw == LOW){
      Esplora.writeRGB(255, 255, 255);
      Serial.println("Switch4 pressed");
      EsploraTFT.background(0, 0, 255);
      Esplora.writeRGB(0, 0, 0);
    }
  }
  
}

No comments:

Post a Comment