#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Initialize the LCD (20x4)
LiquidCrystal_I2C LCD(0x27, 20, 4);
// Define keypad settings
const byte ROWS = 4; // Number of rows
const byte COLS = 3; // Number of columns
// Define the symbols on the keypad keys
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
// Connect keypad ROW pins to these Arduino pins
byte rowPins[ROWS] = {5, 15, 2, 0};
// Connect keypad COL pins to these Arduino pins
byte colPins[COLS] = {4, 16, 17}; // Adjust pins for 3 columns
// Create the Keypad object
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int getSignalStrengthPercent() {
return 75; // Example value for testing
}
// Function to display the Wi-Fi signal strength
void displayWiFiSignal() {
int signalStrength = getSignalStrengthPercent();
String signalText;
// Determine the signal strength description
if (signalStrength <= 0) {
signalText = "No Signal(0%)";
} else if (signalStrength <= 20) {
signalText = "Weak(" + String(signalStrength) + "%)";
} else if (signalStrength <= 40) {
signalText = "Fair(" + String(signalStrength) + "%)";
} else if (signalStrength <= 60) {
signalText = "Good(" + String(signalStrength) + "%)";
} else if (signalStrength <= 80) {
signalText = "Strong(" + String(signalStrength) + "%)";
} else {
signalText = "Excellent(" + String(signalStrength) + "%)";
}
LCD.print("Wifi> ");
LCD.print(signalText);
}
/**
* @brief Displays a confirmation prompt for recharging a specified amount and waits for user input.
*
* This function displays a message on the LCD asking the user to confirm recharging the specified amount.
* The user can press `'#'` to confirm or `'*'` to cancel. It returns `true` for confirmation and `false` for cancellation.
*
* @param amount The recharge amount to be confirmed, displayed on the LCD.
* @return bool Returns `true` if confirmed with `'#'`, and `false` if canceled with `'*'`.
*/
bool confirmRechargeLCD(uint16_t amount) {
// Clear the LCD screen
LCD.clear();
// Display the confirmation message (centered manually)
LCD.setCursor(1, 0);
LCD.print("Confirm Recharge ?");
// Prepare the recharge amount text
String amountText = String(amount) + " Units";
int textLength = amountText.length();
int padding = (20 - textLength) / 2; // Calculate padding for centering
// Display the centered recharge amount
LCD.setCursor(padding, 2);
LCD.print(amountText);
// Display the options
LCD.setCursor(0, 3);
LCD.print("No: '*' Yes: '#'");
// Await user input
while (true) {
char key = keypad.getKey(); // Capture keypad input
// Return true if the user confirms with '#'
if (key == '#') {
return true;
}
// Return false if the user cancels with '*'
if (key == '*') {
return false;
}
delay(50); // Short delay to debounce key presses
}
}
/**
* @brief Displays a confirmation prompt on the LCD and waits for user input.
*
* This function displays a prompt asking the user to confirm (`#`) or cancel (`*`).
* @param Text The message to display.
* @return `true` if confirmed, `false` if canceled.
*/
bool confirmInput(const char* Text) {
// Clear the LCD screen and display the confirmation prompt
LCD.clear();
LCD.setCursor(0, 0);
LCD.print(" Save the entry?");
// Display the detailed prompt message
LCD.setCursor(0, 1);
LCD.print("> ");
LCD.print(Text);
LCD.setCursor(19, 1);
LCD.print("<");
// Display the options at the bottom of the screen
LCD.setCursor(0, 3);
LCD.print("No: '*' Yes: '#'");
// Loop to wait for user input
while (true) {
char key = keypad.getKey(); // Retrieve key press
// Check for confirmation ('#') or cancellation ('*')
if (key == '#') {
return true; // User confirmed
}
if (key == '*') {
return false; // User canceled
}
// Short delay to debounce key presses
delay(50);
}
}
// Placeholder version of LockCardPage
void LockCardPage() {
// Simulating the start of the function loop
startOver:
LCD.clear(); // Clear screen
LCD.setCursor(0, 0);
LCD.print("LOCK CARD |");
// Display current time in top-left corner
LCD.setCursor(14, 0);
LCD.print("11:23");
// Display Wi-Fi signal strength
LCD.setCursor(0, 1);
displayWiFiSignal();
// Display instructions for placing the card
LCD.setCursor(0, 2);
LCD.print(" Place card");
LCD.setCursor(0, 3);
LCD.print(">");
LCD.setCursor(19, 3);
LCD.print("<");
// Simulating interaction with the keypad
while (true) {
// Simulated input for testing (replace with actual keypad interaction)
char character = '1'; // Placeholder for key press (for testing, can simulate '*' or any key)
// If a key is pressed (not NO_KEY)
if (character != NO_KEY) {
// If the '*' key is pressed, exit the Lock Card page
if (character == '*') return;
// Simulate card detection (always returns true for testing)
bool isCardDetected = true;
bool isMasterCard = false;
if (isCardDetected) {
// Simulate locking the card (always succeeds for testing)
if (!isMasterCard) {
bool lockSuccess = true; // Simulated lock result
if (lockSuccess) {
// If successful, display a success message
LCD.setCursor(2, 3);
LCD.print("Locking Succeed!");
//goto startOver; // Restart the process to allow for new operations
} else {
// If locking fails, display a failure message
LCD.setCursor(2, 3);
LCD.print("Locking Failed!");
//goto startOver; // Restart the process for retry
}
}
}
}
// Small delay to debounce the keypad input
delay(200);
}
}
// Function to scroll the text between defined X positions on a specific line
void scrollTextOnLine(String text, int startX, int stopX, int line) {
int len = text.length();
int scrollIndex = 0; // Track the starting character for each scroll
int scrollPosition = startX; // Start scrolling from the left position
// Loop to scroll the text continuously until the text disappears completely
while (scrollPosition < len + 5) {
// Clear the section from startX to stopX
LCD.setCursor(startX, line);
for (int i = 0; i < (stopX - startX + 1); i++) {
LCD.print(" "); // Print spaces to clear the section
}
// Display the text starting from the current scrollIndex
LCD.setCursor(startX, line); // Set the cursor back to startX for each frame
for (int i = scrollIndex; i < scrollIndex + (stopX - startX + 1); i++) {
if (i < len) {
LCD.print(text.charAt(i)); // Print the character at the current index
}
}
delay(300); // Adjust the scroll speed here
// Increment scrollIndex to shift the text one position to the left
scrollIndex++;
// When scrollIndex exceeds the length of the text, we add spaces at the end
if (scrollIndex > len) {
scrollIndex = len; // Keep printing spaces after the text has ended
}
// Move the text one position to the right on the LCD
scrollPosition++;
}
}
void HomePage(const char* nameTop) {
static unsigned long lastUpdate = 0; // Tracks the last update time
static unsigned long lastUpdate01 = 0; // Tracks the last update time for case 0
static int screenState = 0; // Tracks the current screen state
const unsigned long interval = 5000; // Interval for screen change (5 seconds)
unsigned long currentMillis = millis(); // Current time
// Check if the interval has passed for screen state update
if (currentMillis - lastUpdate >= interval) {
lastUpdate = currentMillis; // Update the last update time
// Switch through different screens
switch (screenState) {
case 0: // Display user and time
// Display Home title and time
LCD.setCursor(0, 0);
LCD.print("HOME");
LCD.setCursor(15, 0);
LCD.print("12:25"); // Display current time
// Display user name
LCD.setCursor(0, 1);
LCD.print(" ");
LCD.setCursor(0, 1);
LCD.print("Manager> ");
LCD.print(nameTop);
// Display current date
LCD.setCursor(0, 2);
LCD.print(" ");
LCD.setCursor(0, 2);
LCD.print("Date> ");
LCD.print("13-OCT-2024"); // Display current time
// Display WiFi signal status
LCD.setCursor(0, 3);
displayWiFiSignal();
// Move to the next state to show balance and last communication
screenState = 1;
// Update the last time this screen was shown
while (millis() - lastUpdate01 < interval){
//do some stuff like checking the button and card
};
lastUpdate01 = millis();
break;
case 1: // Display balance and last communication with scrolling
// Display Home title and time again
LCD.setCursor(0, 0);
LCD.print("HOME");
LCD.setCursor(15, 0);
LCD.print("12:25"); // Display current time
// Display balance units
LCD.setCursor(0, 1);
LCD.print(" ");
LCD.setCursor(0, 1);
LCD.print("Balance> ");
LCD.print(2555); // Display balance
LCD.setCursor(15, 1);
LCD.print("Units");
// Display last communication info
LCD.setCursor(0, 2);
LCD.print(" ");
LCD.setCursor(0, 2);
LCD.print("LastComm> ");
scrollTextOnLine("25-OCT-2024 11:04 - 1500", 10, 19, 2); // Scroll last communication details
// Move back to the first screen state after displaying balance and communication
screenState = 0;
break;
}
}
}
void Recherge(){
LCD.setCursor(0, 0);
LCD.print("RECHARGE");
LCD.setCursor(15, 0);
LCD.print("11:03"); // Replace with actual time function
LCD.setCursor(0, 1);
LCD.print("POS > ");
LCD.print(2000);
LCD.setCursor(15, 1);
LCD.print("Units");
LCD.setCursor(0, 2);
LCD.print("L CARD > ");
LCD.print("175"); // Replace with actual date function
LCD.setCursor(15, 2);
LCD.print("Units");
LCD.setCursor(0, 3);
LCD.print("Select> ");
scrollTextOnLine(" 1-100 2-200 3-300", 7,19,3); // Scroll the last communication details
}
void SelectAction(){
LCD.setCursor(0, 0);
LCD.print("SELECT ACTIONS");
LCD.setCursor(15, 0);
LCD.print("11:03"); // Replace with actual time function
LCD.setCursor(0, 1);
LCD.print("01 > ");
LCD.print(" RECHARGE");
LCD.setCursor(0, 2);
LCD.print("02 > ");
LCD.print("SET NUMBER"); // Replace with actual date function
LCD.setCursor(0, 3);
LCD.print("* > ");
LCD.print(" HOME"); // Replace with actual date function
}
void numprint(){
LCD.setCursor(0, 0);
LCD.print("01 > ");
LCD.print("0898356109"); // Replace with actual time function
LCD.print("| SET"); // Replace with actual time function
LCD.setCursor(0, 1);
LCD.print("02 > ");
LCD.print("0896745542");
LCD.print("| THE"); // Replace with actual time function
LCD.setCursor(0, 2);
LCD.print("03 > ");
LCD.print("0054429793"); // Replace with actual date function
LCD.print("| NUM"); // Replace with actual time function
LCD.setCursor(0, 3);
LCD.print("04 > ");
LCD.print("0998800040"); // Replace with actual date func tion
LCD.print("|"); // Replace with actual time function
scrollTextOnLine(" 11:15 ", 16,19,3);
}
void setup() {
// Initialize the LCD
LCD.init();
LCD.backlight();
LCD.clear();
SelectAction();
}
void loop() {
// Recherge();
//HomePage("Rameshk");
numprint();
}