Skip to content

Instantly share code, notes, and snippets.

@Tamakichi
Created November 18, 2018 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tamakichi/b0899b1f4f8d6f6c811a627c05aa1063 to your computer and use it in GitHub Desktop.
Save Tamakichi/b0899b1f4f8d6f6c811a627c05aa1063 to your computer and use it in GitHub Desktop.
Arduino STM32 Blue Pill+TFT((ILI9341 SPI)でJPEG画像表示
//
// Arduino STM32 TFT(ILI9341) SPI接続 jpeg画像表示サンプル
//
// 参考にしたサイト
// 元にしたスケッチ
// ・楽しくやろう。 ESP8266でJPEG画像をTFT LCDに表示する
// https://blog.boochow.com/article/427690966.html
// ライブラリ
// ・MakotoKurauchi/JPEGDecoder
// https://github.com/MakotoKurauchi/JPEGDecoder
// Jpegに関する情報
// ・JPEG/MCU
// https://monobook.org/wiki/JPEG/MCU
//
#include <SPI.h>
#include <Adafruit_GFX_AS.h>
#include <Adafruit_ILI9341_STM.h>
#include <SdFat.h>
#include <JPEGDecoder.h>
// SDカード(SPI2利用)
#if ENABLE_EXTENDED_TRANSFER_CLASS == 1
SdFatEX SD(2);
#else
SdFat SD(2);
#endif
#define SPI_SPEED SD_SCK_MHZ(36) // バスクロック
#define SD_CS PB0 // SDカード選択
// TFT制御用ピン (SPI1利用)
#define TFT_CS PA0
#define TFT_RST PA1
#define TFT_DC PA2
// TFT制御用オブジェクト
Adafruit_ILI9341_STM tft = Adafruit_ILI9341_STM(TFT_CS, TFT_DC, TFT_RST);
void setup() {
Serial.begin(115200);
delay(200);
if (!SD.begin(SD_CS,SPI_SPEED)) {
Serial.println("failed!");
}
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLUE);
Serial.println("OK!");
jpegDraw("/test1.jpg"); // 320x240ドットフルカラー画像の表示
}
void jpegDraw(char* filename) {
char str[100];
uint8_t *pImg;
int x,y,bx,by;
// Decoding start
JpegDec.decode(filename,0);
uint16_t buf[JpegDec.MCUWidth*JpegDec.MCUHeight];
uint16_t pos = 0;
// Image Information
Serial.print("Width :");
Serial.println(JpegDec.width);
Serial.print("Height :");
Serial.println(JpegDec.height);
Serial.print("Components:");
Serial.println(JpegDec.comps);
Serial.print("MCU / row :");
Serial.println(JpegDec.MCUSPerRow);
Serial.print("MCU / col :");
Serial.println(JpegDec.MCUSPerCol);
Serial.print("Scan type :");
Serial.println(JpegDec.scanType);
Serial.print("MCU width :");
Serial.println(JpegDec.MCUWidth);
Serial.print("MCU height:");
Serial.println(JpegDec.MCUHeight);
Serial.println("");
sprintf(str,"#SIZE,%d,%d",JpegDec.width,JpegDec.height);
Serial.println(str);
// Raw Image Data
while( JpegDec.read() ){ // MCU毎の描画処理
pImg = JpegDec.pImage ; // MCUブロックの先頭アドレス
pos = 0; // バッファ位置初期化
// MCUブロック描画ウィンドウの設定
tft.setAddrWindow(JpegDec.MCUx * JpegDec.MCUWidth,
JpegDec.MCUy * JpegDec.MCUHeight,
JpegDec.MCUx * JpegDec.MCUWidth+JpegDec.MCUWidth-1,
JpegDec.MCUy * JpegDec.MCUHeight+JpegDec.MCUHeight-1);
// ウィンドウ領域へのデータ転送
for( by = 0; by < JpegDec.MCUHeight; by++)
for( bx = 0; bx < JpegDec.MCUWidth; bx++, pImg += JpegDec.comps, pos++)
buf[pos] = (JpegDec.comps == 1) ? tft.color565(pImg[0], pImg[0], pImg[0]) : tft.color565(pImg[0], pImg[1], pImg[2]);
tft.pushColors(buf, JpegDec.MCUWidth*JpegDec.MCUHeight, 0);
}
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment