#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>     // Touch screen library

// TFT display pin definitions (change according to your setup)
#define LCD_CS A3 // Chip Select
#define LCD_CD A2 // Command/Data
#define LCD_WR A1 // LCD Write
#define LCD_RD A0 // LCD Read
#define LCD_RESET A4 // Reset pin

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);

const int potentiometerPin = A0; // Analog pin for the potentiometer

void setup() {
  // Initialize the TFT
  tft.begin(0x9341); // Adjust based on your display driver
  tft.setRotation(1);
  tft.fillScreen(0x0000); // Black background

  // Draw the full linear meter
  drawLinearMeter();
}

void loop() {
  // Read the potentiometer value (0-1023)
  int potValue = analogRead(potentiometerPin);

  // Determine the zoom area based on the potentiometer value
  int zoomStart = map(potValue, 0, 1023, 0, 100); // Assuming meter is from 0 to 100
  int zoomEnd = zoomStart + 10; // Zoom in on a range of 10 units

  // Redraw the zoomed area
  drawZoomedArea(zoomStart, zoomEnd);

  // Small delay to allow the screen to update
  delay(100);
}

void drawLinearMeter() {
  // Draw a simple linear meter (for example, a horizontal line with markings)
  tft.drawLine(10, 100, 310, 100, 0xFFFF); // Draw the line (white color)

  // Draw the meter markings (0 to 100)
  for (int i = 0; i <= 100; i += 10) {
    int x = map(i, 0, 100, 10, 310);
    tft.drawLine(x, 95, x, 105, 0xFFFF); // Draw the marking
    tft.setCursor(x - 5, 110);
    tft.setTextColor(0xFFFF);
    tft.setTextSize(1);
    tft.print(i); // Print the value
  }
}

void drawZoomedArea(int start, int end) {
  // Clear the area where the zoomed meter will be displayed
  tft.fillRect(10, 150, 300, 50, 0x0000); // Clear the area (black background)

  // Draw the zoomed meter
  tft.drawLine(10, 175, 310, 175, 0xFFFF); // Draw the line (white color)

  // Draw the meter markings for the zoomed area
  for (int i = start; i <= end; i++) {
    int x = map(i, start, end, 10, 310);
    tft.drawLine(x, 170, x, 180, 0xFFFF); // Draw the marking
    tft.setCursor(x - 5, 185);
    tft.setTextColor(0xFFFF);
    tft.setTextSize(1);
    tft.print(i); // Print the value
  }
}