honey_the_codewitch

i8080 sample

Dec 2nd, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #pragma GCC optimize("O3,unroll-loops")
  2. #include <Arduino.h>
  3. #include "esp_lcd_panel_io.h"
  4. #include "esp_lcd_panel_ops.h"
  5. #include "esp_lcd_panel_vendor.h"
  6. #include <gfx.hpp>
  7. #include "pin_config.h"
  8. using namespace gfx;
  9. constexpr static const size16 screen_size(EXAMPLE_LCD_H_RES,EXAMPLE_LCD_V_RES);
  10. using frame_buffer_t = bitmap<rgb_pixel<16>>;
  11. constexpr static const int frame_buffer_parts = 5;
  12. static uint8_t frame_buffer_data[frame_buffer_t::sizeof_buffer({EXAMPLE_LCD_H_RES,EXAMPLE_LCD_V_RES/frame_buffer_parts})];
  13. frame_buffer_t frame_buffer({EXAMPLE_LCD_H_RES,EXAMPLE_LCD_V_RES/frame_buffer_parts},frame_buffer_data);
  14. esp_lcd_panel_io_handle_t io_handle = NULL;
  15. esp_lcd_panel_handle_t panel_handle = NULL;
  16. const int16_t
  17.   res_bits        = 12,   // Fractional resolution
  18.   pixelWidth  = screen_size.width,  // TFT dimensions
  19.   pixelHeight = screen_size.height,
  20.   iterations  = 20,  // Fractal iteration limit or 'dwell'
  21.   moveX=0,
  22.   moveY=0;
  23. float
  24.   centerReal(-0.6), // Image center point in complex plane
  25.   centerImag(0.0),
  26.   rangeReal(3.0), // Image coverage in complex plane
  27.   rangeImag(3.0),
  28.   incRange(.95);
  29.  
  30. static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx) {
  31.   return false;
  32. }
  33.  
  34. void setup() {
  35.   pinMode(PIN_POWER_ON, OUTPUT);
  36.   digitalWrite(PIN_POWER_ON, HIGH);
  37.   Serial.begin(115200);
  38.  
  39.   pinMode(PIN_LCD_RD, OUTPUT);
  40.   digitalWrite(PIN_LCD_RD, HIGH);
  41.  
  42.   esp_lcd_i80_bus_handle_t i80_bus = NULL;
  43.   esp_lcd_i80_bus_config_t bus_config = {
  44.       .dc_gpio_num = PIN_LCD_DC,
  45.       .wr_gpio_num = PIN_LCD_WR,
  46.       .clk_src = LCD_CLK_SRC_PLL160M,
  47.       .data_gpio_nums =
  48.           {
  49.               PIN_LCD_D0,
  50.               PIN_LCD_D1,
  51.               PIN_LCD_D2,
  52.               PIN_LCD_D3,
  53.               PIN_LCD_D4,
  54.               PIN_LCD_D5,
  55.               PIN_LCD_D6,
  56.               PIN_LCD_D7,
  57.           },
  58.       .bus_width = 8,
  59.       .max_transfer_bytes = frame_buffer_t::sizeof_buffer(screen_size),
  60.   };
  61.   esp_lcd_new_i80_bus(&bus_config, &i80_bus);
  62.  
  63.   esp_lcd_panel_io_i80_config_t io_config = {
  64.       .cs_gpio_num = PIN_LCD_CS,
  65.       .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  66.       .trans_queue_depth = 20,
  67.       .on_color_trans_done = example_notify_lvgl_flush_ready,
  68.       .user_ctx = NULL,
  69.       .lcd_cmd_bits = 8,
  70.       .lcd_param_bits = 8,
  71.       .dc_levels =
  72.           {
  73.               .dc_idle_level = 0,
  74.               .dc_cmd_level = 0,
  75.               .dc_dummy_level = 0,
  76.               .dc_data_level = 1,
  77.           },
  78.   };
  79.   ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));
  80.  
  81.   esp_lcd_panel_dev_config_t panel_config = {
  82.       .reset_gpio_num = PIN_LCD_RES,
  83.       .color_space = ESP_LCD_COLOR_SPACE_RGB,
  84.       .bits_per_pixel = 16,
  85.   };
  86.   esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle);
  87.   esp_lcd_panel_reset(panel_handle);
  88.   esp_lcd_panel_init(panel_handle);
  89.   esp_lcd_panel_invert_color(panel_handle, true);
  90.  
  91.   esp_lcd_panel_swap_xy(panel_handle, true);
  92.   esp_lcd_panel_mirror(panel_handle, false, true);
  93.   // the gap is LCD panel specific, even panels with the same driver IC, can
  94.   // have different gap value
  95.   esp_lcd_panel_set_gap(panel_handle, 0, 35);
  96.  
  97.   /* Lighten the screen with gradient */
  98.   ledcSetup(0, 10000, 8);
  99.   ledcAttachPin(PIN_LCD_BL, 0);
  100.   for (uint8_t i = 0; i < 0xFF; i++) {
  101.     ledcWrite(0, i);
  102.     delay(2);
  103.   }
  104.   draw::filled_rectangle(frame_buffer,frame_buffer.bounds(),color<rgb_pixel<16>>::black);
  105.   int y = 0;
  106.   for(int i = 0;i<frame_buffer_parts;++i) {
  107.     esp_lcd_panel_draw_bitmap(panel_handle, 0, y, screen_size.width, y+(screen_size.height/frame_buffer_parts), frame_buffer.begin());
  108.     y+=(screen_size.height/frame_buffer_parts);
  109.   }
  110.  
  111. }
  112.  
  113. void loop() {
  114.   int64_t       n, a, b, a2, b2, posReal, posImag;
  115.   uint32_t      startTime,elapsedTime;
  116.  
  117.  
  118.   int32_t
  119.     startReal   = (int64_t)((centerReal - rangeReal * 0.5)   * (float)(1 << res_bits)),
  120.     startImag   = (int64_t)((centerImag + rangeImag * 0.5)   * (float)(1 << res_bits)),
  121.     incReal     = (int64_t)((rangeReal / (float)pixelWidth)  * (float)(1 << res_bits)),
  122.     incImag     = (int64_t)((rangeImag / (float)pixelHeight) * (float)(1 << res_bits));
  123.  
  124.   startTime = millis();
  125.   posImag = startImag;
  126.   int yy = 0;
  127.   for (int y = 0; y <= screen_size.height; y++) {
  128.     if(yy==(screen_size.height/frame_buffer_parts)) {
  129.       yy=0;
  130.       esp_lcd_panel_draw_bitmap(panel_handle, 0, y-(screen_size.height/frame_buffer_parts), screen_size.width, y, frame_buffer.begin());
  131.       if(y==screen_size.height) {
  132.         break;
  133.       }
  134.     }
  135.     posReal = startReal;
  136.     for (int x = 0; x < screen_size.width; x++) {
  137.       a = posReal;
  138.       b = posImag;
  139.       for (n = iterations; n > 0 ; n--) {
  140.         a2 = (a * a) >> res_bits;
  141.         b2 = (b * b) >> res_bits;
  142.         if ((a2 + b2) >= (4 << res_bits))
  143.           break;
  144.         b  = posImag + ((a * b) >> (res_bits - 1));
  145.         a  = posReal + a2 - b2;
  146.       }
  147.       frame_buffer_t::pixel_type px;
  148.       px.native_value =  (n * 29)<<8 | (n * 67);
  149.       frame_buffer.point(point16(x,y%(screen_size.height/frame_buffer_parts)),px);
  150.       posReal += incReal;
  151.     }
  152.     posImag -= incImag;
  153.     ++yy;
  154.   }
  155.   elapsedTime = millis()-startTime;
  156.   Serial.print("Took "); Serial.print(elapsedTime); Serial.println(" ms");
  157.  
  158.   rangeReal *= incRange;
  159.   rangeImag *= incRange;
  160. }
  161.  
Add Comment
Please, Sign In to add comment