Skip to content

Instantly share code, notes, and snippets.

@lovyan03
Last active January 5, 2021 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lovyan03/e6e21d4e65919cec34eae403e099876c to your computer and use it in GitHub Desktop.
Save lovyan03/e6e21d4e65919cec34eae403e099876c to your computer and use it in GitHub Desktop.
LovyanGFX touch sample for M5Paper and M5StackCore2
#define LGFX_AUTODETECT
#include <LovyanGFX.hpp>
LGFX gfx;
LGFX_Sprite sp(&gfx);
lgfx::touch_point_t tp;
bool touched = false;
std::int32_t clip_top = INT32_MAX;
std::int32_t clip_left = INT32_MAX;
std::int32_t clip_right = INT32_MIN;
std::int32_t clip_bottom = INT32_MIN;
int32_t x = -1, y = -1;
void setup(void)
{
gfx.init();
gfx.clear(TFT_WHITE);
gfx.setRotation(1);
gfx.setEpdMode(epd_mode_t::epd_fastest);
gfx.startWrite();
sp.setColorDepth(1);
sp.createSprite(gfx.width(), gfx.height());
sp.fillScreen(0);
sp.setColor(1);
sp.setPaletteColor(1, TFT_BLACK);
}
void loop(void)
{
if (gfx.getTouch(&tp))
{
if (x != tp.x || y != tp.y || !touched)
{
if (!touched)
{
x = tp.x;
clip_left = x;
clip_right = x;
y = tp.y;
clip_top = y;
clip_bottom = y;
}
else
{
clip_left = std::min(clip_left , tp.x);
clip_right = std::max(clip_right , tp.x);
clip_top = std::min(clip_top , tp.y);
clip_bottom = std::max(clip_bottom, tp.y);
}
int i = -2;
do
{
int je = 2 - abs(i);
for (int j = -je; j <= je; ++j)
{
sp.drawLine(x + i, y + j, tp.x + i, tp.y + j);
}
} while (++i <= 2);
clip_left -= 3;
clip_right += 3;
clip_top -= 3;
clip_bottom += 3;
gfx.setClipRect(clip_left, clip_top, clip_right - clip_left + 1, clip_bottom - clip_top + 1);
sp.pushSprite(0, 0, 0);
gfx.clearClipRect();
gfx.display();
x = tp.x;
y = tp.y;
clip_left = x;
clip_right = x;
clip_top = y;
clip_bottom = y;
}
touched = true;
}
else
{
touched = false;
delay(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment