#include <Wire.h>

#define CMD_DRAWPIX 0x10
#define CMD_DRAWSCREEN 0x11

typedef struct {
  uint8_t r;
  uint8_t g;
  uint8_t b;
  uint8_t a;
} rgba_t;

void setup() {
  // initialize serial and wait for port to open:
  Wire.begin();        // Initialize the I2C communication
  Wire.setClock(1000000);
  Serial.begin(9600);
}

void drawPixel(uint32_t x, uint32_t y, rgba_t color)
{
  Wire.beginTransmission(0x22);

  Wire.write(CMD_DRAWPIX);

  uint8_t writeValues[4];
  uint8_t posValues[8];

  writeValues[0] = color.r;
  writeValues[1] = color.g;
  writeValues[2] = color.b;
  writeValues[3] = color.a;

  posValues[0] = (x >> 24) & 0xff;
  posValues[1] = (x >> 16) & 0xff;
  posValues[2] = (x >> 8) & 0xff;
  posValues[3] = (x) & 0xff;

  posValues[4] = (y >> 24) & 0xff;
  posValues[5] = (y >> 16) & 0xff;
  posValues[6] = (y >> 8) & 0xff;
  posValues[7] = (y) & 0xff;

  for(int i=0; i<4; i++)
  {
    Wire.write(writeValues[i]);
  }
  for(int i=0; i<8; i++)
  {
    Wire.write(posValues[i]);
  }
  
  Wire.endTransmission();
}
uint32_t x, y=0;

void drawHLine(uint32_t width, uint32_t y, rgba_t color)
{
  for(int x=0; x<width; x++)
  {
    drawPixel(x, y, color);
  }
}
void drawVLine(uint32_t height, uint32_t x, rgba_t color)
{
  for(int y=0; y<height; y++)
  {
    drawPixel(x, y, color);
  }
}

void drawGrid()
{
  for(y=0; y<128; y+=4)
  {
      drawVLine(64, y, (rgba_t){.r=0xff, .g=0xff, .b=0xff, .a=0xff});
  }
  for(x=0; x<64; x+=4)
  {
      drawHLine(128, x, (rgba_t){.r=0xff, .g=0xff, .b=0xff, .a=0xff});
  }
}
void loop() {
  /*
  drawPixel(x,y, (rgba_t){.r=0xff, .g=0xff, .b=0xff, .a=0xff});
  x++;
  if(x > 128)
  {
    y++;
    x=0;
  }
  if(y > 64)
  {
    y=0;
    x=0;
  }
  */
  for (int y = 0; y < 64; y++) {
    // Calculate the gradient color based on the y position
    uint8_t red = y * 255 / (64 - 1);
    uint8_t blue = 255 - red;

    // Create the rgba_t color for the pixel
    rgba_t gradient_color = { .r = red, .g = 0, .b = blue, .a = 255 };

    for (int x = 0; x < 128; x++) {
      drawPixel(x, y, gradient_color);
    }
  }
  drawGrid();

  //delay(100); // Wait for a second before sending the next data
}
bad-chipBreakout