/**
 **************************************************
 * @file        Inkplate6FLICK_Touchscreen_Draw.ino
 * @brief       Touchscreen drawing example for Soldered Inkplate 6FLICK.
 *
 * @details     Demonstrates how to use the Inkplate 6FLICK touchscreen to draw
 *              directly on the e-paper display. The sketch reads touch
 *              coordinates and renders graphics at the touched location.
 *
 *              Two drawing modes are available:
 *              - Line drawing: draws a continuous line following the finger.
 *              - Circle drawing: draws filled circles at touch points.
 *
 *              The display is refreshed using partial updates to allow faster
 *              drawing and improved responsiveness.
 *
 * Requirements:
 * - Board:      Soldered Inkplate 6FLICK
 * - Hardware:   Inkplate 6FLICK, USB cable
 *
 * Configuration:
 * - Select drawing mode by enabling one of the macros:
 *      #define DRAW_LINE
 *      #define DRAW_CIRCLE
 *
 * Don't have Inkplate Boards in Arduino Boards Manager?
 * See https://docs.soldered.com/inkplate/6flick/quick-start-guide/
 *
 * How to use:
 * 1) Upload the sketch to Inkplate 6FLICK.
 * 2) Touch the screen to draw.
 * 3) In line mode, a continuous line follows your finger.
 * 4) In circle mode, filled circles are drawn where the screen is touched.
 *
 * Expected behavior:
 * - Touch events are detected by the touchscreen controller.
 * - Graphics are drawn at the detected coordinates.
 * - Partial updates refresh only the modified area for faster interaction.
 *
 * Notes:
 * - The touchscreen supports up to two simultaneous touch points, but this
 *   example uses only the first detected touch.
 * - Partial updates significantly improve drawing responsiveness compared to
 *   full display refreshes.
 * - Touchscreen coordinates are automatically adjusted based on display
 *   rotation.
 *
 * 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
#ifndef ARDUINO_INKPLATE6FLICK
#error "Wrong board selection for this example, please select Soldered Inkplate 6FLICK"
#endif

#include "Inkplate.h"

// Select to draw a line on screen or filled circle
#define DRAW_LINE
// #define DRAW_CIRCLE

Inkplate display(INKPLATE_1BIT);

#ifdef DRAW_LINE
uint16_t xOld, yOld;
#endif

void setup()
{
    // put your setup code here, to run once:
    Serial.begin(115200);
    display.begin();
    display.display();
    // 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)
            ;
    }
}

void loop()
{
    // Check if there is any touch detected
    if (display.touchscreen.available())
    {
        uint8_t n;
        uint16_t x[2], y[2];
        // See how many fingers are detected (max 2) and copy x and y position of each finger on touchscreen
        n = display.touchscreen.getData(x, y);
        if (n != 0)
        {
#ifdef DRAW_LINE // Draw line from old point to new
            display.drawLine(xOld, yOld, x[0], y[0], BLACK);

            // Save coordinates to use as old next time
            xOld = x[0];
            yOld = y[0];
#endif

#ifdef DRAW_CIRCLE // Draw circle on touch event coordinates
            display.fillCircle(x[0], y[0], 20, BLACK);
#endif
            display.partialUpdate();
        }
    }
}
