Simple program to demo using the SimpleGUI with TouchScreen. Widgets demonstrate handling of single- and double-tap events, as well as touchMover
Dependencies: SimpleGUI SimpleGUITouchScreen TouchScreen UniGraphic mbed-rtos mbed
Diff: main.cpp
- Revision:
- 0:402fb0c36182
- Child:
- 1:ece0ecedf40a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Mar 25 16:49:45 2016 +0000 @@ -0,0 +1,126 @@ +#include "main.h" + + +unsigned short backgroundcolor=Black; +unsigned short foregroundcolor=White; + +ILI932x* mySCREEN; +TouchScreen* touchScreen; +TouchScreenEventSource* eventSource; +EventDispatcher* eventDispatcher; +TextWidget* widget1; +TextWidget* widget2; +TextWidget* widget3; + +Serial *pc; + +volatile bool done; +volatile int xt, yt; +volatile int xmin = 9999; +volatile int xmax = 0; +volatile int ymin = 9999; +volatile int ymax = 0; + + +void touchStart(TouchPosition p) { + eventSource->touchStartHandler(p); +} + +void touchMove(TouchPosition p) { + eventSource->touchMoveHandler(p); +} + +void touchEnd(TouchPosition p) { + eventSource->touchEndHandler(p); +} + +void tapHandler1(Event e, EventListener* target) { + TextWidget* w = (TextWidget*) target; + if(done) { + w->setBackground(Green); + } else { + w->setBackground(Blue); + } + done = !done; + w->draw(); +} +void tapHandler2(Event e, EventListener* target) { + TextWidget* w = (TextWidget*) target; + if(done) { + w->setBackground(Green); + } else { + w->setBackground(Red); + } + done = !done; + w->draw(); +} + +void dragHandler(Event e, EventListener* target) { + TextWidget* w = (TextWidget*) target; + w->setLocation(e.screenX, e.screenY); + w->draw(); +} + +int main() +{ + pc = new Serial(USBTX, USBRX); + pc->baud (9600); + pc->printf("\n\nSystem Core Clock = %.3f MHZ\r\n",(float)SystemCoreClock/1000000); + + mySCREEN = new ILI932x(PAR_8, PortC, LCD_CS, LCD_RESET, LCD_RS, LCD_WR, LCD_RD,"myLCD"); + + pc->printf("Screen id %d\r\n", mySCREEN->tftID); + + mySCREEN->set_orientation(0); + mySCREEN->cls(); // clear the screen + mySCREEN->locate(0,0); + mySCREEN->background(backgroundcolor); // set background to black + mySCREEN->foreground(foregroundcolor); // set chars to white + mySCREEN->set_font((unsigned char*) Arial24x23); + + touchScreen = new TouchScreen(T_DIN, T_DOUT, T_CLK, T_CS, T_IRQ); + touchScreen->setTouchStartHandler(&touchStart); + touchScreen->setTouchMoveHandler(&touchMove); + touchScreen->setTouchEndHandler(&touchEnd); + touchScreen->setLCDGeometry(LCD_X_RES, LCD_Y_RES, TOUCHSCREEN_ORIENTATION_PORTRAIT | TOUCHSCREEN_ORIENTATION_ROTATED); + touchScreen->setCalibration(TOUCH_X_MIN, TOUCH_X_MAX, TOUCH_Y_MIN, TOUCH_Y_MAX); + + eventDispatcher = new EventDispatcher(); + + eventSource = new TouchScreenEventSource(touchScreen, eventDispatcher); + + widget1 = new TextWidget(mySCREEN); + widget1->setLocation(10,10); + widget1->setSize(200,30); + widget1->setText("Hello world I'm going to be clipped"); + widget1->setFont((unsigned char*) Arial24x23); + widget1->draw(); + + widget2 = new TextWidget(mySCREEN); + widget2->setLocation(10,100); + widget2->setSize(200,30); + widget2->setText("I am widget2"); + widget2->setFont((unsigned char*) Arial24x23); + widget2->draw(); + + widget3 = new TextWidget(mySCREEN); + widget3->setLocation(10,150); + widget3->setSize(200,30); + widget3->setText("DRAG ME!!"); + widget3->setFont((unsigned char*) Arial24x23); + widget3->draw(); + + eventDispatcher->attachListener(widget1); + widget1->setEventHandler(touchTapEvent, &tapHandler1); + + eventDispatcher->attachListener(widget2); + widget2->setEventHandler(touchDoubleTapEvent, &tapHandler2); + + eventDispatcher->attachListener(widget3); + widget3->setEventHandler(touchMoveEvent, &dragHandler); + + + while (1) { + Thread::wait(1000); + } +}