Advertisement
bitwise_gamgee

Untitled

Jul 12th, 2023
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include "postfix.h"
  3.  
  4. // LCD display
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6.  
  7. // Keypad
  8. const int rows = 4;
  9. const int cols = 4;
  10. char keymap[rows][cols] = {
  11.   {'1', '5', '9', '*'},
  12.   {'2', '6', '0', '/'},
  13.   {'3', '7', '+', '('},
  14.   {'4', '8', '-', ')'}
  15. };
  16. byte rPins[rows] = {2, 3, 4, 5};
  17. byte cPins[cols] = {6, 7, 8, 9};
  18.  
  19. // Stacks and input string
  20. Stiva stv;
  21. Stiva Op;
  22. String s = "";
  23.  
  24. // Button to trigger evaluation
  25. const int EVAL = 13;
  26.  
  27. // Function prototypes
  28. void setup();
  29. void loop();
  30. char polling();
  31. void processKeypress(char key);
  32. void displayOnLCD(String message);
  33. void handleEvaluation();
  34.  
  35. void setup() {
  36.   // Initialize stacks
  37.   Init(stv);
  38.   Init(Op);
  39.  
  40.   // Initialize LCD
  41.   lcd.init();
  42.   lcd.clear();
  43.   lcd.backlight();
  44.   lcd.setCursor(0, 0);
  45.  
  46.   // Set EVAL button as INPUT_PULLUP
  47.   pinMode(EVAL, INPUT_PULLUP);
  48.  
  49.   // Set row pins as INPUT_PULLUP
  50.   for (int i = 0; i < rows; i++) {
  51.     pinMode(rPins[i], INPUT_PULLUP);
  52.   }
  53.  
  54.   // Set column pins as OUTPUT and HIGH
  55.   for (int i = 0; i < cols; i++) {
  56.     pinMode(cPins[i], OUTPUT);
  57.     digitalWrite(cPins[i], HIGH);
  58.   }
  59. }
  60.  
  61. void loop() {
  62.   char key = polling();
  63.   if (key != ' ') {
  64.     processKeypress(key);
  65.   }
  66.  
  67.   if (!digitalRead(EVAL)) {
  68.     handleEvaluation();
  69.   }
  70. }
  71.  
  72. char polling() {
  73.   char key = ' ';
  74.   for (int i = 0; i < cols; i++) {
  75.     digitalWrite(cPins[i], LOW);
  76.  
  77.     for (int j = 0; j < rows; j++) {
  78.       if (digitalRead(rPins[j]) == LOW) {
  79.         delay(100);
  80.         if (digitalRead(rPins[j]) == LOW) {
  81.           key = keymap[j][i];
  82.           break;
  83.         }
  84.       }
  85.     }
  86.  
  87.     digitalWrite(cPins[i], HIGH);
  88.  
  89.     if (key != ' ') {
  90.       break;
  91.     }
  92.   }
  93.  
  94.   return key;
  95. }
  96.  
  97. void processKeypress(char key) {
  98.   s.concat(key);
  99.   displayOnLCD(s);
  100. }
  101.  
  102. void handleEvaluation() {
  103.   String postfixForm = postfix(stv, s);
  104.   displayOnLCD("Postfix form:");
  105.   displayOnLCD(postfixForm);
  106.  
  107.   displayOnLCD("Result:");
  108.   s = postfixeval(Op, postfixForm);
  109.   displayOnLCD(s);
  110. }
  111.  
  112. void displayOnLCD(String message) {
  113.   lcd.clear();
  114.   int len = message.length();
  115.  
  116.   if (len <= 32) {
  117.     lcd.print(message);
  118.     Serial.println(message);
  119.    
  120.     if (len >= 16) {
  121.       lcd.setCursor(0, 1);
  122.       lcd.print(message.substring(16));
  123.     }
  124.   }
  125.   else {
  126.     lcd.clear();
  127.     lcd.print("Character limit!");
  128.     delay(400);
  129.     lcd.clear();
  130.     s = s.substring(0, s.length() - 1);
  131.     displayOnLCD(s);
  132.   }
  133.  
  134.   delay(1000);
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement