Advertisement
honey_the_codewitch

ESP-IDF GFX font test

Feb 15th, 2022
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. // use C++ with ESP-IDF:
  2. extern "C" { void app_main(); }
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_spiffs.h"
  11. #include "drivers/ili9341.hpp"
  12. #include "gfx_cpp14.hpp"
  13.  
  14. #define LCD_HOST    VSPI_HOST
  15. #define DMA_CHAN    2
  16. #define PIN_NUM_MISO GPIO_NUM_19
  17. #define PIN_NUM_MOSI GPIO_NUM_23
  18. #define PIN_NUM_CLK  GPIO_NUM_18
  19. #define PIN_NUM_CS   GPIO_NUM_5
  20.  
  21. #define PIN_NUM_DC   GPIO_NUM_2
  22. #define PIN_NUM_RST  GPIO_NUM_4
  23. #define PIN_NUM_BCKL GPIO_NUM_15
  24.  
  25. // configure the spi bus. Must be done before the driver
  26. spi_master spi_host(nullptr,
  27.                     LCD_HOST,
  28.                     PIN_NUM_CLK,
  29.                     PIN_NUM_MISO,
  30.                     PIN_NUM_MOSI,
  31.                     GPIO_NUM_NC,
  32.                     GPIO_NUM_NC,
  33.                     320*240+8,
  34.                     DMA_CHAN);
  35.  
  36. // we use the default, modest buffer - it makes things slower but uses less
  37. // memory. it usually works fine at default but you can change it for performance
  38. // tuning. It's the final parameter: Note that it shouldn't be any bigger than
  39. // the DMA size
  40. using lcd_type = ili9341<LCD_HOST,
  41.                         PIN_NUM_CS,
  42.                         PIN_NUM_DC,
  43.                         PIN_NUM_RST,
  44.                         PIN_NUM_BCKL>;
  45.  
  46. lcd_type lcd;
  47.  
  48. using lcd_color = color<typename lcd_type::pixel_type>;
  49.  
  50. void app_main(void)
  51. {
  52.     // we'll be loading the font from SPIFFs.
  53.     // To embed the font instead, run the lib/gfx/tools/fontgen tool to create a header.
  54.     // then include that header. The font will be precreated with the same name as the header
  55.     // sans extension.
  56.  
  57.     // check to make sure SPI was initialized successfully
  58.     if(!spi_host.initialized()) {
  59.         printf("SPI host initialization error.\r\n");
  60.         abort();
  61.     }
  62.     // mount SPIFFS
  63.     esp_err_t ret;
  64.     esp_vfs_spiffs_conf_t conf = {};
  65.     conf.base_path="/spiffs";
  66.     conf.format_if_mount_failed=false;
  67.     conf.max_files=5;
  68.     conf.partition_label="storage";
  69.     ret=esp_vfs_spiffs_register(&conf);
  70.     ESP_ERROR_CHECK(ret);  
  71.    
  72.     file_stream fs("/spiffs/Bm437_Verite_9x16.FON");
  73.  
  74.     font my_font;
  75.     if(gfx::font::result::success!=font::read(&fs,&my_font)) {
  76.         fs.close();
  77.         printf("Error loading font");
  78.         return;
  79.     }
  80.     fs.close();
  81.  
  82.     const char* sz="test!";
  83.    
  84.     for(int i = 0;i<200;++i) {
  85.         // drawing with a non-transparent background is significantly faster
  86.         draw::text(lcd,{0,0,lcd.bounds().x2,lcd.bounds().y2},sz,my_font,lcd_color::white,lcd_color::black,false);
  87.     }
  88.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement