Advertisement
Merevoli

Untitled

May 18th, 2022
522
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.  
  5. #define TFT_CS 8 // khai bao chan cs
  6. #define TFT_RST 9 // khai bao chan reset
  7. #define TFT_DC 53 // khai bao chan dc
  8.  
  9. // -------------------------LCD Setup-----------------------------------------
  10. // initialize ILI9341 TFT library
  11. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  12.  
  13. // -------------------------Keypad Setup---------------------------------------
  14. const byte ROWS = 4; // Four Rows
  15. const byte COLS = 4; // Four Columns
  16. char keys[ROWS][COLS] = {
  17.     {'7','8','9','A'},
  18.     {'4','5','6','B'},
  19.     {'1','2','3','C'},
  20.     {'*','0','#','D'}
  21. };
  22.  
  23. String key_tittle_mapping[16][2] = {
  24.     {"7", "START"},
  25.     {"8", "RESET"},
  26.     {"9", "<"},
  27.     {"A", ">"},
  28.     {"4", "BACK RISE"},
  29.     {"5", "BACK DOWN"},
  30.     {"6", "^"},
  31.     {"B", "v"},
  32.     {"1", "SIT UP"},
  33.     {"2", "LEG DOWN"},
  34.     {"3", "LEFT TURN"},
  35.     {"C", "RIGHT TURN"},
  36.     {"*", "BEDPAN ON"},
  37.     {"0", "BEDPAN OFF"},
  38.     {"#", "AUTO TURN A"},
  39.     {"D", "AUTO TURN B"},
  40. };
  41.  
  42. byte rowPins[ROWS] = {2, 3, 4, 5}; // Arduino pins connected to the row pins of the keypad
  43. byte colPins[COLS] = {6, 7, 8, 9}; // Arduino pins connected to the column pins of the keypad
  44. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  45.  
  46. using namespace std;
  47.  
  48. int find_str(String str, char find_chr){
  49.     for (int pos=0; pos<str.length(); pos++){
  50.         if (str[pos] == find_chr){
  51.             return pos;
  52.         }
  53.     }
  54.     return -1;
  55. }
  56.  
  57. String get_key_tittle_mapping(char key){
  58.     String str_key(1, key);
  59.  
  60.     for (int i=0; i<ROWS * COLS; i++){
  61.         if (key_tittle_mapping[i][0] == str_key){
  62.             return key_tittle_mapping[i][1];
  63.         }
  64.     }
  65.     return "";
  66. }
  67.  
  68. void get_pos(char key, int &pos_x, int &pos_y) {  // get (x, y)
  69.     bool is_found = false;
  70.     for (int col=1; col < COLS; col++){
  71.         for (int row=1; row <= ROWS; row++){
  72.             if (keys[row - 1][col - 1] == key){
  73.                 return {col, row};
  74.                 pos_x = col;
  75.                 pos_y = row;
  76.                 is_found = true;
  77.                 break;
  78.             }
  79.         }
  80.         if (is_found){
  81.             break;
  82.         }
  83.     }
  84.    
  85.     pos_x = 0;
  86.     pos_y = 0;
  87. }
  88.  
  89. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  90.     pos_x = 15 + (pos_x - 1) * 45;
  91.     pos_y = 40 + (pos_y - 1) * 45;
  92.  
  93.     uint16_t button_color;
  94.     if (color == 0) { // green
  95.         button_color = tft.color565(64, 237, 39);
  96.     }
  97.     else { // red
  98.         button_color = tft.color565(238, 38, 91);
  99.     }
  100.     int pos_first_space = find_str(tittle, ' ');
  101.     if (pos_first_space != -1) {
  102.         String word_up = tittle.substring(0, pos_first_space);
  103.         String word_down = tittle.substring(pos_first_space + 1);
  104.  
  105.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  106.         tft.setCursor(pos_x + 10, pos_y + 10);
  107.         tft.setTextColor(tft.color565(0, 0, 0));
  108.         tft.setTextSize(1);
  109.         tft.println(word_up);
  110.  
  111.         tft.setCursor(pos_x + 10, pos_y + 20);
  112.         tft.setTextColor(tft.color565(0, 0, 0));
  113.         tft.setTextSize(1);
  114.         tft.println(word_down);
  115.     }
  116.     else {
  117.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  118.         tft.setCursor(pos_x + 5, pos_y + 15);
  119.         tft.setTextColor(tft.color565(0, 0, 0));
  120.         tft.setTextSize(1);
  121.         tft.println(tittle);
  122.     }
  123. }
  124.  
  125. void reset_screen(){
  126.     tft.begin();
  127.     tft.setRotation(3);
  128.     tft.fillScreen(tft.color565(0, 0, 0));
  129.  
  130.     tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  131.     tft.setCursor(20, 20);
  132.     tft.setTextColor(tft.color565(159, 30, 6));
  133.     tft.setTextSize(2);
  134.     tft.println("MAIN CONTROL");
  135.  
  136.     for (int col=1; col < COLS; col++){
  137.         for (int row=1; row <= ROWS; row++){
  138.             String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  139.             draw_button(tittle, col, row, 0);
  140.         }
  141.     }
  142.  
  143.     tft.drawLine(230, 130, 200, 150, ILI9341_BLACK);
  144.     tft.drawLine(230, 130, 270, 130, ILI9341_BLACK);
  145.     tft.drawLine(270, 130, 300, 100, ILI9341_BLACK);
  146. }
  147.  
  148. void setup() {
  149.     char key = keypad_key.getKey();
  150.  
  151.     if (key != NO_KEY){
  152.         if (key == '7'){
  153.             int pos_x = 0;
  154.             int pos_y = 0;
  155.             get_pos(key, pos_x, pos_y);
  156.             String tittle = get_key_tittle_mapping(key);
  157.             reset_screen()
  158.             draw_button(tittle, pos_x, pos_y, 1);
  159.         }
  160.     }
  161. }
  162.  
  163. void loop() {
  164.     char key = keypad_key.getKey();
  165.  
  166.     if (key != NO_KEY){
  167.         int pos_x = 0;
  168.         int pos_y = 0;
  169.         get_pos(key, pos_x, pos_y);
  170.         String tittle = get_key_tittle_mapping(key);
  171.         reset_screen()
  172.         draw_button(tittle, pos_x, pos_y, 1);
  173.     }
  174. }
Advertisement
RAW Paste Data Copied
Advertisement