Advertisement
Merevoli

Untitled

May 18th, 2022
27
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 "Adafruit_GFX.h" // khai bao thư vien Adafruit graphics library
  3. #include "Adafruit_ILI9341.h" // khai bao thư vien Adafruit ILI9341 TFT library
  4.  
  5. #define TFT_CS 8 // khai bao chan cs
  6. #define TFT_RST 9 // khai bao chan reset
  7. #define TFT_DC 53 // khai bao chan dc
  8.  
  9. // -------------------------LCD Setup-----------------------------------------
  10. // initialize ILI9341 TFT library
  11. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
  12.  
  13. // -------------------------Keypad Setup---------------------------------------
  14. const byte ROWS = 4; // Four Rows
  15. const byte COLS = 4; // Four Columns
  16. char keys[ROWS][COLS] = {
  17. {'7','8','9','A'},
  18. {'4','5','6','B'},
  19. {'1','2','3','C'},
  20. {'*','0','#','D'}
  21. };
  22.  
  23. String key_tittle_mapping[16][2] = {
  24. {"7", "START"},
  25. {"8", "RESET"},
  26. {"9", "<"},
  27. {"A", ">"},
  28. {"4", "BACK RISE"},
  29. {"5", "BACK DOWN"},
  30. {"6", "^"},
  31. {"B", "v"},
  32. {"1", "SIT UP"},
  33. {"2", "LEG DOWN"},
  34. {"3", "LEFT TURN"},
  35. {"C", "RIGHT TURN"},
  36. {"*", "BEDPAN ON"},
  37. {"0", "BEDPAN OFF"},
  38. {"#", "AUTO TURN A"},
  39. {"D", "AUTO TURN B"},
  40. };
  41.  
  42. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  43. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  44. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  45.  
  46. using namespace std;
  47.  
  48. int find_str(String str, char find_chr){
  49. for (int pos=0; pos<str.length(); pos++){
  50. if (str[pos] == find_chr){
  51. return pos;
  52. }
  53. }
  54. return -1;
  55. }
  56.  
  57. String get_key_tittle_mapping(char key){
  58. String str_key = String(key);
  59.  
  60. for (int i=0; i<ROWS * COLS; i++){
  61. if (key_tittle_mapping[i][0] == str_key){
  62. return key_tittle_mapping[i][1];
  63. }
  64. }
  65. return "";
  66. }
  67.  
  68. void get_pos(char key, int &pos_x, int &pos_y) { // get (x, y)
  69. bool is_found = false;
  70. for (int col=1; col <= COLS; col++){
  71. for (int row=1; row <= ROWS; row++){
  72. if (keys[row - 1][col - 1] == key){
  73. pos_x = col;
  74. pos_y = row;
  75. is_found = true;
  76. break;
  77. }
  78. }
  79. if (is_found){
  80. break;
  81. }
  82. }
  83.  
  84. pos_x = 0;
  85. pos_y = 0;
  86. }
  87.  
  88. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  89. pos_x = 15 + (pos_x - 1) * 45;
  90. pos_y = 40 + (pos_y - 1) * 45;
  91.  
  92. uint16_t button_color;
  93. if (color == 0) { // green
  94. button_color = tft.color565(64, 237, 39);
  95. }
  96. else { // red
  97. button_color = tft.color565(238, 38, 91);
  98. }
  99. int pos_first_space = find_str(tittle, ' ');
  100. if (pos_first_space != -1) {
  101. String word_up = tittle.substring(0, pos_first_space);
  102. String word_down = tittle.substring(pos_first_space + 1);
  103.  
  104. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  105. tft.setCursor(pos_x + 5, pos_y + 10);
  106. tft.setTextColor(tft.color565(0, 0, 0));
  107. tft.setTextSize(1);
  108. tft.println(word_up);
  109.  
  110. tft.setCursor(pos_x + 5, pos_y + 20);
  111. tft.setTextColor(tft.color565(0, 0, 0));
  112. tft.setTextSize(1);
  113. tft.println(word_down);
  114. }
  115. else {
  116. int text_size = 1;
  117.  
  118. if (tittle == ">" || tittle == "<" || tittle == "v") {
  119. text_size = 3;
  120. }
  121. else if (tittle == "^"){
  122. text_size = 4;
  123. }
  124. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  125. tft.setCursor(pos_x + 5, pos_y + 15);
  126. tft.setTextColor(tft.color565(0, 0, 0));
  127. tft.setTextSize(text_size);
  128. tft.println(tittle);
  129. }
  130. }
  131.  
  132. void reset_screen(){
  133. tft.fillScreen(tft.color565(0, 0, 0));
  134.  
  135. tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  136. tft.setCursor(20, 20);
  137. tft.setTextColor(tft.color565(159, 30, 6));
  138. tft.setTextSize(2);
  139.  
  140. tft.println("MAIN CONTROL");
  141.  
  142. for (int col=1; col <= COLS; col++){
  143. for (int row=1; row <= ROWS; row++){
  144. String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  145. draw_button(tittle, col, row, 0);
  146. }
  147. }
  148.  
  149. tft.drawLine(230, 130, 200, 150, ILI9341_BLACK);
  150. tft.drawLine(230, 130, 270, 130, ILI9341_BLACK);
  151. tft.drawLine(270, 130, 300, 100, ILI9341_BLACK);
  152. }
  153.  
  154. void setup() {
  155. tft.begin();
  156. tft.setRotation(3);
  157. char key = keypad_key.getKey();
  158.  
  159. if (key != NO_KEY){
  160. if (key == '7'){
  161. int pos_x = 0;
  162. int pos_y = 0;
  163. get_pos(key, pos_x, pos_y);
  164. String tittle = get_key_tittle_mapping(key);
  165. reset_screen();
  166. draw_button(tittle, pos_x, pos_y, 1);
  167. }
  168. }
  169. }
  170.  
  171. void loop() {
  172. char key = keypad_key.getKey();
  173.  
  174. if (key != NO_KEY){
  175. int pos_x = 0;
  176. int pos_y = 0;
  177. get_pos(key, pos_x, pos_y);
  178. String tittle = get_key_tittle_mapping(key);
  179. reset_screen();
  180. draw_button(tittle, pos_x, pos_y, 1);
  181. }
  182. }
Advertisement
RAW Paste Data Copied
Advertisement