#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
#include <Keypad.h>
const byte ROWS = 3;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'A','B','C'},
{'<', '^','D'},
{'_','X','>'}
};
byte rowPins[ROWS] = {6, 5, 4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 15, 16}; //connect to the column pinouts of the keypad
//Create an object of keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define OLED_WHITE_COLOR WHITE
#define OLED_BLACK_COLOR BLACK
void display_border() {
oled.drawRect(0, 0, 128, 64, OLED_WHITE_COLOR);
// display.drawRoundRect(0, 0, 128, 64, 4, OLED_WHITE_COLOR);
}
// void display_fan(int x, int y, int fan_number, int fan_state) {
// oled.setTextColor(OLED_WHITE_COLOR); // set text color
// oled.setTextSize(1);
// oled.drawBitmap(x, y, fan_bitmap_16px, 16, 16, 1);
// oled.setCursor(x, y + 20); // set position to display (x,y)
// oled.print("Fan");
// display.println(fan_number);
// display.fillRoundRect(x + 28, y + 8, 22, 10, 2, OLED_WHITE_COLOR);
// display.setTextColor(OLED_BLACK_COLOR, OLED_WHITE_COLOR); // 'inverted' text
// display.setCursor(x + 30, y + 10); // set position to display (x,y)
// if (fan_state == 0) {
// display.println("OFF"); // set text
// } else {
// display.println("ON"); // set text
// }
// }
struct Menu_Option {
int x;
int y;
String name;
bool selected;
};
const int NUM_MENU_OPTIONS = 12;
Menu_Option Menu_Option_Array[NUM_MENU_OPTIONS] = {
{5, 0, "WIFI",false},
{5, 10, "BLE",true},
{5, 20, "PWM",false},
{5, 30, "DI",false},
{5, 40, "DO",false},
{5, 50, "AI",false},
{60, 0, "A0",false},
{60, 10, "WIFI 8",false},
{60, 20, "WIFI 9",false},
{60, 30, "NULL",false},
{60, 40, "Theme",false},
{60, 50, "Settings",false}
};
void display_option(int x, int y, String option_name, bool selected){
if(selected){
// oled.drawRoundRect(x, y, 120, 20, 4, OLED_WHITE_COLOR);
oled.setCursor(x, y+2);
oled.print("->");
oled.println(option_name); // set text
}else{
oled.setCursor(x+12, y+2);
oled.println(option_name); // set text
}
// oled.drawBitmap(x+5, y+2, logo, 16, 16, 1);
}
void display_menu(){
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
for(int i=0; i<NUM_MENU_OPTIONS; i++)
{
display_option(Menu_Option_Array[i].x, Menu_Option_Array[i].y, Menu_Option_Array[i].name,Menu_Option_Array[i].selected);
}
oled.display();
}
void setup() {
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(5, 25); // set position to display (x,y)
oled.println("Heart Beat"); // set text
oled.display(); // display on OLED
}
void loop(){
display_menu();
char key = keypad.getKey();// Read the key
// Print if key pressed
if (key){
Serial.print("Key Pressed : ");
Serial.println(key);
if(key=='_'){
for(int i=0; i<NUM_MENU_OPTIONS-1; i++)
{
if(Menu_Option_Array[i].selected){
Menu_Option_Array[i].selected = false;
Menu_Option_Array[i+1].selected = true;
break;
}
}
}else if(key=='^'){
for(int i=1; i<NUM_MENU_OPTIONS; i++)
{
if(Menu_Option_Array[i].selected){
Menu_Option_Array[i].selected = false;
Menu_Option_Array[i-1].selected = true;
break;
}
}
}else if(key=='>'){
for(int i=0; i<NUM_MENU_OPTIONS; i++){
if(i+6<NUM_MENU_OPTIONS)
{
if(Menu_Option_Array[i].selected){
Menu_Option_Array[i].selected = false;
Menu_Option_Array[i+6].selected = true;
break;
}
}
}
}else if(key=='<'){
for(int i=0; i<NUM_MENU_OPTIONS; i++){
if(i-6>=0)
{
if(Menu_Option_Array[i].selected){
Menu_Option_Array[i].selected = false;
Menu_Option_Array[i-6].selected = true;
break;
}
}
}
}else if(key=='X'){
for(int i=0; i<NUM_MENU_OPTIONS; i++){
if(Menu_Option_Array[i].selected){
Menu_Option_Array[i].selected = false;
Menu_Option_Array[i-6].selected = true;
break;
}
}
}
// oled.clearDisplay(); // clear display
// oled.setTextSize(3); // set text size
// oled.setTextColor(WHITE); // set text color
// oled.setCursor(50, 20); // set position to display (x,y)
// oled.println(key); // set text
// oled.display();
}
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1