/**
 **************************************************
 * @file        Inkplate6PLUS_Touch_In_Area.ino
 * @brief       Touchscreen touch-in-area demo for Soldered Inkplate 6PLUS.
 *
 * @details     Demonstrates how to use the Inkplate 6PLUS touchscreen by
 *              detecting touches inside a defined rectangular area. A black
 *              rectangle is drawn on the display; when the user touches inside
 *              the rectangle, it moves to a new position. Partial updates are
 *              used for fast redraws, with an occasional full refresh when the
 *              rectangle position resets.
 *
 * Requirements:
 * - Board:      Soldered Inkplate 6PLUS
 * - Hardware:   Inkplate 6PLUS, USB cable
 *
 *
 * Don't have Inkplate Boards in Arduino Boards Manager?
 * See https://docs.soldered.com/inkplate/6PLUS/quick-start-guide/
 *
 * How to use:
 * 1) Upload the sketch to Inkplate 6PLUS.
 * 2) After initialization, a black rectangle appears on the screen.
 * 3) Touch inside the rectangle to move it diagonally across the display.
 * 4) When it reaches the lower area, the rectangle resets to the start and a
 *    full refresh is performed.
 *
 * Expected output:
 * - Rectangle moves when touched inside its bounds.
 * - Serial Monitor prints touchscreen init status.
 *
 * Notes:
 * - Touch detection uses touchscreen.touchInArea(x, y, w, h).
 * - Partial updates are used for faster movement; a full refresh is used on reset.
 * - Touchscreen is initialized and powered on with touchscreen.init(true).
 *
 * Docs:         https://docs.soldered.com/inkplate
 *
 * @author      Soldered Electronics
 * @date        2026-02-27
 * @license     GNU GPL V3
 **************************************************
 */

// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE6PLUS) && !defined(ARDUINO_INKPLATE6PLUSV2)
#error "Wrong board selection for this example, please select e-radionica Inkplate 6Plus or Soldered Inkplate 6Plus in the boards menu."
#endif

#include "Inkplate.h"

int x_position = 50;
int y_position = 50;

Inkplate display(INKPLATE_1BIT);

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    // Uncomment this line if you have a USB Power Only Inkplate6PLUS
    // Must be called before display.begin()!
    //display.setInkplatePowerMode(INKPLATE_USB_PWR_ONLY);
    display.begin();
    display.clearDisplay();
    display.setCursor(100, 300);
    display.setTextSize(3);
    display.print("Touch button example. Touch the black button.");
    display.display();
    delay(3000);
    display.clearDisplay();
    // Init touchscreen and power it on after init (send false as argument to put it in deep sleep right after init)
    if (display.touchscreen.init(true))
    {
        Serial.println("Touchscreen init ok");
    }
    else
    {
        Serial.println("Touchscreen init fail");
        while (true);
    }

    //Draw initial rectangle
    display.fillRect(x_position, y_position, 100, 50, BLACK);
    display.display();
}

void loop()
{
    //Touch in area checks if touch ocured in given coordinates
    if(display.touchscreen.touchInArea(x_position, y_position, 100, 50))
    {
        x_position += 100;
        y_position += 100;

        if(y_position < 660)
        {
            display.clearDisplay();
            display.fillRect(x_position, y_position, 100, 50, BLACK);

            display.partialUpdate();
            delay(100);
        }
        else//Reseting rectangle position and doing full refresh
        {
            x_position = 50;
            y_position = 50;
            
            display.clearDisplay();
            display.fillRect(x_position, y_position, 100, 50, BLACK);
            display.display();
        }
    }

}
