Example that shows how to use the ewgui wrapper classes in combination with Segger's emwin library.
Dependencies: EALib ewgui mbed
Revision 0:6b81fd4666bb, committed 2013-12-16
- Comitter:
- embeddedartists
- Date:
- Mon Dec 16 07:09:17 2013 +0000
- Child:
- 1:140502d3d3ed
- Commit message:
- First commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EALib.lib Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/embeddedartists/code/EALib/#fe3cb3fbb64e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EwGuiImpl.cpp Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,182 @@ +#include "EwGuiImpl.h" + +#include "LcdController.h" +#include "EaLcdBoard.h" +#include "sdram.h" +#include "AR1021.h" +#include "TSC2046.h" + + +#define PIXEL_SIZE (2) + +EwGuiImpl::EwGuiImpl() { + + EaLcdBoard lcdBoard(P0_27, P0_28); + EaLcdBoard::Result result; + LcdController::Config lcdCfg; + EaLcdBoard::TouchParams_t touchParams; + + do { + + result = lcdBoard.open(NULL, NULL); + if (result != EaLcdBoard::Ok) { + printf("Failed to open display: %d\n", result); + break; + } + + result = lcdBoard.getLcdConfig(&lcdCfg); + if (result != EaLcdBoard::Ok) { + printf("Failed to get LCD configuration: %d\n", result); + break; + } + + _width = lcdCfg.width; + _height = lcdCfg.height; + + // allocate buffer, width x height x 2 (2 bytes = 16 bit color data) + _fb = (uint32_t)malloc(_width*_height*PIXEL_SIZE); + if (_fb == 0) { + printf("Failed to allocate frame buffer\n"); + break; + } + + result = lcdBoard.setFrameBuffer(_fb); + if (result != EaLcdBoard::Ok) { + printf("Failed to activate frameBuffer: %d\n", result); + break; + } + + _memSz = 1024*1024*5; + _mem = (uint32_t)malloc(_memSz); + if (_mem == 0) { + printf("Failed to allocate memory block for emwin\n"); + break; + } + + + memset((void*)_fb, 0x0, _width*_height*PIXEL_SIZE); + + result = lcdBoard.getTouchParameters(&touchParams); + if (result != EaLcdBoard::Ok) { + printf("Failed to get touch panel parameters: %d\n", result); + break; + } + + // create touch panel + switch(touchParams.panelId) { + case EaLcdBoard::TouchPanel_AR1021: + printf("creating AR1021 touch panel\n"); + _touch = new AR1021(P2_27, P2_26, P2_22, P2_21, P2_25); + break; + + case EaLcdBoard::TouchPanel_TSC2046: + case EaLcdBoard::TouchPanelUnknown: + default: + // we always default to TSC2046 even if we cannot + // detect which panel is used + + printf("creating TSC2046 touch panel\n"); + + _touch = new TSC2046(P2_27, P2_26, P2_22, P2_21); + break; + + } + + if (!_touch->init(_width, _height)) { + printf("TouchPanel.init failed\n"); + break; + } + + init(); + + } while(0); + + +} + +void* EwGuiImpl::getMemoryBlockAddress() { + return (void*)_mem; +} + +uint32_t EwGuiImpl::getMemoryBlockSize() { + return _memSz; +} + +uint32_t EwGuiImpl::getDisplayWidth() { + return _width; +} + +uint32_t EwGuiImpl::getDisplayHeight() { + return _height; +} + +void* EwGuiImpl::getFrameBufferAddress() { + return (void*)_fb; +} + +void EwGuiImpl::getTouchValues(int32_t* x, int32_t* y, int32_t* z) { + + if (!x || !y || !z) return; + + TouchPanel::touchCoordinate_t coord; + _touch->read(coord); + + *x = coord.x; + *y = coord.y; + *z = coord.z; +} + +void EwGuiImpl::calibrate() { + + uint16_t x = 0; + uint16_t y = 0; + bool hasMorePoints = false; + + EwPainter painter; + painter.saveContext(); + + do { + + printf("Starting calibration\n"); + if (!_touch->calibrateStart()) { + printf("Failed to start calibration\n"); + break; + } + + do { + if (!_touch->getNextCalibratePoint(&x, &y)) { + printf("Failed to get next calibrate point\n"); + break; + } + + printf("calib: x=%d, y=%d\n", x, y); + drawCalibPoint(painter, x, y); + + if (!_touch->waitForCalibratePoint(&hasMorePoints, 0)) { + printf("Failed waiting for calibration point\n"); + break; + } + + } while(hasMorePoints); + + printf("Calibration done\n"); + + + } while (0); + + + painter.clear(); + painter.restoreContext(); +} + +void EwGuiImpl::drawCalibPoint(EwPainter &painter, int32_t x, int32_t y) { + + painter.clear(); + painter.setColor(EW_WHITE); + painter.drawStringHorizCenter("Touch circle to calibrate", + getDisplayWidth()/2, getDisplayHeight()/2); + + painter.fillCircle(x, y, 10); + painter.setColor(EW_BLACK); + painter.fillCircle(x, y, 5); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EwGuiImpl.h Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,41 @@ + +#ifndef EWGUIIMPL_H +#define EWGUIIMPL_H + +#include "mbed.h" +#include "EwGui.h" +#include "EwPainter.h" +#include "TouchPanel.h" + +class EwGuiImpl : public EwGui { +public: + + + EwGuiImpl(); + + virtual void* getMemoryBlockAddress(); + virtual uint32_t getMemoryBlockSize(); + + virtual uint32_t getDisplayWidth(); + virtual uint32_t getDisplayHeight(); + + virtual void* getFrameBufferAddress(); + + virtual void getTouchValues(int32_t* x, int32_t* y, int32_t* z); + + void calibrate(); + + +private: + + uint32_t _width; + uint32_t _height; + uint32_t _fb; + uint32_t _mem; + uint32_t _memSz; + TouchPanel* _touch; + + void drawCalibPoint(EwPainter &painter, int32_t x, int32_t y); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MyWindow.cpp Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,96 @@ +#include "mbed.h" + +#include "MyWindow.h" +#include "EwPainter.h" + +MyWindow::MyWindow(EwWindow* parent) : + EwWindow(5, 2, 300, 250, parent), + _changeBtn(130, 200, 40, 20, this), + _checkBox(0, 0, 80, 25, this), + _dropDownLst(0, 0, 100, 50, this){ + + _changeBtn.setText("Click"); + _changeBtn.setClickedListener(this, &MyWindow::clickListener); + + _checkBox.setText("CheckBox"); + _checkBox.setChangedListener(this, &MyWindow::checkedListener); + + _dropDownLst.addString("First"); + _dropDownLst.addString("Second"); + _dropDownLst.addString("Third"); + + _clickCnt = 0; + _pressed = false; + _pressX = 0; + _pressY = 0; + + resizeTo(parent->getWidth()-15, parent->getHeight()-35); + _changeBtn.moveTo(getWidth()/2-_changeBtn.getWidth()/2, + getHeight()-_changeBtn.getHeight()-5); + + _checkBox.moveTo(5, 35); + + _dropDownLst.move(getWidth()-_dropDownLst.getWidth()-5, 5); +} + +bool MyWindow::paintEvent() { + char buf[30]; + EwPainter painter; + + painter.setBackgroundColor(EW_LIGHTGRAY); + painter.clear(); + + if (_clickCnt > 0) { + painter.setColor(EW_BLUE); + if (_clickCnt == 1) { + sprintf(buf, "Clicked %d time", _clickCnt); + } + else { + sprintf(buf, "Clicked %d times", _clickCnt); + } + painter.drawStringClearEOL(buf, 5, 5); + } + + if (_pressed) { + painter.setColor(GUI_DARKRED); + sprintf(buf, "Touch at %d,%d", _pressX, _pressY); + painter.drawStringClearEOL(buf, 5, 15); + } + + return true; +} + +bool MyWindow::touchEvent(int x, int y, EwTouchState_t state) { + + if (state == TouchStatePressed) { + _pressed = true; + _pressX = x; + _pressY = y; + } + else { + _pressed = false; + } + + invalidate(); + + return true; +} + +void MyWindow::clickListener(EwWindow* w) { + _clickCnt++; + invalidate(); +} + +void MyWindow::checkedListener(EwWindow* w) { + EwCheckBox* b = (EwCheckBox*)w; + + ewCheckBoxState_t state = b->getState(); + + if (state == CheckBoxStateUnchecked) { + b->setText("Unchecked"); + } + else { + b->setText("Checked"); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MyWindow.h Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,36 @@ + +#ifndef MYWINDOW_H +#define MYWINDOW_H + +#include "EwWindow.h" +#include "EwButton.h" +#include "EwCheckBox.h" +#include "EwDropDown.h" + +class MyWindow : public EwWindow { +public: + + MyWindow(EwWindow* parent); + + virtual bool paintEvent(); + virtual bool touchEvent(int x, int y, EwTouchState_t state); + + virtual bool resizedEvent() {printf("MyWindow resized\n"); return true;} + + +private: + + int _clickCnt; + bool _pressed; + int _pressX; + int _pressY; + EwButton _changeBtn; + EwCheckBox _checkBox; + EwDropDown _dropDownLst; + + + void clickListener(EwWindow* w); + void checkedListener(EwWindow* w); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ewgui.lib Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/embeddedartists/code/ewgui/#316c181e9b65
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,90 @@ + +/****************************************************************************** + * Includes + *****************************************************************************/ + +#include "mbed.h" + +#include "sdram.h" + +#include "EwGuiImpl.h" +#include "EwFrameWindow.h" +#include "MyWindow.h" + + +/****************************************************************************** + * Typedefs and defines + *****************************************************************************/ + + +/****************************************************************************** + * Local variables + *****************************************************************************/ + +static EwGuiImpl* gui = NULL; + +/****************************************************************************** + * Local functions + *****************************************************************************/ + +static void pollTouch() { + if (gui == NULL) return; + + gui->execTouch(); +} + +/****************************************************************************** + * Main function + *****************************************************************************/ + +int main (void) { + Ticker ticker; + + // Frame buffer is put in SDRAM + if (sdram_init() == 1) { + printf("Failed to initialize SDRAM\n"); + return 1; + } + + // instantiate and initialize GUI + gui = new EwGuiImpl(); + if (gui == NULL) { + printf("Failed to instantiate EwGuiImpl\n"); + return 1; + } + + // calibrate touch screen + gui->calibrate(); + + // register a callback that will check for touch events + ticker.attach(&pollTouch, 0.10); + + // showing how to enable skinning + BUTTON_SetDefaultSkin (BUTTON_SKIN_FLEX); + CHECKBOX_SetDefaultSkin (CHECKBOX_SKIN_FLEX); + DROPDOWN_SetDefaultSkin (DROPDOWN_SKIN_FLEX); + FRAMEWIN_SetDefaultSkin (FRAMEWIN_SKIN_FLEX); + + // make sure the desktop has a color so that it is redrawn + EwWindow::setDesktopColor(EW_BLACK); + + // create a frame window + EwFrameWindow frameWin(30, 30, gui->getDisplayWidth()-60, + gui->getDisplayHeight()-60, "Window Title"); + frameWin.setTitleHeight(20); + + // create and associate a custom window with the frame window + MyWindow myWindow(&frameWin); + + // application loop running the UI and checking for touch events + while(1) { + + // In this example touch events are checked by interrupt + // (pollTouch function), but we could also have done it + // in this while loop + //gui.execTouch(); + gui->exec(); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Dec 16 07:09:17 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/a842253909c9 \ No newline at end of file