Advertisement
Guest User

Handheld UI Code

a guest
Jan 19th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.30 KB | None | 0 0
  1. #include <Adafruit_GFX.h>    // Core graphics library
  2. #include <Adafruit_TFTLCD.h> // Hardware-specific library
  3. #include <TouchScreen.h> //Touch Screen
  4.  
  5. #define YP A4  // must be an analog pin, use "An" notation!
  6. #define XM A5  // must be an analog pin, use "An" notation!
  7. #define YM 31   // can be a digital pin
  8. #define XP 30   // can be a digital pin
  9.  
  10. #define MINPRESSURE 1
  11. #define MAXPRESSURE 1000
  12.  
  13. // For better pressure precision, we need to know the resistance
  14. // between X+ and X- Use any multimeter to read it
  15. // For the one we're using, its 300 ohms across the X plate
  16. TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
  17.  
  18. #define LCD_CS A3 // Chip Select goes to Analog 3
  19. #define LCD_CD A2 // Command/Data goes to Analog 2
  20. #define LCD_WR A1 // LCD Write goes to Analog 1
  21. #define LCD_RD A0 // LCD Read goes to Analog 0
  22.  
  23. #define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
  24.  
  25. Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
  26.  
  27.  
  28. //COLORS
  29. int HAIR_COL = 0x0000, SKIN_COL = 0xE658, JACKET_COL[3] = {160, 160, 165}, ARMR_COL = 0xC596, ARML_COL = 0xCE38, OUTLINE_COL = 0x0000,
  30.     BG_COL = 0xD79E;
  31.  
  32. //MENU
  33. int MAINMEN_COL[] = {180, 90, 255},
  34.                     MAINMEN_SHADE1[] = {MAINMEN_COL[0] - 10, MAINMEN_COL[1] - 15, MAINMEN_COL[2] - 40},
  35.                                        MAINMEN_SHADE2[] = {MAINMEN_COL[0] - 20, MAINMEN_COL[1] - 20, MAINMEN_COL[2] - 60};
  36. int MAINMEN_COL_ONE[] = {180, 90, 255},
  37.                         MAINMEN_COL_TWO[] = {150, 150, 160};
  38.  
  39.  
  40. int dialog = 0, quote = 0;
  41. bool pauseDialog = false;
  42.  
  43. static const String menu_apps[] = {"Fight", "Scores", "Options"};
  44. static const int menu_length = 3;
  45. static const int menu_colors[][menu_length] = {{180, 220, 180}, {215, 198, 165}, {80, 80, 80}};
  46. static int menu_pos[menu_length][2];
  47. const int menu_fight = 0, menu_scores = 1, menu_options = 2;
  48.  
  49. static int OPTION_BRIGHTNESS = 255, OPTION_SOUND = 255;
  50.  
  51. static const int MENU_TEXT[] = {210, 210, 210};
  52.  
  53. static const unsigned int BUTTON_SIZE = 25, BUTTON_BORDER = 12, BUTTON_SCALE = 4;
  54. unsigned int buttonTick = 0, choice = 0;
  55.  
  56. static uint8_t sequence = 0;
  57. static const unsigned int SEQ_MENU = 1;
  58.  
  59. void calcMenuShades()
  60. {
  61.   MAINMEN_SHADE1[0] = MAINMEN_COL[0] - 10;
  62.   MAINMEN_SHADE1[1] = MAINMEN_COL[1] - 15;
  63.   MAINMEN_SHADE1[2] = MAINMEN_COL[2] - 20;
  64.   MAINMEN_SHADE2[0] = MAINMEN_COL[0] - 20;
  65.   MAINMEN_SHADE2[1] = MAINMEN_COL[1] - 20;
  66.   MAINMEN_SHADE2[2] = MAINMEN_COL[2] - 30;
  67.  
  68.   if (MAINMEN_SHADE1[0] < 0) MAINMEN_SHADE1[0] = 0;
  69.   if (MAINMEN_SHADE1[1] < 0) MAINMEN_SHADE1[1] = 0;
  70.   if (MAINMEN_SHADE1[2] < 0) MAINMEN_SHADE1[2] = 0;
  71.   if (MAINMEN_SHADE2[0] < 0) MAINMEN_SHADE2[0] = 0;
  72.   if (MAINMEN_SHADE2[1] < 0) MAINMEN_SHADE2[1] = 0;
  73.   if (MAINMEN_SHADE2[2] < 0) MAINMEN_SHADE2[2] = 0;
  74. }
  75.  
  76. void setMenuCol(int col[2])
  77. {
  78.   MAINMEN_COL[0] = col[0];
  79.   MAINMEN_COL[1] = col[1];
  80.   MAINMEN_COL[2] = col[2];
  81.   calcMenuShades();
  82. }
  83.  
  84. void setup(void)
  85. {
  86.   Serial.begin(9600);
  87.   pinMode(53, OUTPUT);
  88.  
  89.   //Screen setup
  90.   tft.reset();
  91.   uint16_t identifier = tft.readID();
  92.   if (identifier == 0x8357) Serial.println(F("Found HX8357D LCD driver"));
  93.   else
  94.   {
  95.     Serial.print(F("Unknown LCD driver chip: "));
  96.     Serial.println(identifier, HEX);
  97.     return;
  98.   }
  99.  
  100.   tft.begin(identifier);
  101.   Serial.println(F("Screen initialized. Loading background..."));
  102.   tft.setRotation(1);
  103.   sequence = SEQ_MENU;
  104. }
  105.  
  106. void loop(void)
  107. {
  108.   unsigned long current_time = millis();
  109.   if (sequence == SEQ_MENU) tick_menu(current_time);
  110. }
  111.  
  112. void tick_menu(long current_time)
  113. {
  114.   if (buttonTick > 1 && !isTouch()) unpushButtons();
  115.  
  116.   if (current_time - old_time >= 3000)
  117.   {
  118.     old_time = current_time;
  119.     pingTimer++;
  120.   }
  121.  
  122.   if (current_time - old_half_time >= 500)
  123.   {
  124.     old_half_time = current_time;
  125.     if (buttonTick > 0) buttonTick++;
  126.   }
  127.  
  128.   TSPoint p = ts.getPoint();
  129.   int touchX = p.y;
  130.   int touchY = tft.width() - map(p.x, tft.width(), 0, tft.height(), 0) + (tft.width());
  131.  
  132.   if (isTouch() && buttonTick == 0)
  133.   {
  134.     for (int i = 0; i < menu_length; i++)
  135.       if (touchX > menu_pos[i][0] * 2 && touchY > menu_pos[i][1] * 4)
  136.         if (touchX < (menu_pos[i][0] * 2 + (BUTTON_SIZE * BUTTON_SCALE * 2)) && touchY < (menu_pos[i][1] * 4 + (BUTTON_SIZE * BUTTON_SCALE * 2) + 4))
  137.         {
  138.           pushButton(i, true);
  139.           break;
  140.         }
  141.   }
  142. }
  143.  
  144. void pushButton(int i, bool on)
  145. {
  146.   int butCol = getColor(clampCol(menu_colors[i][0] / 2), clampCol(menu_colors[i][1] / 2), clampCol(menu_colors[i][2] / 2));
  147.   if (!on) butCol = getColor(MAINMEN_SHADE1[0], MAINMEN_SHADE1[1], MAINMEN_SHADE1[2]);
  148.   else
  149.   {
  150.     choice = i;
  151.     buttonTick = 1;
  152.   }
  153.   if (sequence == SEQ_MENU)
  154.   {
  155.     tft.drawRoundRect(menu_pos[i][0] - 1, menu_pos[i][1] - 1, (BUTTON_SIZE * BUTTON_SCALE) + 2, (BUTTON_SIZE * BUTTON_SCALE) + 6, 6, butCol);
  156.     tft.drawRoundRect(menu_pos[i][0] - 2, menu_pos[i][1] - 2, (BUTTON_SIZE * BUTTON_SCALE) + 4, (BUTTON_SIZE * BUTTON_SCALE) + 8, 8, butCol);
  157.     tft.drawRoundRect(menu_pos[i][0] - 3, menu_pos[i][1] - 3, (BUTTON_SIZE * BUTTON_SCALE) + 6, (BUTTON_SIZE * BUTTON_SCALE) + 10, 10, butCol);
  158.     tft.drawRoundRect(menu_pos[i][0] - 4, menu_pos[i][1] - 4, (BUTTON_SIZE * BUTTON_SCALE) + 8, (BUTTON_SIZE * BUTTON_SCALE) + 12, 12, butCol);
  159.     tft.drawRoundRect(menu_pos[i][0] - 5, menu_pos[i][1] - 5, (BUTTON_SIZE * BUTTON_SCALE) + 10, (BUTTON_SIZE * BUTTON_SCALE) + 14, 14, butCol);
  160.     tft.drawRoundRect(menu_pos[i][0] - 6, menu_pos[i][1] - 6, (BUTTON_SIZE * BUTTON_SCALE) + 12, (BUTTON_SIZE * BUTTON_SCALE) + 16, 16, butCol);
  161.     if (on) playNote(0, 1, NOTE_E6, 32);
  162.     else playNote(0, 1, NOTE_D6, 32);
  163.   }
  164.   if (!on && i == choice)
  165.   {
  166.     buttonTick = 0;
  167.     if (choice == menu_options)
  168.     {
  169.       sequence = SEQ_OPTIONS;
  170.       drawBG(sequence);
  171.     }
  172.     else if (choice == menu_fight)
  173.     {
  174.       sequence = SEQ_CUTSCENE;
  175.       drawBG(sequence);
  176.     }
  177.     else if (choice == menu_scores)
  178.     {
  179.       sequence = SEQ_SCORES;
  180.       drawBG(sequence);
  181.     }
  182.   }
  183. }
  184.  
  185. int clamp(int val, int mini, int maxi)
  186. {
  187.   if (val > maxi) return maxi;
  188.   else if (val < mini) return mini;
  189.   else return val;
  190. }
  191.  
  192. int clampCol(int val)
  193. {
  194.   return clamp(val, 0, 255);
  195. }
  196.  
  197. void unpushButtons()
  198. {
  199.   for (int i = 0; i < menu_length; i++) unpushButton(i);
  200. }
  201.  
  202. void unpushButton(int i)
  203. {
  204.   pushButton(i, false);
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement