#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define BUTTON_PIN A2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define button size and layout
#define BUTTON_WIDTH 22
#define BUTTON_HEIGHT 14
#define BUTTON_MARGIN 2
#define BUTTONS_PER_ROW 5 // Number of buttons in each row
typedef struct {
uint16_t sAddress;
uint8_t sCommand;
uint8_t sProtocol;
char name[20];
} Mx750niCommand;
// Sample commands to display as buttons
const Mx750niCommand commands[] PROGMEM = {
{0x1234, 0x01, 0x02, "V+"},
{0x2345, 0x02, 0x03, "V-"},
{0x3456, 0x03, 0x04, "Pwr"},
{0x4567, 0x04, 0x05, "->"},
{0x5678, 0x05, 0x06, "<-"},
{0x6789, 0x06, 0x07, "^"},
{0x789A, 0x07, 0x08, "D"},
{0x89AB, 0x08, 0x09, "Nxt"},
{0x1234, 0x01, 0x02, "V+"},
{0x2345, 0x02, 0x03, "V-"},
{0x3456, 0x03, 0x04, "Pwr"},
{0x4567, 0x04, 0x05, "->"},
{0x5678, 0x05, 0x06, "<-"},
{0x6789, 0x06, 0x07, "^"},
{0x789A, 0x07, 0x08, "D"},
{0x89AB, 0x08, 0x09, "Nxt"}
};
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // OLED I2C address
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
drawButtons();
}
void drawButton(int x, int y, int w, int h, const char *text, bool highlight) {
// Draw the button rectangle
if (highlight) {
display.fillRect(x, y, w, h, SSD1306_WHITE); // Highlighted button background
} else {
display.drawRect(x, y, w, h, SSD1306_WHITE); // Normal button border
}
// Set the text color based on highlight status
if (highlight) {
display.setTextColor(SSD1306_BLACK); // Use black text on highlighted button
} else {
display.setTextColor(SSD1306_WHITE); // Use white text on normal button
}
// Calculate text position to center it within the button
int16_t x1, y1;
uint16_t textWidth, textHeight;
display.getTextBounds(text, x, y, &x1, &y1, &textWidth, &textHeight);
int textX = x + (w - textWidth) / 2;
int textY = y + (h - textHeight) / 2;
// Set cursor to calculated position and print the text
display.setCursor(textX, textY);
display.print(text);
}
void drawButtons() {
display.clearDisplay();
int totalCommands = sizeof(commands) / sizeof(commands[0]);
char name[20];
for (int i = 0; i < totalCommands; i++) {
int buttonX = (i % BUTTONS_PER_ROW) * (BUTTON_WIDTH + BUTTON_MARGIN) + BUTTON_MARGIN;
int buttonY = (i / BUTTONS_PER_ROW) * (BUTTON_HEIGHT + BUTTON_MARGIN) + BUTTON_MARGIN;
strncpy_P(name, commands[i].name, sizeof(name));
name[sizeof(name) - 1] = '\0';
// Draw each button with the corresponding command name
drawButton(buttonX, buttonY, BUTTON_WIDTH, BUTTON_HEIGHT,name, false);
}
display.display();
}
void loop() {
// Example navigation: change pages every 2 seconds
int buttonValue = analogRead(BUTTON_PIN); // Read the analog value from the button pin
Serial.println(buttonValue);
// // Determine which button is pressed based on the analog value
// if (buttonValue < 100) { // Adjust the thresholds according to your resistor values
// } else if (buttonValue < 200) {
// Serial.println("Button 2 Pressed");
// } else if (buttonValue < 400) {
// Serial.println("Button 3 Pressed");
// } else if (buttonValue < 600) {
// Serial.println("Button 4 Pressed");
// } else if (buttonValue < 800) {
// Serial.println("Button 5 Pressed");
// } else {
// Serial.println("No Button Pressed");
// }
delay(1000); // Small delay to avoid spamming the Serial Monitor
}