ui.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <lvgl.h>
  2. #include <TFT_eSPI.h>
  3. #include <ui.h>
  4. /*Don't forget to set Sketchbook location in File/Preferencesto the path of your UI project (the parent foder of this INO file)*/
  5. /*Change to your screen resolution*/
  6. static const uint16_t screenWidth = 240;
  7. static const uint16_t screenHeight = 240;
  8. static lv_disp_draw_buf_t draw_buf;
  9. static lv_color_t buf[ screenWidth * screenHeight / 10 ];
  10. TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */
  11. #if LV_USE_LOG != 0
  12. /* Serial debugging */
  13. void my_print(const char * buf)
  14. {
  15. Serial.printf(buf);
  16. Serial.flush();
  17. }
  18. #endif
  19. /* Display flushing */
  20. void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
  21. {
  22. uint32_t w = ( area->x2 - area->x1 + 1 );
  23. uint32_t h = ( area->y2 - area->y1 + 1 );
  24. tft.startWrite();
  25. tft.setAddrWindow( area->x1, area->y1, w, h );
  26. tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
  27. tft.endWrite();
  28. lv_disp_flush_ready( disp );
  29. }
  30. /*Read the touchpad*/
  31. void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
  32. {
  33. uint16_t touchX = 0, touchY = 0;
  34. bool touched = false;//tft.getTouch( &touchX, &touchY, 600 );
  35. if( !touched )
  36. {
  37. data->state = LV_INDEV_STATE_REL;
  38. }
  39. else
  40. {
  41. data->state = LV_INDEV_STATE_PR;
  42. /*Set the coordinates*/
  43. data->point.x = touchX;
  44. data->point.y = touchY;
  45. Serial.print( "Data x " );
  46. Serial.println( touchX );
  47. Serial.print( "Data y " );
  48. Serial.println( touchY );
  49. }
  50. }
  51. void setup()
  52. {
  53. Serial.begin( 115200 ); /* prepare for possible serial debug */
  54. String LVGL_Arduino = "Hello Arduino! ";
  55. LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
  56. Serial.println( LVGL_Arduino );
  57. Serial.println( "I am LVGL_Arduino" );
  58. lv_init();
  59. #if LV_USE_LOG != 0
  60. lv_log_register_print_cb( my_print ); /* register print function for debugging */
  61. #endif
  62. tft.begin(); /* TFT init */
  63. tft.setRotation( 3 ); /* Landscape orientation, flipped */
  64. lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 );
  65. /*Initialize the display*/
  66. static lv_disp_drv_t disp_drv;
  67. lv_disp_drv_init( &disp_drv );
  68. /*Change the following line to your display resolution*/
  69. disp_drv.hor_res = screenWidth;
  70. disp_drv.ver_res = screenHeight;
  71. disp_drv.flush_cb = my_disp_flush;
  72. disp_drv.draw_buf = &draw_buf;
  73. lv_disp_drv_register( &disp_drv );
  74. /*Initialize the (dummy) input device driver*/
  75. static lv_indev_drv_t indev_drv;
  76. lv_indev_drv_init( &indev_drv );
  77. indev_drv.type = LV_INDEV_TYPE_POINTER;
  78. indev_drv.read_cb = my_touchpad_read;
  79. lv_indev_drv_register( &indev_drv );
  80. ui_init();
  81. Serial.println( "Setup done" );
  82. }
  83. void loop()
  84. {
  85. lv_timer_handler(); /* let the GUI do its work */
  86. delay(5);
  87. }