|
| 1 | + |
| 2 | +#include <Arduino.h> |
| 3 | +#include <TFT_eSPI.h> |
| 4 | +#include <XPT2046_Touchscreen.h> |
| 5 | +#include <lvgl.h> |
| 6 | + |
| 7 | + |
| 8 | +#include "ui.h" |
| 9 | + |
| 10 | +//LV_CONF LV_COLOR_DEPTH must be set to 16 |
| 11 | + |
| 12 | +/*Set to your screen resolution*/ |
| 13 | +static const uint16_t screenWidth = 320; |
| 14 | +static const uint16_t screenHeight = 240; |
| 15 | +//set up screen |
| 16 | +static lv_disp_draw_buf_t draw_buf; |
| 17 | +static lv_color_t buf[ screenWidth * screenHeight / 10 ]; |
| 18 | +TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */ |
| 19 | + |
| 20 | +// set up touch |
| 21 | +#define TIRQ_PIN 2 |
| 22 | +#define XPT2046_IRQ 36 |
| 23 | +#define XPT2046_MOSI 32 |
| 24 | +#define XPT2046_MISO 39 |
| 25 | +#define XPT2046_CLK 25 |
| 26 | +#define XPT2046_CS 33 |
| 27 | +SPIClass mySpi = SPIClass(HSPI); |
| 28 | +XPT2046_Touchscreen ts(XPT2046_CS, XPT2046_IRQ); |
| 29 | + |
| 30 | + |
| 31 | +void my_disp_flush( lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p ) |
| 32 | +{ |
| 33 | + log_i("Flushing"); |
| 34 | + uint32_t w = ( area->x2 - area->x1 + 1 ); |
| 35 | + uint32_t h = ( area->y2 - area->y1 + 1 ); |
| 36 | + |
| 37 | + tft.startWrite(); |
| 38 | + tft.setAddrWindow( area->x1, area->y1, w, h ); |
| 39 | + tft.pushColors( ( uint16_t * )&color_p->full, w * h, true ); |
| 40 | + tft.endWrite(); |
| 41 | + lv_disp_flush_ready( disp_drv ); |
| 42 | +} |
| 43 | +boolean wastouched = true; |
| 44 | +void my_touchpad_read( lv_indev_drv_t * indev_drv, lv_indev_data_t * data ) |
| 45 | +{ |
| 46 | + uint16_t touchX, touchY; |
| 47 | + |
| 48 | + bool touched = (ts.tirqTouched() && ts.touched()); |
| 49 | + |
| 50 | + if( !touched ) |
| 51 | + { |
| 52 | + data->state = LV_INDEV_STATE_REL; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + TS_Point p = ts.getPoint(); |
| 57 | + touchX = map(p.x,200,3700,1,screenWidth); /* Touchscreen X calibration */ |
| 58 | + touchY = map(p.y,240,3800,1,screenHeight); /* Touchscreen X calibration */ |
| 59 | + data->state = LV_INDEV_STATE_PR; |
| 60 | + |
| 61 | + /*Set the coordinates*/ |
| 62 | + data->point.x = touchX; |
| 63 | + data->point.y = touchY; |
| 64 | + // tft.print("touched"); |
| 65 | + Serial.print( "Data x " ); |
| 66 | + Serial.println( touchX ); |
| 67 | + |
| 68 | + Serial.print( "Data y " ); |
| 69 | + Serial.println( touchY ); |
| 70 | + } |
| 71 | +} |
| 72 | +int pollInterval = 15; |
| 73 | +void setup() { |
| 74 | + |
| 75 | + Serial.begin(115000); |
| 76 | + // put your setup code here, to run once: |
| 77 | + lv_init(); |
| 78 | + |
| 79 | + tft.init(); /* TFT init */ |
| 80 | + tft.setRotation( 1 ); /* Landscape orientation, flipped */ |
| 81 | + mySpi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); /* Start second SPI bus for touchscreen */ |
| 82 | + ts.begin(mySpi); /* Touchscreen init */ |
| 83 | + ts.setRotation( 1 ); /* Landscape orientation, flipped */ |
| 84 | + tft.fillScreen(TFT_BLACK); |
| 85 | + |
| 86 | + |
| 87 | + lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * screenHeight / 10 ); |
| 88 | + |
| 89 | + /*Initialize the display*/ |
| 90 | + static lv_disp_drv_t disp_drv; |
| 91 | + lv_disp_drv_init( &disp_drv ); |
| 92 | + /*Change the following line to your display resolution*/ |
| 93 | + disp_drv.hor_res = screenWidth; |
| 94 | + disp_drv.ver_res = screenHeight; |
| 95 | + disp_drv.flush_cb = my_disp_flush; |
| 96 | + disp_drv.draw_buf = &draw_buf; |
| 97 | + lv_disp_drv_register( &disp_drv ); |
| 98 | + |
| 99 | +/*Initialize the (dummy) input device driver*/ |
| 100 | + static lv_indev_drv_t indev_drv; |
| 101 | + lv_indev_drv_init( &indev_drv ); |
| 102 | + indev_drv.type = LV_INDEV_TYPE_POINTER; |
| 103 | + indev_drv.read_cb = my_touchpad_read; |
| 104 | + lv_indev_drv_register( &indev_drv ); |
| 105 | + |
| 106 | + ui_init(); |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +void loop() { |
| 112 | + |
| 113 | + |
| 114 | + lv_timer_handler(); |
| 115 | + //allow stuff to happen |
| 116 | + delay(10); |
| 117 | +} |
| 118 | + |
| 119 | +// put function definitions here: |
0 commit comments