Advertisement
Merevoli

Untitled

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