Advertisement
Merevoli

Untitled

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