#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // Ширина дисплея в пікселях
#define SCREEN_HEIGHT 64 // Висота дисплея в пікселях
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600); // Для налагодження
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
display.display();
}
// Функція displayPrint з коректною обробкою прокрутки та статичного виведення
void displayPrint(const char* text, int width, int x, int y, bool scrollEnabled) {
static int textOffset = 0;
static unsigned long lastScrollTime = 0;
int textWidth = strlen(text) * 6; // Оцінка ширини тексту (6 пікселів на символ)
display.setCursor(x - textOffset, y);
display.print(text);
// Прокрутка тексту, якщо ширина перевищує і scrollEnabled істинне
if (scrollEnabled && textWidth > width) {
if (millis() - lastScrollTime >= 50) {
textOffset += 1; // Зсув на 1 піксель
if (textOffset > textWidth - width) {
textOffset = 0; // Скидання прокрутки після повного проходу
}
lastScrollTime = millis();
}
} else if (!scrollEnabled) {
textOffset = 0; // Скидання зсуву при статичному відображенні
}
}
byte mdf_cursor(byte command) {
static byte cursorPos = 1;
if (command == 1) { // Повертає позицію курсора
return cursorPos;
}
return 0;
}
void loop() {
byte cursorPos = mdf_cursor(1); // Отримуємо поточну позицію курсора
display.clearDisplay();
// Викликаємо функцію для різних пунктів меню, керуючи прокруткою лише для першого пункту
displayPrint("Menu Item 1", 72, 6, 0, cursorPos == 1);
displayPrint("Menu Item 2", 72, 6, 10, cursorPos == 2);
displayPrint("Menu Item 3", 72, 6, 20, cursorPos == 3);
display.display();
}