Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Mandelbrot.c
- *
- * Created: 12/7/2018 5:29:54 PM
- * Author : lucah
- */
- #ifndef F_CPU
- #define F_CPU 25000000
- #endif
- #include <avr/io.h>
- #include <util/delay.h>
- #include "ff.h"
- #include "ata.h"
- void beep(uint8_t b){
- uint8_t counter = 0;
- PORTD |= (1 << PD4);
- while (1)
- {
- PORTD |= (1 << PD4);
- if(b){
- _delay_ms(1.06);
- }else{
- //_delay_us(660);
- for(uint8_t i = 0; i < 33; i++) _delay_us(20);
- }
- PORTD &= ~(1 << PD4);
- if(b){
- _delay_ms(1.06);
- }else{
- //_delay_us(660);
- for(uint8_t i = 0; i < 33; i++) _delay_us(20);
- }
- counter++;
- if(counter >= 151) break;
- }
- PORTD &= ~(1 << PD4);
- }
- long HSBtoRGB(float hue, float saturation, float brightness){
- long r = 0, g = 0, b = 0;
- if(saturation == 0){
- r = g = b = (int) (brightness * 255.0f + 0.5f);
- }else{
- float h = (hue - (float)floor(hue)) * 6.0f;
- float f = h - (float)floor(h);
- float p = brightness * (1.0f - saturation);
- float q = brightness * (1.0f - saturation * f);
- float t = brightness * (1.0f - (saturation * (1.0f - f)));
- switch ((long) h) {
- case 0:
- r = (long) (brightness * 255.0f + 0.5f);
- g = (long) (t * 255.0f + 0.5f);
- b = (long) (p * 255.0f + 0.5f);
- break;
- case 1:
- r = (long) (q * 255.0f + 0.5f);
- g = (long) (brightness * 255.0f + 0.5f);
- b = (long) (p * 255.0f + 0.5f);
- break;
- case 2:
- r = (long) (p * 255.0f + 0.5f);
- g = (long) (brightness * 255.0f + 0.5f);
- b = (long) (t * 255.0f + 0.5f);
- break;
- case 3:
- r = (long) (p * 255.0f + 0.5f);
- g = (long) (q * 255.0f + 0.5f);
- b = (long) (brightness * 255.0f + 0.5f);
- break;
- case 4:
- r = (long) (t * 255.0f + 0.5f);
- g = (long) (p * 255.0f + 0.5f);
- b = (long) (brightness * 255.0f + 0.5f);
- break;
- case 5:
- r = (long) (brightness * 255.0f + 0.5f);
- g = (long) (p * 255.0f + 0.5f);
- b = (long) (q * 255.0f + 0.5f);
- break;
- }
- }
- return 0xff000000 | (r << 16) | (g << 8) | (b << 0);
- }
- int main(void)
- {
- CLKPR = (1 << CLKPCE);
- CLKPR = 0;
- DDRD |= (1 << PD4);
- DDRB |= (1 << PB6);
- DDRB |= (1 << PB5);
- beep(0);
- uint8_t err = ata_init();
- if(err != 0){
- beep(1);
- while(1);
- }
- beep(0);
- static FATFS FATFS_obj;
- FRESULT f_err = f_mount(&FATFS_obj, "", 1);
- if(f_err != FR_OK){
- beep(1);
- while(1);
- }
- beep(0);
- static FIL outputFile;
- f_err = f_open(&outputFile, "mandelbrot.dat", FA_WRITE);
- if(f_err != FR_OK){
- beep(1);
- while(1);
- }
- beep(0);
- f_err = f_lseek(&outputFile, 0);
- f_err |= f_truncate(&outputFile);
- if(f_err != FR_OK){
- beep(1);
- while(1);
- }
- uint8_t colorBuffer[512 * 3];
- for(long i = 0; i < 512; i++){
- long rgb = HSBtoRGB((float)i/256.0f, 1.0f, (float)i/(i+8.0f));
- colorBuffer[i * 3 + 0] = (uint8_t)(rgb >> 16UL);
- colorBuffer[i * 3 + 1] = (uint8_t)(rgb >> 8UL);
- colorBuffer[i * 3 + 2] = (uint8_t)(rgb >> 0UL);
- }
- uint8_t dataBuffer[156];
- dataBuffer[0] = 1;
- int width = 1920;
- int height = 1080;
- dataBuffer[1] = (uint8_t)width;
- dataBuffer[2] = (uint8_t)(width >> 8);
- dataBuffer[3] = (uint8_t)height;
- dataBuffer[4] = (uint8_t)(height >> 8);
- UINT w = 0;
- f_write(&outputFile, dataBuffer, 5, &w);
- int posCntr = 0;
- float re = -0.235125;
- float imag = 0.827215;
- float r = 4.0e-5;
- //double re = 0;
- //double imag = 0;
- //double r = 1;
- const int maxIters = 512;
- float c_re;
- float c_im;
- float x;
- float x_new;
- float y;
- int iteration;
- const float c1 = 4.0/width*r;
- for(int row = 0; row < height; row++){
- if(row >= 360){
- PORTB |= (1 << PB5);
- }
- if(row >= 720){
- PORTB |= (1 << PB6);
- }
- for(int col = 0; col < width; col++){
- c_re = re + (col - width/2.0)*c1;
- c_im = imag + (row - height/2.0)*c1;
- x = 0; y = 0;
- iteration = 0;
- while(x*x+y*y <= 4 && iteration < maxIters){
- x_new = x*x - y*y + c_re;
- y = 2*x*y + c_im;
- x = x_new;
- iteration++;
- }
- if(iteration < maxIters){
- iteration %= 512;
- dataBuffer[posCntr] = colorBuffer[iteration * 3];
- dataBuffer[posCntr + 1] = colorBuffer[iteration * 3 + 1];
- dataBuffer[posCntr + 2] = colorBuffer[iteration * 3 + 2];
- }else{
- dataBuffer[posCntr] = 0;
- dataBuffer[posCntr + 1] = 0;
- dataBuffer[posCntr + 2] = 0;
- }
- PORTD ^= (1 << PD4);
- posCntr+=3;
- if(posCntr == 156){
- f_write(&outputFile, dataBuffer, 156, &w);
- posCntr = 0;
- }
- }
- }
- f_write(&outputFile, dataBuffer, posCntr, &w);
- f_close(&outputFile);
- f_unmount("");
- beep(0);
- while (1)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement