![](/media/cache/profiles/2e72ed76b7b3b9ce2f15fce64178dfaf.jpg.50x50_q85.jpg)
Shows how to use a display, the onboard SD Card and the onboard SPI Flash. Requires a display module with direct Arduino pinning
Dependencies: DmTftLibrary SDFileSystem mbed
main.cpp@4:93ca338876e2, 2014-07-09 (annotated)
- Committer:
- displaymodule
- Date:
- Wed Jul 09 09:38:56 2014 +0000
- Revision:
- 4:93ca338876e2
- Parent:
- 3:e1e1053e286f
- Child:
- 6:0f69891b105e
Updated library dependencies. Fixed warning. Fixed file position bug.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
displaymodule | 0:3ecd25651727 | 1 | /********************************************************************************************** |
displaymodule | 0:3ecd25651727 | 2 | Copyright (c) 2014 DisplayModule. All rights reserved. |
displaymodule | 0:3ecd25651727 | 3 | |
displaymodule | 0:3ecd25651727 | 4 | Redistribution and use of this source code, part of this source code or any compiled binary |
displaymodule | 0:3ecd25651727 | 5 | based on this source code is permitted as long as the above copyright notice and following |
displaymodule | 0:3ecd25651727 | 6 | disclaimer is retained. |
displaymodule | 0:3ecd25651727 | 7 | |
displaymodule | 0:3ecd25651727 | 8 | DISCLAIMER: |
displaymodule | 0:3ecd25651727 | 9 | THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT ANY WARRANTIES AND SUPPORT. DISPLAYMODULE ASSUMES |
displaymodule | 0:3ecd25651727 | 10 | NO RESPONSIBILITY OR LIABILITY FOR THE USE OF THE SOFTWARE. |
displaymodule | 0:3ecd25651727 | 11 | ********************************************************************************************/ |
displaymodule | 0:3ecd25651727 | 12 | |
displaymodule | 0:3ecd25651727 | 13 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 14 | * Includes |
displaymodule | 0:3ecd25651727 | 15 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 16 | |
displaymodule | 0:3ecd25651727 | 17 | #include "mbed.h" |
displaymodule | 0:3ecd25651727 | 18 | |
displaymodule | 0:3ecd25651727 | 19 | #include "DmTftIli9341.h" |
displaymodule | 0:3ecd25651727 | 20 | #include "DmTftSsd2119.h" |
displaymodule | 0:3ecd25651727 | 21 | #include "DmTouch.h" |
displaymodule | 0:3ecd25651727 | 22 | |
displaymodule | 0:3ecd25651727 | 23 | #include "W25Q16BV.h" |
displaymodule | 0:3ecd25651727 | 24 | #include "DmDrawBmpBase.h" |
displaymodule | 0:3ecd25651727 | 25 | #include "SDFileSystem.h" |
displaymodule | 0:3ecd25651727 | 26 | |
displaymodule | 0:3ecd25651727 | 27 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 28 | * Typedefs and defines |
displaymodule | 0:3ecd25651727 | 29 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 30 | |
displaymodule | 2:a77053c4f5cc | 31 | /* Note that there are restrictions on which platforms that can use printf |
displaymodule | 2:a77053c4f5cc | 32 | in combinations with the DmTftLibrary. Some platforms (e.g. LPC1549 LPCXpresso) |
displaymodule | 2:a77053c4f5cc | 33 | use the same pins for USBRX/USBTX and display control. Printing will |
displaymodule | 2:a77053c4f5cc | 34 | cause the display to not work. Read more about this on the display's notebook |
displaymodule | 2:a77053c4f5cc | 35 | page. */ |
displaymodule | 3:e1e1053e286f | 36 | #define log(...) printf(__VA_ARGS__) |
displaymodule | 3:e1e1053e286f | 37 | //#define log(...) |
displaymodule | 0:3ecd25651727 | 38 | |
displaymodule | 0:3ecd25651727 | 39 | #define DM_PIN_SPI_MOSI D11 |
displaymodule | 0:3ecd25651727 | 40 | #define DM_PIN_SPI_MISO D12 |
displaymodule | 0:3ecd25651727 | 41 | #define DM_PIN_SPI_SCLK D13 |
displaymodule | 0:3ecd25651727 | 42 | |
displaymodule | 0:3ecd25651727 | 43 | #define DM_PIN_CS_TOUCH D4 |
displaymodule | 0:3ecd25651727 | 44 | #define DM_PIN_CS_TFT D10 |
displaymodule | 0:3ecd25651727 | 45 | #define DM_PIN_CS_SDCARD D8 |
displaymodule | 0:3ecd25651727 | 46 | #define DM_PIN_CS_FLASH D6 |
displaymodule | 0:3ecd25651727 | 47 | |
displaymodule | 0:3ecd25651727 | 48 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 49 | * Local variables |
displaymodule | 0:3ecd25651727 | 50 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 51 | |
displaymodule | 2:a77053c4f5cc | 52 | //DmTftIli9341 tft; /* DM_TFT28_105 */ |
displaymodule | 2:a77053c4f5cc | 53 | DmTftSsd2119 tft; /* DM_TFT35_107 */ |
displaymodule | 0:3ecd25651727 | 54 | |
displaymodule | 2:a77053c4f5cc | 55 | //DmTouch touch(DmTouch::DM_TFT28_105); |
displaymodule | 2:a77053c4f5cc | 56 | DmTouch touch(DmTouch::DM_TFT35_107); |
displaymodule | 0:3ecd25651727 | 57 | |
displaymodule | 0:3ecd25651727 | 58 | SDFileSystem sd(DM_PIN_SPI_MOSI, DM_PIN_SPI_MISO, DM_PIN_SPI_SCLK, DM_PIN_CS_SDCARD, "sd"); // mosi,miso,clk,cs |
displaymodule | 0:3ecd25651727 | 59 | W25Q16BV flash(DM_PIN_SPI_MOSI, DM_PIN_SPI_MISO, DM_PIN_SPI_SCLK, DM_PIN_CS_FLASH); // mosi,miso,clk,cs |
displaymodule | 0:3ecd25651727 | 60 | |
displaymodule | 0:3ecd25651727 | 61 | DigitalInOut csTouch(DM_PIN_CS_TOUCH, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:3ecd25651727 | 62 | DigitalInOut csDisplay(DM_PIN_CS_TFT, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:3ecd25651727 | 63 | DigitalInOut csSDCard(DM_PIN_CS_SDCARD, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:3ecd25651727 | 64 | DigitalInOut csFlash(DM_PIN_CS_FLASH, PIN_OUTPUT, PullUp, 1); |
displaymodule | 0:3ecd25651727 | 65 | |
displaymodule | 1:76ec0e6dfe81 | 66 | |
displaymodule | 0:3ecd25651727 | 67 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 68 | * Global variables |
displaymodule | 0:3ecd25651727 | 69 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 70 | |
displaymodule | 0:3ecd25651727 | 71 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 72 | * Local functions |
displaymodule | 0:3ecd25651727 | 73 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 74 | |
displaymodule | 0:3ecd25651727 | 75 | static bool sdcardReader(uint32_t userData, uint8_t* data, int offset, int numBytes) { |
displaymodule | 0:3ecd25651727 | 76 | FILE* fp = (FILE*)userData; |
displaymodule | 0:3ecd25651727 | 77 | fseek(fp, offset, SEEK_SET); |
displaymodule | 0:3ecd25651727 | 78 | fread(data, 1, numBytes, fp); |
displaymodule | 0:3ecd25651727 | 79 | return true; |
displaymodule | 0:3ecd25651727 | 80 | } |
displaymodule | 0:3ecd25651727 | 81 | |
displaymodule | 0:3ecd25651727 | 82 | static bool spiFlashReader(uint32_t userData, uint8_t* data, int offset, int numBytes) { |
displaymodule | 0:3ecd25651727 | 83 | W25Q16BV* spiFlash = (W25Q16BV*)userData; |
displaymodule | 0:3ecd25651727 | 84 | spiFlash->readStream(offset, (char*)data, numBytes); |
displaymodule | 0:3ecd25651727 | 85 | return true; |
displaymodule | 0:3ecd25651727 | 86 | } |
displaymodule | 0:3ecd25651727 | 87 | |
displaymodule | 0:3ecd25651727 | 88 | static void copyImageToSPIFlash(const char* fname) { |
displaymodule | 0:3ecd25651727 | 89 | FILE* fp = fopen(fname, "r"); |
displaymodule | 0:3ecd25651727 | 90 | int num, offset; |
displaymodule | 0:3ecd25651727 | 91 | uint8_t buff[128]; |
displaymodule | 0:3ecd25651727 | 92 | |
displaymodule | 0:3ecd25651727 | 93 | do { |
displaymodule | 0:3ecd25651727 | 94 | if (fp == NULL) { |
displaymodule | 0:3ecd25651727 | 95 | log("Failed to open %s, so assuming SPI flash is correct\n", fname); |
displaymodule | 0:3ecd25651727 | 96 | break; |
displaymodule | 0:3ecd25651727 | 97 | } |
displaymodule | 0:3ecd25651727 | 98 | |
displaymodule | 0:3ecd25651727 | 99 | // simple comparison of first 64 bytes in file and SPI flash |
displaymodule | 0:3ecd25651727 | 100 | // to avoid having to write to the flash on every boot |
displaymodule | 0:3ecd25651727 | 101 | num = fread(buff, 1, 64, fp); |
displaymodule | 0:3ecd25651727 | 102 | if (num == 64) { |
displaymodule | 0:3ecd25651727 | 103 | flash.readStream(0, (char*)&buff[64], 64); |
displaymodule | 0:3ecd25651727 | 104 | if (memcmp(buff, &buff[64], 64) == 0) { |
displaymodule | 0:3ecd25651727 | 105 | log("SPI flash content propably correct, not replacing it\n"); |
displaymodule | 0:3ecd25651727 | 106 | break; |
displaymodule | 0:3ecd25651727 | 107 | } |
displaymodule | 0:3ecd25651727 | 108 | } |
displaymodule | 4:93ca338876e2 | 109 | fseek(fp, 0, SEEK_SET); |
displaymodule | 0:3ecd25651727 | 110 | |
displaymodule | 0:3ecd25651727 | 111 | log("Found %s, erasing SPI flash\n", fname); |
displaymodule | 0:3ecd25651727 | 112 | flash.chipErase(); |
displaymodule | 0:3ecd25651727 | 113 | |
displaymodule | 0:3ecd25651727 | 114 | log("Copying %s to SPI flash\n", fname); |
displaymodule | 0:3ecd25651727 | 115 | offset = 0; |
displaymodule | 0:3ecd25651727 | 116 | while ((num = fread(buff, 1, 128, fp)) > 0) { |
displaymodule | 0:3ecd25651727 | 117 | flash.writeStream(offset, (char*)buff, num); |
displaymodule | 0:3ecd25651727 | 118 | offset+=num; |
displaymodule | 0:3ecd25651727 | 119 | } |
displaymodule | 0:3ecd25651727 | 120 | log("Done. Wrote %d bytes to SPI flash\n", offset); |
displaymodule | 0:3ecd25651727 | 121 | |
displaymodule | 0:3ecd25651727 | 122 | } while(false); |
displaymodule | 0:3ecd25651727 | 123 | |
displaymodule | 0:3ecd25651727 | 124 | if (fp != NULL) { |
displaymodule | 0:3ecd25651727 | 125 | fclose(fp); |
displaymodule | 0:3ecd25651727 | 126 | } |
displaymodule | 0:3ecd25651727 | 127 | } |
displaymodule | 0:3ecd25651727 | 128 | |
displaymodule | 0:3ecd25651727 | 129 | /****************************************************************************** |
displaymodule | 0:3ecd25651727 | 130 | * Main |
displaymodule | 0:3ecd25651727 | 131 | *****************************************************************************/ |
displaymodule | 0:3ecd25651727 | 132 | |
displaymodule | 0:3ecd25651727 | 133 | int main() { |
displaymodule | 0:3ecd25651727 | 134 | DmDrawBmpBase bmp; |
displaymodule | 0:3ecd25651727 | 135 | const char* fname = "/sd/logop565.bmp"; |
displaymodule | 0:3ecd25651727 | 136 | |
displaymodule | 0:3ecd25651727 | 137 | copyImageToSPIFlash(fname); |
displaymodule | 0:3ecd25651727 | 138 | |
displaymodule | 0:3ecd25651727 | 139 | log("init tft \r\n"); |
displaymodule | 0:3ecd25651727 | 140 | tft.init(); |
displaymodule | 0:3ecd25651727 | 141 | |
displaymodule | 0:3ecd25651727 | 142 | while (true) { |
displaymodule | 0:3ecd25651727 | 143 | log("Drawing from SD card (%s) ...\n", fname); |
displaymodule | 0:3ecd25651727 | 144 | FILE *fp = fopen(fname, "r"); |
displaymodule | 0:3ecd25651727 | 145 | if (fp != NULL) { |
displaymodule | 0:3ecd25651727 | 146 | bmp.drawBitmap(tft, 0, 0, sdcardReader, (uint32_t)fp); |
displaymodule | 0:3ecd25651727 | 147 | fclose(fp); |
displaymodule | 0:3ecd25651727 | 148 | } else { |
displaymodule | 0:3ecd25651727 | 149 | log("failed to open file\n"); |
displaymodule | 1:76ec0e6dfe81 | 150 | log("Skipping SDCard reading as it is unsupported\n"); |
displaymodule | 1:76ec0e6dfe81 | 151 | tft.drawStringCentered(0, 0, tft.width(), tft.height(), "SD Card Unsupported!"); |
displaymodule | 0:3ecd25651727 | 152 | } |
displaymodule | 0:3ecd25651727 | 153 | |
displaymodule | 0:3ecd25651727 | 154 | wait_ms(3000); |
displaymodule | 0:3ecd25651727 | 155 | tft.clearScreen(RED); |
displaymodule | 0:3ecd25651727 | 156 | wait_ms(1000); |
displaymodule | 0:3ecd25651727 | 157 | |
displaymodule | 0:3ecd25651727 | 158 | log("Attempting to decode image from SPI flash...\n"); |
displaymodule | 1:76ec0e6dfe81 | 159 | if (!bmp.drawBitmap(tft, 0, 0, spiFlashReader, (uint32_t)&flash)) { |
displaymodule | 1:76ec0e6dfe81 | 160 | log("Skipping SPI Flash reading as it is unsupported\n"); |
displaymodule | 1:76ec0e6dfe81 | 161 | tft.drawStringCentered(0, 0, tft.width(), tft.height(), "SPI Flash Unsupported!"); |
displaymodule | 1:76ec0e6dfe81 | 162 | } |
displaymodule | 0:3ecd25651727 | 163 | |
displaymodule | 0:3ecd25651727 | 164 | wait_ms(3000); |
displaymodule | 0:3ecd25651727 | 165 | tft.clearScreen(GREEN); |
displaymodule | 0:3ecd25651727 | 166 | wait_ms(1000); |
displaymodule | 0:3ecd25651727 | 167 | } |
displaymodule | 0:3ecd25651727 | 168 | } |