A basic graphics package for the LPC4088 Display Module.

Dependents:   lpc4088_displaymodule_demo_sphere sampleGUI sampleEmptyGUI lpc4088_displaymodule_fs_aid ... more

Fork of DMBasicGUI by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Mon Nov 04 14:31:50 2019 +0000
Revision:
22:f0d00f29bfeb
Parent:
21:0038059e3a8f
More updates related to mbed OS 5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:4977187e90c7 1 /*
embeddedartists 0:4977187e90c7 2 * Copyright 2014 Embedded Artists AB
embeddedartists 0:4977187e90c7 3 *
embeddedartists 0:4977187e90c7 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:4977187e90c7 5 * you may not use this file except in compliance with the License.
embeddedartists 0:4977187e90c7 6 * You may obtain a copy of the License at
embeddedartists 0:4977187e90c7 7 *
embeddedartists 0:4977187e90c7 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:4977187e90c7 9 *
embeddedartists 0:4977187e90c7 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:4977187e90c7 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:4977187e90c7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:4977187e90c7 13 * See the License for the specific language governing permissions and
embeddedartists 0:4977187e90c7 14 * limitations under the License.
embeddedartists 0:4977187e90c7 15 */
embeddedartists 0:4977187e90c7 16
embeddedartists 0:4977187e90c7 17
embeddedartists 0:4977187e90c7 18 #include "mbed.h"
embeddedartists 0:4977187e90c7 19 #include "EthernetInterface.h"
embeddedartists 0:4977187e90c7 20 #include "AppColorPicker.h"
embeddedartists 0:4977187e90c7 21 #include "lpc_swim_font.h"
embeddedartists 17:6e2abf107800 22
embeddedartists 0:4977187e90c7 23
embeddedartists 0:4977187e90c7 24 /******************************************************************************
embeddedartists 0:4977187e90c7 25 * Defines and typedefs
embeddedartists 0:4977187e90c7 26 *****************************************************************************/
embeddedartists 0:4977187e90c7 27
embeddedartists 17:6e2abf107800 28 #define BOX_SIDE 192
embeddedartists 0:4977187e90c7 29
embeddedartists 0:4977187e90c7 30 #define BTN_OFF 20
embeddedartists 0:4977187e90c7 31
embeddedartists 0:4977187e90c7 32 /******************************************************************************
embeddedartists 0:4977187e90c7 33 * Global variables
embeddedartists 0:4977187e90c7 34 *****************************************************************************/
embeddedartists 0:4977187e90c7 35
embeddedartists 0:4977187e90c7 36 /******************************************************************************
embeddedartists 0:4977187e90c7 37 * Private functions
embeddedartists 0:4977187e90c7 38 *****************************************************************************/
embeddedartists 0:4977187e90c7 39
embeddedartists 0:4977187e90c7 40 static void buttonClicked(uint32_t x)
embeddedartists 0:4977187e90c7 41 {
embeddedartists 0:4977187e90c7 42 bool* done = (bool*)x;
embeddedartists 0:4977187e90c7 43 *done = true;
embeddedartists 0:4977187e90c7 44 }
embeddedartists 0:4977187e90c7 45
embeddedartists 0:4977187e90c7 46 void AppColorPicker::draw()
embeddedartists 0:4977187e90c7 47 {
embeddedartists 0:4977187e90c7 48 // Prepare fullscreen
embeddedartists 0:4977187e90c7 49 swim_window_open(_win,
embeddedartists 0:4977187e90c7 50 _disp->width(), _disp->height(), // full size
embeddedartists 11:265884fa7fdd 51 _fb,
embeddedartists 0:4977187e90c7 52 0,0,_disp->width()-1, _disp->height()-1, // window position and size
embeddedartists 0:4977187e90c7 53 1, // border
embeddedartists 0:4977187e90c7 54 WHITE, WHITE, BLACK); // colors: pen, backgr, forgr
embeddedartists 0:4977187e90c7 55 swim_set_title(_win, "Color Picker", BLACK);
embeddedartists 0:4977187e90c7 56
embeddedartists 0:4977187e90c7 57 swim_window_open(_colorwin,
embeddedartists 0:4977187e90c7 58 _disp->width(), _disp->height(), // full size
embeddedartists 11:265884fa7fdd 59 _fb,
embeddedartists 0:4977187e90c7 60 50,(_disp->height()-BOX_SIDE)/2,50+BOX_SIDE-1, BOX_SIDE+(_disp->height()-BOX_SIDE)/2, // window position and size
embeddedartists 0:4977187e90c7 61 0, // border
embeddedartists 0:4977187e90c7 62 WHITE, WHITE, BLACK); // colors: pen, backgr, forgr
embeddedartists 0:4977187e90c7 63
embeddedartists 0:4977187e90c7 64
embeddedartists 0:4977187e90c7 65 uint16_t r, g, b;
embeddedartists 0:4977187e90c7 66 uint16_t rx = BOX_SIDE/32;
embeddedartists 0:4977187e90c7 67 uint16_t bx = BOX_SIDE/32;
embeddedartists 0:4977187e90c7 68 uint16_t gy = BOX_SIDE/64;
embeddedartists 0:4977187e90c7 69 uint16_t color;
embeddedartists 0:4977187e90c7 70 for (int x = 0; x < BOX_SIDE; x++) {
embeddedartists 0:4977187e90c7 71 r = (x/rx);
embeddedartists 0:4977187e90c7 72 b = 0x1f - (x/bx);
embeddedartists 0:4977187e90c7 73 color = ((r & 0x1f) << 11) | ((b & 0x1f) << 0);
embeddedartists 0:4977187e90c7 74 for (int y = 0; y < BOX_SIDE; y++) {
embeddedartists 0:4977187e90c7 75 g = (y/gy);
embeddedartists 0:4977187e90c7 76 _colorwin->pen = color | ((g & 0x3f) << 5);
embeddedartists 0:4977187e90c7 77 swim_put_pixel(_colorwin, x, y);
embeddedartists 0:4977187e90c7 78 }
embeddedartists 0:4977187e90c7 79 }
embeddedartists 0:4977187e90c7 80
embeddedartists 17:6e2abf107800 81 _btn = new ImageButton(_win->fb, _win->xpmax - BTN_OFF - _resOk->width(), _win->ypmax - BTN_OFF - _resOk->height(), _resOk->width(), _resOk->height());
embeddedartists 17:6e2abf107800 82 _btn->loadImages(_resOk);
embeddedartists 0:4977187e90c7 83 _btn->draw();
embeddedartists 11:265884fa7fdd 84
embeddedartists 11:265884fa7fdd 85 // Copy everything onto the back buffer
embeddedartists 11:265884fa7fdd 86 memcpy(_fb2, _fb, _disp->fbSize());
embeddedartists 0:4977187e90c7 87 }
embeddedartists 0:4977187e90c7 88
embeddedartists 0:4977187e90c7 89 /******************************************************************************
embeddedartists 0:4977187e90c7 90 * Public functions
embeddedartists 0:4977187e90c7 91 *****************************************************************************/
embeddedartists 0:4977187e90c7 92
embeddedartists 11:265884fa7fdd 93 AppColorPicker::AppColorPicker() : _disp(NULL), _win(NULL), _colorwin(NULL),
embeddedartists 17:6e2abf107800 94 _fb(NULL), _fb2(NULL), _btn(NULL), _resOk(NULL)
embeddedartists 0:4977187e90c7 95 {
embeddedartists 17:6e2abf107800 96 if (DMBoard::instance().display()->width() == 480) {
embeddedartists 17:6e2abf107800 97 _resultX = 350;
embeddedartists 17:6e2abf107800 98 _resultY = 70;
embeddedartists 17:6e2abf107800 99 _resultW = 80;
embeddedartists 17:6e2abf107800 100 _resultH = 80;
embeddedartists 17:6e2abf107800 101 } else {
embeddedartists 17:6e2abf107800 102 _resultW = 80*2;
embeddedartists 17:6e2abf107800 103 _resultH = 80*2;
embeddedartists 17:6e2abf107800 104 _resultX = 350;
embeddedartists 17:6e2abf107800 105 _resultY = (DMBoard::instance().display()->height() - (_resultH + 50))/2;
embeddedartists 17:6e2abf107800 106 }
embeddedartists 0:4977187e90c7 107 }
embeddedartists 0:4977187e90c7 108
embeddedartists 0:4977187e90c7 109 AppColorPicker::~AppColorPicker()
embeddedartists 0:4977187e90c7 110 {
embeddedartists 0:4977187e90c7 111 teardown();
embeddedartists 0:4977187e90c7 112 }
embeddedartists 0:4977187e90c7 113
embeddedartists 0:4977187e90c7 114 bool AppColorPicker::setup()
embeddedartists 0:4977187e90c7 115 {
embeddedartists 0:4977187e90c7 116 _disp = DMBoard::instance().display();
embeddedartists 0:4977187e90c7 117 _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
embeddedartists 0:4977187e90c7 118 _colorwin = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
embeddedartists 11:265884fa7fdd 119 _fb = (COLOR_T*)_disp->allocateFramebuffer();
embeddedartists 11:265884fa7fdd 120 _fb2 = (COLOR_T*)_disp->allocateFramebuffer();
embeddedartists 0:4977187e90c7 121
embeddedartists 11:265884fa7fdd 122 return (_win != NULL && _colorwin != NULL && _fb != NULL && _fb2 != NULL);
embeddedartists 0:4977187e90c7 123 }
embeddedartists 0:4977187e90c7 124
embeddedartists 0:4977187e90c7 125 void AppColorPicker::runToCompletion()
embeddedartists 0:4977187e90c7 126 {
embeddedartists 0:4977187e90c7 127 bool done = false;
embeddedartists 0:4977187e90c7 128 draw();
embeddedartists 0:4977187e90c7 129 _btn->setAction(buttonClicked, (uint32_t)&done);
embeddedartists 11:265884fa7fdd 130 void* oldFB = _disp->swapFramebuffer(_fb2);
embeddedartists 0:4977187e90c7 131
embeddedartists 0:4977187e90c7 132 // Wait for touches
embeddedartists 0:4977187e90c7 133 TouchPanel* touch = DMBoard::instance().touchPanel();
embeddedartists 8:19a6b70d42b1 134 touch_coordinate_t coord;
embeddedartists 0:4977187e90c7 135 char buf[10];
embeddedartists 0:4977187e90c7 136 swim_set_pen_color(_win, BLACK);
embeddedartists 11:265884fa7fdd 137 bool showingFB2 = true;
embeddedartists 11:265884fa7fdd 138 COLOR_T lastColor = WHITE;
embeddedartists 0:4977187e90c7 139 while(!done) {
embeddedartists 8:19a6b70d42b1 140 // wait for a new touch signal (signal is sent from AppLauncher,
embeddedartists 8:19a6b70d42b1 141 // which listens for touch events)
embeddedartists 21:0038059e3a8f 142 ThisThread::flags_wait_any(0x1);
embeddedartists 3:3fabfe3339b8 143 if (touch->read(coord) == TouchPanel::TouchError_Ok) {
embeddedartists 3:3fabfe3339b8 144 if (coord.z > 0 &&
embeddedartists 3:3fabfe3339b8 145 coord.x >= _colorwin->xpmin && coord.x <= _colorwin->xpmax &&
embeddedartists 3:3fabfe3339b8 146 coord.y >= _colorwin->ypmin && coord.y <= _colorwin->ypmax) {
embeddedartists 3:3fabfe3339b8 147 int x = coord.x - _colorwin->xpmin;
embeddedartists 3:3fabfe3339b8 148 int y = coord.y - _colorwin->ypmin;
embeddedartists 3:3fabfe3339b8 149 COLOR_T c = ((x/(BOX_SIDE/32))<<11) | ((y/(BOX_SIDE/64))<<5) | ((0x1f-(x/(BOX_SIDE/32)))<<0);
embeddedartists 11:265884fa7fdd 150 if (c != lastColor) {
embeddedartists 3:3fabfe3339b8 151 swim_set_fill_color(_win, c);
embeddedartists 17:6e2abf107800 152 swim_put_box(_win, _resultX, _resultY, _resultX+_resultW, _resultY+_resultH);
embeddedartists 3:3fabfe3339b8 153 sprintf(buf, "0x%04x ", c);
embeddedartists 17:6e2abf107800 154 swim_put_text_xy(_win, buf, _resultX, _resultY+_resultH+10);
embeddedartists 11:265884fa7fdd 155
embeddedartists 11:265884fa7fdd 156 // Swap what is shown and what is drawn on
embeddedartists 11:265884fa7fdd 157 if (showingFB2) {
embeddedartists 11:265884fa7fdd 158 _disp->setFramebuffer(_fb);
embeddedartists 11:265884fa7fdd 159 _win->fb = _fb2;
embeddedartists 11:265884fa7fdd 160 } else {
embeddedartists 11:265884fa7fdd 161 _disp->setFramebuffer(_fb2);
embeddedartists 11:265884fa7fdd 162 _win->fb = _fb;
embeddedartists 11:265884fa7fdd 163 }
embeddedartists 21:0038059e3a8f 164 ThisThread::sleep_for(20);
embeddedartists 11:265884fa7fdd 165 showingFB2 = !showingFB2;
embeddedartists 11:265884fa7fdd 166 lastColor = c;
embeddedartists 11:265884fa7fdd 167 }
embeddedartists 3:3fabfe3339b8 168 }
embeddedartists 3:3fabfe3339b8 169 if (_btn->handle(coord.x, coord.y, coord.z > 0)) {
embeddedartists 11:265884fa7fdd 170 _btn->draw(showingFB2 ? _fb2 : _fb);
embeddedartists 3:3fabfe3339b8 171 }
embeddedartists 0:4977187e90c7 172 }
embeddedartists 0:4977187e90c7 173 }
embeddedartists 0:4977187e90c7 174
embeddedartists 0:4977187e90c7 175 // User has clicked the button, restore the original FB
embeddedartists 0:4977187e90c7 176 _disp->swapFramebuffer(oldFB);
embeddedartists 0:4977187e90c7 177 swim_window_close(_win);
embeddedartists 0:4977187e90c7 178 swim_window_close(_colorwin);
embeddedartists 0:4977187e90c7 179 }
embeddedartists 0:4977187e90c7 180
embeddedartists 0:4977187e90c7 181 bool AppColorPicker::teardown()
embeddedartists 0:4977187e90c7 182 {
embeddedartists 0:4977187e90c7 183 if (_win != NULL) {
embeddedartists 0:4977187e90c7 184 free(_win);
embeddedartists 0:4977187e90c7 185 _win = NULL;
embeddedartists 0:4977187e90c7 186 }
embeddedartists 0:4977187e90c7 187 if (_colorwin != NULL) {
embeddedartists 0:4977187e90c7 188 free(_colorwin);
embeddedartists 0:4977187e90c7 189 _colorwin = NULL;
embeddedartists 0:4977187e90c7 190 }
embeddedartists 0:4977187e90c7 191 if (_fb != NULL) {
embeddedartists 0:4977187e90c7 192 free(_fb);
embeddedartists 0:4977187e90c7 193 _fb = NULL;
embeddedartists 0:4977187e90c7 194 }
embeddedartists 11:265884fa7fdd 195 if (_fb2 != NULL) {
embeddedartists 11:265884fa7fdd 196 free(_fb2);
embeddedartists 11:265884fa7fdd 197 _fb2 = NULL;
embeddedartists 11:265884fa7fdd 198 }
embeddedartists 0:4977187e90c7 199 if (_btn != NULL) {
embeddedartists 0:4977187e90c7 200 delete _btn;
embeddedartists 0:4977187e90c7 201 _btn = NULL;
embeddedartists 0:4977187e90c7 202 }
embeddedartists 0:4977187e90c7 203 return true;
embeddedartists 0:4977187e90c7 204 }
embeddedartists 0:4977187e90c7 205
embeddedartists 17:6e2abf107800 206 void AppColorPicker::addResource(Resources id, Resource* res)
embeddedartists 17:6e2abf107800 207 {
embeddedartists 17:6e2abf107800 208 _resOk = res;
embeddedartists 17:6e2abf107800 209 }