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