Advertisement
Merevoli

Untitled

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