#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // تهيئة شاشة OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
// اطلب من المستخدم إدخال النص
display.println("enter text:");
display.display();
while (!Serial.available()) {
delay(100);
}
String userInput = Serial.readString();
char currentKey = userInput.charAt(0);
// احسب عدد الضغطات على المفتاح
int keyPressCount = countKeyPresses(userInput);
// استدعاء الدالة والحصول على الحرف المقابل
/* char result = getCharacterFromKeyPressCount(currentKey, keyPressCount);
// عرض النتيجة على شاشة OLED
display.clearDisplay();
display.print("ch/num:");
display.setCursor(0, 20);
display.print(result);
display.display();
*/
}
void loop() {
// قراءة المفتاح المضغوط
char key = keypad.getKey();
if (key) {
// احسب عدد الضغطات على المفتاح
int keyPressCount = countKeyPresses (String(key));
// استدعاء الدالة والحصول على الحرف المقابل
char result = getCharacterFromKeyPressCount(key, keyPressCount);
// عرض النتيجة على شاشة OLED
display.clearDisplay();
display.print("ch/num:");
display.setCursor(0, 20);
display.print(result);
display.display();
}
}
int countKeyPresses (String userInput) {
char currentKey = userInput.charAt(0);
int count = 1;
for (int i = 1; i < userInput.length(); i++) {
if (userInput.charAt(i) == currentKey) {
count++;
} else {
break;
}
}
return count;
}
char getCharacterFromKeyPressCount(char key, int count) {
// تحويل المفتاح إلى حرف صغير (إذا كان حرفًا)
char lowercaseKey = tolower(key);
// حساب قيمة المفتاح المقابلة بناءً على عدد الضغطات
int keyIndex;
if (isdigit(lowercaseKey)) {
// إذا كان المفتاح رقمًا
keyIndex = (lowercaseKey - '0' + count) % 10;
return '0' + keyIndex;
} else if (isalpha(lowercaseKey)) {
// إذا كان المفتاح حرفًا
keyIndex = (lowercaseKey - 'a' + count) % 26;
return 'a' + keyIndex;
} else {
// إذا كان المفتاح ليس حرفًا ولا رقمًا، قم بإرجاعه كما هو
return key;
}
}