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
Application/AppColorPicker.cpp@8:19a6b70d42b1, 2015-01-17 (annotated)
- Committer:
- embeddedartists
- Date:
- Sat Jan 17 14:07:25 2015 +0100
- Revision:
- 8:19a6b70d42b1
- Parent:
- 3:3fabfe3339b8
- Child:
- 11:265884fa7fdd
- Updated apps to wait for signal before reading touch event. This eliminates polling and waits
- Redesigned the AppLauncher to listen accept touch events and turn them into signals
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:4977187e90c7 | 22 | |
embeddedartists | 0:4977187e90c7 | 23 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 24 | * Defines and typedefs |
embeddedartists | 0:4977187e90c7 | 25 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 26 | |
embeddedartists | 0:4977187e90c7 | 27 | #define BOX_SIDE 192 //256 |
embeddedartists | 0:4977187e90c7 | 28 | |
embeddedartists | 0:4977187e90c7 | 29 | #define BTN_WIDTH 65 |
embeddedartists | 0:4977187e90c7 | 30 | #define BTN_HEIGHT 40 |
embeddedartists | 0:4977187e90c7 | 31 | #define BTN_OFF 20 |
embeddedartists | 0:4977187e90c7 | 32 | |
embeddedartists | 0:4977187e90c7 | 33 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 34 | * Global variables |
embeddedartists | 0:4977187e90c7 | 35 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 36 | |
embeddedartists | 0:4977187e90c7 | 37 | /****************************************************************************** |
embeddedartists | 0:4977187e90c7 | 38 | * Private functions |
embeddedartists | 0:4977187e90c7 | 39 | *****************************************************************************/ |
embeddedartists | 0:4977187e90c7 | 40 | |
embeddedartists | 0:4977187e90c7 | 41 | static void buttonClicked(uint32_t x) |
embeddedartists | 0:4977187e90c7 | 42 | { |
embeddedartists | 0:4977187e90c7 | 43 | bool* done = (bool*)x; |
embeddedartists | 0:4977187e90c7 | 44 | *done = true; |
embeddedartists | 0:4977187e90c7 | 45 | } |
embeddedartists | 0:4977187e90c7 | 46 | |
embeddedartists | 0:4977187e90c7 | 47 | void AppColorPicker::draw() |
embeddedartists | 0:4977187e90c7 | 48 | { |
embeddedartists | 0:4977187e90c7 | 49 | // Prepare fullscreen |
embeddedartists | 0:4977187e90c7 | 50 | swim_window_open(_win, |
embeddedartists | 0:4977187e90c7 | 51 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 52 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 53 | 0,0,_disp->width()-1, _disp->height()-1, // window position and size |
embeddedartists | 0:4977187e90c7 | 54 | 1, // border |
embeddedartists | 0:4977187e90c7 | 55 | WHITE, WHITE, BLACK); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 56 | swim_set_title(_win, "Color Picker", BLACK); |
embeddedartists | 0:4977187e90c7 | 57 | |
embeddedartists | 0:4977187e90c7 | 58 | swim_window_open(_colorwin, |
embeddedartists | 0:4977187e90c7 | 59 | _disp->width(), _disp->height(), // full size |
embeddedartists | 0:4977187e90c7 | 60 | (COLOR_T*)_fb, |
embeddedartists | 0:4977187e90c7 | 61 | 50,(_disp->height()-BOX_SIDE)/2,50+BOX_SIDE-1, BOX_SIDE+(_disp->height()-BOX_SIDE)/2, // window position and size |
embeddedartists | 0:4977187e90c7 | 62 | 0, // border |
embeddedartists | 0:4977187e90c7 | 63 | WHITE, WHITE, BLACK); // colors: pen, backgr, forgr |
embeddedartists | 0:4977187e90c7 | 64 | |
embeddedartists | 0:4977187e90c7 | 65 | |
embeddedartists | 0:4977187e90c7 | 66 | uint16_t r, g, b; |
embeddedartists | 0:4977187e90c7 | 67 | uint16_t rx = BOX_SIDE/32; |
embeddedartists | 0:4977187e90c7 | 68 | //uint16_t gx = BOX_SIDE/64; |
embeddedartists | 0:4977187e90c7 | 69 | uint16_t bx = BOX_SIDE/32; |
embeddedartists | 0:4977187e90c7 | 70 | //uint16_t ry = BOX_SIDE/32; |
embeddedartists | 0:4977187e90c7 | 71 | uint16_t gy = BOX_SIDE/64; |
embeddedartists | 0:4977187e90c7 | 72 | //uint16_t by = BOX_SIDE/32; |
embeddedartists | 0:4977187e90c7 | 73 | uint16_t color; |
embeddedartists | 0:4977187e90c7 | 74 | for (int x = 0; x < BOX_SIDE; x++) { |
embeddedartists | 0:4977187e90c7 | 75 | r = (x/rx); |
embeddedartists | 0:4977187e90c7 | 76 | b = 0x1f - (x/bx); |
embeddedartists | 0:4977187e90c7 | 77 | color = ((r & 0x1f) << 11) | ((b & 0x1f) << 0); |
embeddedartists | 0:4977187e90c7 | 78 | for (int y = 0; y < BOX_SIDE; y++) { |
embeddedartists | 0:4977187e90c7 | 79 | g = (y/gy); |
embeddedartists | 0:4977187e90c7 | 80 | _colorwin->pen = color | ((g & 0x3f) << 5); |
embeddedartists | 0:4977187e90c7 | 81 | swim_put_pixel(_colorwin, x, y); |
embeddedartists | 0:4977187e90c7 | 82 | } |
embeddedartists | 0:4977187e90c7 | 83 | } |
embeddedartists | 0:4977187e90c7 | 84 | |
embeddedartists | 0:4977187e90c7 | 85 | _btn = new Button("Done", _win->fb, _win->xpmax - BTN_OFF - BTN_WIDTH, _win->ypmax - BTN_OFF - BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT); |
embeddedartists | 0:4977187e90c7 | 86 | _btn->draw(); |
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 | 0:4977187e90c7 | 93 | AppColorPicker::AppColorPicker() : _disp(NULL), _win(NULL), _colorwin(NULL), _fb(NULL), _btn(NULL) |
embeddedartists | 0:4977187e90c7 | 94 | { |
embeddedartists | 0:4977187e90c7 | 95 | } |
embeddedartists | 0:4977187e90c7 | 96 | |
embeddedartists | 0:4977187e90c7 | 97 | AppColorPicker::~AppColorPicker() |
embeddedartists | 0:4977187e90c7 | 98 | { |
embeddedartists | 0:4977187e90c7 | 99 | teardown(); |
embeddedartists | 0:4977187e90c7 | 100 | } |
embeddedartists | 0:4977187e90c7 | 101 | |
embeddedartists | 0:4977187e90c7 | 102 | bool AppColorPicker::setup() |
embeddedartists | 0:4977187e90c7 | 103 | { |
embeddedartists | 0:4977187e90c7 | 104 | _disp = DMBoard::instance().display(); |
embeddedartists | 0:4977187e90c7 | 105 | _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
embeddedartists | 0:4977187e90c7 | 106 | _colorwin = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T)); |
embeddedartists | 0:4977187e90c7 | 107 | _fb = _disp->allocateFramebuffer(); |
embeddedartists | 0:4977187e90c7 | 108 | |
embeddedartists | 0:4977187e90c7 | 109 | return (_win != NULL && _colorwin != NULL && _fb != NULL); |
embeddedartists | 0:4977187e90c7 | 110 | } |
embeddedartists | 0:4977187e90c7 | 111 | |
embeddedartists | 0:4977187e90c7 | 112 | void AppColorPicker::runToCompletion() |
embeddedartists | 0:4977187e90c7 | 113 | { |
embeddedartists | 0:4977187e90c7 | 114 | // Alternative 1: use the calling thread's context to run in |
embeddedartists | 0:4977187e90c7 | 115 | bool done = false; |
embeddedartists | 0:4977187e90c7 | 116 | draw(); |
embeddedartists | 0:4977187e90c7 | 117 | _btn->setAction(buttonClicked, (uint32_t)&done); |
embeddedartists | 0:4977187e90c7 | 118 | void* oldFB = _disp->swapFramebuffer(_fb); |
embeddedartists | 0:4977187e90c7 | 119 | |
embeddedartists | 0:4977187e90c7 | 120 | // Wait for touches |
embeddedartists | 0:4977187e90c7 | 121 | TouchPanel* touch = DMBoard::instance().touchPanel(); |
embeddedartists | 8:19a6b70d42b1 | 122 | touch_coordinate_t coord; |
embeddedartists | 0:4977187e90c7 | 123 | char buf[10]; |
embeddedartists | 0:4977187e90c7 | 124 | swim_set_pen_color(_win, BLACK); |
embeddedartists | 0:4977187e90c7 | 125 | while(!done) { |
embeddedartists | 8:19a6b70d42b1 | 126 | // wait for a new touch signal (signal is sent from AppLauncher, |
embeddedartists | 8:19a6b70d42b1 | 127 | // which listens for touch events) |
embeddedartists | 8:19a6b70d42b1 | 128 | Thread::signal_wait(0x1); |
embeddedartists | 3:3fabfe3339b8 | 129 | if (touch->read(coord) == TouchPanel::TouchError_Ok) { |
embeddedartists | 3:3fabfe3339b8 | 130 | if (coord.z > 0 && |
embeddedartists | 3:3fabfe3339b8 | 131 | coord.x >= _colorwin->xpmin && coord.x <= _colorwin->xpmax && |
embeddedartists | 3:3fabfe3339b8 | 132 | coord.y >= _colorwin->ypmin && coord.y <= _colorwin->ypmax) { |
embeddedartists | 3:3fabfe3339b8 | 133 | int x = coord.x - _colorwin->xpmin; |
embeddedartists | 3:3fabfe3339b8 | 134 | int y = coord.y - _colorwin->ypmin; |
embeddedartists | 3:3fabfe3339b8 | 135 | COLOR_T c = ((x/(BOX_SIDE/32))<<11) | ((y/(BOX_SIDE/64))<<5) | ((0x1f-(x/(BOX_SIDE/32)))<<0); |
embeddedartists | 3:3fabfe3339b8 | 136 | swim_set_fill_color(_win, c); |
embeddedartists | 3:3fabfe3339b8 | 137 | swim_put_box(_win, 350, 70, 430, 150); |
embeddedartists | 3:3fabfe3339b8 | 138 | sprintf(buf, "0x%04x ", c); |
embeddedartists | 3:3fabfe3339b8 | 139 | swim_put_text_xy(_win, buf, 350, 160); |
embeddedartists | 3:3fabfe3339b8 | 140 | } |
embeddedartists | 3:3fabfe3339b8 | 141 | if (_btn->handle(coord.x, coord.y, coord.z > 0)) { |
embeddedartists | 3:3fabfe3339b8 | 142 | _btn->draw(); |
embeddedartists | 3:3fabfe3339b8 | 143 | } |
embeddedartists | 0:4977187e90c7 | 144 | } |
embeddedartists | 0:4977187e90c7 | 145 | } |
embeddedartists | 0:4977187e90c7 | 146 | |
embeddedartists | 0:4977187e90c7 | 147 | // User has clicked the button, restore the original FB |
embeddedartists | 0:4977187e90c7 | 148 | _disp->swapFramebuffer(oldFB); |
embeddedartists | 0:4977187e90c7 | 149 | swim_window_close(_win); |
embeddedartists | 0:4977187e90c7 | 150 | swim_window_close(_colorwin); |
embeddedartists | 0:4977187e90c7 | 151 | } |
embeddedartists | 0:4977187e90c7 | 152 | |
embeddedartists | 0:4977187e90c7 | 153 | bool AppColorPicker::teardown() |
embeddedartists | 0:4977187e90c7 | 154 | { |
embeddedartists | 0:4977187e90c7 | 155 | if (_win != NULL) { |
embeddedartists | 0:4977187e90c7 | 156 | free(_win); |
embeddedartists | 0:4977187e90c7 | 157 | _win = NULL; |
embeddedartists | 0:4977187e90c7 | 158 | } |
embeddedartists | 0:4977187e90c7 | 159 | if (_colorwin != NULL) { |
embeddedartists | 0:4977187e90c7 | 160 | free(_colorwin); |
embeddedartists | 0:4977187e90c7 | 161 | _colorwin = NULL; |
embeddedartists | 0:4977187e90c7 | 162 | } |
embeddedartists | 0:4977187e90c7 | 163 | if (_fb != NULL) { |
embeddedartists | 0:4977187e90c7 | 164 | free(_fb); |
embeddedartists | 0:4977187e90c7 | 165 | _fb = NULL; |
embeddedartists | 0:4977187e90c7 | 166 | } |
embeddedartists | 0:4977187e90c7 | 167 | if (_btn != NULL) { |
embeddedartists | 0:4977187e90c7 | 168 | delete _btn; |
embeddedartists | 0:4977187e90c7 | 169 | _btn = NULL; |
embeddedartists | 0:4977187e90c7 | 170 | } |
embeddedartists | 0:4977187e90c7 | 171 | return true; |
embeddedartists | 0:4977187e90c7 | 172 | } |
embeddedartists | 0:4977187e90c7 | 173 | |
embeddedartists | 0:4977187e90c7 | 174 |