Advertisement
Merevoli

Untitled

May 18th, 2022
431
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Keypad.h" // khai bao thư vien keypad
  2. #include "Adafruit_GFX.h"       // khai bao thư vien Adafruit graphics library
  3. #include "Adafruit_ILI9341.h"   // khai bao thư vien Adafruit ILI9341 TFT library
  4. #include <map>
  5. #include <iostream>
  6.  
  7. #define TFT_CS 8 // khai bao chan cs
  8. #define TFT_RST 9 // khai bao chan reset
  9. #define TFT_DC 53 // khai bao chan dc
  10.  
  11. // -------------------------LCD Setup-----------------------------------------
  12. // initialize ILI9341 TFT library
  13. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  14.  
  15. // -------------------------Keypad Setup---------------------------------------
  16. const byte ROWS = 4; // Four Rows
  17. const byte COLS = 4; // Four Columns
  18. char keys[ROWS][COLS] = {
  19.     {'7','8','9','A'},
  20.     {'4','5','6','B'},
  21.     {'1','2','3','C'},
  22.     {'*','0','#','D'}
  23. };
  24.  
  25. map <char, string> key_tittle_mapping = {
  26.     {'7', "START"},
  27.     {'8', "RESET"},
  28.     {'9', "<"},
  29.     {'A', ">"},
  30.     {'4', "BACK RISE"},
  31.     {'5', "BACK DOWN"},
  32.     {'6', "^"},
  33.     {'B', "v"},
  34.     {'1', "SIT UP"},
  35.     {'2', "LEG DOWN"},
  36.     {'3', "LEFT TURN"},
  37.     {'C', "RIGHT TURN"},
  38.     {'*', "BEDPAN ON"},
  39.     {'0', "BEDPAN OFF"},
  40.     {'#', "AUTO TURN A"},
  41.     {'D', "AUTO TURN B"},
  42. }
  43.  
  44. byte rowPins[ROWS] = {2, 3, 4, 5}; // Arduino pins connected to the row pins of the keypad
  45. byte colPins[COLS] = {6, 7, 8, 9}; // Arduino pins connected to the column pins of the keypad
  46. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  47.  
  48. using namespace std;
  49.  
  50. pair <int, int> get_pos(char key) {  // get (x, y)
  51.     for (int col=1; col < COLS; col++){
  52.         for (int row=1; row <= ROWS; row++){
  53.             if (keys[row - 1][col - 1] == key){
  54.                 return {col, row};
  55.             }
  56.         }
  57.     }
  58.     return {0, 0};
  59. }
  60.  
  61. void draw_button(string tittle, int pos_x, int pos_y, int color = 0){
  62.     pos_x = 15 + (pos_x - 1) * 45;
  63.     pos_y = 40 + (pos_y - 1) * 45;
  64.  
  65.     uint16_t button_color;
  66.     if (color == 0) { // green
  67.         button_color = tft.color565(64, 237, 39);
  68.     }
  69.     else { // red
  70.         button_color = tft.color565(238, 38, 91);
  71.     }
  72.     int pos_first_space = tittle.find(" ");
  73.     if (pos_first_space != string::npos) {
  74.         string word_up = tittle.substr(0, pos_first_space);
  75.         string word_down = tittle.substr(pos_first_space + 1);
  76.  
  77.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  78.         tft.setCursor(pos_x + 10, pos_y + 10);
  79.         tft.setTextColor(tft.color565(0, 0, 0));
  80.         tft.setTextSize(1);
  81.         tft.println(word_up);
  82.  
  83.         tft.setCursor(pos_x + 10, pos_y + 20);
  84.         tft.setTextColor(tft.color565(0, 0, 0));
  85.         tft.setTextSize(1);
  86.         tft.println(word_down);
  87.     }
  88.     else {
  89.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  90.         tft.setCursor(pos_x + 5, pos_y + 15);
  91.         tft.setTextColor(tft.color565(0, 0, 0));
  92.         tft.setTextSize(1);
  93.         tft.println(tittle);
  94.     }
  95. }
  96.  
  97. void reset_screen(){
  98.     tft.begin();
  99.     tft.setRotation(3);
  100.     tft.fillScreen(tft.color565(0, 0, 0));
  101.  
  102.     tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  103.     tft.setCursor(20, 20);
  104.     tft.setTextColor(tft.color565(159, 30, 6));
  105.     tft.setTextSize(2);
  106.     tft.println("MAIN CONTROL");
  107.  
  108.     for (int col=1; col < COLS; col++){
  109.         for (int row=1; row <= ROWS; row++){
  110.             string tittle = key_tittle_mapping[keys[row - 1][col - 1]];
  111.             draw_button(tittle, col, row, 0);
  112.         }
  113.     }
  114.  
  115.     tft.drawLine(230, 130, 200, 150, ILI9341_BLACK);
  116.     tft.drawLine(230, 130, 270, 130, ILI9341_BLACK);
  117.     tft.drawLine(270, 130, 300, 100, ILI9341_BLACK);
  118. }
  119.  
  120. void setup() {
  121.     char key = keypad_key.getKey();
  122.  
  123.     if (key != NO_KEY){
  124.         if (key == '7'){
  125.             pair <int, int> pos = get_pos(key);
  126.             string tittle = key_tittle_mapping[key];
  127.             reset_screen()
  128.             draw_button(tittle, pos.first, pos.second, 1);
  129.         }
  130.     }
  131. }
  132.  
  133. void loop() {
  134.     char key = keypad_key.getKey();
  135.  
  136.     if (key != NO_KEY){
  137.         pair <int, int> pos = get_pos(key);
  138.         string tittle = key_tittle_mapping[key];
  139.         reset_screen()
  140.         draw_button(tittle, pos.first, pos.second, 1);
  141.     }
  142. }
Advertisement
RAW Paste Data Copied
Advertisement