/*************************************************** This is our Bitmap drawing example for the Adafruit ILI9341 Breakout and Shield ----> http://www.adafruit.com/products/1651 Check out the links above for our tutorials and wiring diagrams These displays use SPI to communicate, 4 or 5 pins are required to interface (RST is optional) Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. MIT license, all text above must be included in any redistribution ****************************************************/// SDカード用ライブラリの選択#defineUSE_SDFAT1// 0:SDライブラリ利用, 1:SdFatライブラリ利用// SdFatライブラリ利用時は下記の設定を好みで行う// SdFatConfig.h:// ENABLE_EXTENDED_TRANSFER_CLASS (1の場合、SdFatEXを使う) // USE_SEPARATE_FAT_CACHE// USE_MULTI_BLOCK_IO#include<SPI.h>#ifUSE_SDFAT==1#include<SdFat.h>#ifENABLE_EXTENDED_TRANSFER_CLASS==1SdFatEXSD(2);#elseSdFatSD(2);#endif#defineSPI_SPEEDSD_SCK_MHZ(18)#else#include<SD.h>#endif#include<Adafruit_GFX_AS.h>// Core graphics library#include<Adafruit_ILI9341_STM.h>// Hardware-specific library#include<XPT2046_touch.h>// TFT display and SD card will share the hardware SPI interface.// Hardware SPI pins are specific to the Arduino board type and// cannot be remapped to alternate pins. For Arduino Uno,// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.// タッチスクリーン領域#defineTS_MINX600#defineTS_MINY440#defineTS_MAXX3460#defineTS_MAXY3460// タッチスクリーンCSピン#defineTS_CSPA3// TFT制御用ピン#defineTFT_CSPA0#defineTFT_RSTPA1#defineTFT_DCPA2SPIClassSPI_2(2);// タッチスクリーンで共有// タッチスクリーン制御用XPT2046_touchts(TS_CS,SPI_2);// Chip Select pin, SPI port// TFT制御用Adafruit_ILI9341_STMtft=Adafruit_ILI9341_STM(TFT_CS,TFT_DC,TFT_RST);
//SPISettingsTFT_SPISet(SAFE_FREQ,MSBFIRST,SPI_MODE0,DATA_SIZE_16BIT);// SDカード選択#defineSD_CSPB0// Size of the color selection boxes and the paintbrush size#defineBOXSIZE40#definePENRADIUS3intoldcolor,currentcolor;// システム情報の表示voidiinfo(){chartop='t';uint32_tadr=(uint32_t)⊤uint8_t*tmp=(uint8_t*)malloc(1);uint32_thadr=(uint32_t)tmp;free(tmp);// スタック領域先頭アドレスの表示Serial.print("Stack Top:");Serial.println(adr,HEX);// ヒープ領域先頭アドレスの表示Serial.print("Heap Top :");Serial.println(hadr,HEX);// SRAM未使用領域の表示Serial.print("SRAM Free:");Serial.println(adr-hadr,DEC);}voidsetup(void){Serial.begin(115200);delay(1000);Serial.println((uint32_t)SPI.dev(),HEX);#ifUSE_SDFAT==1if(!SD.begin(SD_CS,SPI_SPEED)){#elseif(!SD.begin(SD_CS)){#endifSerial.println("failed!");}ts.begin();tft.begin();Serial.println((uint32_t)SPI.dev(),HEX);//Serial.println(micros(),DEC);tft.fillScreen(ILI9341_BLUE);//Serial.println(micros(),DEC);Serial.println("OK!");bmpDraw("CAT.BMP",0,0);iinfo();// make the color selection boxes//SPI_2.beginTransaction(TFT_SPISet);tft.fillRect(0,0,BOXSIZE,BOXSIZE,ILI9341_RED);tft.fillRect(BOXSIZE,0,BOXSIZE,BOXSIZE,ILI9341_YELLOW);tft.fillRect(BOXSIZE*2,0,BOXSIZE,BOXSIZE,ILI9341_GREEN);tft.fillRect(BOXSIZE*3,0,BOXSIZE,BOXSIZE,ILI9341_CYAN);tft.fillRect(BOXSIZE*4,0,BOXSIZE,BOXSIZE,ILI9341_BLUE);tft.fillRect(BOXSIZE*5,0,BOXSIZE,BOXSIZE,ILI9341_MAGENTA);// select the current color 'red'tft.drawRect(0,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);currentcolor=ILI9341_RED;//SPI_2.endTransaction();}voidloop(){// Retrieve a pointTS_Pointp=ts.getPoint();if((p.z<=800)||(p.z>3000))return;int16_ttmp=p.x;p.x=p.y;p.y=tmp;Serial.print("Z = ");Serial.print(p.z);Serial.print(" X = ");Serial.print(p.x);Serial.print(" Y = ");Serial.print(p.y);
//SPI_2.beginTransaction(TFT_SPISet);// Scale from ~0 ~ TS_MAXX to tft.width using the calibration #'sp.x=tft.width()-map(p.x,TS_MINX,TS_MAXX,0,tft.width());p.y=map(p.y,TS_MINY,TS_MAXY,0,tft.height());Serial.print("(");Serial.print(p.x);Serial.print(", ");Serial.print(p.y);Serial.println(")");if(p.y<BOXSIZE){oldcolor=currentcolor;if(p.x<BOXSIZE){currentcolor=ILI9341_RED;tft.drawRect(0,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}elseif(p.x<BOXSIZE*2){currentcolor=ILI9341_YELLOW;tft.drawRect(BOXSIZE,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}elseif(p.x<BOXSIZE*3){currentcolor=ILI9341_GREEN;tft.drawRect(BOXSIZE*2,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}elseif(p.x<BOXSIZE*4){currentcolor=ILI9341_CYAN;tft.drawRect(BOXSIZE*3,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}elseif(p.x<BOXSIZE*5){currentcolor=ILI9341_BLUE;tft.drawRect(BOXSIZE*4,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}elseif(p.x<BOXSIZE*6){currentcolor=ILI9341_MAGENTA;tft.drawRect(BOXSIZE*5,0,BOXSIZE,BOXSIZE,ILI9341_WHITE);}if(oldcolor!=currentcolor){if(oldcolor==ILI9341_RED)tft.fillRect(0,0,BOXSIZE,BOXSIZE,ILI9341_RED);if(oldcolor==ILI9341_YELLOW)tft.fillRect(BOXSIZE,0,BOXSIZE,BOXSIZE,ILI9341_YELLOW);if(oldcolor==ILI9341_GREEN)tft.fillRect(BOXSIZE*2,0,BOXSIZE,BOXSIZE,ILI9341_GREEN);if(oldcolor==ILI9341_CYAN)tft.fillRect(BOXSIZE*3,0,BOXSIZE,BOXSIZE,ILI9341_CYAN);if(oldcolor==ILI9341_BLUE)tft.fillRect(BOXSIZE*4,0,BOXSIZE,BOXSIZE,ILI9341_BLUE);if(oldcolor==ILI9341_MAGENTA)tft.fillRect(BOXSIZE*5,0,BOXSIZE,BOXSIZE,ILI9341_MAGENTA);}}if(((p.y-PENRADIUS)>BOXSIZE)&&((p.y+PENRADIUS)<tft.height())){tft.fillCircle(p.x,p.y,PENRADIUS,currentcolor);}
//SPI_2.endTransaction();}// 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.#defineBUFFPIXEL20voidbmpDraw(char*filename,uint8_tx,uint16_ty){FilebmpFile;intbmpWidth,bmpHeight;// W+H in pixelsuint8_tbmpDepth;// Bit depth (currently must be 24)uint32_tbmpImageoffset;// Start of image data in fileuint32_trowSize;// Not always = bmpWidth; may have paddinguint8_tsdbuffer[3*BUFFPIXEL];// pixel buffer (R+G+B per pixel)uint8_tbuffidx=sizeof(sdbuffer);// Current position in sdbufferbooleangoodBmp=false;// Set to true on valid header parsebooleanflip=true;// BMP is stored bottom-to-topintw,h,row,col;uint8_tr,g,b;uint32_tpos=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 cardif((bmpFile=SD.open(filename))==NULL){Serial.print(F("File not found"));return;}// Parse BMP headerif(read16(bmpFile)==0x4D42){// BMP signatureSerial.print(F("File size: "));Serial.println(read32(bmpFile));(void)read32(bmpFile);// Read & ignore creator bytesbmpImageoffset=read32(bmpFile);// Start of image dataSerial.print(F("Image Offset: "));Serial.println(bmpImageoffset,DEC);// Read DIB headerSerial.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 pixelSerial.print(F("Bit Depth: "));Serial.println(bmpDepth);if((bmpDepth==24)&&(read32(bmpFile)==0)){// 0 = uncompressedgoodBmp=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 boundaryrowSize=(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 loadedw=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 boundstft.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-bottompos=bmpImageoffset+row*rowSize;if(bmpFile.position()!=pos){// Need seek?bmpFile.seek(pos);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)){// IndeedbmpFile.read(sdbuffer,sizeof(sdbuffer));buffidx=0;// Set index to beginning}// Convert pixel from BMP to TFT format, push to displayb=sdbuffer[buffidx++];g=sdbuffer[buffidx++];r=sdbuffer[buffidx++];tft.pushColor(tft.color565(r,g,b));}// end pixel}// end scanlineSerial.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_tread16(File&f){uint16_tresult;((uint8_t*)&result)[0]=f.read();// LSB((uint8_t*)&result)[1]=f.read();// MSBreturnresult;}uint32_tread32(File&f){uint32_tresult;((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();// MSBreturnresult;}
16ビットカラーのビットマップ画像対応ではbmpDraw()関数を次のように修正しています。
voidbmpDraw(char*filename,uint8_tx,uint16_ty){FilebmpFile;intbmpWidth,bmpHeight;// W+H in pixelsuint8_tbmpDepth;// Bit depth (currently must be 24)uint32_tbmpImageoffset;// Start of image data in fileuint32_trowSize;// Not always = bmpWidth; may have paddinguint8_tsdbuffer[320*2];// pixel buffer (R5+G6+B5 per pixel)uint8_tbuffidx=sizeof(sdbuffer);// Current position in sdbufferbooleangoodBmp=false;// Set to true on valid header parsebooleanflip=true;// BMP is stored bottom-to-topintw,h,row,col;uint8_tr,g,b;uint32_tpos=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 cardif((bmpFile=SD.open(filename))==NULL){Serial.print(F("File not found"));return;}// Parse BMP headerif(read16(bmpFile)==0x4D42){// BMP signatureSerial.print(F("File size: "));Serial.println(read32(bmpFile));(void)read32(bmpFile);// Read & ignore creator bytesbmpImageoffset=read32(bmpFile);// Start of image dataSerial.print(F("Image Offset: "));Serial.println(bmpImageoffset,DEC);// Read DIB headerSerial.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 pixelSerial.print(F("Bit Depth: "));Serial.println(bmpDepth);//if((bmpDepth == 16) && (read32(bmpFile) == 0)) { // 0 = uncompressedif((bmpDepth==16)){// 0 = uncompressedread32(bmpFile);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 boundaryrowSize=(bmpWidth*2+2)&~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 loadedw=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 boundstft.setAddrWindow(x,y,x+w-1,y+h-1);for(row=0;row<h;row++){// For each scanline...if(flip)// Bitmap is stored bottom-to-top order (normal BMP)pos=bmpImageoffset+(bmpHeight-1-row)*rowSize;else// Bitmap is stored top-to-bottompos=bmpImageoffset+row*rowSize;if(bmpFile.position()!=pos){// Need seek?bmpFile.seek(pos);buffidx=sizeof(sdbuffer);// Force buffer reload}bmpFile.read(sdbuffer,w*2);tft.pushColors((&sdbuffer[0]),w,1);}// end scanlineSerial.print(F("Loaded in "));Serial.print(millis()-startTime);Serial.println(" ms");}// end goodBmp}}bmpFile.close();if(!goodBmp)Serial.println(F("BMP format not recognized."));}
/*************************************************** This is our Bitmap drawing example for the Adafruit ILI9341 Breakout and Shield ----> http://www.adafruit.com/products/1651 Check out the links above for our tutorials and wiring diagrams These displays use SPI to communicate, 4 or 5 pins are required to interface (RST is optional) Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. MIT license, all text above must be included in any redistribution ****************************************************/#include<Adafruit_GFX_AS.h>// Core graphics library#include"Adafruit_ILI9341_STM.h"// Hardware-specific library#include<SPI.h>#include<SD.h>// TFT display and SD card will share the hardware SPI interface.// Hardware SPI pins are specific to the Arduino board type and// cannot be remapped to alternate pins. For Arduino Uno,// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.#defineTS_MINX600#defineTS_MINY440#defineTS_MAXX3460#defineTS_MAXY3460// TFT制御用ピン#defineTFT_CSPA0#defineTFT_RSTPA1#defineTFT_DCPA2// TFT制御用Adafruit_ILI9341_STMtft=Adafruit_ILI9341_STM(TFT_CS,TFT_DC,TFT_RST);SPISettingsTFT_SPISet(SAFE_FREQ,MSBFIRST,SPI_MODE0,DATA_SIZE_16BIT);SPIClassSPI_2(2);// SDカード選択#defineSD_CSPB0voidsetup(void){Serial.begin(115200);delay(1000);Serial.println((uint32_t)SPI.dev(),HEX);tft.begin(SPI_2);Serial.println((uint32_t)SPI.dev(),HEX);tft.fillScreen(ILI9341_BLUE);SPI.setModule(1);if(!SD.begin(SD_CS)){Serial.println("failed!");}Serial.println("OK!");bmpDraw("CAT.BMP",0,0);}voidloop(){}// 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.#defineBUFFPIXEL20voidbmpDraw(char*filename,uint8_tx,uint16_ty){FilebmpFile;intbmpWidth,bmpHeight;// W+H in pixelsuint8_tbmpDepth;// Bit depth (currently must be 24)uint32_tbmpImageoffset;// Start of image data in fileuint32_trowSize;// Not always = bmpWidth; may have paddinguint8_tsdbuffer[3*BUFFPIXEL];// pixel buffer (R+G+B per pixel)uint8_tbuffidx=sizeof(sdbuffer);// Current position in sdbufferbooleangoodBmp=false;// Set to true on valid header parsebooleanflip=true;// BMP is stored bottom-to-topintw,h,row,col;uint8_tr,g,b;uint32_tpos=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 cardif((bmpFile=SD.open(filename))==NULL){Serial.print(F("File not found"));return;}// Parse BMP headerif(read16(bmpFile)==0x4D42){// BMP signatureSerial.print(F("File size: "));Serial.println(read32(bmpFile));(void)read32(bmpFile);// Read & ignore creator bytesbmpImageoffset=read32(bmpFile);// Start of image dataSerial.print(F("Image Offset: "));Serial.println(bmpImageoffset,DEC);// Read DIB headerSerial.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 pixelSerial.print(F("Bit Depth: "));Serial.println(bmpDepth);if((bmpDepth==24)&&(read32(bmpFile)==0)){// 0 = uncompressedgoodBmp=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 boundaryrowSize=(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 loadedw=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 boundstft.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-bottompos=bmpImageoffset+row*rowSize;if(bmpFile.position()!=pos){// Need seek?bmpFile.seek(pos);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)){// IndeedbmpFile.read(sdbuffer,sizeof(sdbuffer));buffidx=0;// Set index to beginning}// Convert pixel from BMP to TFT format, push to displayb=sdbuffer[buffidx++];g=sdbuffer[buffidx++];r=sdbuffer[buffidx++];SPI.setModule(2);tft.pushColor(tft.color565(r,g,b));SPI.setModule(1);}// end pixel}// end scanlineSerial.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_tread16(File&f){uint16_tresult;((uint8_t*)&result)[0]=f.read();// LSB((uint8_t*)&result)[1]=f.read();// MSBreturnresult;}uint32_tread32(File&f){uint32_tresult;((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();// MSBreturnresult;}
最近のコメント