Advertisement
Merevoli

Untitled

May 18th, 2022
520
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] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  43. byte colPins[COLS] = {A12, A13, A14, A15}; // 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 = String(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.     for (int col=1; col <= COLS; col++){
  70.         for (int row=1; row <= ROWS; row++){
  71.             if (keys[row - 1][col - 1] == key){
  72.                 pos_x = col;
  73.                 pos_y = row;
  74.                 return;
  75.             }
  76.         }
  77.     }
  78.    
  79.     pos_x = 0;
  80.     pos_y = 0;
  81. }
  82.  
  83. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  84.     pos_x = 15 + (pos_x - 1) * 45;
  85.     pos_y = 40 + (pos_y - 1) * 45;
  86.  
  87.     uint16_t button_color;
  88.     if (color == 0) { // green
  89.         button_color = tft.color565(64, 237, 39);
  90.     }
  91.     else { // red
  92.         button_color = tft.color565(238, 38, 91);
  93.     }
  94.     int pos_first_space = find_str(tittle, ' ');
  95.     if (pos_first_space != -1) {
  96.         String word_up = tittle.substring(0, pos_first_space);
  97.         String word_down = tittle.substring(pos_first_space + 1);
  98.  
  99.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  100.         tft.setCursor(pos_x + 5, pos_y + 10);
  101.         tft.setTextColor(tft.color565(0, 0, 0));
  102.         tft.setTextSize(1);
  103.         tft.println(word_up);
  104.  
  105.         tft.setCursor(pos_x + 5, pos_y + 20);
  106.         tft.setTextColor(tft.color565(0, 0, 0));
  107.         tft.setTextSize(1);
  108.         tft.println(word_down);
  109.     }
  110.     else {
  111.         int text_size = 1;
  112.  
  113.         if (tittle == ">" || tittle == "<" || tittle == "v") {
  114.             text_size = 3;
  115.         }
  116.         else if (tittle == "^"){
  117.             text_size = 4;
  118.         }
  119.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  120.         tft.setCursor(pos_x + 5, pos_y + 15);
  121.         tft.setTextColor(tft.color565(0, 0, 0));
  122.         tft.setTextSize(text_size);
  123.         tft.println(tittle);
  124.     }
  125. }
  126.  
  127. void init_screen(){
  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.     tft.drawLine(230, 130, 200, 150, ILI9341_BLACK);
  137.     tft.drawLine(230, 130, 270, 130, ILI9341_BLACK);
  138.     tft.drawLine(270, 130, 300, 100, ILI9341_BLACK);
  139. }
  140.  
  141. void reset_screen(){
  142.     for (int col=1; col <= COLS; col++){
  143.         for (int row=1; row <= ROWS; row++){
  144.             String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  145.             draw_button(tittle, col, row, 0);
  146.         }
  147.     }
  148. }
  149.  
  150. void setup() {
  151.     tft.begin();
  152.     tft.setRotation(3);
  153.     char key = keypad_key.waitForKey();
  154.  
  155.     if (key != NO_KEY){
  156.         if (key == '7'){
  157.             init_screen();
  158.             int pos_x = 0;
  159.             int pos_y = 0;
  160.             get_pos(key, pos_x, pos_y);
  161.             String tittle = get_key_tittle_mapping(key);
  162.             reset_screen();
  163.             draw_button(tittle, pos_x, pos_y, 1);
  164.         }
  165.     }
  166. }
  167.  
  168. void loop() {
  169.     char key = keypad_key.getKey();
  170.  
  171.     if (key != NO_KEY){
  172.         int pos_x = 0;
  173.         int pos_y = 0;
  174.         get_pos(key, pos_x, pos_y);
  175.         String tittle = get_key_tittle_mapping(key);
  176.         reset_screen();
  177.         draw_button(tittle, pos_x, pos_y, 1);
  178.     }
  179. }
Advertisement
RAW Paste Data Copied
Advertisement