Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This demo shows a number of dots projected on a rotating sphere.

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Fri Oct 03 13:15:25 2014 +0000
Revision:
0:79b286950b60
First version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddedartists 0:79b286950b60 1 /*
embeddedartists 0:79b286950b60 2 * Copyright 2013 Embedded Artists AB
embeddedartists 0:79b286950b60 3 *
embeddedartists 0:79b286950b60 4 * Licensed under the Apache License, Version 2.0 (the "License");
embeddedartists 0:79b286950b60 5 * you may not use this file except in compliance with the License.
embeddedartists 0:79b286950b60 6 * You may obtain a copy of the License at
embeddedartists 0:79b286950b60 7 *
embeddedartists 0:79b286950b60 8 * http://www.apache.org/licenses/LICENSE-2.0
embeddedartists 0:79b286950b60 9 *
embeddedartists 0:79b286950b60 10 * Unless required by applicable law or agreed to in writing, software
embeddedartists 0:79b286950b60 11 * distributed under the License is distributed on an "AS IS" BASIS,
embeddedartists 0:79b286950b60 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
embeddedartists 0:79b286950b60 13 * See the License for the specific language governing permissions and
embeddedartists 0:79b286950b60 14 * limitations under the License.
embeddedartists 0:79b286950b60 15 */
embeddedartists 0:79b286950b60 16
embeddedartists 0:79b286950b60 17 /******************************************************************************
embeddedartists 0:79b286950b60 18 * Includes
embeddedartists 0:79b286950b60 19 *****************************************************************************/
embeddedartists 0:79b286950b60 20
embeddedartists 0:79b286950b60 21 #include "mbed.h"
embeddedartists 0:79b286950b60 22 #include "TestDisplay.h"
embeddedartists 0:79b286950b60 23 #include "sdram.h"
embeddedartists 0:79b286950b60 24
embeddedartists 0:79b286950b60 25 #include "GlobeDemo.h"
embeddedartists 0:79b286950b60 26
embeddedartists 0:79b286950b60 27 /******************************************************************************
embeddedartists 0:79b286950b60 28 * Defines and typedefs
embeddedartists 0:79b286950b60 29 *****************************************************************************/
embeddedartists 0:79b286950b60 30
embeddedartists 0:79b286950b60 31 #define LCD_CONFIGURATION_43 \
embeddedartists 0:79b286950b60 32 40, /* horizontalBackPorch */ \
embeddedartists 0:79b286950b60 33 5, /* horizontalFrontPorch */ \
embeddedartists 0:79b286950b60 34 2, /* hsync */ \
embeddedartists 0:79b286950b60 35 480, /* width */ \
embeddedartists 0:79b286950b60 36 8, /* verticalBackPorch */ \
embeddedartists 0:79b286950b60 37 8, /* verticalFrontPorch */ \
embeddedartists 0:79b286950b60 38 2, /* vsync */ \
embeddedartists 0:79b286950b60 39 272, /* height */ \
embeddedartists 0:79b286950b60 40 false, /* invertOutputEnable */ \
embeddedartists 0:79b286950b60 41 false, /* invertPanelClock */ \
embeddedartists 0:79b286950b60 42 true, /* invertHsync */ \
embeddedartists 0:79b286950b60 43 true, /* invertVsync */ \
embeddedartists 0:79b286950b60 44 1, /* acBias */ \
embeddedartists 0:79b286950b60 45 LcdController::Bpp_16_565, /* bpp */ \
embeddedartists 0:79b286950b60 46 9000000, /* optimalClock */ \
embeddedartists 0:79b286950b60 47 LcdController::Tft, /* panelType */ \
embeddedartists 0:79b286950b60 48 false /* dualPanel */
embeddedartists 0:79b286950b60 49
embeddedartists 0:79b286950b60 50 #define LCD_INIT_STRING_43 (char*)"v1,cd0,c50,cc0,c30,d100,c31,d100,cd1,d10,o,c51,cc100"
embeddedartists 0:79b286950b60 51
embeddedartists 0:79b286950b60 52 #define LCD_CONFIGURATION_50 \
embeddedartists 0:79b286950b60 53 46, /* horizontalBackPorch */ \
embeddedartists 0:79b286950b60 54 20, /* horizontalFrontPorch */ \
embeddedartists 0:79b286950b60 55 2, /* hsync */ \
embeddedartists 0:79b286950b60 56 800, /* width */ \
embeddedartists 0:79b286950b60 57 23, /* verticalBackPorch */ \
embeddedartists 0:79b286950b60 58 10, /* verticalFrontPorch */ \
embeddedartists 0:79b286950b60 59 3, /* vsync */ \
embeddedartists 0:79b286950b60 60 480, /* height */ \
embeddedartists 0:79b286950b60 61 false, /* invertOutputEnable */ \
embeddedartists 0:79b286950b60 62 false, /* invertPanelClock */ \
embeddedartists 0:79b286950b60 63 true, /* invertHsync */ \
embeddedartists 0:79b286950b60 64 true, /* invertVsync */ \
embeddedartists 0:79b286950b60 65 1, /* acBias */ \
embeddedartists 0:79b286950b60 66 LcdController::Bpp_16_565, /* bpp */ \
embeddedartists 0:79b286950b60 67 30000000, /* optimalClock */ \
embeddedartists 0:79b286950b60 68 LcdController::Tft, /* panelType */ \
embeddedartists 0:79b286950b60 69 false /* dualPanel */
embeddedartists 0:79b286950b60 70
embeddedartists 0:79b286950b60 71 #define LCD_INIT_STRING_50 (char*)"v1,cc0,c31,d50,o,d200,c51,cc100"
embeddedartists 0:79b286950b60 72
embeddedartists 0:79b286950b60 73 #define MY_ABS(__a) (((__a) < 0) ? -(__a) : (__a))
embeddedartists 0:79b286950b60 74
embeddedartists 0:79b286950b60 75 /******************************************************************************
embeddedartists 0:79b286950b60 76 * Public Functions
embeddedartists 0:79b286950b60 77 *****************************************************************************/
embeddedartists 0:79b286950b60 78
embeddedartists 0:79b286950b60 79 /*
embeddedartists 0:79b286950b60 80 Prerequisites:
embeddedartists 0:79b286950b60 81
embeddedartists 0:79b286950b60 82 - A display must be connected to the LPC4088 Experiment Base Board
embeddedartists 0:79b286950b60 83 with the FPC connector
embeddedartists 0:79b286950b60 84
embeddedartists 0:79b286950b60 85 - The touch controller uses the I2C bus so for this test to work
embeddedartists 0:79b286950b60 86 jumpers JP8 and JP9 on the LPC4088 Experiment Base Board must
embeddedartists 0:79b286950b60 87 both be in positions 1-2
embeddedartists 0:79b286950b60 88
embeddedartists 0:79b286950b60 89 */
embeddedartists 0:79b286950b60 90
embeddedartists 0:79b286950b60 91 TestDisplay::TestDisplay(WhichDisplay which) :
embeddedartists 0:79b286950b60 92 _initStr(NULL),
embeddedartists 0:79b286950b60 93 _lcdCfg(NULL),
embeddedartists 0:79b286950b60 94 _lcdBoard(P0_27, P0_28),
embeddedartists 0:79b286950b60 95 _touch(P0_27, P0_28, P2_25) {
embeddedartists 0:79b286950b60 96
embeddedartists 0:79b286950b60 97 switch (which) {
embeddedartists 0:79b286950b60 98 case TFT_5:
embeddedartists 0:79b286950b60 99 _lcdCfg = new LcdController::Config(LCD_CONFIGURATION_50);
embeddedartists 0:79b286950b60 100 _initStr = LCD_INIT_STRING_50;
embeddedartists 0:79b286950b60 101 break;
embeddedartists 0:79b286950b60 102 case TFT_4_3:
embeddedartists 0:79b286950b60 103 _lcdCfg = new LcdController::Config(LCD_CONFIGURATION_43);
embeddedartists 0:79b286950b60 104 _initStr = LCD_INIT_STRING_43;
embeddedartists 0:79b286950b60 105 break;
embeddedartists 0:79b286950b60 106 default:
embeddedartists 0:79b286950b60 107 mbed_die();
embeddedartists 0:79b286950b60 108 }
embeddedartists 0:79b286950b60 109
embeddedartists 0:79b286950b60 110 if (sdram_init() == 1) {
embeddedartists 0:79b286950b60 111 printf("Failed to initialize SDRAM\n");
embeddedartists 0:79b286950b60 112 _framebuffer = 0;
embeddedartists 0:79b286950b60 113 } else {
embeddedartists 0:79b286950b60 114 _framebuffer = (uint32_t) malloc(_lcdCfg->width * _lcdCfg->height * 2 * 3); // 2 is for 16 bit color, 3 is the number of buffers
embeddedartists 0:79b286950b60 115 if (_framebuffer != 0) {
embeddedartists 0:79b286950b60 116 memset((uint8_t*)_framebuffer, 0, _lcdCfg->width * _lcdCfg->height * 2 * 3);
embeddedartists 0:79b286950b60 117 }
embeddedartists 0:79b286950b60 118 }
embeddedartists 0:79b286950b60 119 }
embeddedartists 0:79b286950b60 120
embeddedartists 0:79b286950b60 121 TestDisplay::~TestDisplay() {
embeddedartists 0:79b286950b60 122 if (_framebuffer != 0) {
embeddedartists 0:79b286950b60 123 free((void*)_framebuffer);
embeddedartists 0:79b286950b60 124 _framebuffer = 0;
embeddedartists 0:79b286950b60 125 }
embeddedartists 0:79b286950b60 126 }
embeddedartists 0:79b286950b60 127
embeddedartists 0:79b286950b60 128 bool TestDisplay::runTest() {
embeddedartists 0:79b286950b60 129 do {
embeddedartists 0:79b286950b60 130 if (_framebuffer == 0) {
embeddedartists 0:79b286950b60 131 printf("Failed to allocate memory for framebuffer\n");
embeddedartists 0:79b286950b60 132 break;
embeddedartists 0:79b286950b60 133 }
embeddedartists 0:79b286950b60 134
embeddedartists 0:79b286950b60 135 EaLcdBoard::Result result = _lcdBoard.open(_lcdCfg, _initStr);
embeddedartists 0:79b286950b60 136 if (result != EaLcdBoard::Ok) {
embeddedartists 0:79b286950b60 137 printf("Failed to open display, error %d\n", result);
embeddedartists 0:79b286950b60 138 break;
embeddedartists 0:79b286950b60 139 }
embeddedartists 0:79b286950b60 140
embeddedartists 0:79b286950b60 141 result = _lcdBoard.setFrameBuffer(_framebuffer);
embeddedartists 0:79b286950b60 142 if (result != EaLcdBoard::Ok) {
embeddedartists 0:79b286950b60 143 printf("Failed to set framebuffer, error %d\n", result);
embeddedartists 0:79b286950b60 144 break;
embeddedartists 0:79b286950b60 145 }
embeddedartists 0:79b286950b60 146
embeddedartists 0:79b286950b60 147 GlobeDemo globeDemo((uint8_t *)_framebuffer, _lcdCfg->width, _lcdCfg->height);
embeddedartists 0:79b286950b60 148 while (1) {
embeddedartists 0:79b286950b60 149 globeDemo.run(_lcdBoard, 400, 30);
embeddedartists 0:79b286950b60 150 }
embeddedartists 0:79b286950b60 151 } while(0);
embeddedartists 0:79b286950b60 152
embeddedartists 0:79b286950b60 153 return false;
embeddedartists 0:79b286950b60 154 }
embeddedartists 0:79b286950b60 155
embeddedartists 0:79b286950b60 156 void TestDisplay::calibrate_drawMarker(Graphics &g, uint16_t x, uint16_t y, bool erase) {
embeddedartists 0:79b286950b60 157 uint16_t color = (erase ? 0x0000 : 0xffff);
embeddedartists 0:79b286950b60 158 g.put_line(x-15, y, x+15, y, color);
embeddedartists 0:79b286950b60 159 g.put_line(x, y-15, x, y+15, color);
embeddedartists 0:79b286950b60 160 g.put_circle(x, y, color, 10, false);
embeddedartists 0:79b286950b60 161 }
embeddedartists 0:79b286950b60 162
embeddedartists 0:79b286950b60 163 bool TestDisplay::calibrate_display() {
embeddedartists 0:79b286950b60 164 bool morePoints = true;
embeddedartists 0:79b286950b60 165 uint16_t x, y;
embeddedartists 0:79b286950b60 166 int point = 0;
embeddedartists 0:79b286950b60 167 Graphics g((uint16_t*)_framebuffer, _lcdCfg->width, _lcdCfg->height);
embeddedartists 0:79b286950b60 168
embeddedartists 0:79b286950b60 169 do {
embeddedartists 0:79b286950b60 170 if (!_touch.init(_lcdCfg->width, _lcdCfg->height)) {
embeddedartists 0:79b286950b60 171 printf("Failed to initialize touch controller\n");
embeddedartists 0:79b286950b60 172 break;
embeddedartists 0:79b286950b60 173 }
embeddedartists 0:79b286950b60 174 if (!_touch.calibrateStart()) {
embeddedartists 0:79b286950b60 175 printf("Failed to start calibration\n");
embeddedartists 0:79b286950b60 176 break;
embeddedartists 0:79b286950b60 177 }
embeddedartists 0:79b286950b60 178 while (morePoints) {
embeddedartists 0:79b286950b60 179 if (point++ > 0) {
embeddedartists 0:79b286950b60 180 // erase old location
embeddedartists 0:79b286950b60 181 calibrate_drawMarker(g, x, y, true);
embeddedartists 0:79b286950b60 182 }
embeddedartists 0:79b286950b60 183 if (!_touch.getNextCalibratePoint(&x, &y)) {
embeddedartists 0:79b286950b60 184 printf("Failed to get calibration point\n");
embeddedartists 0:79b286950b60 185 break;
embeddedartists 0:79b286950b60 186 }
embeddedartists 0:79b286950b60 187 calibrate_drawMarker(g, x, y, false);
embeddedartists 0:79b286950b60 188 if (!_touch.waitForCalibratePoint(&morePoints, 0)) {
embeddedartists 0:79b286950b60 189 printf("Failed to get user click\n");
embeddedartists 0:79b286950b60 190 break;
embeddedartists 0:79b286950b60 191 }
embeddedartists 0:79b286950b60 192 }
embeddedartists 0:79b286950b60 193 if (morePoints) {
embeddedartists 0:79b286950b60 194 // aborted calibration due to error(s)
embeddedartists 0:79b286950b60 195 break;
embeddedartists 0:79b286950b60 196 }
embeddedartists 0:79b286950b60 197
embeddedartists 0:79b286950b60 198 // erase old location
embeddedartists 0:79b286950b60 199 calibrate_drawMarker(g, x, y, true);
embeddedartists 0:79b286950b60 200
embeddedartists 0:79b286950b60 201 // allow user to draw for 5999 seconds
embeddedartists 0:79b286950b60 202 Timer t;
embeddedartists 0:79b286950b60 203 t.start();
embeddedartists 0:79b286950b60 204 TouchPanel::touchCoordinate_t tc;
embeddedartists 0:79b286950b60 205 while(t.read() < 6000) {
embeddedartists 0:79b286950b60 206 if (_touch.read(tc)) {
embeddedartists 0:79b286950b60 207 //printf("TC: x,y,z = {%5d, %5d, %5d}\n", tc.x, tc.y, tc.z);
embeddedartists 0:79b286950b60 208 if (tc.z) {
embeddedartists 0:79b286950b60 209 g.put_dot(tc.x, tc.y, 0xffff);
embeddedartists 0:79b286950b60 210 }
embeddedartists 0:79b286950b60 211 }
embeddedartists 0:79b286950b60 212 }
embeddedartists 0:79b286950b60 213 } while(0);
embeddedartists 0:79b286950b60 214
embeddedartists 0:79b286950b60 215 return !morePoints;
embeddedartists 0:79b286950b60 216 }