#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const char* buttonNames[16] = {
"A", "B", "X", "Y",
"A1", "A2", "RB", "LB",
"Se", "St",
"dU", "dD",
"dL", "dR",
"RT", "LT"
};
byte listButton[16 + 2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 16};
class ButGamepad {
public:
ButGamepad(int pin, int buttonIndex) : pin(pin), buttonIndex(buttonIndex), lastState(HIGH), buttonCheckFlag(false) {
pinMode(pin, INPUT_PULLUP);
}
int gamepadButton(bool press_check) {
int currentState = digitalRead(pin);
// Якщо кнопка натиснута (LOW) і раніше була відпущена
if (currentState == LOW && !buttonCheckFlag) {
buttonCheckFlag = true;
return gamepad_button_pressed; // Кнопка натиснута
}
// Якщо кнопка відпущена (HIGH) і раніше була натиснута
else if (currentState == HIGH && buttonCheckFlag) {
buttonCheckFlag = false;
return gamepad_button_release; // Кнопка відпущена
}
// Повертає поточний стан залежно від того, що вказано в press_check
return press_check ? gamepad_button_pressed : gamepad_button_release;
}
private:
int pin;
int buttonIndex;
bool buttonCheckFlag; // Для зберігання попереднього стану кнопки
int lastState; // Зберігає попередній стан кнопки
};
ButGamepad butGamepad[16 + 2] = {
ButGamepad(2, 0), ButGamepad(4, 1), ButGamepad(5, 2), ButGamepad(18, 3),
ButGamepad(19, 4), ButGamepad(21, 5), ButGamepad(22, 6), ButGamepad(23, 7),
ButGamepad(25, 8), ButGamepad(26, 9), ButGamepad(27, 10), ButGamepad(32, 11),
ButGamepad(33, 12), ButGamepad(34, 13), ButGamepad(35, 14), ButGamepad(36, 15),
ButGamepad(14, 16), ButGamepad(13, 17)
};
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextSize(1);
display.setTextColor(WHITE);
display.clearDisplay();
Serial.begin(9600);
}
void loop() {
display.clearDisplay();
drawSquaresWithNames();
drawRectanglesWithNames();
display.display();
}
void drawSquaresWithNames() {
static bool inverted[16] = {false};
int squareSize = 15;
int padding = 0;
int gridSize = 4;
int totalWidth = gridSize * squareSize + (gridSize - 1) * padding;
int xOffset = 128 - totalWidth;
for (int buttonIndex = 0; buttonIndex < 16; buttonIndex++) {
int row = buttonIndex / gridSize;
int col = buttonIndex % gridSize;
int x = xOffset + col * (squareSize + padding);
int y = row * (squareSize + padding);
if (butGamepad[buttonIndex].gamepadBattonPress()) {
int squareIndex = listButton[buttonIndex] - 1;
if (squareIndex >= 0) {
inverted[squareIndex] = !inverted[squareIndex];
display.fillRect(x, y, squareSize, squareSize, WHITE);
display.setTextColor(BLACK);
}
} else {
display.drawRect(x, y, squareSize, squareSize, WHITE);
display.setTextColor(WHITE);
}
int textX = (strlen(buttonNames[buttonIndex]) == 1) ? x + 5 : x + 2;
int textY = y + 4;
display.setCursor(textX, textY);
display.print(buttonNames[buttonIndex]);
}
}
void drawRectanglesWithNames() {
drawButton("Rs", 16, 0, 0); // Зсунули вправо на 1 піксель
drawButton("Rm", 17, 0, 16); // Зсунули вправо на 1 піксель
}
void drawButton(const char* buttonLabel, int buttonIndex, int x, int y) {
int squareSize = 15;
int padding = 2;
bool isPressed = butGamepad[buttonIndex].gamepadBattonPress();
// Визначаємо дублювання
int duplicateIndex = listButton[buttonIndex] - 1;
const char* displayName = " ";
if (duplicateIndex >= 0 && duplicateIndex < 16) {
displayName = buttonNames[duplicateIndex];
}
// Нова змінна для позиції другого квадрата по осі X
int secondSquareOffsetX = 24; // Встановіть це значення для корекції
// Відображення квадратів
if (isPressed) {
display.fillRect(x, y, squareSize, squareSize, WHITE);
display.fillRect(x + secondSquareOffsetX, y, squareSize, squareSize, WHITE); // Використання нової змінної
} else {
display.drawRect(x, y, squareSize, squareSize, WHITE);
display.drawRect(x + secondSquareOffsetX, y, squareSize, squareSize, WHITE); // Використання нової змінної
}
// Текст кнопки Rs або Rm
display.setCursor(x - 1 + 3, y + 4);
display.setTextColor(isPressed ? BLACK : WHITE);
display.print(buttonLabel);
// Знак рівності (завжди білий)
display.setTextColor(WHITE); // Знак рівності завжди білий
display.setCursor(x - 1 + squareSize + 3, y + 4);
display.print("=");
// Виводимо знак "?" або назву дубльованої кнопки
int equalSignWidth = 6; // Ширина знаку рівності в пікселях
int textOffset = -10; // Відступ тексту від знака "="
int textX = x + secondSquareOffsetX + squareSize + textOffset;
if (strlen(displayName) == 2) {
textX -= 3; // Зсув для центрування
}
display.setCursor(textX, y + 4); // Корекція позиції для тексту
display.setTextColor(isPressed ? BLACK : WHITE); // Колір тексту для дубльованої кнопки
display.print(displayName); // Текст "?" або дубльована кнопка
display.setTextColor( WHITE);
//byte Lines[] = {48};// передаємо координати переміщення функції
//mdf_cursor(cursor_read_encoder, sizeof(Lines), Lines);
display.setCursor(0, 48);
display.println(" Back" );
}