Had a bit of time this weekend to play with my little Arduino Mega, and pushed my personal boundaries a few inches forward.
YAY!
I really liked the rush of getting my 2.8 TFT screen working finally earlier last week with the original “Paint” proggy offered on the net.
The 1st impression though, was that if I could have thinner brush-strokes, I could draw a little more detail in the picture.
So I figured out fairly quickly what variable needed to go from “2” to “1” to get a 1 pixel stroke.
Then, natural progression…
I wanted a thicker line again, as I drew…not requiring resetting the bitmap all over again.
So, With a little grinding time today finally…I’m pleased to offer other Arduino Noobs the sketch for the improved or ‘expanded’ “Paint” program!
I reduced the color swatch widths to give a little space for 4 more buttons on the right, for 4 different thicknesses of brush stroke.
With the super-thin 1-pixel stroke, its recommended to use something like a pointy stylus or if your a Gurl, your fingernail tip. heh
And now with the expanded strokes, its possible to really crank out some fairly intriguing detail with this hopped-up Paint.
The sketch loads the same as original, but when you 1st start, you must pick a stroke thickness from the 4 settings before it puts any ink down on the pad.
Don’t laugh at any silly mistakes I may have made…it works!
I know this might seem lame to the heavy guns, but I think this is a good example to other budding Arduino beginners of how to take some existing code and twist it a bit to get something better. I was inspired by it!
The next time I’m blessed with more playtime, Im gonna try and figure out how to save the bitmap to a storage device of some kind. Then I think I will be getting somewhere…
Enjoy!
//~~~ original headings and comments I left intact (I think),
//~~~ plus a few pieces of my comments littered in there somewhere
// Paint application - Demonstrate both TFT and Touch Screen
// Revision 1.2 —variable thickness strokes ---- by CircuitBurner ---- 2/25/13
#include <stdint.h>
#include <TouchScreen.h>
#include <TFT.h>
//Measured ADC values for (0,0) and (210-1,320-1)
//TS_MINX corresponds to ADC value when X = 0
//TS_MINY corresponds to ADC value when Y = 0
//TS_MAXX corresponds to ADC value when X = 240 -1
//TS_MAXY corresponds to ADC value when Y = 320 -1
static unsigned int TS_MINX, TS_MAXX, TS_MINY, TS_MAXY;
//Touch Screen Co-ordinate mapping register
static unsigned int MapX1, MapX2, MapY1, MapY2;
// The declaration of the new S variable -
static unsigned int s;
// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// The 2.8" TFT Touch shield has 300 ohms across the X plate
/* Usage: TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Where, XP = X plus, YP = Y plus, XM = X minus and YM = Y minus */
//init TouchScreen port pins. This would be reinitialized in setup() based on the hardware detected.
TouchScreen ts = TouchScreen(17, A2, A1, 14, 300);
int color = WHITE; //Default Paint brush color
void setup()
{
Tft.init(); //init TFT library
initTouchScreenParameters(); // initializes Touch Screen parameters based on the detected TFT Touch Schield hardware
// Serial.begin(9600); //debug
//Draw the palette
Tft.fillRectangle(0,0,20,20,BLACK);
Tft.fillRectangle(20,0,20,20,RED);
Tft.fillRectangle(40,0,20,20,GREEN);
Tft.fillRectangle(60,0,20,20,BLUE);
Tft.fillRectangle(80,0,20,20,CYAN);
Tft.fillRectangle(100,0,20,20,YELLOW);
Tft.fillRectangle(120,0,20,20,WHITE);
Tft.fillRectangle(140,0,20,20,GRAY1);
// the extra buttons boundaries for stroke thickness -
Tft.drawRectangle(161,0,18,20,GRAY1);
Tft.drawRectangle(181,0,18,20,GRAY1);
Tft.drawRectangle(201,0,18,20,GRAY1);
Tft.drawRectangle(221,0,18,20,GRAY1);
}
void loop()
{
// Serial.println(Tft.IC_CODE,HEX); //debug
// a point object holds x y and z coordinates.
Point p = ts.getPoint();
// we have some minimum pressure we consider ‘valid’
// pressure of 0 means no pressing!
if (p.z > ts.pressureThreshhold) {
//map the ADC value read to into pixel co-ordinates
p.x = map(p.x, TS_MINX, TS_MAXX, MapX1, MapX2);
p.y = map(p.y, TS_MINY, TS_MAXY, MapY1, MapY2);
// Detect paint brush color change
if(p.y < 20)
{
if(p.x >= 0 && p.x < 20)
{
color = BLACK;
}
if(p.x >= 20 && p.x < 40)
{
color = RED;
}
if(p.x >= 40 && p.x < 60)
{
color = GREEN;
}
if(p.x >= 60 && p.x < 80)
{
color = BLUE;
}
if(p.x >= 80 && p.x < 100)
{
color = CYAN;
}
if(p.x >= 100 && p.x < 120)
{
color = YELLOW;
}
if(p.x >= 120 && p.x < 140)
{
color = WHITE;
}
if(p.x >= 140 && p.x < 160)
{
color = GRAY1;
}
// The Extra buttons contact patches -
// Change s = 6 to s = 12 or higher for a really fatass brush stroke!
if(p.x >= 161 && p.x < 181)
{
s = 6;
}
if(p.x >= 182 && p.x < 201)
{
s = 4;
}
if(p.x >= 202 && p.x < 220)
{
s = 2;
}
if(p.x >= 221 && p.x < 240)
{
s = 1;
}
}
else
{
// Added thickness variable as “s” -
Tft.fillCircle(p.x,p.y,s,color);
}
}
}
void initTouchScreenParameters()
{
//This function initializes Touch Screen parameters based on the detected TFT Touch Schield hardware
if(Tft.IC_CODE == 0x5408) //SPFD5408A TFT driver based Touchscreen hardware detected
{
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
ts = TouchScreen(54, A1, A2, 57, 300); //init TouchScreen port pins
#else
ts = TouchScreen(14, A1, A2, 17, 300); //init TouchScreen port pins
#endif
//Touchscreen parameters for this hardware
TS_MINX = 120;
TS_MAXX = 910;
TS_MINY = 120;
TS_MAXY = 950;
MapX1 = 239;
MapX2 = 0;
MapY1 = 0;
MapY2 = 319;
}
else //ST7781R TFT driver based Touchscreen hardware detected
{
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
ts = TouchScreen(57, A2, A1, 54, 300); //init TouchScreen port pins
#else
ts = TouchScreen(17, A2, A1, 14, 300); //init TouchScreen port pins
#endif
// The silly high contrast logo at bottom of screen, edit this to make it whatever -
Tft.drawString("Rusty’s ",1,299,2,RED);
Tft.drawString("Rusty’s ",2,300,2,RED);
Tft.drawString("Rusty’s ",3,301,2,YELLOW);
Tft.drawString(“Mini-Paint v1.2”,118,308,1,BLUE);
Tft.drawString(“Mini-Paint v1.2”,120,309,1,WHITE);
Tft.drawString(“+”,164,4,2,YELLOW);
Tft.drawString(“-”,223,5,2,YELLOW);
Tft.drawString(“+”,165,4,2,YELLOW);
Tft.drawString(“+”,164,5,2,YELLOW);
Tft.drawString(“+”,164,4,2,YELLOW);
Tft.drawString(“-”,225,4,2,YELLOW);
Tft.drawString(“-”,225,6,2,YELLOW);
Tft.drawString(“-”,223,6,2,YELLOW);
Tft.drawString(“-”,223,4,2,YELLOW);
Tft.drawString(“+”,166,5,2,YELLOW);
Tft.drawString(“+”,166,3,2,YELLOW);
Tft.drawString(“+”,164,3,2,YELLOW);
Tft.drawString(“-”,224,5,2,BLUE);
Tft.drawString(“+”,165,4,2,BLUE);
Tft.drawString(“size”,184,5,1,BLUE);
Tft.drawString(“size”,186,7,1,CYAN);
//Touchscreen parameters for this hardware
TS_MINX = 140;
TS_MAXX = 900;
TS_MINY = 120;
TS_MAXY = 940;
MapX1 = 239;
MapX2 = 0;
MapY1 = 319;
MapY2 = 0;
}
}