Advertisement
Merevoli

Untitled

May 18th, 2022
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  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. return {col, row};
  74. pos_x = col;
  75. pos_y = row;
  76. is_found = true;
  77. break;
  78. }
  79. }
  80. if (is_found){
  81. break;
  82. }
  83. }
  84.  
  85. pos_x = 0;
  86. pos_y = 0;
  87. }
  88.  
  89. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  90. pos_x = 15 + (pos_x - 1) * 45;
  91. pos_y = 40 + (pos_y - 1) * 45;
  92.  
  93. uint16_t button_color;
  94. if (color == 0) { // green
  95. button_color = tft.color565(64, 237, 39);
  96. }
  97. else { // red
  98. button_color = tft.color565(238, 38, 91);
  99. }
  100. int pos_first_space = find_str(tittle, ' ');
  101. if (pos_first_space != -1) {
  102. String word_up = tittle.substring(0, pos_first_space);
  103. String word_down = tittle.substring(pos_first_space + 1);
  104.  
  105. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  106. tft.setCursor(pos_x + 10, pos_y + 10);
  107. tft.setTextColor(tft.color565(0, 0, 0));
  108. tft.setTextSize(1);
  109. tft.println(word_up);
  110.  
  111. tft.setCursor(pos_x + 10, pos_y + 20);
  112. tft.setTextColor(tft.color565(0, 0, 0));
  113. tft.setTextSize(1);
  114. tft.println(word_down);
  115. }
  116. else {
  117. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  118. tft.setCursor(pos_x + 5, pos_y + 15);
  119. tft.setTextColor(tft.color565(0, 0, 0));
  120. tft.setTextSize(1);
  121. tft.println(tittle);
  122. }
  123. }
  124.  
  125. void reset_screen(){
  126. tft.fillScreen(tft.color565(0, 0, 0));
  127.  
  128. tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  129. tft.setCursor(20, 20);
  130. tft.setTextColor(tft.color565(159, 30, 6));
  131. tft.setTextSize(2);
  132.  
  133. String tittle = get_key_tittle_mapping(keys[0][0]);
  134. tft.println(tittle);
  135.  
  136. for (int col=1; col <= COLS; col++){
  137. for (int row=1; row <= ROWS; row++){
  138. String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  139. draw_button(tittle, col, row, 0);
  140. }
  141. }
  142.  
  143. tft.drawLine(230, 130, 200, 150, ILI9341_BLACK);
  144. tft.drawLine(230, 130, 270, 130, ILI9341_BLACK);
  145. tft.drawLine(270, 130, 300, 100, ILI9341_BLACK);
  146. }
  147.  
  148. void setup() {
  149. tft.begin();
  150. tft.setRotation(3);
  151. char key = keypad_key.getKey();
  152.  
  153. if (key != NO_KEY){
  154. if (key == '7'){
  155. int pos_x = 0;
  156. int pos_y = 0;
  157. get_pos(key, pos_x, pos_y);
  158. String tittle = get_key_tittle_mapping(key);
  159. reset_screen();
  160. draw_button(tittle, pos_x, pos_y, 1);
  161. }
  162. }
  163. }
  164.  
  165. void loop() {
  166. char key = keypad_key.getKey();
  167.  
  168. if (key != NO_KEY){
  169. int pos_x = 0;
  170. int pos_y = 0;
  171. get_pos(key, pos_x, pos_y);
  172. String tittle = get_key_tittle_mapping(key);
  173. reset_screen();
  174. draw_button(tittle, pos_x, pos_y, 1);
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement