C++ Gurus
Karl W4KRL
W4KRL at arrl.net
Fri Feb 22 16:29:50 CST 2013
I am creating a Button class to use with a touchscreen display. The
touchscreen is actually a separate device glued to the front of the LCD
display. The class must translate the touchscreen coordinates to the LCD
coordinates to test if the user is touching the screen at the button
position.
Each button has several unique visible properties like position, size and
color. The parameters needed to translate the touchscreen to LCD are the
same for each button. However, they are unknown with precision until the
screen is calibrated. I do have some generic parameters that are close
enough without calibration.
How do I get the touchscreen parameters into the class so that all objects
will be able to do the translation? At the moment the generic parameters are
hard coded into the class constructor. Is there a clever way to get the
parameters into the class?
Thanks, Karl W4KRL
PS This code snippet works.
class Button {
public:
Button();
// constructor - TBD
void setButton(int x, int y, int width, int height, unsigned int color);
// place a button on the screen with upper left corner at (x, y) with
width w, height h, and color
void updateColor(unsigned int color); // must have more features - TBD
void setTouch(int tXmin, int tXmax, int tYmin, int tYmax, int tZmin, int
tZmax);
// set touchscreen limits
bool isPressed();
// true if button is pressed
private:
Point p; // place where user
is pressing
int _x, _y, _w, _h; // button coords &
dimensions
unsigned int _c; // button
background color
int _tXmin, _tXmax, _tYmin, _tYmax, _tZmin, _tZmax; // touch plate ADC
readings
};
void Button::setButton (int x, int y, int width, int height, unsigned int
color) {
_x = x;
_y = y;
_w = width;
_h = height;
_c = color;
lcd.fillRoundRect(_x, _y, _w, _h, 5, _c); // radius 5
}
void Button::updateColor (unsigned int color) {setButton(_x, _y, _w, _h,
color);} // is this clever?
// first cut at constructor
Button::Button() {
_x = 10; //generic button parameters
_y = 10;
_w = 10;
_h = 10;
_c = WHITE;
_tXmin = 175; // generic touchscreen parameters
_tXmax = 915;
_tYmin = 155;
_tYmax = 920;
_tZmin = 10;
_tZmax = 1000;
}
void Button::setTouch(int tXmin, int tXmax, int tYmin, int tYmax, int tZmin,
int tZmax) {
_tXmin = tXmin;
_tXmax = tXmax;
_tYmin = tYmin;
_tYmax = tYmax;
_tZmin = tZmin;
_tZmax = tZmax;
}
bool Button::isPressed () {
//this is a first cut
Point p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// must set the pinMode to OUTPUT or the LCD will not write
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// map from analog input 0->1023 to lcd.width
p.x = map(p.x, TS_MINX, TS_MAXX, lcd.width(), 0);
p.y = map(p.y, TS_MINY, TS_MAXY, lcd.height(), 0);
}
return ((p.x >= _x) && (p.x <= _x + _w) && (p.y >= _y) && (p.y <= _y +
_h));
}
Button b1, b2; // instantiate a few smart
button objects
void setup(void) {
Serial.begin(9600);
Serial.println("Button_OOP01");
lcd.reset();
lcd.initDisplay();
lcd.fillScreen(BLACK);
b1.setButton(50, 50, 50, 50, RED); // set the visible properties
b2.setButton(100,100,20,20, YELLOW);
}
void loop() {
Point p = ts.getPoint();
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
if (b1.isPressed()) {
Serial.println("B1: ");
}
if (b2.isPressed()) {
Serial.println("B2: ");
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://amrad.org/pipermail/tacos/attachments/20130222/a83cdc2f/attachment.html>
More information about the Tacos
mailing list