// oroginal sketch https://wokwi.com/projects/311598148845830720
#include "TextPaint.h"
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
TextPaint myPaint = TextPaint(TFT_CS, TFT_DC);
#define PENRADIUS 3
int oldcolor, currentcolor;
uint16_t colors[]={ILI9341_RED, ILI9341_YELLOW, ILI9341_GREEN, ILI9341_CYAN, ILI9341_BLUE, ILI9341_MAGENTA, ILI9341_BLACK};
int colorsLen = sizeof(colors) / sizeof(colors[0]);;
int BOXSIZE = 240 / colorsLen;
void setup(void) {
myPaint.begin();
myPaint.tft.fillScreen(ILI9341_BLACK);
for(int i=0; i<colorsLen; i++)
myPaint.squareFill(BOXSIZE*i, 0, BOXSIZE, colors[i]);
myPaint.square(0, 0, BOXSIZE, ILI9341_WHITE);
currentcolor = colors[0];
}
void loop() {
TS_Point p = myPaint.getTouch();
// Перевірка, чи торкання було в зоні палітри
if (p.y < BOXSIZE && p.y > 0) {
int newColorIndex = -1;
// 1. Визначаємо, на який саме колір натиснули
for (int i = 0; i < colorsLen; i++) if (p.x < BOXSIZE * (i + 1)){
newColorIndex = i;
break; // Обов'язково зупиняємо цикл!
}
if (newColorIndex != -1) {
uint16_t selectedColor = colors[newColorIndex];
if (selectedColor != currentcolor) {
// 2. Повертаємо старий колір на місце (затираємо білу рамку)
// Шукаємо індекс старого кольору, щоб знайти його X
for (int i = 0; i < colorsLen; i++) if (currentcolor == colors[i]) {
myPaint.squareFill(BOXSIZE * i, 0, BOXSIZE, colors[i]);
break;
}
// 3. Встановлюємо новий колір і малюємо рамку вибору
currentcolor = selectedColor;
myPaint.square(BOXSIZE * newColorIndex, 0, BOXSIZE, ILI9341_WHITE);
}
}
}
// Малювання
if (((p.y - PENRADIUS) > BOXSIZE) && ((p.y + PENRADIUS) < myPaint.tft.height()))
if (p.x > 0) myPaint.tft.fillCircle(p.x, p.y, PENRADIUS, currentcolor);
}