ui.ino 3.0 KB

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