Advertisement
jrullan

Touchscreen_GUI Demo Code

Sep 16th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * This demo demonstrates the power and
  3.  * flexibility of this library.
  4.  * This demo runs on an Arduino Uno or
  5.  * any other Arduino compatible microcontrollers
  6.  * including:
  7.  * ESP8266
  8.  * ESP32
  9.  * STM32
  10.  *
  11.  * The demo showcases the following features:
  12.  * Display widgets used to show a text
  13.  * Buttons and Round Buttons
  14.  * Multiple Screens
  15.  * Use of the Numkey widget as a password entry
  16.  * Dial indicator
  17.  * Terminal widget useful for on-screen information
  18.  *
  19.  * Created by: Jose Rullan
  20.  * Date: September 3, 2018
  21.  */
  22.  
  23. // Use either of these three and
  24. // its corresponding class
  25. //#include <Canvas_SEEEDTOUCH.h>
  26. //#include <Canvas_FT6206.h>
  27. #include <Canvas_STMPE610.h>
  28.  
  29. #include <Screen.h>
  30. #include <Terminal.h>
  31. #include <Display.h>
  32. #include <Button.h>
  33. #include <Dial.h>
  34. #include <Numkey.h>
  35. #include <IconButton.h>
  36. #include <icons.h>
  37.  
  38. // Use either of these three and
  39. // its corresponding include above
  40. //Canvas_SEEEDTOUCH canvas = Canvas_SEEEDTOUCH(TFT_PORTRAIT,BLACK);
  41. //Canvas_FT6206 canvas = Canvas_FT6206(TFT_PORTRAIT,BLACK);
  42. Canvas_STMPE610 canvas = Canvas_STMPE610(TFT_PORTRAIT,BLACK);
  43.  
  44. Display header = Display(240,40,BLUE,WHITE,WHITE,20);
  45. Display title = Display(20);
  46. Terminal terminal = Terminal(240,50,TERMINAL_SCROLL_UP);
  47. Numkey numkey = Numkey();
  48. Button btnMain = Button();
  49. Button btnDial = Button();
  50. Button btnButtons = Button();
  51.  
  52. Screen screen_main = Screen(&canvas,0,40,240,190);
  53. Screen screen_dial = Screen(&canvas,0,40,240,190);
  54. Screen screen_buttons = Screen(&canvas,0,40,240,190);
  55.  
  56. Dial dial = Dial();
  57. Button btnPlus = Button(20,GRAY1,WHITE,ILI9341_LIGHTGREY);  //Initialization version for round buttons
  58. Button btnMinus = Button(20,GRAY1,WHITE,ILI9341_LIGHTGREY); //Initialization version for round buttons
  59.  
  60. IconButton btnBulb = IconButton(50,50,lightbulb_off,lightbulb_on);
  61. IconButton btnSlider = IconButton(60,30,slider_off,slider_on);
  62.  
  63. const char increment = 1;
  64.  
  65. //==================================
  66. // EVENT HANDLING ROUTINES
  67. //==================================
  68.  
  69. void btnIconEventHandler(Button* btn){
  70.   if(btn == &btnBulb){
  71.     terminal.print((btn->touched)?"Bulb pressed":"Bulb unpressed",YELLOW);
  72.   }
  73.   if(btn == &btnSlider){
  74.     terminal.print((btn->touched)?"Slider On":"Slider Off",GREEN);
  75.   }
  76.   btn->update();
  77. }
  78.  
  79. void btnMainEventHandler(Button* btn){
  80.   header.setText("Main");
  81.   canvas.setScreen(&screen_main);
  82.   terminal.clear();
  83.   welcomeMessage();
  84. }
  85.  
  86. void btnDialEventHandler(Button* btn){
  87.   // Show the numkey to enter a password
  88.   // The numkey event handler will verify the password
  89.   // and change the screen if the password is correct
  90.   //canvas.add(&numkey,60,50);
  91.   header.setText("Dial");
  92.   canvas.setScreen(&screen_dial);
  93.   terminal.clear();
  94.   terminal.print("Dial reprents a value in a range");
  95.   terminal.print("Press the + and - buttons");
  96.   terminal.print("To change it's value");
  97. }
  98.  
  99. void btnButtonsEventHandler(Button* btn){
  100.   header.setText("Buttons");
  101.   canvas.setScreen(&screen_buttons);
  102.   terminal.clear();
  103.   terminal.print("Icon buttons use images to");
  104.   terminal.print("represent the button state");
  105.   terminal.print("Press a button to see it's state");
  106. }
  107.  
  108. void btnPlusEventHandler(Button* btn){
  109.   if(dial.getCV() >= dial.scaleMax){
  110.     terminal.print("Maximum value reached",RED);
  111.     return;
  112.   }
  113.   dial.setCV(dial.getCV()+increment);
  114. }
  115.  
  116. void btnMinusEventHandler(Button* btn){
  117.   if(dial.getCV() <= dial.scaleMin){
  118.     terminal.print("Minimum value reached",BLUE);
  119.     return;
  120.   }
  121.   dial.setCV(dial.getCV()-increment);
  122. }
  123.  
  124. void numkeyEventHandler(Numkey* nk){
  125.   char* password = "1234";
  126.   if(nk->getTextSize() == Widget::getTextLength(password)){
  127.     bool match = true;
  128.     for(int i=0;i<nk->getTextSize();i++){
  129.       if(nk->contents.text[i] != password[i]) match = false;
  130.     }
  131.     if(match){
  132.       header.setText("Dial");
  133.       canvas.setScreen(&screen_dial);
  134.       terminal.print("Password OK",GREEN);
  135.       nk->clear();
  136.       return;
  137.     }
  138.   }
  139.   terminal.print("Wrong password entered",RED);
  140.   nk->clear();
  141. }
  142.  
  143. void welcomeMessage(){
  144.   terminal.print("Welcome to Touchscreen GUI demo",YELLOW);
  145.   terminal.print("This demo shows up some features",GREEN);
  146.   terminal.print("of the library. Enjoy!");  
  147. }
  148.  
  149. //==================================
  150. // SETUP
  151. //==================================
  152. void guiSetup(){
  153.   canvas.init();
  154.  
  155.   // ===== MAIN SCREEN =====
  156.   screen_main.bgColor = 0xDFE;
  157.   title.setSize(240,40);
  158.   title.setColors(screen_main.bgColor,GRAY2,screen_main.bgColor);
  159.   title.borderWidth = 0;
  160.   title.setText("GUI DEMO",false);
  161.   screen_main.add(&title,0,screen_main.h/2-FONT_Y*title.fontSize);
  162.  
  163.   // ===== SCREEN TWO =====
  164.   btnPlus.setText("+");
  165.   btnPlus.setEventHandler(&btnPlusEventHandler);
  166.   btnPlus.init();
  167.   btnPlus.setDebounce(25);  
  168.  
  169.   btnMinus.setText("-");
  170.   btnMinus.setEventHandler(&btnMinusEventHandler);
  171.   btnMinus.init();
  172.   btnMinus.setDebounce(25);  
  173.  
  174.   dial.init();
  175.   dial.setSize(50);
  176.   dial.borderWidth = 5;
  177.   dial.setColors(WHITE,GRAY2,GRAY2);
  178.   dial.setLimits(60,70,90);
  179.   dial.setHiLimit(75,RED);
  180.   dial.setSP(70,BLUE);
  181.   dial.setLowLimit(70,BLUE);
  182.   dial.setCV(72,false);
  183.  
  184.   screen_dial.bgColor = screen_main.bgColor;//0xf80f;
  185.   screen_dial.add(&dial,100,screen_dial.h/2);
  186.   screen_dial.add(&btnPlus,160,screen_dial.h/2 - dial.radius);
  187.   screen_dial.add(&btnMinus,160,screen_dial.h/2 + dial.radius - btnMinus.h);
  188.  
  189.   // ===== SCREEN THREE - BUTTONS  =====  
  190.   screen_buttons.bgColor = 0xDFE;
  191.  
  192.   btnBulb.setEventHandler(&btnIconEventHandler);
  193.   btnBulb.transparentColor = BLACK;
  194.   btnBulb.init();
  195.  
  196.   btnSlider.setEventHandler(&btnIconEventHandler);
  197.   btnSlider.transparentColor = BLACK;
  198.   btnSlider.init();
  199.  
  200.   screen_buttons.add(&btnBulb,95,50);
  201.   screen_buttons.add(&btnSlider,90,110);
  202.  
  203.   // ===== CANVAS GENERAL ITEMS  =====  
  204.   btnMain.setSize(80,40);
  205.   btnMain.setColors(GRAY1,WHITE,ILI9341_LIGHTGREY);
  206.   btnMain.cornerRadius = 8;
  207.   btnMain.setEventHandler(&btnMainEventHandler);
  208.   btnMain.setText("Main");
  209.   btnMain.setDebounce(200);
  210.   btnMain.init();
  211.  
  212.   btnDial.setSize(80,40);
  213.   btnDial.setColors(GRAY1,WHITE,ILI9341_LIGHTGREY);
  214.   btnDial.cornerRadius = 8;
  215.   btnDial.setEventHandler(&btnDialEventHandler);
  216.   btnDial.setText("Dial");
  217.   btnDial.setDebounce(200);
  218.   btnDial.init();
  219.  
  220.   btnButtons.setSize(80,40);
  221.   btnButtons.setColors(GRAY1,WHITE,ILI9341_LIGHTGREY);
  222.   btnButtons.cornerRadius = 8;
  223.   btnButtons.setEventHandler(&btnButtonsEventHandler);
  224.   btnButtons.setText("Btns");
  225.   btnButtons.setDebounce(200);
  226.   btnButtons.init();
  227.  
  228.   numkey.setSize(120,180);
  229.   numkey.setColors(GRAY1,BLACK,WHITE);
  230.   numkey.init();
  231.   numkey.setNumkeyEventHandler(&numkeyEventHandler);
  232.   numkey.setDebounce(100);
  233.  
  234.   header.setText("Main Screen",false);
  235.   terminal.setColors(BLACK,WHITE,WHITE);
  236.    
  237.   canvas.add(&header,0,0,false);
  238.   canvas.add(&btnMain,0,230,false);
  239.   canvas.add(&btnDial,80,230,false);
  240.   canvas.add(&btnButtons,160,230,false);  
  241.   canvas.add(&terminal,0,270,false);
  242.   canvas.setScreen(&screen_main);  
  243.  
  244.   welcomeMessage();
  245. }
  246.  
  247. void setup() {
  248.   Serial.begin(115200);
  249.   guiSetup();
  250. }
  251.  
  252. //==================================
  253. // LOOP
  254. //==================================
  255. void loop() {
  256.   // Scan for touch events
  257.   // Every widget is notified and passed
  258.   // the coordinates of the event.
  259.   // The corresponding widget invokes it's event
  260.   // handler to react to the event.
  261.   canvas.scan();
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement