see https://developer.mbed.org/users/okini3939/notebook/graphic-poi/

Dependencies:   IAP RAM_DISK USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers file.cpp Source File

file.cpp

00001 #include "mbed.h"
00002 #include "USBMSD_Ram.h"
00003 #include "IAP.h"
00004 #include "file.h"
00005 
00006 //USBMSD_Ram msd;
00007 IAP iap;
00008 extern DigitalOut led;
00009 
00010 int parseSector = 0;
00011 int modeBmp = 0;
00012 int filesize;
00013 int count = 0;
00014 int sector = 0;
00015 
00016 int parseBmp (const uint8_t *buf, int len) {
00017     int i, n = 0;
00018 
00019     if (modeBmp == 0) {
00020         struct BmpHeader *header = (struct BmpHeader *)buf;
00021         if (header->magic[0] != 'B' || header->magic[1] != 'M') return -1;
00022         filesize = header->size;
00023         if (header->header_size != 40) return -1;
00024         DBG(" %d %d %d %d\r\n", filesize, header->width, header->height, header->bits);
00025         if (header->compression != 0 || header->ncolours != 0) return -1;
00026         if (header->size >= 4096 || header->bits != 24) return -1;
00027 
00028         __disable_irq();
00029         iap.prepare(FLASH_SECTOR + sector, FLASH_SECTOR + sector);
00030         iap.erase(FLASH_SECTOR + sector, FLASH_SECTOR + sector);
00031         __enable_irq();
00032         count = 0;
00033         modeBmp = 1;
00034     }
00035     
00036     if (modeBmp) {
00037         led = 0;
00038         __disable_irq();
00039         iap.prepare(FLASH_SECTOR + sector, FLASH_SECTOR + sector);
00040         iap.write((char*)buf, sector_start_adress[FLASH_SECTOR + sector] + 512 * count, len);
00041         __enable_irq();
00042         count += len / 512;
00043         filesize -= len;
00044 
00045         if (filesize <= 0 || count >= (FLASH_SECTOR_SIZE / 512)) {
00046             modeBmp = 0;
00047             led = 1;
00048             sector ++;
00049             if (sector >= MAX_SECTOR) {
00050                 sector = 0;
00051             }
00052             return -1;
00053         }
00054     }
00055     return 0;
00056 }
00057 
00058 void parseFat(int sector, const uint8_t *buf, int len) {
00059     int i;
00060 
00061     if (sector == 2) {
00062         // root dir
00063         struct FatInfo *fat = (struct FatInfo *)buf;
00064         parseSector = 0;
00065         for (i = 0; i < 16; i ++) {
00066             if (fat[i].Name[0] == 0) break;
00067             if (fat[i].Name[0] != 0xe5 && fat[i].Name[8] == 'B' && fat[i].Name[9] == 'M' && fat[i].Name[10] == 'P') {
00068                 parseSector = (fat[i].FstClusHI<<16) || fat[i].FstClusLO;
00069                 break;
00070             }
00071         }
00072     } else
00073     if (sector > 2) {
00074         // data
00075         if (parseSector && sector >= parseSector) {
00076             if (parseBmp(buf, len)) {
00077                 parseSector = 0;
00078             }
00079         } else {
00080             parseSector = 0;
00081         }
00082     }
00083 }
00084 
00085 char *getPicture (int n) {
00086     char *p = sector_start_adress[FLASH_SECTOR + n];
00087     if (p[0] == 'B' && p[1] == 'M') {
00088         return p;
00089     }
00090     return NULL;
00091 }
00092 
00093 void workMsd () {
00094     USBMSD_Ram msd;
00095 
00096     msd.parseFat = &parseFat;
00097 
00098     for (;;) {
00099         led = 1;
00100         wait(1);
00101     }
00102 }