As I used the given example-code I thought it would be easier to download it directly.
Here it comes.
My modified version of TouchLib_Demo.pde. (commented with "<---- this is the change"
/* Arduino touch library v0.1 example
* Copyright (C) 2011 Manfred Fuchs
* www.mafu-foto.de
* manfred@fuchsrudel.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <MI0283QT2.h>
#include <ADS7846.h>
#include <TouchLib.h>
#define TP_EEPROMADDR (0x00) //eeprom address for calibration data
MI0283QT2 lcd;
ADS7846 tp;
Touch touch(&lcd, &tp, 5);
void tp_calibration(uint8_t check_eeprom)
{
int i;
CAL_POINT lcd_points[3] = {{CAL_POINT_X1,CAL_POINT_Y1},
{CAL_POINT_X2,CAL_POINT_Y2},
{CAL_POINT_X3,CAL_POINT_Y3}}; //calibration point postions
CAL_POINT tp_points[3], *p;
//calibration data in EEPROM?
if(tp.readCalibration(TP_EEPROMADDR) && check_eeprom)
{
return;
}
//wait for touch release
lcd.clear(COLOR_WHITE);
lcd.drawText((lcd.getWidth()/2)-50, (lcd.getHeight()/2)-10, "Calibration", 1, COLOR_BLACK, COLOR_WHITE);
while(tp.getPressure() > 5){ tp.service(); };
//show calibration points
i = 0;
do
{
//draw points
lcd.drawCircle(lcd_points[i].x, lcd_points[i].y, 1, RGB(0,0,0));
lcd.drawCircle(lcd_points[i].x, lcd_points[i].y, 4, RGB(0,0,0));
lcd.drawCircle(lcd_points[i].x, lcd_points[i].y, 8, RGB(255,0,0));
//run service routine
tp.service();
//press dectected?
if(tp.getPressure() > 5)
{
tp_points[i].x = tp.getXraw();
tp_points[i].y = tp.getYraw();
i++;
lcd.clear(COLOR_WHITE);
delay(100);
}
}while(i<3);
//calulate calibration matrix
tp.setCalibration(lcd_points, tp_points);
//save calibration matrix
tp.writeCalibration(TP_EEPROMADDR);
}
void button2()
{
touch.buttonAdd( 10, 70, 160, 40, COLOR_BLUE, COLOR_RED, COLOR_WHITE, true, false, 0, 2, "Button 2");
}
void setup()
{
//init Display
lcd.init(4); //spi-clk = Fcpu/4
//init Touch-Controller
tp.init();
//clear Screen
lcd.clear(COLOR_WHITE);
//Touch-Panel Calibration
tp.service();
if(tp.getPressure() > 5)
{
tp_calibration(0); //dont check EEPROM for calibration data
}
else
{
tp_calibration(1); //check EEPROM for calibration data
}
//Backlight LED
lcd.led(60); //backlight 0...100%
lcd.drawText( 2, 2, "BL", 1, COLOR_RED, COLOR_WHITE);
lcd.drawText(20, 2, 60, 1, COLOR_RED, COLOR_WHITE);
//Cal text
lcd.drawText(lcd.getWidth()-30, 2, "CAL", 1, COLOR_RED, COLOR_WHITE);
touch.buttonAdd( 10, 20, 160, 40, COLOR_BLUE, COLOR_RED, COLOR_WHITE, true, false, 0, 2, "Button 1");
button2; // <---- this is the change
/*touch.buttonAdd( 10, 70, 160, 40, COLOR_BLUE, COLOR_RED, COLOR_WHITE, true, false, 0, 2, "Button 2");
touch.buttonAdd( 10, 120, 160, 40, COLOR_BLUE, COLOR_RED, COLOR_WHITE, false, true, 0, 2, "Button 3");
touch.buttonAdd( 10, 170, 160, 40, COLOR_BLUE, COLOR_RED, COLOR_WHITE, false, true, 0, 2, "Button 4");
touch.buttonAdd(200, 26, 90, 28, COLOR_BLUE, COLOR_RED, COLOR_WHITE, true, false, 0, 1, "Button 5");
touch.buttonAdd(200, 76, 90, 28, COLOR_BLUE, COLOR_RED, COLOR_WHITE, false, true, 1, 1, "Button 6");
touch.buttonSetDown(touch.buttonAdd(200, 126, 90, 28, COLOR_BLUE, COLOR_RED, COLOR_WHITE, false, true, 1, 1, "Button 7"), true);
touch.buttonAdd(200, 176, 90, 28, COLOR_BLUE, COLOR_RED, COLOR_WHITE, false, true, 1, 1, "Button 8");*/
touch.buttonsDraw();
}
void loop()
{
char tmp[128];
static uint16_t last_x=0, last_y=0;
static uint8_t led=60;
unsigned long m;
static unsigned long prevMillis=0;
TouchButtonPtr btn;
uint16_t txtPos;
//service routine
tp.service();
//show tp data
sprintf(tmp, "X:%03i|%04i Y:%03i|%04i P:%03i", tp.getX(), tp.getXraw(), tp.getY(), tp.getYraw(), tp.getPressure());
lcd.drawText(45, 2, tmp, 1, COLOR_BLACK, COLOR_WHITE);
btn = touch.check();
if (btn != NULL)
{
txtPos = lcd.drawText(10, 220, "\"", 1, COLOR_BLACK, COLOR_WHITE);
txtPos = lcd.drawText(txtPos, 220, btn->caption, 1, COLOR_BLACK, COLOR_WHITE);
lcd.drawText(txtPos, 220, "\" pressed", 1, COLOR_BLACK, COLOR_WHITE);
}
if(tp.getPressure() > 3) //touch press?
{
//change backlight power
if((tp.getX() < 45) && (tp.getY() < 15))
{
m = millis();
if((m - prevMillis) > 800) //change only every 800ms
{
prevMillis = m;
led += 10;
if(led > 100)
{
led = 10;
}
lcd.led(led);
}
}
//calibrate touch panel
else if((tp.getX() > (lcd.getWidth()-30)) && (tp.getY() < 15))
{
tp_calibration(0);
}
//show backlight power and cal text
lcd.drawText( 2, 2, "BL ", 1, COLOR_RED, COLOR_WHITE);
lcd.drawText(20, 2, led, 1, COLOR_RED, COLOR_WHITE);
lcd.drawText(lcd.getWidth()-30, 2, "CAL", 1, COLOR_RED, COLOR_WHITE);
}
else
{
last_x = 0;
}
}
TouchLib.h
/* Arduino touch library v0.1
* Copyright (C) 2011 Manfred Fuchs
* www.mafu-foto.de
* manfred@fuchsrudel.de
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef TouchLib_h
#define TouchLib_h
/**
* max length of button caption
*/
#define TOUCH_MAX_BUTTON_CAPTION_LEN 32
/**
* size of button shadow
*/
#define BUTTON_SHADOW 2
/**
* color macros
*/
#define RGB_R(rgb) ((rgb>>8)&0x00F8)
#define RGB_G(rgb) ((rgb>>3)&0x00FC)
#define RGB_B(rgb) ((rgb<<3)&0x00F8)
/**
* define pointer type for LCD and panel
*/
typedef MI0283QT2 *lcdPtr;
typedef ADS7846 *tpPtr;
/**
* button format struct
*/
typedef struct
{
void *next;
uint16_t left;
uint16_t top;
uint16_t width;
uint16_t height;
uint16_t col_normal;
uint16_t col_down;
uint16_t col_text;
uint8_t size;
uint8_t group;
bool round;
bool toggle;
bool down;
char caption[TOUCH_MAX_BUTTON_CAPTION_LEN+1];
} TouchButton;
typedef TouchButton *TouchButtonPtr;
class Touch
{
private:
lcdPtr lcd;
tpPtr tp;
uint8_t pressure;
TouchButtonPtr buttons;
TouchButtonPtr startBtn;
TouchButtonPtr lastBtn;
bool pressed;
uint16_t startX;
uint16_t startY;
uint16_t posX;
uint16_t posY;
void buttonDraw(TouchButtonPtr _button, bool _down);
void buttonDrawRect(uint16_t _l, uint16_t _t, uint16_t _w, uint16_t _h, uint16_t _c);
void buttonDrawRound(uint16_t _l, uint16_t _t, uint16_t _w, uint16_t _h, uint16_t _c);
TouchButtonPtr buttonFromPos(uint16_t _x, uint16_t _y);
void buttonState(TouchButtonPtr _current);
bool checkPressure(bool _pressed);
public:
Touch(lcdPtr _lcd, tpPtr _tp, uint8_t _pressure);
TouchButtonPtr buttonAdd(uint16_t _left, uint16_t _top, uint16_t _width, uint16_t _height, uint16_t _col_normal, uint16_t _col_down, uint16_t _col_text, bool _round, bool _toggle, uint8_t _group, uint8_t _size, char *_caption);
bool buttonDown(TouchButtonPtr _button);
void buttonSetDown(TouchButtonPtr _button, bool _down);
void buttonsClear();
void buttonsDraw();
TouchButtonPtr check();
};
#endif
...