lcd_example.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // ZIP library example sketch
  3. //
  4. // Written by Larry Bank
  5. // June 9, 2021
  6. //
  7. // This example shows how to do the following:
  8. // - Step through all of the files in a ZIP archive
  9. // - Display the name of each file
  10. // - Allocate a buffer for the uncompressed file size
  11. // - Read (decompress) the data into the buffer
  12. // - Display the results (in this case a BMP file) on the SPI LCD
  13. //
  14. #include <unzipLIB.h>
  15. #include <bb_spi_lcd.h>
  16. // Set these to the appropriate values for your SPI LCD display
  17. // -1 means the pin is not connected
  18. // The CLK and MOSI pins might be -1 for your board if it has a default SPI setup
  19. #define TFT_CS 10
  20. #define TFT_RST -1
  21. #define TFT_DC 9
  22. #define TFT_CLK 13
  23. #define TFT_MOSI 11
  24. // Use a zip file in memory for this test
  25. #include "bmp_icons.h"
  26. UNZIP zip; // Statically allocate the 41K UNZIP class/structure
  27. SPILCD lcd; // my display library
  28. void setup() {
  29. Serial.begin(115200);
  30. delay(1000); // give Serial a little time to start
  31. spilcdInit(&lcd, LCD_ILI9341, FLAGS_NONE, 40000000, TFT_CS, TFT_DC, TFT_RST, -1, -1, TFT_MOSI, TFT_CLK);
  32. spilcdSetOrientation(&lcd, LCD_ORIENTATION_90); // for the ILI9341 it's nice to use it in 320x240 orientation
  33. }
  34. void loop() {
  35. int rc, x, y;
  36. char szComment[256], szName[256];
  37. unz_file_info fi;
  38. uint8_t *ucBitmap; // temp storage for each icon bitmap
  39. spilcdFill(&lcd, 0, DRAW_TO_LCD); // Erase the display to black
  40. spilcdWriteString(&lcd, 0, 0, (char *)"Unzip BMP Files Test", 0xffff, 0, FONT_12x16, DRAW_TO_LCD); // white text on a black background
  41. x = 0; y = 24; // starting point to draw bitmaps
  42. rc = zip.openZIP((uint8_t *)bmp_icons, sizeof(bmp_icons));
  43. if (rc == UNZ_OK) {
  44. rc = zip.getGlobalComment(szComment, sizeof(szComment));
  45. Serial.print("Global comment: ");
  46. Serial.println(szComment);
  47. zip.gotoFirstFile();
  48. rc = UNZ_OK;
  49. while (rc == UNZ_OK) { // Display all files contained in the archive
  50. rc = zip.getFileInfo(&fi, szName, sizeof(szName), NULL, 0, szComment, sizeof(szComment));
  51. if (rc == UNZ_OK) {
  52. ucBitmap = (uint8_t *)malloc(fi.uncompressed_size); // allocate enough to hold the bitmap
  53. if (ucBitmap != NULL) { // malloc succeeded (it should, these bitmaps are only 2K bytes each)
  54. zip.openCurrentFile(); // if you don't open it explicitly, readCurrentFile will fail with UNZ_PARAMERROR
  55. spilcdWriteString(&lcd, 0, 224, " ", 0xff1f, 0, FONT_12x16, DRAW_TO_LCD); // erase old name
  56. spilcdWriteString(&lcd, 0, 224, szName, 0xff1f, 0, FONT_12x16, DRAW_TO_LCD); // display current file name at the bottom
  57. rc = zip.readCurrentFile(ucBitmap, 2102); // we know the uncompressed size of these BMP images
  58. if (rc != fi.uncompressed_size) {
  59. Serial.print("Read error, rc=");
  60. Serial.println(rc, DEC);
  61. }
  62. spilcdDrawBMP(&lcd, ucBitmap, x, y, 1, -1, DRAW_TO_LCD); // Display the BMP file stretched 2X starting at (x,y)
  63. x += 64; // Draw them across the display from left to right
  64. if (x >= 256) { // move down for the next row
  65. x = 0;
  66. y += 64;
  67. }
  68. free(ucBitmap); // finished with this bitmap
  69. }
  70. delay(1000); // Allow time to see it happen, otherwise it will zip by too quickly
  71. }
  72. rc = zip.gotoNextFile();
  73. }
  74. zip.closeZIP();
  75. }
  76. // while (1) {};
  77. }