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

Application/AppColorPicker.cpp

Committer:
embeddedartists
Date:
2015-01-17
Revision:
8:19a6b70d42b1
Parent:
3:3fabfe3339b8
Child:
11:265884fa7fdd

File content as of revision 8:19a6b70d42b1:

/*
 *  Copyright 2014 Embedded Artists AB
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


#include "mbed.h"
#include "EthernetInterface.h"
#include "AppColorPicker.h"
#include "lpc_swim_font.h"

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/
 
#define BOX_SIDE   192 //256
 
#define BTN_WIDTH  65
#define BTN_HEIGHT 40
#define BTN_OFF    20
 
/******************************************************************************
 * Global variables
 *****************************************************************************/

/******************************************************************************
 * Private functions
 *****************************************************************************/

static void buttonClicked(uint32_t x)
{
  bool* done = (bool*)x;
  *done = true;
}

void AppColorPicker::draw()
{
    // Prepare fullscreen
    swim_window_open(_win, 
                   _disp->width(), _disp->height(),         // full size
                   (COLOR_T*)_fb,
                   0,0,_disp->width()-1, _disp->height()-1, // window position and size
                   1,                                       // border
                   WHITE, WHITE, BLACK);                    // colors: pen, backgr, forgr
    swim_set_title(_win, "Color Picker", BLACK);

    swim_window_open(_colorwin, 
                     _disp->width(), _disp->height(),         // full size
                     (COLOR_T*)_fb,
                     50,(_disp->height()-BOX_SIDE)/2,50+BOX_SIDE-1, BOX_SIDE+(_disp->height()-BOX_SIDE)/2,                 // window position and size
                     0,                                       // border
                     WHITE, WHITE, BLACK);                    // colors: pen, backgr, forgr
    

    uint16_t r, g, b;
    uint16_t rx = BOX_SIDE/32;
    //uint16_t gx = BOX_SIDE/64;
    uint16_t bx = BOX_SIDE/32;
    //uint16_t ry = BOX_SIDE/32;
    uint16_t gy = BOX_SIDE/64;
    //uint16_t by = BOX_SIDE/32;
    uint16_t color;
    for (int x = 0; x < BOX_SIDE; x++) {
        r = (x/rx);
        b = 0x1f - (x/bx);
        color = ((r & 0x1f) << 11) | ((b & 0x1f) << 0);
        for (int y = 0; y < BOX_SIDE; y++) {
            g = (y/gy);
            _colorwin->pen = color | ((g & 0x3f) << 5);
            swim_put_pixel(_colorwin, x, y);
        }
    }
    
    _btn = new Button("Done", _win->fb, _win->xpmax - BTN_OFF - BTN_WIDTH, _win->ypmax - BTN_OFF - BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT);
    _btn->draw();
}

/******************************************************************************
 * Public functions
 *****************************************************************************/

AppColorPicker::AppColorPicker() : _disp(NULL), _win(NULL), _colorwin(NULL), _fb(NULL), _btn(NULL)
{
}

AppColorPicker::~AppColorPicker()
{
    teardown();
}

bool AppColorPicker::setup()
{
    _disp = DMBoard::instance().display();
    _win = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
    _colorwin = (SWIM_WINDOW_T*)malloc(sizeof(SWIM_WINDOW_T));
    _fb = _disp->allocateFramebuffer();

    return (_win != NULL && _colorwin != NULL && _fb != NULL);
}

void AppColorPicker::runToCompletion()
{
    // Alternative 1: use the calling thread's context to run in
    bool done = false;
    draw();
    _btn->setAction(buttonClicked, (uint32_t)&done);
    void* oldFB = _disp->swapFramebuffer(_fb);
    
    // Wait for touches
    TouchPanel* touch = DMBoard::instance().touchPanel();
    touch_coordinate_t coord;
    char buf[10];
    swim_set_pen_color(_win, BLACK);
    while(!done) {
      // wait for a new touch signal (signal is sent from AppLauncher,
      // which listens for touch events)
      Thread::signal_wait(0x1);
      if (touch->read(coord) == TouchPanel::TouchError_Ok) {
        if (coord.z > 0 &&
            coord.x >= _colorwin->xpmin && coord.x <= _colorwin->xpmax && 
            coord.y >= _colorwin->ypmin && coord.y <= _colorwin->ypmax) {
            int x = coord.x - _colorwin->xpmin;
            int y = coord.y - _colorwin->ypmin;
            COLOR_T c = ((x/(BOX_SIDE/32))<<11) | ((y/(BOX_SIDE/64))<<5) | ((0x1f-(x/(BOX_SIDE/32)))<<0);
            swim_set_fill_color(_win, c);
            swim_put_box(_win, 350, 70, 430, 150);
            sprintf(buf, "0x%04x  ", c);
            swim_put_text_xy(_win, buf, 350, 160);
        }
        if (_btn->handle(coord.x, coord.y, coord.z > 0)) {
            _btn->draw();
        }
      }
    }
    
    // User has clicked the button, restore the original FB
    _disp->swapFramebuffer(oldFB);
    swim_window_close(_win);
    swim_window_close(_colorwin);
}

bool AppColorPicker::teardown()
{
    if (_win != NULL) {
        free(_win);
        _win = NULL;
    }
    if (_colorwin != NULL) {
        free(_colorwin);
        _colorwin = NULL;
    }
    if (_fb != NULL) {
        free(_fb);
        _fb = NULL;
    }
    if (_btn != NULL) {
        delete _btn;
        _btn = NULL;
    }
    return true;
}