Advertisement
TheGhastModding

AVR Mandelbrot

Dec 8th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.50 KB | None | 0 0
  1. /*
  2.  * Mandelbrot.c
  3.  *
  4.  * Created: 12/7/2018 5:29:54 PM
  5.  * Author : lucah
  6.  */
  7.  
  8. #ifndef F_CPU
  9. #define F_CPU 25000000
  10. #endif
  11.  
  12. #include <avr/io.h>
  13. #include <util/delay.h>
  14. #include "ff.h"
  15. #include "ata.h"
  16.  
  17. void beep(uint8_t b){
  18.     uint8_t counter = 0;
  19.     PORTD |= (1 << PD4);
  20.     while (1)
  21.     {
  22.         PORTD |= (1 << PD4);
  23.         if(b){
  24.             _delay_ms(1.06);
  25.         }else{
  26.             //_delay_us(660);
  27.             for(uint8_t i = 0; i < 33; i++) _delay_us(20);
  28.         }
  29.         PORTD &= ~(1 << PD4);
  30.         if(b){
  31.             _delay_ms(1.06);
  32.         }else{
  33.             //_delay_us(660);
  34.             for(uint8_t i = 0; i < 33; i++) _delay_us(20);
  35.         }
  36.         counter++;
  37.         if(counter >= 151) break;
  38.     }
  39.     PORTD &= ~(1 << PD4);
  40. }
  41.  
  42. long HSBtoRGB(float hue, float saturation, float brightness){
  43.     long r = 0, g = 0, b = 0;
  44.     if(saturation == 0){
  45.         r = g = b = (int) (brightness * 255.0f + 0.5f);
  46.     }else{
  47.         float h = (hue - (float)floor(hue)) * 6.0f;
  48.         float f = h - (float)floor(h);
  49.         float p = brightness * (1.0f - saturation);
  50.         float q = brightness * (1.0f - saturation * f);
  51.         float t = brightness * (1.0f - (saturation * (1.0f - f)));
  52.         switch ((long) h) {
  53.         case 0:
  54.             r = (long) (brightness * 255.0f + 0.5f);
  55.             g = (long) (t * 255.0f + 0.5f);
  56.             b = (long) (p * 255.0f + 0.5f);
  57.             break;
  58.         case 1:
  59.             r = (long) (q * 255.0f + 0.5f);
  60.             g = (long) (brightness * 255.0f + 0.5f);
  61.             b = (long) (p * 255.0f + 0.5f);
  62.             break;
  63.         case 2:
  64.             r = (long) (p * 255.0f + 0.5f);
  65.             g = (long) (brightness * 255.0f + 0.5f);
  66.             b = (long) (t * 255.0f + 0.5f);
  67.             break;
  68.         case 3:
  69.             r = (long) (p * 255.0f + 0.5f);
  70.             g = (long) (q * 255.0f + 0.5f);
  71.             b = (long) (brightness * 255.0f + 0.5f);
  72.             break;
  73.         case 4:
  74.             r = (long) (t * 255.0f + 0.5f);
  75.             g = (long) (p * 255.0f + 0.5f);
  76.             b = (long) (brightness * 255.0f + 0.5f);
  77.             break;
  78.         case 5:
  79.             r = (long) (brightness * 255.0f + 0.5f);
  80.             g = (long) (p * 255.0f + 0.5f);
  81.             b = (long) (q * 255.0f + 0.5f);
  82.             break;
  83.         }
  84.     }
  85.     return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
  86. }
  87.  
  88. int main(void)
  89. {
  90.     CLKPR = (1 << CLKPCE);
  91.     CLKPR = 0;
  92.     DDRD |= (1 << PD4);
  93.     DDRB |= (1 << PB6);
  94.     DDRB |= (1 << PB5);
  95.    
  96.     beep(0);
  97.    
  98.     uint8_t err = ata_init();
  99.     if(err != 0){
  100.         beep(1);
  101.         while(1);
  102.     }
  103.    
  104.     beep(0);
  105.    
  106.     static FATFS FATFS_obj;
  107.     FRESULT f_err = f_mount(&FATFS_obj, "", 1);
  108.     if(f_err != FR_OK){
  109.         beep(1);
  110.         while(1);
  111.     }
  112.    
  113.     beep(0);
  114.    
  115.     static FIL outputFile;
  116.     f_err = f_open(&outputFile, "mandelbrot.dat", FA_WRITE);
  117.     if(f_err != FR_OK){
  118.         beep(1);
  119.         while(1);
  120.     }
  121.    
  122.     beep(0);
  123.    
  124.     f_err = f_lseek(&outputFile, 0);
  125.     f_err |= f_truncate(&outputFile);
  126.     if(f_err != FR_OK){
  127.         beep(1);
  128.         while(1);
  129.     }
  130.    
  131.     uint8_t colorBuffer[512 * 3];
  132.     for(long i = 0; i < 512; i++){
  133.         long rgb = HSBtoRGB((float)i/256.0f, 1.0f, (float)i/(i+8.0f));
  134.         colorBuffer[i * 3 + 0] = (uint8_t)(rgb >> 16UL);
  135.         colorBuffer[i * 3 + 1] = (uint8_t)(rgb >> 8UL);
  136.         colorBuffer[i * 3 + 2] = (uint8_t)(rgb >> 0UL);
  137.     }
  138.    
  139.     uint8_t dataBuffer[156];
  140.     dataBuffer[0] = 1;
  141.     int width = 1920;
  142.     int height = 1080;
  143.     dataBuffer[1] = (uint8_t)width;
  144.     dataBuffer[2] = (uint8_t)(width >> 8);
  145.     dataBuffer[3] = (uint8_t)height;
  146.     dataBuffer[4] = (uint8_t)(height >> 8);
  147.    
  148.     UINT w = 0;
  149.    
  150.     f_write(&outputFile, dataBuffer, 5, &w);
  151.    
  152.     int posCntr = 0;
  153.     float re = -0.235125;
  154.     float imag = 0.827215;
  155.     float r = 4.0e-5;
  156.     //double re = 0;
  157.     //double imag = 0;
  158.     //double r = 1;
  159.    
  160.     const int maxIters = 512;
  161.     float c_re;
  162.     float c_im;
  163.     float x;
  164.     float x_new;
  165.     float y;
  166.     int iteration;
  167.     const float c1 = 4.0/width*r;
  168.     for(int row = 0; row < height; row++){
  169.         if(row >= 360){
  170.             PORTB |= (1 << PB5);
  171.         }
  172.         if(row >= 720){
  173.             PORTB |= (1 << PB6);
  174.         }
  175.         for(int col = 0; col < width; col++){
  176.             c_re = re + (col - width/2.0)*c1;
  177.             c_im = imag + (row - height/2.0)*c1;
  178.             x = 0; y = 0;
  179.             iteration = 0;
  180.             while(x*x+y*y <= 4 && iteration < maxIters){
  181.                 x_new = x*x - y*y + c_re;
  182.                 y = 2*x*y + c_im;
  183.                 x = x_new;
  184.                 iteration++;
  185.             }
  186.             if(iteration < maxIters){
  187.                 iteration %= 512;
  188.                 dataBuffer[posCntr] = colorBuffer[iteration * 3];
  189.                 dataBuffer[posCntr + 1] = colorBuffer[iteration * 3 + 1];
  190.                 dataBuffer[posCntr + 2] = colorBuffer[iteration * 3 + 2];
  191.             }else{
  192.                 dataBuffer[posCntr] = 0;
  193.                 dataBuffer[posCntr + 1] = 0;
  194.                 dataBuffer[posCntr + 2] = 0;
  195.             }
  196.             PORTD ^= (1 << PD4);
  197.             posCntr+=3;
  198.             if(posCntr == 156){
  199.                 f_write(&outputFile, dataBuffer, 156, &w);
  200.                 posCntr = 0;
  201.             }
  202.         }
  203.     }
  204.     f_write(&outputFile, dataBuffer, posCntr, &w);
  205.    
  206.     f_close(&outputFile);
  207.     f_unmount("");
  208.     beep(0);
  209.     while (1)
  210.     {
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement