Advertisement
Merevoli

Untitled

May 20th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. #include "Keypad.h" // khai bao thư vien keypad
  2. #include <ArduinoSTL.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 <Servo.h>
  6. #include <vector>
  7. #include <map>
  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.  
  28. Servo myservo;
  29. int degree = 0;
  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. std::map <char, std::vector <std::pair <string, std::vector <int>>>> motor_movement_mapping = {
  51. {'8', {
  52. {"pin", {LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, LOW, HIGH, HIGH, HIGH}},
  53. {"delay", {3000}},
  54. {"pin_reset_high", {}}
  55. }},
  56. {'9', {
  57. {"pin_reset_high", {}},
  58. {"degree_add", {10}}
  59. }},
  60. {'A', {
  61. {"pin_reset_high", {}},
  62. {"degree", {150}},
  63. {"delay", {15}}
  64. }},
  65. {'4', {
  66. {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  67. }},
  68. {'5', {
  69. {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  70. }},
  71. {'6', {
  72. {"pin", {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW, HIGH}},
  73. {"delay", {250}},
  74. {"pin_reset_high", {}}
  75. }},
  76. {'B', {
  77. {"pin", {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, LOW}},
  78. {"delay", {250}},
  79. {"pin_reset_high", {}}
  80. }},
  81. {'1', {
  82. {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  83. }},
  84. {'2', {
  85. {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  86. }},
  87. {'3', {
  88. {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  89. }},
  90. {'C', {
  91. {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  92. }},
  93. {'*', {
  94. {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  95. }},
  96. {'0', {
  97. {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  98. }},
  99. {'#', {
  100. {"pin", {-1, HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  101. }},
  102. {'D', {
  103. {"pin", {HIGH, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}}
  104. }},
  105. }
  106.  
  107.  
  108.  
  109. byte rowPins[ROWS] = {A8, A9, A10, A11}; // Arduino pins connected to the row pins of the keypad
  110. byte colPins[COLS] = {A12, A13, A14, A15}; // Arduino pins connected to the column pins of the keypad
  111. Keypad keypad_key = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); // Keypad Library definition
  112.  
  113. using namespace std;
  114.  
  115. int find_str(String str, char find_chr){
  116. for (int pos=0; pos<str.length(); pos++){
  117. if (str[pos] == find_chr){
  118. return pos;
  119. }
  120. }
  121. return -1;
  122. }
  123.  
  124. String get_key_tittle_mapping(char key){
  125. String str_key = String(key);
  126.  
  127. for (int i=0; i<ROWS * COLS; i++){
  128. if (key_tittle_mapping[i][0] == str_key){
  129. return key_tittle_mapping[i][1];
  130. }
  131. }
  132. return "";
  133. }
  134.  
  135. void get_pos(char key, int &pos_x, int &pos_y) { // get (x, y)
  136. for (int col=1; col <= COLS; col++){
  137. for (int row=1; row <= ROWS; row++){
  138. if (keys[row - 1][col - 1] == key){
  139. pos_x = col;
  140. pos_y = row;
  141. return;
  142. }
  143. }
  144. }
  145.  
  146. pos_x = 0;
  147. pos_y = 0;
  148. }
  149.  
  150. void draw_button(String tittle, int pos_x, int pos_y, int color = 0){
  151. pos_x = 15 + (pos_x - 1) * 45;
  152. pos_y = 40 + (pos_y - 1) * 45;
  153.  
  154. uint16_t button_color;
  155. if (color == 0) { // green
  156. button_color = tft.color565(64, 237, 39);
  157. }
  158. else { // red
  159. button_color = tft.color565(238, 38, 91);
  160. }
  161. int pos_first_space = find_str(tittle, ' ');
  162. if (pos_first_space != -1) {
  163. String word_up = tittle.substring(0, pos_first_space);
  164. String word_down = tittle.substring(pos_first_space + 1);
  165.  
  166. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  167. tft.setCursor(pos_x + 3, pos_y + 10);
  168. tft.setTextColor(tft.color565(0, 0, 0));
  169. tft.setTextSize(1);
  170. tft.println(word_up);
  171.  
  172. tft.setCursor(pos_x + 3, pos_y + 20);
  173. tft.setTextColor(tft.color565(0, 0, 0));
  174. tft.setTextSize(1);
  175. tft.println(word_down);
  176. }
  177. else {
  178. int text_size = 1;
  179.  
  180. if (tittle == ">" || tittle == "<" || tittle == "v") {
  181. text_size = 3;
  182. }
  183. else if (tittle == "^"){
  184. text_size = 4;
  185. }
  186. tft.fillRect(pos_x, pos_y, 40, 40, button_color);
  187. tft.setCursor(pos_x + 5, pos_y + 15);
  188. tft.setTextColor(tft.color565(0, 0, 0));
  189. tft.setTextSize(text_size);
  190. tft.println(tittle);
  191. }
  192. }
  193.  
  194. void poweron_screen() {
  195. tft.fillRect(0, 0, 320, 240, tft.color565(200,200, 200));
  196. tft.fillRoundRect(20, 20, 280, 200, 20, tft.color565(243,87, 35));
  197. tft.setCursor(90, 90);
  198. tft.setTextColor(tft.color565(255, 255, 255));
  199. tft.setTextSize(8);
  200. tft.println("M2D ");
  201. }
  202.  
  203. void startup_screen() {
  204. tft.fillRect(0, 0, 320, 240, tft.color565(164,255, 255));
  205. tft.setCursor(50, 80);
  206. tft.setTextColor(tft.color565(0, 164, 164));
  207. tft.setTextSize(3);
  208. tft.println("GIUONG HO TRO ");
  209. tft.setCursor(40, 110);
  210. tft.setTextColor(tft.color565(243, 87, 35));
  211. tft.setTextSize(3);
  212. tft.println("NGUOI TAI BIEN");
  213. }
  214.  
  215. void init_screen(){
  216. tft.fillScreen(tft.color565(0, 0, 0));
  217.  
  218. tft.fillRect(10, 10, 300, 220, tft.color565(116, 139, 248));
  219. tft.setCursor(20, 20);
  220. tft.setTextColor(tft.color565(159, 30, 6));
  221. tft.setTextSize(2);
  222. tft.println("MAIN CONTROL");
  223.  
  224. tft.drawLine(230, 130, 200, 150, ILI9341_WHITE);
  225. tft.drawLine(230, 130, 270, 130, ILI9341_WHITE);
  226. tft.drawLine(270, 130, 300, 100, ILI9341_WHITE);
  227. }
  228.  
  229. void reset_screen(){
  230. for (int col=1; col <= COLS; col++){
  231. for (int row=1; row <= ROWS; row++){
  232. String tittle = get_key_tittle_mapping(keys[row - 1][col - 1]);
  233. draw_button(tittle, col, row, 0);
  234. }
  235. }
  236. }
  237.  
  238. void handle_motor(char key){
  239. std::vector <std::pair <string, std::vector <int>>> motor_steps = motor_movement_mapping[key];
  240.  
  241. for (std::pair <string, std::vector <int>> step : motor_steps) {
  242. if (step.first == "pin") {
  243. for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  244. int signal = step.second[pin_no - 1];
  245.  
  246. if (signal != -1){
  247. digitalWrite(MOTOR_PINS[pin_no], signal);
  248. }
  249. }
  250. }
  251. else if (step_first == "pin_reset_high") {
  252. for (int pin_no = 1; pin_no <= MOTOR_PIN_SIZE; pin_no++){
  253. digitalWrite(MOTOR_PINS[pin_no], HIGH);
  254. }
  255. }
  256. else if (step.first == "delay") {
  257. int delay_time = step.second[0];
  258. delay(delay_time);
  259. }
  260. else if (step.first == "degree_add") {
  261. int degree_add_value = step.second[0];
  262. degree += degree_add_value;
  263. }
  264. else if (step.first == "degree") {
  265. int degree_new_value = step.second[0];
  266. degree = degree_new_value;
  267. }
  268. }
  269. }
  270.  
  271. void make_red_start_button(){
  272. char key = '7';
  273. int pos_x = 0;
  274. int pos_y = 0;
  275. get_pos(key, pos_x, pos_y);
  276. String tittle = get_key_tittle_mapping(key);
  277. draw_button(tittle, pos_x, pos_y, 1);
  278. }
  279.  
  280. void setup() {
  281. tft.begin();
  282. tft.setRotation(3);
  283. bool is_init_screen = false;
  284. poweron_screen();
  285.  
  286. while (!is_init_screen){
  287. char key = keypad_key.waitForKey();
  288. if (key != NO_KEY){
  289. if (key == '7'){
  290. startup_screen();
  291. delay(1000);
  292. init_screen();
  293. is_init_screen = true;
  294. int pos_x = 0;
  295. int pos_y = 0;
  296. get_pos(key, pos_x, pos_y);
  297. String tittle = get_key_tittle_mapping(key);
  298. reset_screen();
  299. draw_button(tittle, pos_x, pos_y, 1);
  300. }
  301. }
  302. pinMode(chan1, OUTPUT);
  303. pinMode(chan2, OUTPUT);
  304. pinMode(chan3, OUTPUT);
  305. pinMode(chan4, OUTPUT);
  306. pinMode(chan5, OUTPUT);
  307. pinMode(chan6, OUTPUT);
  308. pinMode(chan7, OUTPUT);
  309. pinMode(chan8, OUTPUT);
  310. pinMode(chan9, OUTPUT);
  311. pinMode(chan10, OUTPUT);
  312. pinMode(chan11, OUTPUT);
  313. pinMode(chan12, OUTPUT);
  314. myservo.attach(11, 500, 2500);
  315. }
  316. }
  317.  
  318. void loop() {
  319. char key = keypad_key.waitForKey();
  320.  
  321. if (key != NO_KEY){
  322. int pos_x = 0;
  323. int pos_y = 0;
  324. get_pos(key, pos_x, pos_y);
  325. String tittle = get_key_tittle_mapping(key);
  326. reset_screen();
  327. make_red_start_button();
  328. draw_button(tittle, pos_x, pos_y, 1);
  329.  
  330. myservo.write(degree);
  331. handle_motor(key);
  332. }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement