This program show jpeg file in microSD card, run on Nucleo and Aitendo 2.4 inch TFT shield.
Dependencies: AitendoTFT SDFileSystem TinyJpgDec mbed
Fork of SDFileSystem_HelloWorld by
main.cpp@1:28ab8d9ab8e4, 2014-04-30 (annotated)
- Committer:
- h_nari
- Date:
- Wed Apr 30 08:07:51 2014 +0000
- Revision:
- 1:28ab8d9ab8e4
- Parent:
- 0:bdbd3d6fc5d5
First Version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:bdbd3d6fc5d5 | 1 | #include "mbed.h" |
mbed_official | 0:bdbd3d6fc5d5 | 2 | #include "SDFileSystem.h" |
h_nari | 1:28ab8d9ab8e4 | 3 | #include "AitendoTFT.h" |
h_nari | 1:28ab8d9ab8e4 | 4 | #include "TinyJpgDec.h" |
h_nari | 1:28ab8d9ab8e4 | 5 | |
h_nari | 1:28ab8d9ab8e4 | 6 | AitendoTFT tft; |
h_nari | 1:28ab8d9ab8e4 | 7 | SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, "sd"); // the pinout on the mbed Cool Components workshop board |
h_nari | 1:28ab8d9ab8e4 | 8 | |
h_nari | 1:28ab8d9ab8e4 | 9 | JDEC jdec; |
h_nari | 1:28ab8d9ab8e4 | 10 | WORD work[3100/sizeof(WORD)]; |
h_nari | 1:28ab8d9ab8e4 | 11 | FileHandle *fh; |
h_nari | 1:28ab8d9ab8e4 | 12 | |
h_nari | 1:28ab8d9ab8e4 | 13 | UINT jpeg_input_func(JDEC *jd, BYTE *buff, UINT ndata) |
h_nari | 1:28ab8d9ab8e4 | 14 | { |
h_nari | 1:28ab8d9ab8e4 | 15 | if(buff) { |
h_nari | 1:28ab8d9ab8e4 | 16 | size_t n = fh->read(buff, ndata); |
h_nari | 1:28ab8d9ab8e4 | 17 | return n == (size_t)-1 ? 0 : n; |
h_nari | 1:28ab8d9ab8e4 | 18 | } else { |
h_nari | 1:28ab8d9ab8e4 | 19 | off_t t = fh->lseek( ndata, SEEK_CUR); |
h_nari | 1:28ab8d9ab8e4 | 20 | return t == (off_t)-1 ? 0 : ndata; |
mbed_official | 0:bdbd3d6fc5d5 | 21 | } |
h_nari | 1:28ab8d9ab8e4 | 22 | } |
h_nari | 1:28ab8d9ab8e4 | 23 | |
h_nari | 1:28ab8d9ab8e4 | 24 | UINT jpeg_output_func(JDEC *jd, void *bitmap, JRECT *rect) |
h_nari | 1:28ab8d9ab8e4 | 25 | { |
h_nari | 1:28ab8d9ab8e4 | 26 | WORD *src = (WORD *)bitmap; |
h_nari | 1:28ab8d9ab8e4 | 27 | int x0 = rect->left; |
h_nari | 1:28ab8d9ab8e4 | 28 | int x1 = rect->right; |
h_nari | 1:28ab8d9ab8e4 | 29 | int y0 = rect->top; |
h_nari | 1:28ab8d9ab8e4 | 30 | int y1 = rect->bottom; |
h_nari | 1:28ab8d9ab8e4 | 31 | int w = x1 - x0 + 1; |
h_nari | 1:28ab8d9ab8e4 | 32 | |
h_nari | 1:28ab8d9ab8e4 | 33 | if(y0 >= AitendoTFT::TFT_HEIGHT ||x0 >= AitendoTFT::TFT_WIDTH) |
h_nari | 1:28ab8d9ab8e4 | 34 | return 1; |
h_nari | 1:28ab8d9ab8e4 | 35 | |
h_nari | 1:28ab8d9ab8e4 | 36 | if(x1 > AitendoTFT::TFT_WIDTH-1) x1 = AitendoTFT::TFT_WIDTH - 1; |
h_nari | 1:28ab8d9ab8e4 | 37 | if(y1 > AitendoTFT::TFT_HEIGHT-1) y1 = AitendoTFT::TFT_HEIGHT - 1; |
h_nari | 1:28ab8d9ab8e4 | 38 | |
h_nari | 1:28ab8d9ab8e4 | 39 | if(x0 == 0) |
h_nari | 1:28ab8d9ab8e4 | 40 | printf("\r %d", y0); |
h_nari | 1:28ab8d9ab8e4 | 41 | |
h_nari | 1:28ab8d9ab8e4 | 42 | for(int y= y0; y <= y1; y++) { |
h_nari | 1:28ab8d9ab8e4 | 43 | tft.setPos(x0, y); |
h_nari | 1:28ab8d9ab8e4 | 44 | WORD *p = src + w * (y - y0); |
h_nari | 1:28ab8d9ab8e4 | 45 | for(int x=x0; x <= x1; x++) |
h_nari | 1:28ab8d9ab8e4 | 46 | tft.put(*p++); |
h_nari | 1:28ab8d9ab8e4 | 47 | } |
h_nari | 1:28ab8d9ab8e4 | 48 | return 1; |
mbed_official | 0:bdbd3d6fc5d5 | 49 | } |
h_nari | 1:28ab8d9ab8e4 | 50 | |
h_nari | 1:28ab8d9ab8e4 | 51 | int main() |
h_nari | 1:28ab8d9ab8e4 | 52 | { |
h_nari | 1:28ab8d9ab8e4 | 53 | JRESULT r; |
h_nari | 1:28ab8d9ab8e4 | 54 | int cFile; |
h_nari | 1:28ab8d9ab8e4 | 55 | char path[80]; |
h_nari | 1:28ab8d9ab8e4 | 56 | |
h_nari | 1:28ab8d9ab8e4 | 57 | |
h_nari | 1:28ab8d9ab8e4 | 58 | printf("Nucleo Picture Viewer\n"); |
h_nari | 1:28ab8d9ab8e4 | 59 | |
h_nari | 1:28ab8d9ab8e4 | 60 | while(1) { |
h_nari | 1:28ab8d9ab8e4 | 61 | cFile = 0; |
h_nari | 1:28ab8d9ab8e4 | 62 | DirHandle *dh = sd.opendir("/images"); |
h_nari | 1:28ab8d9ab8e4 | 63 | if(dh == NULL) |
h_nari | 1:28ab8d9ab8e4 | 64 | error("/images not found.\n"); |
h_nari | 1:28ab8d9ab8e4 | 65 | else { |
h_nari | 1:28ab8d9ab8e4 | 66 | while(1) { |
h_nari | 1:28ab8d9ab8e4 | 67 | struct dirent *de = dh->readdir(); |
h_nari | 1:28ab8d9ab8e4 | 68 | if(de == NULL) break; |
h_nari | 1:28ab8d9ab8e4 | 69 | char *ext = strrchr(de->d_name,'.'); |
h_nari | 1:28ab8d9ab8e4 | 70 | if(ext == NULL || strcmp(ext,".jpg")!=0) continue; |
h_nari | 1:28ab8d9ab8e4 | 71 | cFile++; |
h_nari | 1:28ab8d9ab8e4 | 72 | snprintf(path, sizeof path, "images/%s", de->d_name); |
h_nari | 1:28ab8d9ab8e4 | 73 | printf("\nshow %s\n", path); |
h_nari | 1:28ab8d9ab8e4 | 74 | fh = sd.open(path, 0); |
h_nari | 1:28ab8d9ab8e4 | 75 | if(fh == NULL) error("%s not found.",path); |
h_nari | 1:28ab8d9ab8e4 | 76 | |
h_nari | 1:28ab8d9ab8e4 | 77 | r = jd_prepare(&jdec, jpeg_input_func, work, sizeof work, fh); |
h_nari | 1:28ab8d9ab8e4 | 78 | if(r != JDR_OK) error("jd_prepare error:%d", r); |
h_nari | 1:28ab8d9ab8e4 | 79 | r = jd_decomp(&jdec, jpeg_output_func, 0); |
h_nari | 1:28ab8d9ab8e4 | 80 | fh->close(); |
h_nari | 1:28ab8d9ab8e4 | 81 | if( r != JDR_OK) error("jd_decomp error:%d", r); |
h_nari | 1:28ab8d9ab8e4 | 82 | wait(10); |
h_nari | 1:28ab8d9ab8e4 | 83 | } |
h_nari | 1:28ab8d9ab8e4 | 84 | dh->closedir(); |
h_nari | 1:28ab8d9ab8e4 | 85 | } |
h_nari | 1:28ab8d9ab8e4 | 86 | if(cFile == 0) error("no .jpg file found in /images"); |
h_nari | 1:28ab8d9ab8e4 | 87 | } |
h_nari | 1:28ab8d9ab8e4 | 88 | } |