Graphical demo for the LPC4088 Experiment Base Board with one of the Display Expansion Kits. This program displays a number of dots in a 3D-projected wave.

Dependencies:   EALib mbed

Committer:
embeddedartists
Date:
Fri Oct 03 13:37:30 2014 +0000
Revision:
0:d1e4929f6956
First version

Who changed what in which revision?

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