#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8

Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

// Sample bitmap (replace with your own image data)
const uint8_t rotatingImage[] PROGMEM = {
     // Width and height of the image
 56, 43,
  B00000000, B00000000, B00000001, B11111111, B10000000, B00000000, B00000000, 
  B00000000, B00000000, B00001111, B11111111, B11110000, B00000000, B00000000, 
  B00000000, B00000000, B00011111, B11111111, B11111100, B00000000, B00000000, 
  B00000000, B00000000, B00111111, B11111111, B11111110, B00000000, B00000000, 
  B00000000, B00000000, B00111111, B11111111, B11111110, B00000000, B00000000, 
  B00000000, B00000000, B01111111, B11111111, B11111111, B00000000, B00000000, 
  B00000000, B00000000, B01111111, B11100111, B11111111, B00000000, B00000000, 
  B00000000, B00000000, B11111100, B00000000, B00111111, B00000000, B00000000, 
  B00000000, B00000000, B11111111, B00000000, B11111111, B10000000, B00000000, 
  B00000000, B00000001, B11111111, B11111111, B11111111, B11000000, B00000000, 
  B00000000, B00000011, B11111111, B11111111, B11111111, B11000000, B00000000, 
  B00000000, B00000111, B11110000, B00000000, B00001111, B11100000, B00000000, 
  B00000000, B00001111, B10000000, B00000000, B00000001, B11110000, B00000000, 
  B00000000, B00011110, B00000000, B00000000, B00000000, B01110000, B00000000, 
  B00000000, B01111100, B00000000, B00000000, B00000000, B00111110, B00000000, 
  B00000001, B11111000, B00000000, B00000000, B00000000, B00011111, B10000000, 
  B00000111, B11111000, B00000000, B00000000, B00000000, B00011111, B11100000, 
  B00011111, B11110000, B00000000, B00000000, B00000000, B00001111, B11111000, 
  B00111110, B00110000, B00000000, B00000000, B00000000, B00001100, B00111110, 
  B11100000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000001, 
  B00000000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00110000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00011000, B00000000, B00000000, B00000000, B00001100, B00000000, 
  B00000000, B00011000, B00000000, B00000000, B00000000, B00011000, B00000000, 
  B00000000, B00011000, B00000000, B00000000, B00000000, B00011000, B00000000, 
  B00000000, B00001100, B00000000, B00000000, B00000000, B00011000, B00000000, 
  B00000000, B00001100, B00000000, B00000000, B00000000, B00110000, B00000000, 
  B00000000, B00000110, B00000000, B00000000, B00000000, B00110000, B00000000, 
  B00000000, B00000111, B00000000, B00000000, B00000000, B01100000, B00000000, 
  B00000000, B00000011, B00000000, B00000000, B00000000, B01100000, B00000000, 
  B00000000, B00000001, B10000000, B00000000, B00000000, B11000000, B00000000, 
  B00000000, B00000000, B11000000, B00000000, B00000001, B10000000, B00000000, 
  B00000000, B00000000, B00100000, B00000000, B00000000, B00000000, B00000000, 
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, 
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, 
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, 
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, 
  B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111
};

void setup() {
    Serial.begin(57600);
    tft.initR(INITR_MINI160x80); // Initialize ST7735S mini display
    tft.setRotation(3); // Adjust rotation based on your display orientation
}

void loop() {
    for (int angle = 0; angle < 360; angle += 6) {
        tft.fillScreen(ST7735_BLACK); // Clear the display
        drawRotatedBitmap(80, 40, rotatingImage, angle);
        delay(10); // Pause so we see the rotation
    }
}

void drawRotatedBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint16_t angle) {
    uint8_t w = pgm_read_byte(bitmap++);
    uint8_t h = pgm_read_byte(bitmap++);
    int16_t rotatedx, rotatedy;
    uint8_t data = 0;

    float cosa = cos(angle * 0.0174532925);
    float sina = sin(angle * 0.0174532925);

    x = x - ((w * cosa / 2) - (h * sina / 2));
    y = y - ((h * cosa / 2) + (w * sina / 2));

    for (int16_t j = 0; j < h; j++) {
        for (int16_t i = 0; i < w; i++) {
            if ((j * w + i) & 7)
                data <<= 1;
            else
                data = pgm_read_byte(bitmap++);

            rotatedx = 0.5 + x + ((i * cosa) - (j * sina));
            rotatedy = 0.5 + y + ((j * cosa) + (i * sina));

            if (data & 0x80)
                tft.drawPixel(rotatedx, rotatedy, ST7735_WHITE);
        }
    }
}