-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot_fractal.ino
90 lines (65 loc) · 1.56 KB
/
mandelbrot_fractal.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <Wire.h>
#include "ssd1306.h"
#include "intf/ssd1306_interface.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define s_ssd1306_invertByte 0x00000000
#define iter_max 200
void setup() {
Serial.begin(9600);
delay(20);
ssd1306_128x64_i2c_init();
ssd1306_clearScreen();
outputMatrix();
}
void loop() {
unsigned long start = millis();
for (int i=0; i<10; i++) {
outputMatrix();
}
Serial.print(F("Gens/s:"));
Serial.println(10000/(millis() - start));
}
void outputMatrix() {
//uint8_t s_ssd1306_invertByte = 0x00000000;
ssd1306_lcd.set_block(0, 0, 128);
for (uint16_t col = 0; col < 8; col++) {
for (uint16_t row = 0; row <= 127; row++) {
ssd1306_lcd.send_pixels1(s_ssd1306_invertByte^gen_mb_byte(row, col));
//spi.write(Matrix[row].b[col]);
//delay(2);
}
ssd1306_lcd.next_page();
}
ssd1306_intf.stop();
}
byte gen_mb_byte(uint16_t x, uint16_t y){
byte out = B00000000;
float a,b,ca,cb,aa,bb;
byte n;
for (uint16_t i = 0; i < 8; i++) {
a = -2.5*(0.5 - float(x)/64)*0.9 - 1.5;
b = -2.5*(0.5 - float(y*8+i)/64)*0.9;
ca = a;
cb = b;
n = 0;
while (n < iter_max) {
aa = a * a - b * b;
bb = 2 * a * b;
a = aa + ca;
b = bb + cb;
if (abs(a + b) > 50) {
break;
}
n++;
}
if (n == iter_max) {
out = out | (1 << i);
continue;
}
if(byte(random(iter_max)) + 6 < n){
out = out | (1 << i);
}
}
return ~out;
}