ui.ino 3.1 KB

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