Created
February 22, 2024 02:47
-
-
Save Ajak58a/512edf2b4035a3fb7920cd674b6ebd93 to your computer and use it in GitHub Desktop.
How to build a paint application using Arduino
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
#include <Adafruit_GFX.h> | |
#include <MCUFRIEND_kbv.h> | |
#include "TouchScreen.h" | |
#define BLACK 0x0000 | |
#define BLUE 0x001F | |
#define RED 0xF800 | |
#define GREEN 0x07E0 | |
#define CYAN 0x07FF | |
#define MAGENTA 0xF81F | |
#define YELLOW 0xFFE0 | |
#define WHITE 0xFFFF | |
MCUFRIEND_kbv tft; | |
#define MINPRESSURE 10 | |
#define MAXPRESSURE 1000 | |
const int XP=6,XM=A2,YP=A1,YM=7; //320x480 ID=0x9486 | |
const int TS_LEFT=931,TS_RT=168,TS_TOP=452,TS_BOT=191; | |
#define TS_MINX 125 | |
#define TS_MINY 85 | |
#define TS_MAXX 965 | |
#define TS_MAXY 905 | |
TouchScreen ts(XP, YP, XM, YM, 300); //re-initialised after diagnose | |
TSPoint tp; | |
#define BOXSIZE 40 | |
unsigned int PENRADIUS = 3; | |
int oldcolor, currentcolor; | |
void setup(void) { | |
Serial.begin(9600); | |
Serial.println(F("Paint!")); | |
tft.reset(); | |
tft.begin(tft.readID()); | |
Serial.println(); | |
Serial.print("reading id..."); | |
delay(500); | |
Serial.println(tft.readID(), HEX); | |
tft.fillScreen(WHITE); | |
tft.fillRect(0, 0, BOXSIZE, BOXSIZE, YELLOW); | |
tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, MAGENTA); | |
tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, CYAN); | |
tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, GREEN); | |
tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, RED); | |
tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, BLUE); | |
tft.fillRect(BOXSIZE * 6, 0, BOXSIZE, BOXSIZE, BLACK); | |
tft.fillRect(BOXSIZE * 7, 0, BOXSIZE, BOXSIZE, WHITE); | |
tft.drawRect(BOXSIZE * 7, 0, BOXSIZE, BOXSIZE, BLACK); | |
currentcolor = YELLOW; | |
pinMode(13, OUTPUT); | |
} | |
#define MINPRESSURE 10 | |
#define MAXPRESSURE 1000 | |
void loop() { | |
digitalWrite(13, HIGH); | |
TSPoint p = ts.getPoint(); | |
digitalWrite(13, LOW); | |
pinMode(XM, OUTPUT); | |
pinMode(YP, OUTPUT); | |
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) { | |
if (p.y < (TS_MINY - 5)) { | |
Serial.println("erase"); | |
tft.fillRect(0, BOXSIZE, tft.width(), tft.height() - BOXSIZE, WHITE); | |
} | |
p.x = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0); | |
p.y = map(p.y, TS_MINY, TS_MAXY, tft.height(), 0); | |
// Serial.print("X:"); // I used this to get the accurate touch points for X and Y axis | |
// Serial.print(p.x); | |
// Serial.print(" "); | |
// Serial.print("Y:"); | |
// Serial.println(p.y); | |
if (p.y < 0) { | |
oldcolor = currentcolor; | |
if (p.x < BOXSIZE) { | |
currentcolor = YELLOW; | |
tft.drawRect(0, 0, BOXSIZE, BOXSIZE, WHITE); | |
} else if (p.x < BOXSIZE * 2) { | |
currentcolor = MAGENTA; | |
tft.drawRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, WHITE); | |
} else if (p.x < BOXSIZE * 3) { | |
currentcolor = CYAN; | |
tft.drawRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, WHITE); | |
} else if (p.x < BOXSIZE * 4) { | |
currentcolor = GREEN; | |
tft.drawRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, WHITE); | |
} else if (p.x < BOXSIZE * 5) { | |
currentcolor = RED; | |
tft.drawRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, WHITE); | |
} else if (p.x < BOXSIZE * 6) { | |
currentcolor = BLUE; | |
tft.drawRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, WHITE); | |
}else if (p.x < BOXSIZE * 7) { | |
currentcolor = BLACK; | |
tft.drawRect(BOXSIZE * 6, 0, BOXSIZE, BOXSIZE, WHITE); | |
}else if (p.x < BOXSIZE * 8) { | |
currentcolor = WHITE; | |
tft.drawRect(BOXSIZE * 7, 0, BOXSIZE, BOXSIZE, BLACK); | |
} | |
if (oldcolor != currentcolor) { | |
if (oldcolor == YELLOW) tft.fillRect(0, 0, BOXSIZE, BOXSIZE, YELLOW); | |
if (oldcolor == MAGENTA) tft.fillRect(BOXSIZE, 0, BOXSIZE, BOXSIZE, MAGENTA); | |
if (oldcolor == CYAN) tft.fillRect(BOXSIZE * 2, 0, BOXSIZE, BOXSIZE, CYAN); | |
if (oldcolor == GREEN) tft.fillRect(BOXSIZE * 3, 0, BOXSIZE, BOXSIZE, GREEN); | |
if (oldcolor == RED) tft.fillRect(BOXSIZE * 4, 0, BOXSIZE, BOXSIZE, RED); | |
if (oldcolor == BLUE) tft.fillRect(BOXSIZE * 5, 0, BOXSIZE, BOXSIZE, BLUE); | |
if (oldcolor == BLACK) tft.fillRect(BOXSIZE * 6, 0, BOXSIZE, BOXSIZE, BLACK); | |
if (oldcolor == WHITE) tft.fillRect(BOXSIZE * 7, 0, BOXSIZE, BOXSIZE, WHITE); | |
} | |
} | |
if ((p.y > 5) && (p.y < tft.height()) && currentcolor != WHITE) { | |
PENRADIUS = 3; | |
tft.fillCircle((p.x-PENRADIUS), (p.y+BOXSIZE) , PENRADIUS, currentcolor); | |
} | |
if ((p.y > 5) && (p.y < tft.height()) && currentcolor == WHITE) { | |
PENRADIUS = 10; | |
tft.fillCircle((p.x-PENRADIUS), (p.y+BOXSIZE) , PENRADIUS, currentcolor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment