Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DMBasicGUI by
Application/AppColorPicker.cpp@0:4977187e90c7, 2014-12-11 (annotated)
- Committer:
- embeddedartists
- Date:
- Thu Dec 11 11:03:57 2014 +0000
- Revision:
- 0:4977187e90c7
- Child:
- 3:3fabfe3339b8
First version
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 | 0:4977187e90c7 | 122 | TouchPanel::touchCoordinate_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 | 0:4977187e90c7 | 126 | touch->read(coord); |
| embeddedartists | 0:4977187e90c7 | 127 | if (coord.z > 0 && |
| embeddedartists | 0:4977187e90c7 | 128 | coord.x >= _colorwin->xpmin && coord.x <= _colorwin->xpmax && |
| embeddedartists | 0:4977187e90c7 | 129 | coord.y >= _colorwin->ypmin && coord.y <= _colorwin->ypmax) { |
| embeddedartists | 0:4977187e90c7 | 130 | int x = coord.x - _colorwin->xpmin; |
| embeddedartists | 0:4977187e90c7 | 131 | int y = coord.y - _colorwin->ypmin; |
| embeddedartists | 0:4977187e90c7 | 132 | COLOR_T c = ((x/(BOX_SIDE/32))<<11) | ((y/(BOX_SIDE/64))<<5) | ((0x1f-(x/(BOX_SIDE/32)))<<0); |
| embeddedartists | 0:4977187e90c7 | 133 | swim_set_fill_color(_win, c); |
| embeddedartists | 0:4977187e90c7 | 134 | swim_put_box(_win, 350, 70, 430, 150); |
| embeddedartists | 0:4977187e90c7 | 135 | sprintf(buf, "0x%04x ", c); |
| embeddedartists | 0:4977187e90c7 | 136 | swim_put_text_xy(_win, buf, 350, 160); |
| embeddedartists | 0:4977187e90c7 | 137 | } |
| embeddedartists | 0:4977187e90c7 | 138 | if (_btn->handle(coord.x, coord.y, coord.z > 0)) { |
| embeddedartists | 0:4977187e90c7 | 139 | _btn->draw(); |
| embeddedartists | 0:4977187e90c7 | 140 | } |
| embeddedartists | 0:4977187e90c7 | 141 | } |
| embeddedartists | 0:4977187e90c7 | 142 | |
| embeddedartists | 0:4977187e90c7 | 143 | // User has clicked the button, restore the original FB |
| embeddedartists | 0:4977187e90c7 | 144 | _disp->swapFramebuffer(oldFB); |
| embeddedartists | 0:4977187e90c7 | 145 | swim_window_close(_win); |
| embeddedartists | 0:4977187e90c7 | 146 | swim_window_close(_colorwin); |
| embeddedartists | 0:4977187e90c7 | 147 | } |
| embeddedartists | 0:4977187e90c7 | 148 | |
| embeddedartists | 0:4977187e90c7 | 149 | bool AppColorPicker::teardown() |
| embeddedartists | 0:4977187e90c7 | 150 | { |
| embeddedartists | 0:4977187e90c7 | 151 | if (_win != NULL) { |
| embeddedartists | 0:4977187e90c7 | 152 | free(_win); |
| embeddedartists | 0:4977187e90c7 | 153 | _win = NULL; |
| embeddedartists | 0:4977187e90c7 | 154 | } |
| embeddedartists | 0:4977187e90c7 | 155 | if (_colorwin != NULL) { |
| embeddedartists | 0:4977187e90c7 | 156 | free(_colorwin); |
| embeddedartists | 0:4977187e90c7 | 157 | _colorwin = NULL; |
| embeddedartists | 0:4977187e90c7 | 158 | } |
| embeddedartists | 0:4977187e90c7 | 159 | if (_fb != NULL) { |
| embeddedartists | 0:4977187e90c7 | 160 | free(_fb); |
| embeddedartists | 0:4977187e90c7 | 161 | _fb = NULL; |
| embeddedartists | 0:4977187e90c7 | 162 | } |
| embeddedartists | 0:4977187e90c7 | 163 | if (_btn != NULL) { |
| embeddedartists | 0:4977187e90c7 | 164 | delete _btn; |
| embeddedartists | 0:4977187e90c7 | 165 | _btn = NULL; |
| embeddedartists | 0:4977187e90c7 | 166 | } |
| embeddedartists | 0:4977187e90c7 | 167 | return true; |
| embeddedartists | 0:4977187e90c7 | 168 | } |
| embeddedartists | 0:4977187e90c7 | 169 | |
| embeddedartists | 0:4977187e90c7 | 170 |
