![](/media/cache/group/mbed_GUI_wo_mbed.png.50x50_q85.png)
My first trial of using UniGraphic library.
Dependencies: MMA8451Q SPI_STMPE610 UniGraphic mbed
main.cpp@0:be311b6a294c, 2015-02-17 (annotated)
- Committer:
- Rhyme
- Date:
- Tue Feb 17 13:22:06 2015 +0000
- Revision:
- 0:be311b6a294c
- Child:
- 1:6cd56ddffb40
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:be311b6a294c | 1 | #include "mbed.h" |
Rhyme | 0:be311b6a294c | 2 | #include "MMA8451Q.h" |
Rhyme | 0:be311b6a294c | 3 | #include <ILI9341.h> |
Rhyme | 0:be311b6a294c | 4 | #include "Arial12x12.h" |
Rhyme | 0:be311b6a294c | 5 | #include "Arial24x23.h" |
Rhyme | 0:be311b6a294c | 6 | #include "Arial28x28.h" |
Rhyme | 0:be311b6a294c | 7 | #include "Arial43x48_numb.h" |
Rhyme | 0:be311b6a294c | 8 | #include "SPI_STMPE610.h" |
Rhyme | 0:be311b6a294c | 9 | #include "main.h" |
Rhyme | 0:be311b6a294c | 10 | |
Rhyme | 0:be311b6a294c | 11 | DigitalOut myled(LED1); |
Rhyme | 0:be311b6a294c | 12 | ILI9341 TFT(SPI_8, 10000000, |
Rhyme | 0:be311b6a294c | 13 | PIN_MOSI, PIN_MISO, PIN_SCLK, |
Rhyme | 0:be311b6a294c | 14 | PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ; |
Rhyme | 0:be311b6a294c | 15 | |
Rhyme | 0:be311b6a294c | 16 | SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ; |
Rhyme | 0:be311b6a294c | 17 | |
Rhyme | 0:be311b6a294c | 18 | DigitalOut backlight(PIN_BL_TFT) ; |
Rhyme | 0:be311b6a294c | 19 | |
Rhyme | 0:be311b6a294c | 20 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
Rhyme | 0:be311b6a294c | 21 | MMA8451Q *acc = 0 ; |
Rhyme | 0:be311b6a294c | 22 | |
Rhyme | 0:be311b6a294c | 23 | int page = 0 ; |
Rhyme | 0:be311b6a294c | 24 | int numPage = 3 ; |
Rhyme | 0:be311b6a294c | 25 | |
Rhyme | 0:be311b6a294c | 26 | void initTFT(void) |
Rhyme | 0:be311b6a294c | 27 | { |
Rhyme | 0:be311b6a294c | 28 | //Configure the display driver |
Rhyme | 0:be311b6a294c | 29 | TFT.background(Black); |
Rhyme | 0:be311b6a294c | 30 | TFT.foreground(White); |
Rhyme | 0:be311b6a294c | 31 | wait(0.01) ; |
Rhyme | 0:be311b6a294c | 32 | TFT.cls(); |
Rhyme | 0:be311b6a294c | 33 | } |
Rhyme | 0:be311b6a294c | 34 | |
Rhyme | 0:be311b6a294c | 35 | void screen1(void) // Welcome Screen |
Rhyme | 0:be311b6a294c | 36 | { |
Rhyme | 0:be311b6a294c | 37 | backlight = 0 ; |
Rhyme | 0:be311b6a294c | 38 | TFT.background(White) ; |
Rhyme | 0:be311b6a294c | 39 | TFT.cls() ; |
Rhyme | 0:be311b6a294c | 40 | |
Rhyme | 0:be311b6a294c | 41 | wait(0.1) ; |
Rhyme | 0:be311b6a294c | 42 | TFT.set_font((unsigned char*) Arial24x23); |
Rhyme | 0:be311b6a294c | 43 | TFT.foreground(Red) ; |
Rhyme | 0:be311b6a294c | 44 | TFT.locate(80, 40) ; |
Rhyme | 0:be311b6a294c | 45 | TFT.printf("MBED") ; |
Rhyme | 0:be311b6a294c | 46 | TFT.foreground(Blue); |
Rhyme | 0:be311b6a294c | 47 | TFT.locate(60, 80) ; |
Rhyme | 0:be311b6a294c | 48 | TFT.printf("2.8\"TFT") ; |
Rhyme | 0:be311b6a294c | 49 | TFT.locate(40, 120) ; |
Rhyme | 0:be311b6a294c | 50 | TFT.printf("with touch") ; |
Rhyme | 0:be311b6a294c | 51 | TFT.foreground(Black); |
Rhyme | 0:be311b6a294c | 52 | TFT.set_font((unsigned char*) Arial12x12); |
Rhyme | 0:be311b6a294c | 53 | TFT.foreground(Blue) ; |
Rhyme | 0:be311b6a294c | 54 | TFT.locate(10, 180) ; |
Rhyme | 0:be311b6a294c | 55 | TFT.printf("This program is running on") ; |
Rhyme | 0:be311b6a294c | 56 | TFT.locate(10, 200) ; |
Rhyme | 0:be311b6a294c | 57 | TFT.printf("freescale FRDM-KL25Z with") ; |
Rhyme | 0:be311b6a294c | 58 | TFT.locate(10, 220) ; |
Rhyme | 0:be311b6a294c | 59 | TFT.printf("a program developed on mbed") ; |
Rhyme | 0:be311b6a294c | 60 | TFT.foreground(Green) ; |
Rhyme | 0:be311b6a294c | 61 | TFT.locate(10, 260) ; |
Rhyme | 0:be311b6a294c | 62 | TFT.printf("To advance demo page, touch") ; |
Rhyme | 0:be311b6a294c | 63 | TFT.locate(10, 280) ; |
Rhyme | 0:be311b6a294c | 64 | TFT.printf("and hold right side of screen") ; |
Rhyme | 0:be311b6a294c | 65 | TFT.locate(10, 300) ; |
Rhyme | 0:be311b6a294c | 66 | TFT.printf("until the next screen starts") ; |
Rhyme | 0:be311b6a294c | 67 | backlight = 1 ; |
Rhyme | 0:be311b6a294c | 68 | } |
Rhyme | 0:be311b6a294c | 69 | |
Rhyme | 0:be311b6a294c | 70 | void screen2(void) // Graphics |
Rhyme | 0:be311b6a294c | 71 | { |
Rhyme | 0:be311b6a294c | 72 | //Draw some graphics |
Rhyme | 0:be311b6a294c | 73 | int i, x[2], y[2] ; |
Rhyme | 0:be311b6a294c | 74 | backlight = 0 ; |
Rhyme | 0:be311b6a294c | 75 | |
Rhyme | 0:be311b6a294c | 76 | TFT.background(Black); |
Rhyme | 0:be311b6a294c | 77 | TFT.foreground(White); |
Rhyme | 0:be311b6a294c | 78 | TFT.cls() ; |
Rhyme | 0:be311b6a294c | 79 | TFT.set_font((unsigned char*) Arial12x12); |
Rhyme | 0:be311b6a294c | 80 | TFT.locate(90,0); |
Rhyme | 0:be311b6a294c | 81 | TFT.printf("Graphics"); |
Rhyme | 0:be311b6a294c | 82 | |
Rhyme | 0:be311b6a294c | 83 | x[0] = 25 ; x[1] = 224 ; |
Rhyme | 0:be311b6a294c | 84 | y[0] = 20 ; y[1] = 219 ; |
Rhyme | 0:be311b6a294c | 85 | for (i = 20 ; i < 220 ; i += 10) { |
Rhyme | 0:be311b6a294c | 86 | TFT.line(i+5, y[0], i+5, y[1], Blue) ; |
Rhyme | 0:be311b6a294c | 87 | TFT.line(x[0], i, x[1], i, Blue) ; |
Rhyme | 0:be311b6a294c | 88 | } |
Rhyme | 0:be311b6a294c | 89 | TFT.line(125, y[0], 125, y[1], Green) ; |
Rhyme | 0:be311b6a294c | 90 | TFT.line(x[0], 120, x[1], 120, Green) ; |
Rhyme | 0:be311b6a294c | 91 | TFT.rect(x[0],y[0], x[1], y[1], Green) ; |
Rhyme | 0:be311b6a294c | 92 | TFT.locate(10, 20) ; |
Rhyme | 0:be311b6a294c | 93 | TFT.printf("V") ; |
Rhyme | 0:be311b6a294c | 94 | TFT.locate(0, 115) ; |
Rhyme | 0:be311b6a294c | 95 | TFT.printf("0.0") ; |
Rhyme | 0:be311b6a294c | 96 | TFT.locate(115, 225) ; |
Rhyme | 0:be311b6a294c | 97 | TFT.printf("0.0") ; |
Rhyme | 0:be311b6a294c | 98 | TFT.locate(215, 225) ; |
Rhyme | 0:be311b6a294c | 99 | TFT.printf("T") ; |
Rhyme | 0:be311b6a294c | 100 | |
Rhyme | 0:be311b6a294c | 101 | double s; |
Rhyme | 0:be311b6a294c | 102 | for (int i = x[0]; i < 225; i++) { |
Rhyme | 0:be311b6a294c | 103 | s = 40 * sin((long double)i / 20); |
Rhyme | 0:be311b6a294c | 104 | TFT.pixel(i, 120 + (int)s, White); |
Rhyme | 0:be311b6a294c | 105 | } |
Rhyme | 0:be311b6a294c | 106 | |
Rhyme | 0:be311b6a294c | 107 | TFT.fillrect(10, 240, 229, 309, White) ; |
Rhyme | 0:be311b6a294c | 108 | TFT.rect(10, 240, 229, 309, Red) ; |
Rhyme | 0:be311b6a294c | 109 | TFT.rect(11, 241, 228, 308, Red) ; |
Rhyme | 0:be311b6a294c | 110 | |
Rhyme | 0:be311b6a294c | 111 | TFT.background(White) ; |
Rhyme | 0:be311b6a294c | 112 | TFT.foreground(Black) ; |
Rhyme | 0:be311b6a294c | 113 | TFT.locate(20, 250) ; |
Rhyme | 0:be311b6a294c | 114 | TFT.printf("With QVGA resolution") ; |
Rhyme | 0:be311b6a294c | 115 | TFT.locate(20, 270) ; |
Rhyme | 0:be311b6a294c | 116 | TFT.printf("simple graphics drawing") ; |
Rhyme | 0:be311b6a294c | 117 | TFT.locate(20, 290) ; |
Rhyme | 0:be311b6a294c | 118 | TFT.printf("capability is provided") ; |
Rhyme | 0:be311b6a294c | 119 | backlight = 1 ; |
Rhyme | 0:be311b6a294c | 120 | } |
Rhyme | 0:be311b6a294c | 121 | |
Rhyme | 0:be311b6a294c | 122 | double clip(double src) |
Rhyme | 0:be311b6a294c | 123 | { |
Rhyme | 0:be311b6a294c | 124 | double value ; |
Rhyme | 0:be311b6a294c | 125 | value = src ; |
Rhyme | 0:be311b6a294c | 126 | if (value < 0.0) { |
Rhyme | 0:be311b6a294c | 127 | value = 0.0 ; |
Rhyme | 0:be311b6a294c | 128 | } else if (value > 2.0) { |
Rhyme | 0:be311b6a294c | 129 | value = 2.0 ; |
Rhyme | 0:be311b6a294c | 130 | } |
Rhyme | 0:be311b6a294c | 131 | return( value ) ; |
Rhyme | 0:be311b6a294c | 132 | } |
Rhyme | 0:be311b6a294c | 133 | |
Rhyme | 0:be311b6a294c | 134 | void screen3(void) |
Rhyme | 0:be311b6a294c | 135 | { |
Rhyme | 0:be311b6a294c | 136 | int t = 0 ; |
Rhyme | 0:be311b6a294c | 137 | int pt = 0 ; // previous t |
Rhyme | 0:be311b6a294c | 138 | int i, x, y ; |
Rhyme | 0:be311b6a294c | 139 | unsigned int data[3] ; // for x, y, z |
Rhyme | 0:be311b6a294c | 140 | unsigned int prev[3] ; |
Rhyme | 0:be311b6a294c | 141 | unsigned short signalHeight = 39 ; |
Rhyme | 0:be311b6a294c | 142 | unsigned short xoffset = 30 ; |
Rhyme | 0:be311b6a294c | 143 | unsigned short yoffset = 120 ; |
Rhyme | 0:be311b6a294c | 144 | unsigned short zoffset = 210 ; |
Rhyme | 0:be311b6a294c | 145 | unsigned short paneX[2] = {20, 235} ; |
Rhyme | 0:be311b6a294c | 146 | unsigned short paneH = 81 ; |
Rhyme | 0:be311b6a294c | 147 | |
Rhyme | 0:be311b6a294c | 148 | backlight = 0 ; |
Rhyme | 0:be311b6a294c | 149 | TFT.background(Black) ; |
Rhyme | 0:be311b6a294c | 150 | TFT.foreground(White) ; |
Rhyme | 0:be311b6a294c | 151 | // TFT.cls() ; |
Rhyme | 0:be311b6a294c | 152 | |
Rhyme | 0:be311b6a294c | 153 | |
Rhyme | 0:be311b6a294c | 154 | TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; |
Rhyme | 0:be311b6a294c | 155 | TFT.fillrect(paneX[0], yoffset, paneX[1], yoffset+paneH, Black) ; |
Rhyme | 0:be311b6a294c | 156 | TFT.fillrect(paneX[0], zoffset, paneX[1], zoffset+paneH, Black) ; |
Rhyme | 0:be311b6a294c | 157 | TFT.fillrect(paneX[0], xoffset, paneX[1], xoffset+paneH, Black) ; |
Rhyme | 0:be311b6a294c | 158 | for (i = 0 ; i < 10 ; i++ ) { |
Rhyme | 0:be311b6a294c | 159 | y = i * 8 ; |
Rhyme | 0:be311b6a294c | 160 | TFT.line(paneX[0], xoffset + y, paneX[1], xoffset + y, Blue) ; |
Rhyme | 0:be311b6a294c | 161 | TFT.line(paneX[0], yoffset + y, paneX[1], yoffset + y, Blue) ; |
Rhyme | 0:be311b6a294c | 162 | TFT.line(paneX[0], zoffset + y, paneX[1], zoffset + y, Blue) ; |
Rhyme | 0:be311b6a294c | 163 | } |
Rhyme | 0:be311b6a294c | 164 | for (x = 30 ; x < paneX[1] ; x += 10 ) { |
Rhyme | 0:be311b6a294c | 165 | TFT.line(x, xoffset, x, xoffset+paneH, Blue) ; |
Rhyme | 0:be311b6a294c | 166 | TFT.line(x, yoffset, x, yoffset+paneH, Blue) ; |
Rhyme | 0:be311b6a294c | 167 | TFT.line(x, zoffset, x, zoffset+paneH, Blue) ; |
Rhyme | 0:be311b6a294c | 168 | } |
Rhyme | 0:be311b6a294c | 169 | TFT.rect(paneX[0], xoffset, paneX[1], xoffset+paneH, White) ; |
Rhyme | 0:be311b6a294c | 170 | TFT.rect(paneX[0], yoffset, paneX[1], yoffset+paneH, White) ; |
Rhyme | 0:be311b6a294c | 171 | TFT.rect(paneX[0], zoffset, paneX[1], zoffset+paneH, White) ; |
Rhyme | 0:be311b6a294c | 172 | TFT.set_font((unsigned char*) Arial12x12); |
Rhyme | 0:be311b6a294c | 173 | TFT.locate(5, xoffset+30) ; |
Rhyme | 0:be311b6a294c | 174 | TFT.printf("X") ; |
Rhyme | 0:be311b6a294c | 175 | TFT.locate(5, yoffset+30) ; |
Rhyme | 0:be311b6a294c | 176 | TFT.printf("Y") ; |
Rhyme | 0:be311b6a294c | 177 | TFT.locate(5, zoffset+30) ; |
Rhyme | 0:be311b6a294c | 178 | TFT.printf("Z") ; |
Rhyme | 0:be311b6a294c | 179 | TFT.locate(50, 10) ; |
Rhyme | 0:be311b6a294c | 180 | TFT.printf("Xtrinsic Accelerometer") ; |
Rhyme | 0:be311b6a294c | 181 | TFT.locate(90, 300) ; |
Rhyme | 0:be311b6a294c | 182 | TFT.printf("MMA8451Q") ; |
Rhyme | 0:be311b6a294c | 183 | |
Rhyme | 0:be311b6a294c | 184 | prev[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ; |
Rhyme | 0:be311b6a294c | 185 | prev[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ; |
Rhyme | 0:be311b6a294c | 186 | prev[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ; |
Rhyme | 0:be311b6a294c | 187 | pt = paneX[0] ; |
Rhyme | 0:be311b6a294c | 188 | backlight = 1 ; |
Rhyme | 0:be311b6a294c | 189 | for(t = 21 ; t < paneX[1] ; t++) { |
Rhyme | 0:be311b6a294c | 190 | data[0] = xoffset + (signalHeight * clip((1.0 + acc->getAccX()))) ; |
Rhyme | 0:be311b6a294c | 191 | data[1] = yoffset + (signalHeight * clip((1.0 + acc->getAccY()))) ; |
Rhyme | 0:be311b6a294c | 192 | data[2] = zoffset + (signalHeight * clip((1.0 + acc->getAccZ()))) ; |
Rhyme | 0:be311b6a294c | 193 | TFT.line(pt, prev[0], t, data[0], Red) ; |
Rhyme | 0:be311b6a294c | 194 | TFT.line(pt, prev[1], t, data[1], Green) ; |
Rhyme | 0:be311b6a294c | 195 | TFT.line(pt, prev[2], t, data[2], Yellow) ; |
Rhyme | 0:be311b6a294c | 196 | prev[0] = data[0] ; |
Rhyme | 0:be311b6a294c | 197 | prev[1] = data[1] ; |
Rhyme | 0:be311b6a294c | 198 | prev[2] = data[2] ; |
Rhyme | 0:be311b6a294c | 199 | pt = t ; |
Rhyme | 0:be311b6a294c | 200 | wait(0.01) ; |
Rhyme | 0:be311b6a294c | 201 | } |
Rhyme | 0:be311b6a294c | 202 | } |
Rhyme | 0:be311b6a294c | 203 | |
Rhyme | 0:be311b6a294c | 204 | void incPage(void) |
Rhyme | 0:be311b6a294c | 205 | { |
Rhyme | 0:be311b6a294c | 206 | page++ ; |
Rhyme | 0:be311b6a294c | 207 | if (page >= numPage) { |
Rhyme | 0:be311b6a294c | 208 | page = 0 ; |
Rhyme | 0:be311b6a294c | 209 | } |
Rhyme | 0:be311b6a294c | 210 | } |
Rhyme | 0:be311b6a294c | 211 | |
Rhyme | 0:be311b6a294c | 212 | void decPage(void) |
Rhyme | 0:be311b6a294c | 213 | { |
Rhyme | 0:be311b6a294c | 214 | page-- ; |
Rhyme | 0:be311b6a294c | 215 | if (page < 0) { |
Rhyme | 0:be311b6a294c | 216 | page = numPage - 1 ; |
Rhyme | 0:be311b6a294c | 217 | } |
Rhyme | 0:be311b6a294c | 218 | } |
Rhyme | 0:be311b6a294c | 219 | |
Rhyme | 0:be311b6a294c | 220 | int main() { |
Rhyme | 0:be311b6a294c | 221 | uint16_t x, y, z ; |
Rhyme | 0:be311b6a294c | 222 | int prevPage = 0 ; |
Rhyme | 0:be311b6a294c | 223 | |
Rhyme | 0:be311b6a294c | 224 | acc = new MMA8451Q(PTE25, PTE24, MMA8451_I2C_ADDRESS) ; |
Rhyme | 0:be311b6a294c | 225 | |
Rhyme | 0:be311b6a294c | 226 | initTFT() ; |
Rhyme | 0:be311b6a294c | 227 | |
Rhyme | 0:be311b6a294c | 228 | screen1() ; |
Rhyme | 0:be311b6a294c | 229 | |
Rhyme | 0:be311b6a294c | 230 | printf("Program Started!\n\r") ; |
Rhyme | 0:be311b6a294c | 231 | |
Rhyme | 0:be311b6a294c | 232 | for(;;) { |
Rhyme | 0:be311b6a294c | 233 | // printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ; |
Rhyme | 0:be311b6a294c | 234 | switch(page) { |
Rhyme | 0:be311b6a294c | 235 | case 0: |
Rhyme | 0:be311b6a294c | 236 | if (prevPage != page) { |
Rhyme | 0:be311b6a294c | 237 | screen1() ; |
Rhyme | 0:be311b6a294c | 238 | } |
Rhyme | 0:be311b6a294c | 239 | break ; |
Rhyme | 0:be311b6a294c | 240 | case 1: |
Rhyme | 0:be311b6a294c | 241 | if (prevPage != page) { |
Rhyme | 0:be311b6a294c | 242 | screen2() ; |
Rhyme | 0:be311b6a294c | 243 | } |
Rhyme | 0:be311b6a294c | 244 | break ; |
Rhyme | 0:be311b6a294c | 245 | case 2: |
Rhyme | 0:be311b6a294c | 246 | if (prevPage != page) { |
Rhyme | 0:be311b6a294c | 247 | TFT.background(Black) ; |
Rhyme | 0:be311b6a294c | 248 | TFT.foreground(White) ; |
Rhyme | 0:be311b6a294c | 249 | TFT.cls() ; |
Rhyme | 0:be311b6a294c | 250 | } |
Rhyme | 0:be311b6a294c | 251 | screen3() ; |
Rhyme | 0:be311b6a294c | 252 | wait(2) ; |
Rhyme | 0:be311b6a294c | 253 | break ; |
Rhyme | 0:be311b6a294c | 254 | default: |
Rhyme | 0:be311b6a294c | 255 | page = 0 ; |
Rhyme | 0:be311b6a294c | 256 | break ; |
Rhyme | 0:be311b6a294c | 257 | } |
Rhyme | 0:be311b6a294c | 258 | prevPage = page ; |
Rhyme | 0:be311b6a294c | 259 | |
Rhyme | 0:be311b6a294c | 260 | TSC.getRAWPoint(&x, &y, &z) ; |
Rhyme | 0:be311b6a294c | 261 | if ((x != 0)||(y != 0) || (z != 0)) { |
Rhyme | 0:be311b6a294c | 262 | if (x < 1000) { // left |
Rhyme | 0:be311b6a294c | 263 | decPage() ; |
Rhyme | 0:be311b6a294c | 264 | } else if (x > 3000) { // right |
Rhyme | 0:be311b6a294c | 265 | incPage() ; |
Rhyme | 0:be311b6a294c | 266 | } |
Rhyme | 0:be311b6a294c | 267 | } |
Rhyme | 0:be311b6a294c | 268 | // wait(1) ; |
Rhyme | 0:be311b6a294c | 269 | } |
Rhyme | 0:be311b6a294c | 270 | } |