#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define potv A2
#define poth A3
#define but 4
#define TFT_DC 2
#define TFT_CS 3
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Переменные для управления
int potv_val = 0, poth_val = 0;
int x = 0, y = 0, lx = 0, ly = 0;
bool buttonState = HIGH, lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 30;
unsigned long lastUpdateTime = 0;
unsigned long updateInterval = 50; // 20 кадров/с = 50 мс
// Настройки оформления
int textColor = ILI9341_GREEN;
int backgroundColor = ILI9341_BLACK;
int textSize = 3;
bool inverted = false;
// Размеры текста (приблизительные)
const int textWidth = 150;
const int textHeight = 24;
void setup() {
pinMode(potv, INPUT);
pinMode(poth, INPUT);
pinMode(but, INPUT_PULLUP);
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK);
// Начальная позиция
x = tft.width() / 2 - textWidth / 2;
y = tft.height() / 2 - textHeight / 2;
lx = x;
ly = y;
// Первоначальная отрисовка
drawTextWithFrame();
}
void loop() {
unsigned long currentTime = millis();
// Ограничение частоты обновления (20 кадров/с)
if (currentTime - lastUpdateTime < updateInterval) {
return;
}
// Чтение потенциометров
potv_val = analogRead(potv);
poth_val = analogRead(poth);
// Преобразование координат с учетом границ экрана
int newX = map(poth_val, 0, 1023, 0, tft.width() - textWidth);
int newY = map(potv_val, 0, 1023, 0, tft.height() - textHeight);
// Обработка кнопки с антидребезгом
bool reading = digitalRead(but);
if (reading != lastButtonState) {
lastDebounceTime = currentTime;
}
if ((currentTime - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
changeAppearance();
}
}
}
lastButtonState = reading;
// Проверка изменения позиции
if (newX != x || newY != y) {
x = newX;
y = newY;
// Перерисовка только если позиция изменилась
if (x != lx || y != ly) {
clearPreviousFrame();
drawTextWithFrame();
lx = x;
ly = y;
}
}
lastUpdateTime = currentTime;
}
void changeAppearance() {
// Переключение между тремя режимами оформления
if (textColor == ILI9341_GREEN && textSize == 3 && !inverted) {
// Режим 2: желтый текст, размер 2
textColor = ILI9341_YELLOW;
textSize = 2;
} else if (textColor == ILI9341_YELLOW && textSize == 2 && !inverted) {
// Режим 3: инверсия (белый текст на черном фоне)
textColor = ILI9341_WHITE;
backgroundColor = ILI9341_BLACK;
inverted = true;
} else {
// Возврат к режиму 1: зеленый текст, размер 3
textColor = ILI9341_GREEN;
textSize = 3;
backgroundColor = ILI9341_BLACK;
inverted = false;
}
// Принудительная перерисовка при изменении оформления
clearPreviousFrame();
drawTextWithFrame();
}
void clearPreviousFrame() {
// Очистка предыдущей позиции текста и рамки
int framePadding = 5;
tft.fillRect(lx - framePadding, ly - framePadding,
textWidth + 2 * framePadding, textHeight + 2 * framePadding,
ILI9341_BLACK);
}
void drawTextWithFrame() {
int framePadding = 5;
// Рисуем рамку
tft.drawRect(x - framePadding, y - framePadding,
textWidth + 2 * framePadding, textHeight + 2 * framePadding,
ILI9341_WHITE);
// Рисуем текст
tft.setCursor(x, y);
tft.setTextColor(textColor, backgroundColor);
tft.setTextSize(textSize);
tft.println("RTU MIREA");
}