Created
September 21, 2017 04:20
-
-
Save kghose/0434bfa77d47ddabc4418efa3eee2a31 to your computer and use it in GitHub Desktop.
Quick experiment to demonstrate touch screen debouncing/smoothing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Quick experiment to demonstrate touch screen debouncing/smoothing | |
*/ | |
#include <Elegoo_GFX.h> // Core graphics library | |
#include <Elegoo_TFTLCD.h> // Hardware-specific library | |
#include <TouchScreen.h> | |
#define YP A3 // must be an analog pin, use "An" notation! | |
#define XM A2 // must be an analog pin, use "An" notation! | |
#define YM 9 // can be a digital pin | |
#define XP 8 // can be a digital pin | |
//Touch For New ILI9341 TP | |
#define TS_MINX 120 | |
#define TS_MAXX 900 | |
#define TS_MINY 70 | |
#define TS_MAXY 920 | |
// For better pressure precision, we need to know the resistance | |
// between X+ and X- Use any multimeter to read it | |
// For the one we're using, its 300 ohms across the X plate | |
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); | |
#define LCD_CS A3 | |
#define LCD_CD A2 | |
#define LCD_WR A1 | |
#define LCD_RD A0 | |
// optional | |
#define LCD_RESET A4 | |
// Assign human-readable names to some common 16-bit color values: | |
#define BLACK 0x0000 | |
#define BLUE 0x001F | |
#define RED 0xF800 | |
#define GREEN 0x07E0 | |
#define CYAN 0x07FF | |
#define MAGENTA 0xF81F | |
#define YELLOW 0xFFE0 | |
#define WHITE 0xFFFF | |
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET); | |
int x = 0, y = 0, z = 0, trace_x = 0; | |
void draw_text( uint16_t color ) | |
{ | |
tft.setCursor( 10, 10 ); | |
tft.setTextSize( 1 ); | |
tft.setTextColor( color ); | |
tft.print( String( x ) + ", " + String( y ) + ", " + String( z ) ); | |
} | |
void draw_cursor(TSPoint p) | |
{ | |
tft.drawPixel(p.y, p.x, BLUE); | |
} | |
struct ExpFilter | |
{ | |
float k; | |
int x_old; | |
ExpFilter(float _k) { k = _k; x_old = 0; } | |
void reset() { x_old = 0; } | |
int filter(float x) | |
{ | |
x_old = k * x + (1.0 - k) * x_old; | |
return x_old; | |
} | |
}; | |
ExpFilter f(0.1); | |
int scale_z( int z ) | |
{ | |
return map(z, 0, 500, tft.height() - 10, 10); | |
} | |
void draw_trace(TSPoint p) | |
{ | |
tft.drawFastVLine(trace_x, 0, tft.height(), BLACK); | |
tft.drawFastVLine(trace_x + 1, 0, tft.height(), WHITE); | |
tft.drawPixel(trace_x, scale_z( p.z ), BLUE); | |
tft.drawPixel(trace_x, scale_z( f.filter( p.z ) ), GREEN ); | |
trace_x++; | |
if( trace_x >= tft.width() ) trace_x = 0; | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
tft.reset(); | |
tft.begin( 0x9341 ); | |
tft.setRotation( 1 ); | |
tft.fillScreen(BLACK); | |
pinMode(13, OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
digitalWrite(13, HIGH); | |
TSPoint p = ts.getPoint(); | |
digitalWrite(13, LOW); | |
// if sharing pins, you'll need to fix the directions of the touchscreen pins | |
pinMode(XM, OUTPUT); | |
pinMode(YP, OUTPUT); | |
p.x = map(p.x, TS_MINX, TS_MAXX, tft.height(), 0); | |
p.y = map(p.y, TS_MINY, TS_MAXY, tft.width(), 0); | |
draw_trace( p ); | |
// draw_text( BLACK ); | |
// x = p.x; y = p.y; z = p.z; | |
// draw_text( BLUE ); | |
//delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment