#include <U8g2lib.h>
// Initialize display for ESP32-C3 with specified I2C pins (GPIO 8 and GPIO 7)
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE, /* clock=*/8, /* data=*/7);
// Bitmap icons
const uint8_t bitmap_icon1[] U8X8_PROGMEM = {
0x00, 0x00, 0x07, 0xC0, 0x1F, 0xE0, 0x3F, 0xF0,
0x7F, 0xF8, 0x7F, 0xF8, 0x3F, 0xF0, 0x1F, 0xE0,
0x07, 0xC0, 0x07, 0xC0, 0x1F, 0xE0, 0x3F, 0xF0,
0x7F, 0xF8, 0x7F, 0xF8, 0x3F, 0xF0, 0x1F, 0xE0
};
const uint8_t bitmap_icon2[] U8X8_PROGMEM = {
0x00, 0x18, 0x24, 0x42, 0x81, 0x42, 0x24, 0x18,
0x18, 0x24, 0x42, 0x81, 0x42, 0x24, 0x18, 0x00
};
// Array of bitmap pointers
const uint8_t* bitmap_icons[] = {bitmap_icon1, bitmap_icon2};
const char* menu_items[] = {"Option 1", "Option 2"};
uint8_t item_selected = 0;
// Define button pins for ESP32-C3
const int button_up = 3; // GPIO 3 for UP button
const int button_down = 4; // GPIO 4 for DOWN button
void setup() {
Serial.begin(115200); // Debugging
u8g2.begin();
// Initialize buttons
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
Serial.println("Setup complete! Ready to display menu.");
}
void loop() {
// Check button presses
if (digitalRead(button_up) == LOW) {
item_selected = (item_selected + 1) % 2; // Next menu item
delay(200); // Debounce
Serial.println("Button UP pressed!");
}
if (digitalRead(button_down) == LOW) {
item_selected = (item_selected - 1 + 2) % 2; // Previous menu item
delay(200); // Debounce
Serial.println("Button DOWN pressed!");
}
// Clear and render the display
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_7x14B_tr);
u8g2.drawStr(25, 37, menu_items[item_selected]); // Draw menu text
u8g2.drawXBMP(4, 24, 16, 16, bitmap_icons[item_selected]); // Draw bitmap icon
u8g2.sendBuffer();
delay(100); // Small delay for display refresh
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1