The demo mentions that one may have to change the orientation to 0 if the touchscreen appears to be upside down. Technically this is correct, but may still be misunderstood. The setting only applies to the touch panel, not to the display panel. To avoid any confusion I changed my sketch to set the orientation for BOTH the display and the touch panel to the orientation I want, using this:
// LV_DISPLAY_ROTATION_0: portrait, USB bottom side
// LV_DISPLAY_ROTATION_90: landscape, USB right side
// LV_DISPLAY_ROTATION_180: portrait, USB top side
// LV_DISPLAY_ROTATION_270: landscape USB left side
#define TFT_ROTATION LV_DISPLAY_ROTATION_270
// swap height and width according to rotation setting
#if ((TFT_ROTATION == LV_DISPLAY_ROTATION_0) || (TFT_ROTATION == LV_DISPLAY_ROTATION_180))
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
#else
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#endif
and this:
touchscreen.setRotation(TFT_ROTATION);
lv_display_set_rotation(disp, TFT_ROTATION);
the LV_DISPLAY_ROTATION enum values are defined in lv_display.h
Now, my display and touch screen always have the same orientation and screen width and height are always correct for the selected orientation.
Just one little side effect (not sure if this is also the case in the original sketch): when I get close to the bottom and/or left edge of the screen, I get a warning in the serial monitor that x and/or y have a negative value. That’s because the screen is not calibrated. This can be avoided by either calibrating the touch screen or by adding constraints to the map function to get x and y of the touched point.