Advertisement
Merevoli

Untitled

May 21st, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.33 KB | None | 0 0
  1. #include "Keypad.h" // khai bao thư vien keypad
  2. #include "Servo.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 "Thread.h"
  6.  
  7. #define TFT_CS 8 // khai bao chan cs
  8. #define TFT_RST 9 // khai bao chan reset
  9. #define TFT_DC 53 // khai bao chan dc
  10.  
  11. // -------------------------LCD Setup-----------------------------------------
  12. // initialize ILI9341 TFT library
  13. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  14.  
  15. // -------------------------Keypad Setup---------------------------------------
  16. const byte ROWS = 4; // Four Rows
  17. const byte COLS = 4; // Four Columns
  18. char keys[ROWS][COLS] = {
  19.     {'7','8','9','A'},
  20.     {'4','5','6','B'},
  21.     {'1','2','3','C'},
  22.     {'*','0','#','D'}
  23. };
  24. const int MOTOR_PIN_SIZE = 12;
  25. const int MOTOR_PINS[13] = {-1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33};
  26. int MOTOR_PIN_STATUS[13] = {-1, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
  27.  
  28. Servo myservo;
  29. int degree = 30;
  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. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  51. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  52. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  53. Thread bed_animation = Thread();
  54. int DOWN_MOVES[8][2] = {
  55.     {194, 80},
  56.     {194, 83},
  57.     {194, 86},
  58.     {195, 89},
  59.     {196, 92},
  60.     {197, 95},
  61.     {199, 98},
  62.     {200, 100}
  63. };
  64.  
  65. int TOP_MOVES[8][2] = {
  66.     {306, 80},
  67.     {306, 77},
  68.     {306, 73},
  69.     {305, 70},
  70.     {304, 68},
  71.     {303, 66},
  72.     {302, 63},
  73.     {300, 60}
  74. };
  75.  
  76. int current_top_pos = 0;
  77. int current_down_pos = 0;
  78.  
  79.  
  80. int find_str(String str, char find_chr){
  81.     for (int pos=0; pos<str.length(); pos++){
  82.         if (str[pos] == find_chr){
  83.             return pos;
  84.         }
  85.     }
  86.     return -1;
  87. }
  88.  
  89. String get_key_tittle_mapping(char key){
  90.     String str_key = String(key);
  91.  
  92.     for (int i=0; i<ROWS * COLS; i++){
  93.         if (key_tittle_mapping[i][0] == str_key){
  94.             return key_tittle_mapping[i][1];
  95.         }
  96.     }
  97.     return "";
  98. }
  99.  
  100. void get_pos(char key, int &pos_x, int &pos_y) {  // get (x, y)
  101.     for (int col=1; col <= COLS; col++){
  102.         for (int row=1; row <= ROWS; row++){
  103.             if (keys[row - 1][col - 1] == key){
  104.                 pos_x = col;
  105.                 pos_y = row;
  106.                 return;
  107.             }
  108.         }
  109.     }
  110.    
  111.     pos_x = 0;
  112.     pos_y = 0;
  113. }
  114.  
  115. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  116.     pos_x = 15 + (pos_x - 1) * 45;
  117.     pos_y = 40 + (pos_y - 1) * 45;
  118.  
  119.     uint16_t button_color;
  120.     if (color == 0) { // green
  121.         button_color = tft.color565(64, 237, 39);
  122.     }
  123.     else { // red
  124.         button_color = tft.color565(238, 38, 91);
  125.     }
  126.     int pos_first_space = find_str(tittle, ' ');
  127.     if (pos_first_space != -1) {
  128.         String word_up = tittle.substring(0, pos_first_space);
  129.         String word_down = tittle.substring(pos_first_space + 1);
  130.  
  131.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  132.         tft.setCursor(pos_x + 3, pos_y + 10);
  133.         tft.setTextColor(tft.color565(0, 0, 0));
  134.         tft.setTextSize(1);
  135.         tft.println(word_up);
  136.  
  137.         tft.setCursor(pos_x + 3, pos_y + 20);
  138.         tft.setTextColor(tft.color565(0, 0, 0));
  139.         tft.setTextSize(1);
  140.         tft.println(word_down);
  141.     }
  142.     else {
  143.         int text_size = 1;
  144.  
  145.         if (tittle == ">" || tittle == "<" || tittle == "v") {
  146.             text_size = 3;
  147.         }
  148.         else if (tittle == "^"){
  149.             text_size = 4;
  150.         }
  151.         tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  152.         tft.setCursor(pos_x + 5, pos_y + 15);
  153.         tft.setTextColor(tft.color565(0, 0, 0));
  154.         tft.setTextSize(text_size);
  155.         tft.println(tittle);
  156.     }
  157. }
  158.  
  159. void poweron_screen() {
  160.     tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
  161.     tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
  162.     tft.setCursor(90, 90);
  163.     tft.setTextColor(tft.color565(255, 255, 255));
  164.     tft.setTextSize(8);
  165.     tft.println("M2D ");
  166. }
  167.  
  168. void startup_screen() {
  169.     tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
  170.     tft.setCursor(50, 80);
  171.     tft.setTextColor(tft.color565(0, 164, 164));
  172.     tft.setTextSize(3);
  173.     tft.println("GIUONG HO TRO ");
  174.     tft.setCursor(40, 110);
  175.     tft.setTextColor(tft.color565(243, 87, 35));
  176.     tft.setTextSize(3);
  177.     tft.println("NGUOI TAI BIEN");
  178. }
  179.  
  180. void init_screen(){
  181.     tft.fillScreen(tft.color565(0, 0, 0));
  182.  
  183.     tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  184.     tft.setCursor(20, 20);
  185.     tft.setTextColor(tft.color565(159, 30, 6));
  186.     tft.setTextSize(2);
  187.     tft.println("MAIN CONTROL");
  188.    
  189.     tft.drawLine(230, 80, 194, 80, ILI9341_WHITE);
  190.     tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
  191.     tft.drawLine(270, 80, 106, 80, ILI9341_WHITE);
  192. }
  193.  
  194. void reset_screen(){
  195.     for (int col=1; col <= COLS; col++){
  196.         for (int row=1; row <= ROWS; row++){
  197.             String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  198.             draw_button(tittle, col, row, 0);
  199.         }
  200.     }
  201. }
  202.  
  203. void handle_motor_step(
  204.     String type,
  205.     int data_1 = 0,
  206.     int data_2 = 0,
  207.     int data_3 = 0,
  208.     int data_4 = 0,
  209.     int data_5 = 0,
  210.     int data_6 = 0,
  211.     int data_7 = 0,
  212.     int data_8 = 0,
  213.     int data_9 = 0,
  214.     int data_10 = 0,
  215.     int data_11 = 0,
  216.     int data_12 = 0
  217. ){
  218.     if (type == "pin") {
  219.         int signal[12] = {data_1, data_2, data_3, data_4, data_5, data_6, data_7, data_8, data_9, data_10, data_11, data_12};
  220.  
  221.         for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  222.             if (signal[pin_no - 1] != -1){
  223.                 digitalWrite(MOTOR_PINS[pin_no], signal[pin_no - 1]);
  224.                 MOTOR_PIN_STATUS[pin_no] = signal[pin_no - 1];
  225.             }
  226.         }
  227.     }
  228.     else if (type == "pin_reset_high") {
  229.         for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  230.             digitalWrite(MOTOR_PINS[pin_no], HIGH);
  231.             MOTOR_PIN_STATUS[pin_no] = HIGH;
  232.         }
  233.     }
  234.     else if (type == "delay") {
  235.         int delay_time = data_1*1000;
  236.         delay(delay_time);
  237.     }
  238.     else if (type == "degree_add") {
  239.         int degree_add_value = data_1;
  240.         degree += degree_add_value;
  241.     }
  242.     else if (type == "degree") {
  243.         int degree_new_value = data_1;
  244.         degree = degree_new_value;
  245.     }
  246. }
  247.  
  248.  
  249. void handle_motor(char key) {
  250.     switch (key)
  251.     {
  252.     case '8': // RESET
  253.         handle_motor_step("pin", LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH);
  254.         handle_motor_step("delay", 4);
  255.         handle_motor_step("pin_reset_high");
  256.         break;
  257.    
  258.     case '9': // <
  259.         handle_motor_step("degree", min(60, degree + 10));
  260.         break;
  261.    
  262.     case 'A': // >
  263.         handle_motor_step("degree", max(0, degree - 10));
  264.         break;
  265.  
  266.     case '6': // ^
  267.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW);
  268.         handle_motor_step("delay", 3);
  269.         handle_motor_step("pin_reset_high");
  270.         break;
  271.  
  272.     case 'B': // v
  273.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH);
  274.         handle_motor_step("delay", 3);
  275.         handle_motor_step("pin_reset_high");
  276.         break;
  277.  
  278.     case '4': // BACK RISE
  279.         handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  280.         handle_motor_step("delay", 4);
  281.         handle_motor_step("pin_reset_high");
  282.         break;
  283.  
  284.     case '5': // BACK DOWN
  285.         handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  286.         handle_motor_step("delay", 4);
  287.         handle_motor_step("pin_reset_high");
  288.         break;
  289.  
  290.     case '1': // SIT UP
  291.         handle_motor_step("pin", HIGH, LOW, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  292.         handle_motor_step("delay", 4);
  293.         handle_motor_step("pin_reset_high");
  294.         break;
  295.  
  296.     case '2': // LEG DOWN
  297.         handle_motor_step("pin", -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1);
  298.         handle_motor_step("delay", 4);
  299.         handle_motor_step("pin_reset_high");
  300.         break;
  301.  
  302.     case 'C': // RIGHT TURN
  303.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  304.         handle_motor_step("delay", 4);
  305.         handle_motor_step("pin_reset_high");
  306.         break;
  307.  
  308.     case '3': // LEFT TURN
  309.         handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  310.         handle_motor_step("delay", 4);
  311.         handle_motor_step("pin_reset_high");
  312.         break;
  313.     case '#': // AUTO TURN A
  314.         handle_motor_step("pin", -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1, -1, -1);
  315.         handle_motor_step("delay", 4);
  316.         handle_motor_step("pin_reset_high");
  317.         handle_motor_step("delay", 5);
  318.         handle_motor_step("pin", -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1, -1, -1);
  319.         handle_motor_step("delay", 4);
  320.         handle_motor_step("pin_reset_high");
  321.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1, -1, -1);
  322.         handle_motor_step("delay", 4);
  323.         handle_motor_step("pin_reset_high");
  324.         handle_motor_step("delay", 5);
  325.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1, -1, -1);
  326.         handle_motor_step("delay", 4);
  327.         handle_motor_step("pin_reset_high");
  328.         break;
  329.     case 'D': // AUTO TURN B
  330.         handle_motor_step("pin", HIGH, LOW, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  331.         handle_motor_step("delay", 4);
  332.         handle_motor_step("pin_reset_high");
  333.         handle_motor_step("delay", 5);
  334.         handle_motor_step("pin", LOW, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  335.         handle_motor_step("delay", 4);
  336.         handle_motor_step("pin_reset_high");
  337.         handle_motor_step("delay", 5);
  338.         handle_motor_step("pin_reset_high");
  339.         break;
  340.     case '*': // BEDPAN ON
  341.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, HIGH, LOW, -1, -1);
  342.         handle_motor_step("delay", 4);
  343.         handle_motor_step("pin_reset_high");
  344.         break;
  345.     case '0': // BEDPAN OFF
  346.         handle_motor_step("pin", -1, -1, -1, -1, -1, -1, -1, -1, LOW, HIGH, -1, -1);
  347.         handle_motor_step("delay", 4);
  348.         handle_motor_step("pin_reset_high");
  349.         break;
  350.     default:
  351.         break;
  352.     }
  353. }
  354.  
  355. void make_red_start_button(){
  356.     char key = '7';
  357.     int pos_x = 0;
  358.     int pos_y = 0;
  359.     get_pos(key, pos_x, pos_y);
  360.     String tittle = get_key_tittle_mapping(key);
  361.     draw_button(tittle, pos_x, pos_y, 1);
  362. }
  363.  
  364. void handle_bed_animation(){
  365.     tft.fillRect(194, 10, 300, 220, tft.color565(116, 139, 248));
  366.  
  367.     if (MOTOR_PIN_STATUS[1] != MOTOR_PIN_STATUS[2]){
  368.         if (MOTOR_PIN_STATUS[1] = LOW){
  369.             current_top_pos = max(0, current_top_pos - 1);
  370.         }
  371.         else{
  372.             current_top_pos = min(7, current_top_pos + 1);
  373.         }
  374.     }
  375.  
  376.     if (MOTOR_PIN_STATUS[3] != MOTOR_PIN_STATUS[4]){
  377.         if (MOTOR_PIN_STATUS[3] = LOW){
  378.             current_down_pos = max(0, current_down_pos - 1);
  379.         }
  380.         else{
  381.             current_down_pos = min(7, current_down_pos + 1);
  382.         }
  383.     }
  384.  
  385.     tft.drawLine(230, 80, DOWN_MOVES[current_down_pos][0], DOWN_MOVES[current_down_pos][1], ILI9341_WHITE);
  386.     tft.drawLine(230, 80, 270, 80, ILI9341_WHITE);
  387.     tft.drawLine(270, 80, TOP_MOVES[current_top_pos][0], TOP_MOVES[current_top_pos][1], ILI9341_WHITE);
  388. }
  389.  
  390. void setup() {
  391.     bed_animation.setInterval(200);
  392.     bed_animation.onRun(handle_bed_animation);
  393.  
  394.     if (bed_animation.shouldRun()){
  395.         bed_animation.run();
  396.     }
  397.  
  398.     for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  399.         pinMode(MOTOR_PINS[pin_no], OUTPUT);
  400.     }
  401.  
  402.     tft.begin();
  403.     tft.setRotation(3);
  404.     bool is_init_screen = false;
  405.     poweron_screen();
  406.  
  407.     while (!is_init_screen){
  408.         char key = keypad_key.waitForKey();
  409.         if (key != NO_KEY){
  410.             if (key == '7'){
  411.                 startup_screen();
  412.                 delay(1000);
  413.                 init_screen();
  414.                 is_init_screen = true;
  415.                 int pos_x = 0;
  416.                 int pos_y = 0;
  417.                 get_pos(key, pos_x, pos_y);
  418.                 String tittle = get_key_tittle_mapping(key);
  419.                 reset_screen();
  420.                 draw_button(tittle, pos_x, pos_y, 1);
  421.             }
  422.         }
  423.        
  424.         myservo.attach(11, 500, 2500);
  425.     }
  426. }
  427.  
  428. void loop() {
  429.     char key = keypad_key.getKey();
  430.  
  431.     if (key != NO_KEY){
  432.         int pos_x = 0;
  433.         int pos_y = 0;
  434.         get_pos(key, pos_x, pos_y);
  435.         String tittle = get_key_tittle_mapping(key);
  436.         reset_screen();
  437.         make_red_start_button();
  438.         draw_button(tittle, pos_x, pos_y, 1);
  439.         handle_motor(key);
  440.     } myservo.write(degree);
  441.  
  442. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement