#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const unsigned char bitmap_border_rec [] PROGMEM = {
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20,
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'box_rec', 128x20px
const unsigned char bitmap_box_rec [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// 'thick_box', 128x20px
const unsigned char bitmap_thick_box [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0,
0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00,
0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00,
0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00,
0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07,
0xc0, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff
};
// 'thick_rec', 128x20px
const unsigned char bitmap_thick_rec [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80,
0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00,
0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00,
0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00,
0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01,
0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0xff,
0xff, 0xff, 0xff, 0xff
};
#define ENTER_BUTTON 5
#define BACK_BUTTON 4
#define UP_BUTTON 18
#define DOWN_BUTTON 19
typedef struct
{
int oled_mode;
int enter_btn;
int back_btn;
int up_btn;
int down_btn;
} buttons_gui_t;
buttons_gui_t Buttons_1;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
#define LED_PIN 2
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
enum oled_mode
{
OLED_MONITOR,
OLED_MENU,
OLED_SETTING,
OLED_INFO,
};
bool read_button(int btn, void (*callback_func)() = NULL)
{
volatile int holdTimeCount = 0;
if (digitalRead(btn) == HIGH)
{
vTaskDelay(20/portTICK_PERIOD_MS);
if (digitalRead(btn) == HIGH)
{
while (digitalRead(btn) == HIGH)
{
vTaskDelay(20/portTICK_PERIOD_MS);
}
if (callback_func != NULL)
{
callback_func();
}
return true;
}
}
return false;
}
void oled128x32_drawBitmap(const unsigned char *bitmap_array,
int x = 0, int y = 0, int size_x=0,
int size_y=0, bool is_cleared_display=true)
{
if (is_cleared_display == true)
display.clearDisplay();
display.drawBitmap(x, y, bitmap_array, size_x, size_y, 2);
display.display();
}
void oled128x32_printText(const char *text, int x = 0, int y = 0, int size_text=1)
{
display.setTextSize(size_text);
display.setTextColor(WHITE);
display.setCursor(x, y);
display.println(text);
display.display();
}
void update_menu_mode()
{
if (Buttons_1.enter_btn == 0)
{
oled128x32_drawBitmap(bitmap_thick_rec, 0, 0, 128, 20, false);
oled128x32_drawBitmap(bitmap_border_rec, 0, 21, 128, 20, false);
oled128x32_drawBitmap(bitmap_border_rec, 0, 42, 128, 20, false);
}
else if (Buttons_1.enter_btn == 1)
{
oled128x32_drawBitmap(bitmap_border_rec, 0, 0, 128, 20, false);
oled128x32_drawBitmap(bitmap_thick_rec, 0, 21, 128, 20, false);
oled128x32_drawBitmap(bitmap_border_rec, 0, 42, 128, 20, false);
}
else
{
oled128x32_drawBitmap(bitmap_border_rec, 0, 0, 128, 20, false);
oled128x32_drawBitmap(bitmap_border_rec, 0, 21, 128, 20, false);
oled128x32_drawBitmap(bitmap_thick_rec, 0, 42, 128, 20, false);
}
oled128x32_printText("Monitor", 5, 5, 1);
oled128x32_printText("Set time", 5, 26, 1);
oled128x32_printText("Info", 5, 47, 1);
display.clearDisplay();
}
void update_monitor_mode()
{
oled128x32_printText("System ON", 10, 20, 2);
oled128x32_printText("10:20", 10, 38, 1);
oled128x32_printText("WiFi", 10, 50, 1);
display.clearDisplay();
}
void handle_back_menu()
{
Buttons_1.enter_btn = (Buttons_1.enter_btn + 1) % 3;
update_menu_mode();
}
void handle_enter_menu()
{
if (Buttons_1.enter_btn == 0)
{
Buttons_1.enter_btn = 0;
Buttons_1.oled_mode = OLED_MONITOR;
update_monitor_mode();
}
else if (Buttons_1.enter_btn == 1)
{
Buttons_1.oled_mode = OLED_SETTING;
}
else if (Buttons_1.enter_btn == 2)
{
Buttons_1.oled_mode = OLED_INFO;
}
}
void handle_enter_monitor()
{
Buttons_1.enter_btn = (Buttons_1.enter_btn + 1) % 2;
if (Buttons_1.enter_btn == 0)
{
//
}
else if (Buttons_1.enter_btn == 1)
{
//
}
update_monitor_mode();
}
void handle_back_monitor()
{
Buttons_1.enter_btn = 0;
Buttons_1.oled_mode = OLED_MENU;
update_menu_mode();
}
void handle_mode_oled()
{
switch (Buttons_1.oled_mode)
{
case OLED_MONITOR:
{
read_button(BACK_BUTTON, handle_back_monitor);
read_button(ENTER_BUTTON, handle_enter_monitor);
break;
}
case OLED_MENU:
{
read_button(BACK_BUTTON, handle_back_menu);
read_button(ENTER_BUTTON, handle_enter_menu);
break;
}
case OLED_SETTING:
{
// read_button(ENTER_BUTTON, handle_enter_menu);
read_button(BACK_BUTTON, handle_back_monitor);
// read_button(UP_BUTTON, handle_back_menu);
// read_button(DOWN_BUTTON, handle_back_menu);
break;
}
case OLED_INFO:
{
// read_button(BACK_BUTTON, handle_back_menu);
break;
}
}
}
void setup()
{
// put your setup code here, to run once:
pinMode(ENTER_BUTTON, INPUT_PULLUP);
pinMode(BACK_BUTTON, INPUT_PULLUP);
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("Hello, ESP32!");
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
{
Serial.println(F("SSD1306 allocation failed"));
while (1)
{
Serial.println("Failed to initialize oled display!");
delay(500);
}
}
Buttons_1.oled_mode = OLED_MENU;
display.clearDisplay();
// update_menu_mode();
// // Display Text
oled128x32_drawBitmap(bitmap_thick_box, 40, 10, 40, 20);
oled128x32_printText("WiFi", 16, 16, 1);
// display.setTextSize(1.5);
// display.setTextColor(WHITE);
// display.setCursor(0,28);
// display.println("Este proyecto");
// display.println("es muy interesante");
// display.display();
}
void loop()
{
// put your main code here, to run repeatedly:
// handle_mode_oled();
delay(10); // this speeds up the simulation
}