/*
SPIFFSにあるBMPファイルを表示する
LED制御はGPIO5で行う
ホスト名はesp8266_ftp.local
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266FtpServer.h>
#include <ESP8266mDNS.h>
#include <FS.h>
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
const char* ssid = "Your SSID";
const char* password = "Your Password";
//set #define FTP_DEBUG in ESP8266FtpServer.h to see ftp
verbose on serial
FtpServer ftpSrv;
#define TFT_DC 15
#define TFT_CS 2
#define TFT_RST 4
#define TFT_LED 5
#define DNS_NAME "esp8266_ftp"
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC,
TFT_RST);
long lastTime = 0;
void listDir() {
char cwdName[2];
strcpy(cwdName, "/");
Dir dir = SPIFFS.openDir(cwdName);
while ( dir.next()) {
String fn, fs;
fn = dir.fileName();
fn.remove(0, 1);
fs = String(dir.fileSize());
Serial.println("<" + fn + ">
size=" + fs);
} // end while
}
void display_Running_Sketch (void) {
String the_path = __FILE__;
int slash_loc = the_path.lastIndexOf('/');
String the_cpp_name = the_path.substring(slash_loc
+ 1);
int dot_loc = the_cpp_name.lastIndexOf('.');
String the_sketchname = the_cpp_name.substring(0,
dot_loc);
Serial.print("\nArduino is running Sketch: ");
Serial.println(the_sketchname);
Serial.print("Compiled on: ");
Serial.print(__DATE__);
Serial.print(" at ");
Serial.print(__TIME__);
Serial.print("\n");
}
void setup(void) {
delay(1000);
Serial.begin(9600);
Serial.print("TFT_DC=");
Serial.println(TFT_DC);
Serial.print("TFT_CS=");
Serial.println(TFT_CS);
Serial.print("TFT_RST="); Serial.println(TFT_RST);
Serial.print("TFT_LED="); Serial.println(TFT_LED);
display_Running_Sketch();
pinMode(TFT_LED, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start the mDNS responder
// You can connect DNS_NAME.local
if (!MDNS.begin(DNS_NAME)) {
while (1) {
Serial.println("Error
setting up MDNS responder!");
delay(1000);
}
}
Serial.println("mDNS responder started");
tft.begin();
if (SPIFFS.begin()) {
Serial.println("SPIFFS opened!");
File bmpFile;
int
bmpWidth, bmpHeight; // W+H in pixels
uint32_t
bmpImageoffset;
// Start of image data in file
// Check BMP file
if (bmpFile = SPIFFS.open("/image.bmp",
"r")) {
Serial.println("BMP File
found");
if (read16(bmpFile) ==
0x4D42) { // BMP signature
Serial.print(F("File size: "));
Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset
= read32(bmpFile); // Start of image data
Serial.print(F("Image Offset: "));
Serial.println(bmpImageoffset, DEC);
// Read DIB
header
Serial.print(F("Header size: "));
Serial.println(read32(bmpFile));
bmpWidth
= read32(bmpFile);
bmpHeight =
read32(bmpFile);
Serial.print(F("Image size: "));
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
}
bmpFile.close();
// Show BMP file
digitalWrite(TFT_LED,
HIGH);
tft.fillScreen(ILI9341_BLUE);
tft.setRotation(3);
int offsetX = (320 -
bmpWidth) / 2;
bmpDraw("/image.bmp",
offsetX, 0);
} else {
Serial.println("BMP File
NOT found");
digitalWrite(TFT_LED, LOW);
tft.fillScreen(ILI9341_BLACK);
}
//FTP Setup, ensure SPIFFS is started
before ftp;
//username, password for ftp. set
ports in ESP8266FtpServer.h (default 21, 50009 for
PASV)
ftpSrv.begin("esp8266", "esp8266");
} else {
while (1) {
Serial.println("SPIFFS
failed!");
delay(1000);
}
}
}
void loop(void) {
static long ResetTime = 0x7fffffff;
ftpSrv.handleFTP();
yield();
long now = millis();
if (now - lastTime > 1000) {
lastTime = now;
Serial.print("IP Address=");
Serial.print(WiFi.localIP());
Serial.print(" ResetTime=");
Serial.print(ResetTime);
Serial.print(" ");
Serial.print("now=");
Serial.println(now);
if (ResetTime < now) ESP.reset();
File txtFile;
// upload.txtが有れば10秒後に強制Reset
if (txtFile =
SPIFFS.open("/upload.txt", "r")) {
Serial.println("upload.txt
found");
txtFile.close();
#if 0
ここでは表示しない
再起動時にBMPファイルが有れば表示する
// Show BMP file
tft.fillScreen(ILI9341_BLUE);
tft.setRotation(3);
bmpDraw("/image.bmp", 45,
0);
#endif
SPIFFS.remove("/upload.txt");
ResetTime = now + 10000;
//10秒後にreset
}
}
}
// This function opens a Windows Bitmap (BMP) file and
// displays it at the given coordinates. It's sped
up
// by reading many pixels worth of data at a time
// (rather than pixel by pixel). Increasing the
buffer
// size takes more of the Arduino's precious RAM but
// makes loading a little faster. 20 pixels seems a
// good balance.
#define BUFFPIXEL 20
void bmpDraw(char *filename, uint8_t x, uint16_t y) {
File bmpFile;
int bmpWidth,
bmpHeight; // W+H in pixels
uint8_t
bmpDepth;
// Bit depth (currently must be 24)
uint32_t
bmpImageoffset;
// Start of image data in file
uint32_t
rowSize;
// Not always = bmpWidth; may have padding
uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel
buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); //
Current position in sdbuffer
boolean goodBmp =
false; // Set to true
on valid header parse
boolean flip =
true; // BMP is
stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if ((x >= tft.width()) || (y >=
tft.height())) return;
Serial.println();
Serial.print(F("Loading image '"));
Serial.print(filename);
Serial.println('\'');
// Open requested file on SD card
;
if (!(bmpFile = SPIFFS.open(filename, "r"))) {
Serial.print(F("File not found"));
return;
}
// Parse BMP header
if (read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print(F("File size: "));
Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read &
ignore creator bytes
bmpImageoffset = read32(bmpFile); //
Start of image data
Serial.print(F("Image Offset: "));
Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print(F("Header size: "));
Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if (read16(bmpFile) == 1) { // # planes
-- must be '1'
bmpDepth = read16(bmpFile);
// bits per pixel
Serial.print(F("Bit Depth:
")); Serial.println(bmpDepth);
if ((bmpDepth == 24)
&& (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true;
// Supported BMP format -- proceed!
Serial.print(F("Image size: "));
Serial.print(bmpWidth);
Serial.print('x');
Serial.println(bmpHeight);
// BMP rows are
padded (if needed) to 4-byte boundary
rowSize =
(bmpWidth * 3 + 3) & ~3;
// If bmpHeight
is negative, image is in top-down order.
// This is not
canon but has been observed in the wild.
if (bmpHeight
< 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to
be loaded
w = bmpWidth;
h = bmpHeight;
if ((x + w - 1)
>= tft.width()) w = tft.width() - x;
if ((y + h - 1)
>= tft.height()) h = tft.height() - y;
// Set TFT
address window to clipped image bounds
tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
for (row = 0;
row < h; row++) { // For each scanline...
//
Seek to start of scan line. It might seem labor-
//
intensive to be doing this on every line, but this
//
method covers a lot of gritty details like cropping
//
and scanline padding. Also, the seek only takes
//
place if the file position actually needs to change
//
(avoids a lot of cluster math in SD library).
if
(flip) // Bitmap is stored bottom-to-top order (normal
BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored
top-to-bottom
pos = bmpImageoffset + row * rowSize;
if
(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos, SeekSet);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for
(col = 0; col < w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
//tft.pushColor(tft.color565(r,g,b)); // なぜか動かなくなった
tft.drawPixel(col, row, tft.color565(r, g, b));
}
// end pixel
} // end
scanline
Serial.print(F("Loaded in "));
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if (!goodBmp) Serial.println(F("BMP format not
recognized."));
}
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is
little-endian too.
// May need to reverse subscript order if porting
elsewhere.
uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File &f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
|