#include <Arduino.h>
#include <U8g2lib.h>
#include <Keypad.h>
#include <Keyboard.h>
U8G2_SSD1322_NHD_128X64_F_4W_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 9, 8);
const byte ROWS = 5;
const byte COLS = 5;
byte rowPins[ROWS] = {2, 3, 4, 5, 6};
byte colPins[COLS] = {A0, A1, A2, A3, A4};
char keys[ROWS][COLS] = {
{'p', '1', '2', '3', 'A'},
{'4', '5', '6', '7', 'B'},
{'8', '9', '0', '#', 'C'},
{'*', 'F', 'E', 'D', 'G'},
{'H', 'I', 'J', 'K'}
};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
bool powerOn = false;
bool buttonPressed = false;
unsigned long buttonPressStartTime = 0;
void setup() {
Serial.begin(9600);
Keyboard.begin();
u8g2.begin();
u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFontRefHeightExtendedText();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
u8g2.drawStr(5, 10, "Power is OFF");
u8g2.sendBuffer();
}
void loop() {
char key = keypad.getKey();
if (key == 'p') {
buttonPressed = true;
buttonPressStartTime = millis();
} else if (buttonPressed && key == NO_KEY) {
buttonPressed = false;
unsigned long duration = millis() - buttonPressStartTime;
if (duration >= 3000) {
powerOn = !powerOn;
if (powerOn) {
displayMessage("Power is ON");
delay(2000);
clearDisplay();
displayMessage("System is Ready");
delay(2000);
clearDisplay();
displayMessage("Press Enter Keys");
delay(2000);
clearDisplay();
} else {
displayMessage("Power is OFF");
delay(2000);
clearDisplay();
}
}
}
if (powerOn && key != NO_KEY) {
Keyboard.write(key);
displayMessage(String(key) + " [ OK ]");
delay(1000);
clearDisplay();
}
}
void displayMessage(const String& message) {
u8g2.clearBuffer();
u8g2.drawStr(5, 10, message.c_str());
u8g2.sendBuffer();
}
void clearDisplay() {
u8g2.clearBuffer();
u8g2.sendBuffer();
}