Advertisement
honey_the_codewitch

mandelbrot

Dec 1st, 2022
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <gfx.hpp>
  3. #include <st7789.hpp>
  4. #include <htcw_button.hpp>
  5. #include <tft_io.hpp>
  6. //#include <FixedPoints.h>
  7. #define LCD_WIDTH 135
  8. #define LCD_HEIGHT 240
  9. #define LCD_HOST VSPI
  10. #define PIN_NUM_MISO -1
  11. #define PIN_NUM_MOSI 19
  12. #define PIN_NUM_CLK 18
  13. #define PIN_NUM_CS 5
  14. #define PIN_NUM_DC 16
  15. #define PIN_NUM_RST 23
  16. #define PIN_NUM_BCKL 4
  17. using namespace arduino;
  18. using namespace gfx;
  19. using namespace std;
  20. using bus_t = tft_spi_ex<LCD_HOST, PIN_NUM_CS, PIN_NUM_MOSI, PIN_NUM_MISO, PIN_NUM_CLK, SPI_MODE0>;
  21. using display_t = st7789<LCD_WIDTH, LCD_HEIGHT, PIN_NUM_DC, PIN_NUM_RST, PIN_NUM_BCKL, bus_t, 0, true, 400, 200>;
  22. using color_t = color<typename display_t::pixel_type>;
  23. using fix_t =float;//SFixed<12,19>;
  24. using button_1_t = button<35,10,true>;
  25. using button_2_t = button<0,10,true>;
  26. using rgb_t = rgb_pixel<24>;
  27.  
  28. static display_t dsp;
  29.  
  30. button_1_t button_1;
  31. button_2_t button_2;
  32.  
  33. const int16_t
  34. res_bits = 12, // Fractional resolution
  35. pixelWidth = dsp.dimensions().width, // TFT dimensions
  36. pixelHeight = dsp.dimensions().height,
  37. iterations = 20; // Fractal iteration limit or 'dwell'
  38. fix_t
  39. centerReal(-0.6), // Image center point in complex plane
  40. centerImag(0.0),
  41. rangeReal(3.0), // Image coverage in complex plane
  42. rangeImag(3.0),
  43. incRange(.95);
  44.  
  45. void setup() {
  46. Serial.begin(115200);
  47. draw::filled_rectangle(dsp,dsp.bounds(),color_t::black);
  48. }
  49. void loop() {
  50. int64_t n, a, b, a2, b2, posReal, posImag;
  51. uint32_t startTime,elapsedTime;
  52.  
  53.  
  54. int32_t
  55. startReal = (int64_t)((centerReal - rangeReal * 0.5) * (float)(1 << res_bits)),
  56. startImag = (int64_t)((centerImag + rangeImag * 0.5) * (float)(1 << res_bits)),
  57. incReal = (int64_t)((rangeReal / (float)pixelWidth) * (float)(1 << res_bits)),
  58. incImag = (int64_t)((rangeImag / (float)pixelHeight) * (float)(1 << res_bits));
  59.  
  60. startTime = millis();
  61. posImag = startImag;
  62. auto bt = draw::batch(dsp,dsp.bounds());
  63. for (int y = 0; y < dsp.dimensions().height; y++) {
  64. posReal = startReal;
  65. for (int x = 0; x < dsp.dimensions().width; x++) {
  66. a = posReal;
  67. b = posImag;
  68. for (n = iterations; n > 0 ; n--) {
  69. a2 = (a * a) >> res_bits;
  70. b2 = (b * b) >> res_bits;
  71. if ((a2 + b2) >= (4 << res_bits))
  72. break;
  73. b = posImag + ((a * b) >> (res_bits - 1));
  74. a = posReal + a2 - b2;
  75. }
  76. display_t::pixel_type px;
  77. px.native_value = (n * 29)<<8 | (n * 67);
  78. bt.write(px);
  79.  
  80. posReal += incReal;
  81. }
  82. posImag -= incImag;
  83. }
  84. bt.commit();
  85. elapsedTime = millis()-startTime;
  86. Serial.print("Took "); Serial.print(elapsedTime); Serial.println(" ms");
  87.  
  88. rangeReal *= incRange;
  89. rangeImag *= incRange;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement