Advertisement
honey_the_codewitch

draw_text example

Apr 9th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <tft_io.hpp>
  3. #include <ili9341.hpp>
  4. #include <gfx_cpp14.hpp>
  5.  
  6. #include "Maziro.h"
  7.  
  8. using namespace arduino;
  9. using namespace gfx;
  10. #define LCD_HOST    VSPI
  11. #define PIN_NUM_MISO 25
  12. #define PIN_NUM_MOSI 23
  13. #define PIN_NUM_CLK  19
  14. #define PIN_NUM_CS   22
  15. #define LCD_WRITE_SPEED_PERCENT 400 // 40MHz
  16. #define LCD_READ_SPEED_PERCENT 200 // 20MHz
  17. #define PIN_NUM_DC   21
  18. #define PIN_NUM_RST  18
  19. #define PIN_NUM_BKL 5
  20. #define LCD_BKL_HIGH false
  21. #define LCD_WIDTH 240
  22. #define LCD_HEIGHT 320
  23. #define LCD_ROTATION 1
  24.  
  25. using bus_type = tft_spi_ex<LCD_HOST,
  26.                             PIN_NUM_CS,
  27.                             PIN_NUM_MOSI,
  28.                             PIN_NUM_MISO,
  29.                             PIN_NUM_CLK,
  30.                             SPI_MODE0,
  31.                             PIN_NUM_MISO<0
  32. #ifdef OPTIMIZE_DMA
  33.                             ,(LCD_WIDTH*LCD_HEIGHT)*2+8
  34. #endif
  35. >;
  36.  
  37. using lcd_type = ili9341<PIN_NUM_DC,
  38.                         PIN_NUM_RST,
  39.                         PIN_NUM_BKL,
  40.                         bus_type,
  41.                         LCD_ROTATION,
  42.                         LCD_BKL_HIGH,
  43.                         LCD_WRITE_SPEED_PERCENT,
  44.                         LCD_READ_SPEED_PERCENT>;
  45.  
  46. using lcd_color = color<typename lcd_type::pixel_type>;
  47. lcd_type lcd;
  48.  
  49. void draw_text(spoint16 loc, const open_font& fnt,const char* text, float scale, rgb_pixel<16> fg,rgb_pixel<16> bg) {
  50.   size16 text_size = (size16)fnt.measure_text((ssize16)lcd.dimensions(),{0,0},text,scale);
  51.   // just a little padding
  52.   text_size.width+=2;
  53.   text_size.height+=2;
  54.   using bt = bitmap<rgb_pixel<16>>;
  55.   uint8_t* buf = (uint8_t*)malloc(bt::sizeof_buffer(text_size));
  56.   if(nullptr==buf) {
  57.     return;
  58.   }
  59.   bt tmp(text_size,buf);
  60.   tmp.fill(tmp.bounds(),bg);
  61.   draw::text(tmp,(srect16)tmp.bounds(),{0,0},text,fnt,scale,fg);
  62.   draw::bitmap(lcd,srect16(loc,(spoint16)lcd.bounds().bottom_right()),tmp,tmp.bounds());
  63.   free(buf);
  64. }
  65.  
  66. void setup() {
  67.   // put your setup code here, to run once:
  68.   lcd.fill(lcd.bounds(),lcd_color::white);
  69.   draw_text({10,10},Maziro_ttf,"HELLO WORLD!",Maziro_ttf.scale(40),lcd_color::indian_red,lcd_color::white);
  70. }
  71.  
  72. void loop() {
  73.   // put your main code here, to run repeatedly:
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement