// TinyGlitchI2C.ino
// 
// 19 January, Version 1, by Koepel, sketch from https://github.com/uNetworking/SSD1306
//
// Testing the "drazzy" ATTinyCore I2C
// For: https://forum.arduino.cc/t/attiny85-i2c-not-reliable/948669/
//
// SDA = PB0
// SCL = PB2
//
// It does not work !
// Maybe the minimal wrapper uses the USI and maybe that is not supported by Wokwi yet.
//

#include "i2c_wrapper.h"
#include "ssd1306_minimal.h"

void setup() {
  I2C_WRAPPER_init();
  SSD1306_MINIMAL_init();
}

void loop() {
  /* Clear it */
  for (int i = 0; i < 1024; i++) {
    SSD1306_MINIMAL_framebuffer[i] = 0;
  }
  
  const int height = 64;
  const int width = 128;
  const int max = 15;

  /* Let's draw the mandelbrot set on screen */
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      float c_re = (col - width / 2.0) * 4.0 / width;
      float c_im = (row - height / 2.0) * 4.0 / width;
      float x = 0, y = 0;
      int iteration = 0;
      while (x * x + y * y <= 4 && iteration < max) {
          float x_new = x * x - y * y + c_re;
          y = 2 * x * y + c_im;
          x = x_new;
          iteration++;
      }
      if (iteration == max) {
        /* This is where we actually set a pixel */
        SSD1306_MINIMAL_setPixel(-col - 30, row);
      }
    }
  }
  
  /* Transfer framebuffer to device */
  SSD1306_MINIMAL_transferFramebuffer();
}
ATTINY8520PU