Advertisement
Merevoli

Untitled

May 20th, 2022
536
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 <ArduinoSTL.h>
  3. #include "Adafruit_GFX.h"       // khai bao thư vien Adafruit graphics library
  4. #include "Adafruit_ILI9341.h"   // khai bao thư vien Adafruit ILI9341 TFT library
  5. #include <Servo.h>
  6. #include <vector>
  7. #include <map>
  8. #define TFT_CS 8 // khai bao chan cs
  9. #define TFT_RST 9 // khai bao chan reset
  10. #define TFT_DC 53 // khai bao chan dc
  11.  
  12. // -------------------------LCD Setup-----------------------------------------
  13. // initialize ILI9341 TFT library
  14. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  15.  
  16. // -------------------------Keypad Setup---------------------------------------
  17. const byte ROWS = 4; // Four Rows
  18. const byte COLS = 4; // Four Columns
  19. char keys[ROWS][COLS] = {
  20.     {'7','8','9','A'},
  21.     {'4','5','6','B'},
  22.     {'1','2','3','C'},
  23.     {'*','0','#','D'}
  24. };
  25. const int MOTOR_PIN_SIZE = 12;
  26. const int MOTOR_PINS[13] = {-1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
  27.  
  28. Servo myservo;
  29. int degree = 0;
  30.  
  31. String key_tittle_mapping[16][2] = {
  32.     {"7", "START"},
  33.     {"8", "RESET"},
  34.     {"9", "<"},
  35.     {"A", ">"},
  36.     {"4", "BACK RISE"},
  37.     {"5", "BACK DOWN"},
  38.     {"6", "^"},
  39.     {"B", "v"},
  40.     {"1", "SIT UP"},
  41.     {"2", "LEG DOWN"},
  42.     {"3", "LEFT TURN"},
  43.     {"C", "RIGHT TURN"},
  44.     {"*", "BEDPAN ON"},
  45.     {"0", "BEDPAN OFF"},
  46.     {"#", "AUTO TURN A"},
  47.     {"D", "AUTO TURN B"},
  48. };
  49.  
  50. std::map <char, std::vector <std::pair <std::string, std::vector <int>>>> motor_movement_mapping = {
  51.     {'8', {
  52.         {"pin", {LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH}},
  53.         {"delay", {3000}},
  54.         {"pin_reset_high", {}}
  55.     }},
  56.     {'9', {
  57.         {"pin_reset_high", {}},
  58.         {"degree_add", {10}}
  59.     }},
  60.     {'A', {
  61.         {"pin_reset_high", {}},
  62.         {"degree", {150}},
  63.         {"delay", {15}}
  64.     }},
  65.     {'4', {
  66.         {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  67.     }},
  68.     {'5', {
  69.         {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  70.     }},
  71.     {'6', {
  72.         {"pin", {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH}},
  73.         {"delay", {250}},
  74.         {"pin_reset_high", {}}
  75.     }},
  76.     {'B', {
  77.         {"pin", {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}},
  78.         {"delay", {250}},
  79.         {"pin_reset_high", {}}
  80.     }},
  81.     {'1', {
  82.         {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  83.     }},
  84.     {'2', {
  85.         {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  86.     }},
  87.     {'3', {
  88.         {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  89.     }},
  90.     {'C', {
  91.         {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  92.     }},
  93.     {'*', {
  94.         {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  95.     }},
  96.     {'0', {
  97.         {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  98.     }},
  99.     {'#', {
  100.         {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  101.     }},
  102.     {'D', {
  103.         {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  104.     }},
  105. };
  106.  
  107. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  108. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  109. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  110.  
  111. int find_str(String str, char find_chr){
  112.     for (int pos=0; pos<str.length(); pos++){
  113.         if (str[pos] == find_chr){
  114.             return pos;
  115.         }
  116.     }
  117.     return -1;
  118. }
  119.  
  120. String get_key_tittle_mapping(char key){
  121.     String str_key = String(key);
  122.  
  123.     for (int i=0; i<ROWS * COLS; i++){
  124.         if (key_tittle_mapping[i][0] == str_key){
  125.             return key_tittle_mapping[i][1];
  126.         }
  127.     }
  128.     return "";
  129. }
  130.  
  131. void get_pos(char key, int &pos_x, int &pos_y) {  // get (x, y)
  132.     for (int col=1; col <= COLS; col++){
  133.         for (int row=1; row <= ROWS; row++){
  134.             if (keys[row - 1][col - 1] == key){
  135.                 pos_x = col;
  136.                 pos_y = row;
  137.                 return;
  138.             }
  139.         }
  140.     }
  141.    
  142.     pos_x = 0;
  143.     pos_y = 0;
  144. }
  145.  
  146. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  147.     pos_x = 15 + (pos_x - 1) * 45;
  148.     pos_y = 40 + (pos_y - 1) * 45;
  149.  
  150.     uint16_t button_color;
  151.     if (color == 0) { // green
  152.         button_color = tft.color565(64, 237, 39);
  153.     }
  154.     else { // red
  155.         button_color = tft.color565(238, 38, 91);
  156.     }
  157.     int pos_first_space = find_str(tittle, ' ');
  158.     if (pos_first_space != -1) {
  159.         String word_up = tittle.substring(0, pos_first_space);
  160.         String word_down = tittle.substring(pos_first_space + 1);
  161.  
  162.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  163.         tft.setCursor(pos_x + 3, pos_y + 10);
  164.         tft.setTextColor(tft.color565(0, 0, 0));
  165.         tft.setTextSize(1);
  166.         tft.println(word_up);
  167.  
  168.         tft.setCursor(pos_x + 3, pos_y + 20);
  169.         tft.setTextColor(tft.color565(0, 0, 0));
  170.         tft.setTextSize(1);
  171.         tft.println(word_down);
  172.     }
  173.     else {
  174.         int text_size = 1;
  175.  
  176.         if (tittle == ">" || tittle == "<" || tittle == "v") {
  177.             text_size = 3;
  178.         }
  179.         else if (tittle == "^"){
  180.             text_size = 4;
  181.         }
  182.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  183.         tft.setCursor(pos_x + 5, pos_y + 15);
  184.         tft.setTextColor(tft.color565(0, 0, 0));
  185.         tft.setTextSize(text_size);
  186.         tft.println(tittle);
  187.     }
  188. }
  189.  
  190. void poweron_screen() {
  191.     tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
  192.     tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
  193.     tft.setCursor(90, 90);
  194.     tft.setTextColor(tft.color565(255, 255, 255));
  195.     tft.setTextSize(8);
  196.     tft.println("M2D ");
  197. }
  198.  
  199. void startup_screen() {
  200.     tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
  201.     tft.setCursor(50, 80);
  202.     tft.setTextColor(tft.color565(0, 164, 164));
  203.     tft.setTextSize(3);
  204.     tft.println("GIUONG HO TRO ");
  205.     tft.setCursor(40, 110);
  206.     tft.setTextColor(tft.color565(243, 87, 35));
  207.     tft.setTextSize(3);
  208.     tft.println("NGUOI TAI BIEN");
  209. }
  210.  
  211. void init_screen(){
  212.     tft.fillScreen(tft.color565(0, 0, 0));
  213.  
  214.     tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  215.     tft.setCursor(20, 20);
  216.     tft.setTextColor(tft.color565(159, 30, 6));
  217.     tft.setTextSize(2);
  218.     tft.println("MAIN CONTROL");
  219.    
  220.     tft.drawLine(230, 130, 200, 150, ILI9341_WHITE);
  221.     tft.drawLine(230, 130, 270, 130, ILI9341_WHITE);
  222.     tft.drawLine(270, 130, 300, 100, ILI9341_WHITE);
  223. }
  224.  
  225. void reset_screen(){
  226.     for (int col=1; col <= COLS; col++){
  227.         for (int row=1; row <= ROWS; row++){
  228.             String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  229.             draw_button(tittle, col, row, 0);
  230.         }
  231.     }
  232. }
  233.  
  234. void handle_motor(char key){
  235.     std::vector <std::pair <std::string, std::vector <int>>> motor_steps = motor_movement_mapping[key];
  236.  
  237.     for (std::pair <std::string, std::vector <int>> step : motor_steps) {
  238.         if (step.first == "pin") {
  239.             for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  240.                 int signal = step.second[pin_no - 1];
  241.  
  242.                 if (signal != -1){
  243.                     digitalWrite(MOTOR_PINS[pin_no], signal);
  244.                 }
  245.             }
  246.         }
  247.         else if (step.first == "pin_reset_high") {
  248.             for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  249.                 digitalWrite(MOTOR_PINS[pin_no], HIGH);
  250.             }
  251.         }
  252.         else if (step.first == "delay") {
  253.             int delay_time = step.second[0];
  254.             delay(delay_time);
  255.         }
  256.         else if (step.first == "degree_add") {
  257.             int degree_add_value = step.second[0];
  258.             degree += degree_add_value;
  259.         }
  260.         else if (step.first == "degree") {
  261.             int degree_new_value = step.second[0];
  262.             degree = degree_new_value;
  263.         }
  264.     }
  265. }
  266.  
  267. void make_red_start_button(){
  268.     char key = '7';
  269.     int pos_x = 0;
  270.     int pos_y = 0;
  271.     get_pos(key, pos_x, pos_y);
  272.     String tittle = get_key_tittle_mapping(key);
  273.     draw_button(tittle, pos_x, pos_y, 1);
  274. }
  275.  
  276. void setup() {
  277.     tft.begin();
  278.     tft.setRotation(3);
  279.     bool is_init_screen = false;
  280.     poweron_screen();
  281.  
  282.     while (!is_init_screen){
  283.         char key = keypad_key.waitForKey();
  284.         if (key != NO_KEY){
  285.             if (key == '7'){
  286.                 startup_screen();
  287.                 delay(1000);
  288.                 init_screen();
  289.                 is_init_screen = true;
  290.                 int pos_x = 0;
  291.                 int pos_y = 0;
  292.                 get_pos(key, pos_x, pos_y);
  293.                 String tittle = get_key_tittle_mapping(key);
  294.                 reset_screen();
  295.                 draw_button(tittle, pos_x, pos_y, 1);
  296.             }
  297.         }
  298.        
  299.         for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  300.             pinMode(MOTOR_PINS[pin_no], OUTPUT);
  301.         }
  302.        
  303.         myservo.attach(11, 500, 2500);
  304.     }
  305. }
  306.  
  307. void loop() {
  308.     char key = keypad_key.waitForKey();
  309.  
  310.     if (key != NO_KEY){
  311.         int pos_x = 0;
  312.         int pos_y = 0;
  313.         get_pos(key, pos_x, pos_y);
  314.         String tittle = get_key_tittle_mapping(key);
  315.         reset_screen();
  316.         make_red_start_button();
  317.         draw_button(tittle, pos_x, pos_y, 1);
  318.  
  319.         myservo.write(degree);
  320.         handle_motor(key);
  321.     }
  322. }
Advertisement
RAW Paste Data Copied
Advertisement