Advertisement
Merevoli

Untitled

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