This demo reads a bitmap from a FAT formatted SD-card, copies it to flash and displays it on the screen. The demo is based on the following project: https://os.mbed.com/users/DieterGraef/code/DISCO-F746NG_SDFileSystem/
Dependencies: LCD_DISCO_F746NG TS_DISCO_F746NG mbed FATFileSystem
Fork of DISCO-F746NG_SDFileSystem by
main.cpp@4:95e30a911d97, 2018-04-19 (annotated)
- Committer:
- Lightsource
- Date:
- Thu Apr 19 19:59:54 2018 +0000
- Revision:
- 4:95e30a911d97
- Parent:
- 1:7f463f6f904e
Demo that reads a bitmap from SD-card, copies it to flash and displays it on the screen.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DieterGraef | 0:134f7a094930 | 1 | #include "mbed.h" |
DieterGraef | 0:134f7a094930 | 2 | #include "SDFileSystem.h" |
Lightsource | 4:95e30a911d97 | 3 | #include "LCD_DISCO_F746NG.h" |
Lightsource | 4:95e30a911d97 | 4 | #include "TS_DISCO_F746NG.h" |
DieterGraef | 0:134f7a094930 | 5 | #include <stdio.h> |
Lightsource | 4:95e30a911d97 | 6 | #include <stdlib.h> |
Lightsource | 4:95e30a911d97 | 7 | #include <string> |
DieterGraef | 0:134f7a094930 | 8 | |
Lightsource | 4:95e30a911d97 | 9 | void drawImage(char * name, uint16_t x, uint16_t y); |
Lightsource | 4:95e30a911d97 | 10 | |
Lightsource | 4:95e30a911d97 | 11 | LCD_DISCO_F746NG lcd; |
DieterGraef | 0:134f7a094930 | 12 | DigitalOut myled(LED1); |
DieterGraef | 0:134f7a094930 | 13 | SDFileSystem sd("sd"); |
Lightsource | 4:95e30a911d97 | 14 | TS_DISCO_F746NG ts; |
DieterGraef | 0:134f7a094930 | 15 | |
Lightsource | 4:95e30a911d97 | 16 | int main() { |
Lightsource | 4:95e30a911d97 | 17 | uint8_t TS_Status; |
Lightsource | 4:95e30a911d97 | 18 | |
Lightsource | 4:95e30a911d97 | 19 | lcd.SetBackColor(LCD_COLOR_BLACK); |
Lightsource | 4:95e30a911d97 | 20 | |
Lightsource | 4:95e30a911d97 | 21 | // In this example, the TS is initialized but not used. |
DieterGraef | 0:134f7a094930 | 22 | |
Lightsource | 4:95e30a911d97 | 23 | TS_Status = ts.Init(lcd.GetXSize(), lcd.GetYSize()); |
Lightsource | 4:95e30a911d97 | 24 | if (TS_Status != TS_OK) { |
Lightsource | 4:95e30a911d97 | 25 | lcd.Clear(LCD_COLOR_RED); |
Lightsource | 4:95e30a911d97 | 26 | lcd.SetBackColor(LCD_COLOR_RED); |
Lightsource | 4:95e30a911d97 | 27 | lcd.SetTextColor(LCD_COLOR_WHITE); |
Lightsource | 4:95e30a911d97 | 28 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE); |
Lightsource | 4:95e30a911d97 | 29 | } else { |
Lightsource | 4:95e30a911d97 | 30 | lcd.Clear(LCD_COLOR_BLACK); |
Lightsource | 4:95e30a911d97 | 31 | lcd.SetBackColor(LCD_COLOR_BLACK); |
Lightsource | 4:95e30a911d97 | 32 | lcd.SetTextColor(LCD_COLOR_WHITE); |
Lightsource | 4:95e30a911d97 | 33 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE); |
Lightsource | 4:95e30a911d97 | 34 | } |
Lightsource | 4:95e30a911d97 | 35 | |
Lightsource | 4:95e30a911d97 | 36 | wait(1); |
Lightsource | 4:95e30a911d97 | 37 | |
Lightsource | 4:95e30a911d97 | 38 | //Mounting SD-based filesystem |
Lightsource | 4:95e30a911d97 | 39 | sd.mount(); |
Lightsource | 4:95e30a911d97 | 40 | |
Lightsource | 4:95e30a911d97 | 41 | //Drawing BMP file from SD-card |
Lightsource | 4:95e30a911d97 | 42 | drawImage("/sd/interface.bmp", 0, 0); |
DieterGraef | 0:134f7a094930 | 43 | } |
DieterGraef | 0:134f7a094930 | 44 | |
DieterGraef | 0:134f7a094930 | 45 | |
Lightsource | 4:95e30a911d97 | 46 | void drawImage(char * name, uint16_t x, uint16_t y){ |
Lightsource | 4:95e30a911d97 | 47 | int fileSize; |
Lightsource | 4:95e30a911d97 | 48 | char * buffer; |
Lightsource | 4:95e30a911d97 | 49 | FILE *Image = fopen(name, "rb"); // open the bmp file |
Lightsource | 4:95e30a911d97 | 50 | |
Lightsource | 4:95e30a911d97 | 51 | //obtain file size: |
Lightsource | 4:95e30a911d97 | 52 | fseek (Image , 0 , SEEK_END); |
Lightsource | 4:95e30a911d97 | 53 | fileSize = ftell (Image); |
Lightsource | 4:95e30a911d97 | 54 | rewind (Image); |
Lightsource | 4:95e30a911d97 | 55 | |
Lightsource | 4:95e30a911d97 | 56 | // allocate memory to contain the whole file: |
Lightsource | 4:95e30a911d97 | 57 | buffer = (char*) malloc (sizeof(char)*fileSize); |
Lightsource | 4:95e30a911d97 | 58 | |
Lightsource | 4:95e30a911d97 | 59 | // copy the file into the buffer: |
Lightsource | 4:95e30a911d97 | 60 | fseek (Image, 0 , SEEK_SET ); |
Lightsource | 4:95e30a911d97 | 61 | // set SD file data start position |
Lightsource | 4:95e30a911d97 | 62 | fread (buffer,1,fileSize,Image); |
Lightsource | 4:95e30a911d97 | 63 | fclose (Image); |
DieterGraef | 0:134f7a094930 | 64 | |
Lightsource | 4:95e30a911d97 | 65 | //Draw image |
Lightsource | 4:95e30a911d97 | 66 | lcd.DrawBitmap(x,y,(uint8_t *)buffer); |
Lightsource | 4:95e30a911d97 | 67 | |
Lightsource | 4:95e30a911d97 | 68 | //Free allocated memory |
Lightsource | 4:95e30a911d97 | 69 | free (buffer); |
Lightsource | 4:95e30a911d97 | 70 | } |