Advertisement
Merevoli

Untitled

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