HX8347D LCD

Fork of HX8347D by k og

Committer:
og
Date:
Fri Sep 02 00:45:38 2016 +0000
Revision:
3:233295bc63af
Parent:
2:11512f68bcc8
??????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
og 1:8dfbf62450e6 1 #include "HX8347D.h"
og 1:8dfbf62450e6 2 #include "glcdfont.h"
og 1:8dfbf62450e6 3
og 1:8dfbf62450e6 4 #define X(x) (x)
og 1:8dfbf62450e6 5 #define I_X(x) (TFTWIDTH - x - 1)
og 1:8dfbf62450e6 6 #define Y(y) (y)
og 1:8dfbf62450e6 7 #define I_Y(y) (TFTHEIGHT - y - 1)
og 1:8dfbf62450e6 8
og 1:8dfbf62450e6 9 size_t HX8347D::println(char *c){
og 1:8dfbf62450e6 10 while (c[0] != 0) {
og 1:8dfbf62450e6 11 write(c[0]);
og 1:8dfbf62450e6 12 c++;
og 1:8dfbf62450e6 13 }
og 1:8dfbf62450e6 14 //drawString(cursor_x, cursor_y, c, textcolor);
og 1:8dfbf62450e6 15 return sizeof(c);
og 1:8dfbf62450e6 16 }
og 1:8dfbf62450e6 17 /*
og 1:8dfbf62450e6 18 size_t TFTLCD::println(unsigned long n , int t){
og 1:8dfbf62450e6 19 //drawString(cursor_x, cursor_y, n, textcolor);
og 1:8dfbf62450e6 20 }
og 1:8dfbf62450e6 21 */
og 1:8dfbf62450e6 22
og 1:8dfbf62450e6 23 HX8347D::HX8347D(
og 1:8dfbf62450e6 24 PinName rd, PinName wr, PinName rs, PinName cs, PinName rst,
og 1:8dfbf62450e6 25 PinName d0, PinName d1, PinName d2, PinName d3,
og 1:8dfbf62450e6 26 PinName d4, PinName d5, PinName d6, PinName d7
og 1:8dfbf62450e6 27 ):
og 1:8dfbf62450e6 28 _rd(rd), _wr(wr), _rs(rs), _cs(cs), _rst(rst),
og 1:8dfbf62450e6 29 _d(d0, d1, d2, d3, d4, d5, d6, d7)
og 1:8dfbf62450e6 30 {
og 1:8dfbf62450e6 31 rotation = 0;
og 1:8dfbf62450e6 32 _width = TFTWIDTH;
og 1:8dfbf62450e6 33 _height = TFTHEIGHT;
og 1:8dfbf62450e6 34
og 1:8dfbf62450e6 35 _rd=1;
og 1:8dfbf62450e6 36 _wr=1;
og 1:8dfbf62450e6 37 _rs=1;
og 1:8dfbf62450e6 38 _cs=1;
og 1:8dfbf62450e6 39 _rst = 1;
og 1:8dfbf62450e6 40
og 1:8dfbf62450e6 41 cursor_y = cursor_x = 0;
og 1:8dfbf62450e6 42 textsize = 1;
og 1:8dfbf62450e6 43 textcolor = 0xFFFF;
og 1:8dfbf62450e6 44 }
og 1:8dfbf62450e6 45
og 1:8dfbf62450e6 46
og 1:8dfbf62450e6 47 void HX8347D::goHome(void) {
og 1:8dfbf62450e6 48 goTo(0,0);
og 1:8dfbf62450e6 49 }
og 1:8dfbf62450e6 50
og 1:8dfbf62450e6 51 uint16_t HX8347D::width(void) {
og 1:8dfbf62450e6 52 return _width;
og 1:8dfbf62450e6 53 }
og 1:8dfbf62450e6 54 uint16_t HX8347D::height(void) {
og 1:8dfbf62450e6 55 return _height;
og 1:8dfbf62450e6 56 }
og 1:8dfbf62450e6 57
og 1:8dfbf62450e6 58 void HX8347D::setCursor(uint16_t x, uint16_t y) {
og 1:8dfbf62450e6 59 cursor_x = x;
og 1:8dfbf62450e6 60 cursor_y = y;
og 1:8dfbf62450e6 61 }
og 1:8dfbf62450e6 62
og 1:8dfbf62450e6 63 void HX8347D::setTextSize(uint8_t s) {
og 1:8dfbf62450e6 64 textsize = s;
og 1:8dfbf62450e6 65 }
og 1:8dfbf62450e6 66
og 1:8dfbf62450e6 67 void HX8347D::setTextColor(uint16_t c) {
og 1:8dfbf62450e6 68 textcolor = c;
og 1:8dfbf62450e6 69 }
og 1:8dfbf62450e6 70
og 1:8dfbf62450e6 71 size_t HX8347D::write(uint8_t c) {
og 1:8dfbf62450e6 72 if (c == '\n') {
og 1:8dfbf62450e6 73 cursor_y += textsize*8;
og 1:8dfbf62450e6 74 cursor_x = 0;
og 1:8dfbf62450e6 75 } else if (c == '\r') {
og 1:8dfbf62450e6 76 // skip em
og 1:8dfbf62450e6 77 } else {
og 1:8dfbf62450e6 78 drawChar(cursor_x, cursor_y, c, textcolor, textsize);
og 1:8dfbf62450e6 79 cursor_x += textsize*6;
og 1:8dfbf62450e6 80 }
og 1:8dfbf62450e6 81 return 0;
og 1:8dfbf62450e6 82 }
og 1:8dfbf62450e6 83
og 1:8dfbf62450e6 84 void HX8347D::drawString(uint16_t x, uint16_t y, char *c,
og 1:8dfbf62450e6 85 uint16_t color, uint8_t size) {
og 1:8dfbf62450e6 86 while (c[0] != 0) {
og 1:8dfbf62450e6 87 drawChar(x, y, c[0], color, size);
og 1:8dfbf62450e6 88 x += size*6;
og 1:8dfbf62450e6 89 c++;
og 1:8dfbf62450e6 90 }
og 1:8dfbf62450e6 91 }
og 1:8dfbf62450e6 92 // draw a character
og 1:8dfbf62450e6 93 void HX8347D::drawChar(uint16_t x, uint16_t y, char c,
og 1:8dfbf62450e6 94 uint16_t color, uint8_t size) {
og 1:8dfbf62450e6 95 for (uint8_t i =0; i<5; i++ ) {
og 1:8dfbf62450e6 96 uint8_t line = font[(c*5)+i];
og 1:8dfbf62450e6 97 for (uint8_t j = 0; j<8; j++) {
og 1:8dfbf62450e6 98 if (line & 0x1) {
og 1:8dfbf62450e6 99 if (size == 1) // default size
og 1:8dfbf62450e6 100 drawPixel(x+i, y+j, color);
og 1:8dfbf62450e6 101 else { // big size
og 1:8dfbf62450e6 102 fillRect(x+i*size, y+j*size, size, size, color);
og 1:8dfbf62450e6 103 }
og 1:8dfbf62450e6 104 }
og 1:8dfbf62450e6 105 line >>= 1;
og 1:8dfbf62450e6 106 }
og 1:8dfbf62450e6 107 }
og 1:8dfbf62450e6 108 }
og 1:8dfbf62450e6 109
og 1:8dfbf62450e6 110
og 1:8dfbf62450e6 111 void HX8347D::drawTriangle(uint16_t x0, uint16_t y0,
og 1:8dfbf62450e6 112 uint16_t x1, uint16_t y1,
og 1:8dfbf62450e6 113 uint16_t x2, uint16_t y2, uint16_t color)
og 1:8dfbf62450e6 114 {
og 1:8dfbf62450e6 115 drawLine(x0, y0, x1, y1, color);
og 1:8dfbf62450e6 116 drawLine(x1, y1, x2, y2, color);
og 1:8dfbf62450e6 117 drawLine(x2, y2, x0, y0, color);
og 1:8dfbf62450e6 118 }
og 1:8dfbf62450e6 119
og 1:8dfbf62450e6 120 void HX8347D::fillTriangle ( int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
og 1:8dfbf62450e6 121 {
og 1:8dfbf62450e6 122 if (y0 > y1) {
og 1:8dfbf62450e6 123 swap(y0, y1); swap(x0, x1);
og 1:8dfbf62450e6 124 }
og 1:8dfbf62450e6 125 if (y1 > y2) {
og 1:8dfbf62450e6 126 swap(y2, y1); swap(x2, x1);
og 1:8dfbf62450e6 127 }
og 1:8dfbf62450e6 128 if (y0 > y1) {
og 1:8dfbf62450e6 129 swap(y0, y1); swap(x0, x1);
og 1:8dfbf62450e6 130 }
og 1:8dfbf62450e6 131
og 1:8dfbf62450e6 132 int32_t dx1, dx2, dx3; // Interpolation deltas
og 1:8dfbf62450e6 133 int32_t sx1, sx2, sy; // Scanline co-ordinates
og 1:8dfbf62450e6 134
og 1:8dfbf62450e6 135 sx2=(int32_t)x0 * (int32_t)1000; // Use fixed point math for x axis values
og 1:8dfbf62450e6 136 sx1 = sx2;
og 1:8dfbf62450e6 137 sy=y0;
og 1:8dfbf62450e6 138
og 1:8dfbf62450e6 139 // Calculate interpolation deltas
og 1:8dfbf62450e6 140 if (y1-y0 > 0) dx1=((x1-x0)*1000)/(y1-y0);
og 1:8dfbf62450e6 141 else dx1=0;
og 1:8dfbf62450e6 142 if (y2-y0 > 0) dx2=((x2-x0)*1000)/(y2-y0);
og 1:8dfbf62450e6 143 else dx2=0;
og 1:8dfbf62450e6 144 if (y2-y1 > 0) dx3=((x2-x1)*1000)/(y2-y1);
og 1:8dfbf62450e6 145 else dx3=0;
og 1:8dfbf62450e6 146
og 1:8dfbf62450e6 147 // Render scanlines (horizontal lines are the fastest rendering method)
og 1:8dfbf62450e6 148 if (dx1 > dx2)
og 1:8dfbf62450e6 149 {
og 1:8dfbf62450e6 150 for(; sy<=y1; sy++, sx1+=dx2, sx2+=dx1)
og 1:8dfbf62450e6 151 {
og 1:8dfbf62450e6 152 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 1:8dfbf62450e6 153 }
og 1:8dfbf62450e6 154 sx2 = x1*1000;
og 1:8dfbf62450e6 155 sy = y1;
og 1:8dfbf62450e6 156 for(; sy<=y2; sy++, sx1+=dx2, sx2+=dx3)
og 1:8dfbf62450e6 157 {
og 1:8dfbf62450e6 158 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 1:8dfbf62450e6 159 }
og 1:8dfbf62450e6 160 }
og 1:8dfbf62450e6 161 else
og 1:8dfbf62450e6 162 {
og 1:8dfbf62450e6 163 for(; sy<=y1; sy++, sx1+=dx1, sx2+=dx2)
og 1:8dfbf62450e6 164 {
og 1:8dfbf62450e6 165 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 1:8dfbf62450e6 166 }
og 1:8dfbf62450e6 167 sx1 = x1*1000;
og 1:8dfbf62450e6 168 sy = y1;
og 1:8dfbf62450e6 169 for(; sy<=y2; sy++, sx1+=dx3, sx2+=dx2)
og 1:8dfbf62450e6 170 {
og 1:8dfbf62450e6 171 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 1:8dfbf62450e6 172 }
og 1:8dfbf62450e6 173 }
og 1:8dfbf62450e6 174 }
og 1:8dfbf62450e6 175
og 1:8dfbf62450e6 176 uint16_t HX8347D::Color565(uint8_t r, uint8_t g, uint8_t b) {
og 1:8dfbf62450e6 177 uint16_t c;
og 1:8dfbf62450e6 178 c = r >> 3;
og 1:8dfbf62450e6 179 c <<= 6;
og 1:8dfbf62450e6 180 c |= g >> 2;
og 1:8dfbf62450e6 181 c <<= 5;
og 1:8dfbf62450e6 182 c |= b >> 3;
og 1:8dfbf62450e6 183
og 1:8dfbf62450e6 184 //printf("%x\r\n", c);
og 1:8dfbf62450e6 185 return c;
og 1:8dfbf62450e6 186 }
og 1:8dfbf62450e6 187
og 1:8dfbf62450e6 188
og 1:8dfbf62450e6 189 // draw a rectangle
og 1:8dfbf62450e6 190 void HX8347D::drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
og 1:8dfbf62450e6 191 uint16_t color) {
og 1:8dfbf62450e6 192 // smarter version
og 1:8dfbf62450e6 193 drawHorizontalLine(x, y, w, color);
og 1:8dfbf62450e6 194 drawHorizontalLine(x, y+h-1, w, color);
og 1:8dfbf62450e6 195 drawVerticalLine(x, y, h, color);
og 1:8dfbf62450e6 196 drawVerticalLine(x+w-1, y, h, color);
og 1:8dfbf62450e6 197 }
og 1:8dfbf62450e6 198
og 1:8dfbf62450e6 199 // draw a rounded rectangle
og 1:8dfbf62450e6 200 void HX8347D::drawRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
og 1:8dfbf62450e6 201 uint16_t color) {
og 1:8dfbf62450e6 202 // smarter version
og 1:8dfbf62450e6 203 drawHorizontalLine(x+r, y, w-2*r, color);
og 1:8dfbf62450e6 204 drawHorizontalLine(x+r, y+h-1, w-2*r, color);
og 1:8dfbf62450e6 205 drawVerticalLine(x, y+r, h-2*r, color);
og 1:8dfbf62450e6 206 drawVerticalLine(x+w-1, y+r, h-2*r, color);
og 1:8dfbf62450e6 207 // draw four corners
og 1:8dfbf62450e6 208 drawCircleHelper(x+r, y+r, r, 1, color);
og 1:8dfbf62450e6 209 drawCircleHelper(x+w-r-1, y+r, r, 2, color);
og 1:8dfbf62450e6 210 drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
og 1:8dfbf62450e6 211 drawCircleHelper(x+r, y+h-r-1, r, 8, color);
og 1:8dfbf62450e6 212 }
og 1:8dfbf62450e6 213
og 1:8dfbf62450e6 214
og 1:8dfbf62450e6 215 // fill a rounded rectangle
og 1:8dfbf62450e6 216 void HX8347D::fillRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
og 1:8dfbf62450e6 217 uint16_t color) {
og 1:8dfbf62450e6 218 // smarter version
og 1:8dfbf62450e6 219 fillRect(x+r, y, w-2*r, h, color);
og 1:8dfbf62450e6 220
og 1:8dfbf62450e6 221 // draw four corners
og 1:8dfbf62450e6 222 fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
og 1:8dfbf62450e6 223 fillCircleHelper(x+r, y+r, r, 2, h-2*r-1, color);
og 1:8dfbf62450e6 224 }
og 1:8dfbf62450e6 225
og 1:8dfbf62450e6 226 // fill a circle
og 1:8dfbf62450e6 227 void HX8347D::fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
og 1:8dfbf62450e6 228 //writeRegister(TFTLCD_ENTRY_MOD, 0x1030);
og 1:8dfbf62450e6 229 drawVerticalLine(x0, y0-r, 2*r+1, color);
og 1:8dfbf62450e6 230 fillCircleHelper(x0, y0, r, 3, 0, color);
og 1:8dfbf62450e6 231 }
og 1:8dfbf62450e6 232
og 1:8dfbf62450e6 233
og 1:8dfbf62450e6 234 // used to do circles and roundrects!
og 1:8dfbf62450e6 235 void HX8347D::fillCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername, uint16_t delta,
og 1:8dfbf62450e6 236 uint16_t color) {
og 1:8dfbf62450e6 237
og 1:8dfbf62450e6 238 int16_t f = 1 - r;
og 1:8dfbf62450e6 239 int16_t ddF_x = 1;
og 1:8dfbf62450e6 240 int16_t ddF_y = -2 * r;
og 1:8dfbf62450e6 241 int16_t x = 0;
og 1:8dfbf62450e6 242 int16_t y = r;
og 1:8dfbf62450e6 243
og 1:8dfbf62450e6 244 while (x<y) {
og 1:8dfbf62450e6 245 if (f >= 0) {
og 1:8dfbf62450e6 246 y--;
og 1:8dfbf62450e6 247 ddF_y += 2;
og 1:8dfbf62450e6 248 f += ddF_y;
og 1:8dfbf62450e6 249 }
og 1:8dfbf62450e6 250 x++;
og 1:8dfbf62450e6 251 ddF_x += 2;
og 1:8dfbf62450e6 252 f += ddF_x;
og 1:8dfbf62450e6 253
og 1:8dfbf62450e6 254 if (cornername & 0x1) {
og 1:8dfbf62450e6 255 drawVerticalLine(x0+x, y0-y, 2*y+1+delta, color);
og 1:8dfbf62450e6 256 drawVerticalLine(x0+y, y0-x, 2*x+1+delta, color);
og 1:8dfbf62450e6 257 }
og 1:8dfbf62450e6 258 if (cornername & 0x2) {
og 1:8dfbf62450e6 259 drawVerticalLine(x0-x, y0-y, 2*y+1+delta, color);
og 1:8dfbf62450e6 260 drawVerticalLine(x0-y, y0-x, 2*x+1+delta, color);
og 1:8dfbf62450e6 261 }
og 1:8dfbf62450e6 262 }
og 1:8dfbf62450e6 263 }
og 1:8dfbf62450e6 264
og 1:8dfbf62450e6 265
og 1:8dfbf62450e6 266 // draw a circle outline
og 1:8dfbf62450e6 267
og 1:8dfbf62450e6 268 void HX8347D::drawCircle(uint16_t x0, uint16_t y0, uint16_t r,
og 1:8dfbf62450e6 269 uint16_t color) {
og 1:8dfbf62450e6 270 drawPixel(x0, y0+r, color);
og 1:8dfbf62450e6 271 drawPixel(x0, y0-r, color);
og 1:8dfbf62450e6 272 drawPixel(x0+r, y0, color);
og 1:8dfbf62450e6 273 drawPixel(x0-r, y0, color);
og 1:8dfbf62450e6 274
og 1:8dfbf62450e6 275 drawCircleHelper(x0, y0, r, 0xF, color);
og 1:8dfbf62450e6 276 }
og 1:8dfbf62450e6 277
og 1:8dfbf62450e6 278 void HX8347D::drawCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername,
og 1:8dfbf62450e6 279 uint16_t color) {
og 1:8dfbf62450e6 280 int16_t f = 1 - r;
og 1:8dfbf62450e6 281 int16_t ddF_x = 1;
og 1:8dfbf62450e6 282 int16_t ddF_y = -2 * r;
og 1:8dfbf62450e6 283 int16_t x = 0;
og 1:8dfbf62450e6 284 int16_t y = r;
og 1:8dfbf62450e6 285
og 1:8dfbf62450e6 286
og 1:8dfbf62450e6 287 while (x<y) {
og 1:8dfbf62450e6 288 if (f >= 0) {
og 1:8dfbf62450e6 289 y--;
og 1:8dfbf62450e6 290 ddF_y += 2;
og 1:8dfbf62450e6 291 f += ddF_y;
og 1:8dfbf62450e6 292 }
og 1:8dfbf62450e6 293 x++;
og 1:8dfbf62450e6 294 ddF_x += 2;
og 1:8dfbf62450e6 295 f += ddF_x;
og 1:8dfbf62450e6 296 if (cornername & 0x4) {
og 1:8dfbf62450e6 297 drawPixel(x0 + x, y0 + y, color);
og 1:8dfbf62450e6 298 drawPixel(x0 + y, y0 + x, color);
og 1:8dfbf62450e6 299 }
og 1:8dfbf62450e6 300 if (cornername & 0x2) {
og 1:8dfbf62450e6 301 drawPixel(x0 + x, y0 - y, color);
og 1:8dfbf62450e6 302 drawPixel(x0 + y, y0 - x, color);
og 1:8dfbf62450e6 303 }
og 1:8dfbf62450e6 304 if (cornername & 0x8) {
og 1:8dfbf62450e6 305 drawPixel(x0 - y, y0 + x, color);
og 1:8dfbf62450e6 306 drawPixel(x0 - x, y0 + y, color);
og 1:8dfbf62450e6 307 }
og 1:8dfbf62450e6 308 if (cornername & 0x1) {
og 1:8dfbf62450e6 309 drawPixel(x0 - y, y0 - x, color);
og 1:8dfbf62450e6 310 drawPixel(x0 - x, y0 - y, color);
og 1:8dfbf62450e6 311 }
og 1:8dfbf62450e6 312 }
og 1:8dfbf62450e6 313 }
og 1:8dfbf62450e6 314
og 1:8dfbf62450e6 315 // fill a rectangle
og 1:8dfbf62450e6 316 void HX8347D::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
og 1:8dfbf62450e6 317 uint16_t fillcolor) {
og 1:8dfbf62450e6 318 // smarter version
og 1:8dfbf62450e6 319 while (h--)
og 1:8dfbf62450e6 320 drawHorizontalLine(x, y++, w, fillcolor);
og 1:8dfbf62450e6 321 }
og 1:8dfbf62450e6 322
og 1:8dfbf62450e6 323
og 1:8dfbf62450e6 324 void HX8347D::drawVerticalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
og 1:8dfbf62450e6 325 {
og 1:8dfbf62450e6 326 if (x >= _width) return;
og 1:8dfbf62450e6 327
og 1:8dfbf62450e6 328 drawFastLine(x,y,length,color,1);
og 1:8dfbf62450e6 329 //drawLine(x, y, x, y+length, color);
og 1:8dfbf62450e6 330 }
og 1:8dfbf62450e6 331
og 1:8dfbf62450e6 332 void HX8347D::drawHorizontalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
og 1:8dfbf62450e6 333 {
og 1:8dfbf62450e6 334 if (y >= _height) return;
og 1:8dfbf62450e6 335 drawFastLine(x,y,length,color,0);
og 1:8dfbf62450e6 336 //drawLine(x, y, x+length, y, color);
og 1:8dfbf62450e6 337 }
og 1:8dfbf62450e6 338
og 1:8dfbf62450e6 339
og 1:8dfbf62450e6 340 void HX8347D::setRotation(uint8_t x) {
og 1:8dfbf62450e6 341
og 1:8dfbf62450e6 342 x %= 4; // cant be higher than 3
og 1:8dfbf62450e6 343 rotation = x;
og 1:8dfbf62450e6 344 switch (x) {
og 1:8dfbf62450e6 345 case 0:
og 2:11512f68bcc8 346 writeRegister(HX8347D_MEM_ACC_CTRL, 0x08); //0x1030
og 1:8dfbf62450e6 347 _width = TFTWIDTH;
og 1:8dfbf62450e6 348 _height = TFTHEIGHT;
og 1:8dfbf62450e6 349 break;
og 1:8dfbf62450e6 350 case 1:
og 2:11512f68bcc8 351 writeRegister(HX8347D_MEM_ACC_CTRL, 0x68); //0x01028
og 1:8dfbf62450e6 352 _width = TFTHEIGHT;
og 1:8dfbf62450e6 353 _height = TFTWIDTH;
og 1:8dfbf62450e6 354 break;
og 1:8dfbf62450e6 355 case 2:
og 2:11512f68bcc8 356 writeRegister(HX8347D_MEM_ACC_CTRL, 0xC8); //0x1000
og 1:8dfbf62450e6 357 _width = TFTWIDTH;
og 1:8dfbf62450e6 358 _height = TFTHEIGHT;
og 1:8dfbf62450e6 359 break;
og 1:8dfbf62450e6 360 case 3:
og 2:11512f68bcc8 361 writeRegister(HX8347D_MEM_ACC_CTRL, 0xA8 ); //0x1018
og 1:8dfbf62450e6 362 _width = TFTHEIGHT;
og 1:8dfbf62450e6 363 _height = TFTWIDTH;
og 1:8dfbf62450e6 364 break;
og 1:8dfbf62450e6 365 }
og 1:8dfbf62450e6 366 //setDefaultViewport();
og 1:8dfbf62450e6 367 }
og 1:8dfbf62450e6 368
og 1:8dfbf62450e6 369 void HX8347D::drawFastLine(uint16_t x, uint16_t y, uint16_t length,
og 1:8dfbf62450e6 370 uint16_t color, uint8_t rotflag)
og 1:8dfbf62450e6 371 {
og 1:8dfbf62450e6 372 uint16_t x1 = width() - 1;
og 1:8dfbf62450e6 373 uint16_t y1 = height() - 1;
og 1:8dfbf62450e6 374 switch (rotation) {
og 1:8dfbf62450e6 375 case 0:
og 1:8dfbf62450e6 376 x = x;
og 1:8dfbf62450e6 377 y = y;
og 1:8dfbf62450e6 378 if (rotflag) {
og 1:8dfbf62450e6 379 x1 = x;
og 1:8dfbf62450e6 380 y1 = y + length;
og 1:8dfbf62450e6 381 }
og 1:8dfbf62450e6 382 else {
og 1:8dfbf62450e6 383 x1 = x + length;
og 1:8dfbf62450e6 384 y1 = y;
og 1:8dfbf62450e6 385 }
og 1:8dfbf62450e6 386 break;
og 1:8dfbf62450e6 387 case 1:
og 1:8dfbf62450e6 388 x = x;
og 1:8dfbf62450e6 389 y = y;
og 1:8dfbf62450e6 390 if (rotflag) {
og 1:8dfbf62450e6 391 x1 = x;
og 1:8dfbf62450e6 392 y1 = y + length;
og 1:8dfbf62450e6 393 }
og 1:8dfbf62450e6 394 else {
og 1:8dfbf62450e6 395 x1 = x + length;
og 1:8dfbf62450e6 396 y1 = y;
og 1:8dfbf62450e6 397 }
og 1:8dfbf62450e6 398 break;
og 1:8dfbf62450e6 399 case 2:
og 1:8dfbf62450e6 400 x = x;
og 1:8dfbf62450e6 401 y = y;
og 1:8dfbf62450e6 402 if (rotflag) {
og 1:8dfbf62450e6 403 x1 = x;
og 1:8dfbf62450e6 404 y1 = y + length;
og 1:8dfbf62450e6 405 }
og 1:8dfbf62450e6 406 else {
og 1:8dfbf62450e6 407 x1 = x + length;
og 1:8dfbf62450e6 408 y1 = y;
og 1:8dfbf62450e6 409 }
og 1:8dfbf62450e6 410 break;
og 1:8dfbf62450e6 411 case 3:
og 1:8dfbf62450e6 412 x = x;
og 1:8dfbf62450e6 413 y = y;
og 1:8dfbf62450e6 414 if (rotflag) {
og 1:8dfbf62450e6 415 x1 = x;
og 1:8dfbf62450e6 416 y1 = y + length;
og 1:8dfbf62450e6 417 }
og 1:8dfbf62450e6 418 else {
og 1:8dfbf62450e6 419 x1 = x + length;
og 1:8dfbf62450e6 420 y1 = y;
og 1:8dfbf62450e6 421 }
og 1:8dfbf62450e6 422 //swap(x, x1)
og 1:8dfbf62450e6 423 break;
og 1:8dfbf62450e6 424 }
og 1:8dfbf62450e6 425 if (x1 >= width()) {
og 1:8dfbf62450e6 426 x1 = width() - 1;
og 1:8dfbf62450e6 427 }
og 1:8dfbf62450e6 428 if (y1 >= height()) {
og 1:8dfbf62450e6 429 y1 = height() - 1;
og 1:8dfbf62450e6 430 }
og 1:8dfbf62450e6 431
og 1:8dfbf62450e6 432 writeRegister(HX8347D_COL_AD_START2,x>>8);
og 1:8dfbf62450e6 433 writeRegister(HX8347D_COL_AD_START1,x);
og 1:8dfbf62450e6 434 writeRegister(HX8347D_COL_AD_END2,x1>>8);
og 1:8dfbf62450e6 435 writeRegister(HX8347D_COL_AD_END1,x1);
og 1:8dfbf62450e6 436 writeRegister(HX8347D_ROW_AD_START2,y>>8);
og 1:8dfbf62450e6 437 writeRegister(HX8347D_ROW_AD_START1,y);
og 1:8dfbf62450e6 438 writeRegister(HX8347D_ROW_AD_END2,y1>>8);
og 1:8dfbf62450e6 439 writeRegister(HX8347D_ROW_AD_END1,y1);
og 1:8dfbf62450e6 440
og 1:8dfbf62450e6 441 writeCommand(HX8347D_SRAM_WR_CTRL); // Write Data to GRAM (R22h)
og 1:8dfbf62450e6 442
og 1:8dfbf62450e6 443
og 1:8dfbf62450e6 444 setWriteDir();
og 1:8dfbf62450e6 445 _cs = 0;
og 1:8dfbf62450e6 446 //digitalWrite(_cs, LOW);
og 1:8dfbf62450e6 447 _rs = 1;
og 1:8dfbf62450e6 448 //digitalWrite(_cd, HIGH);
og 1:8dfbf62450e6 449 _rd = 1;
og 1:8dfbf62450e6 450 //digitalWrite(_rd, HIGH);
og 1:8dfbf62450e6 451 _wr = 1;
og 1:8dfbf62450e6 452 //digitalWrite(_wr, HIGH);
og 1:8dfbf62450e6 453
og 1:8dfbf62450e6 454 while (length--) {
og 1:8dfbf62450e6 455 writeData_unsafe(color);
og 1:8dfbf62450e6 456 }
og 1:8dfbf62450e6 457
og 1:8dfbf62450e6 458 // set back to default
og 1:8dfbf62450e6 459 _cs = 1;
og 1:8dfbf62450e6 460 //digitalWrite(_cs, HIGH);
og 1:8dfbf62450e6 461
og 1:8dfbf62450e6 462 }
og 1:8dfbf62450e6 463
og 1:8dfbf62450e6 464
og 1:8dfbf62450e6 465 void HX8347D::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
og 1:8dfbf62450e6 466 uint16_t color) {
og 1:8dfbf62450e6 467 // if you're in rotation 1 or 3, we need to swap the X and Y's
og 1:8dfbf62450e6 468
og 1:8dfbf62450e6 469 int16_t steep = abs(y1 - y0) > abs(x1 - x0);
og 1:8dfbf62450e6 470 if (steep) {
og 1:8dfbf62450e6 471 swap(x0, y0);
og 1:8dfbf62450e6 472 swap(x1, y1);
og 1:8dfbf62450e6 473 }
og 1:8dfbf62450e6 474
og 1:8dfbf62450e6 475 if (x0 > x1) {
og 1:8dfbf62450e6 476 swap(x0, x1);
og 1:8dfbf62450e6 477 swap(y0, y1);
og 1:8dfbf62450e6 478 }
og 1:8dfbf62450e6 479
og 1:8dfbf62450e6 480 int16_t dx, dy;
og 1:8dfbf62450e6 481 dx = x1 - x0;
og 1:8dfbf62450e6 482 //dy = abs(y1 - y0);
og 1:8dfbf62450e6 483 dy = abs(y1 - y0);
og 1:8dfbf62450e6 484
og 1:8dfbf62450e6 485 int16_t err = dx / 2;
og 1:8dfbf62450e6 486 int16_t ystep;
og 1:8dfbf62450e6 487
og 1:8dfbf62450e6 488 if (y0 < y1) {
og 1:8dfbf62450e6 489 ystep = 1;
og 1:8dfbf62450e6 490 } else {
og 1:8dfbf62450e6 491 ystep = -1;}
og 1:8dfbf62450e6 492
og 1:8dfbf62450e6 493 for (; x0<=x1; x0++) {
og 1:8dfbf62450e6 494 if (steep) {
og 1:8dfbf62450e6 495 drawPixel(y0, x0, color);
og 1:8dfbf62450e6 496 } else {
og 1:8dfbf62450e6 497 drawPixel(x0, y0, color);
og 1:8dfbf62450e6 498 }
og 1:8dfbf62450e6 499 err -= dy;
og 1:8dfbf62450e6 500 if (err < 0) {
og 1:8dfbf62450e6 501 y0 += ystep;
og 1:8dfbf62450e6 502 err += dx;
og 1:8dfbf62450e6 503 }
og 1:8dfbf62450e6 504 }
og 1:8dfbf62450e6 505 }
og 1:8dfbf62450e6 506
og 1:8dfbf62450e6 507
og 1:8dfbf62450e6 508
og 1:8dfbf62450e6 509
og 1:8dfbf62450e6 510 void HX8347D::fillScreen(uint16_t color) {
og 1:8dfbf62450e6 511 goHome();
og 1:8dfbf62450e6 512 uint32_t i;
og 1:8dfbf62450e6 513 i = 320;
og 1:8dfbf62450e6 514 i *= 240;
og 1:8dfbf62450e6 515 _cs = 0;
og 1:8dfbf62450e6 516 _rs = 1;
og 1:8dfbf62450e6 517 _rd = 1;
og 1:8dfbf62450e6 518 _wr = 1;
og 1:8dfbf62450e6 519 setWriteDir();
og 1:8dfbf62450e6 520 while (i--) {
og 1:8dfbf62450e6 521 writeData_unsafe(color);
og 1:8dfbf62450e6 522 }
og 1:8dfbf62450e6 523 _cs = 1;
og 1:8dfbf62450e6 524 }
og 1:8dfbf62450e6 525
og 1:8dfbf62450e6 526 void HX8347D::drawPixel(uint16_t x, uint16_t y, uint16_t color)
og 1:8dfbf62450e6 527 {
og 1:8dfbf62450e6 528 writeRegister(HX8347D_COL_AD_START2,x>>8);
og 1:8dfbf62450e6 529 writeRegister(HX8347D_COL_AD_START1,x);
og 1:8dfbf62450e6 530 writeRegister(HX8347D_COL_AD_END2,x>>8);
og 1:8dfbf62450e6 531 writeRegister(HX8347D_COL_AD_END1,x);
og 1:8dfbf62450e6 532 writeRegister(HX8347D_ROW_AD_START2,y>>8);
og 1:8dfbf62450e6 533 writeRegister(HX8347D_ROW_AD_START1,y);
og 1:8dfbf62450e6 534 writeRegister(HX8347D_ROW_AD_END2,y>>8);
og 1:8dfbf62450e6 535 writeRegister(HX8347D_ROW_AD_END1,y);
og 1:8dfbf62450e6 536
og 1:8dfbf62450e6 537 writeCommand(HX8347D_SRAM_WR_CTRL); // Write Data to GRAM (R22h)
og 1:8dfbf62450e6 538 writeData(color);
og 1:8dfbf62450e6 539
og 1:8dfbf62450e6 540 }
og 1:8dfbf62450e6 541
og 1:8dfbf62450e6 542
og 1:8dfbf62450e6 543 static const uint16_t _regValues[] = {
og 1:8dfbf62450e6 544 //HX8347D_CM22()
og 1:8dfbf62450e6 545 0x002E,0x0079, //
og 1:8dfbf62450e6 546 0x00EE,0x000C, //
og 1:8dfbf62450e6 547 //Driving ability Setting
og 1:8dfbf62450e6 548 0x00EA,0x0000, //PTBA[15:8]
og 1:8dfbf62450e6 549 0x00EB,0x0020, //PTBA[7:0]
og 1:8dfbf62450e6 550 0x00EC,0x0008, //STBA[15:8]
og 1:8dfbf62450e6 551 0x00ED,0x00C4, //STBA[7:0]
og 1:8dfbf62450e6 552 0x00E8,0x0040, //OPON[7:0]
og 1:8dfbf62450e6 553 0x00E9,0x0038, //OPON1[7:0]
og 1:8dfbf62450e6 554 0x00F1,0x0001, //OTPS1B
og 1:8dfbf62450e6 555 0x00F2,0x0010, //GEN
og 1:8dfbf62450e6 556 0x0027,0x00A3, //
og 1:8dfbf62450e6 557 //Gamma 2.2 Setting
og 1:8dfbf62450e6 558 0x0040,0x0000, //
og 1:8dfbf62450e6 559 0x0041,0x0000, //
og 1:8dfbf62450e6 560 0x0042,0x0001, //
og 1:8dfbf62450e6 561 0x0043,0x0012, //
og 1:8dfbf62450e6 562 0x0044,0x0010, //
og 1:8dfbf62450e6 563 0x0045,0x0026, //
og 1:8dfbf62450e6 564 0x0046,0x0008, //
og 1:8dfbf62450e6 565 0x0047,0x0053, //
og 1:8dfbf62450e6 566 0x0048,0x0002, //
og 1:8dfbf62450e6 567 0x0049,0x0015, //
og 1:8dfbf62450e6 568 0x004A,0x0019, //
og 1:8dfbf62450e6 569 0x004B,0x0019, //
og 1:8dfbf62450e6 570 0x004C,0x0016, //
og 1:8dfbf62450e6 571 0x0050,0x0019, //
og 1:8dfbf62450e6 572 0x0051,0x002F, //
og 1:8dfbf62450e6 573 0x0052,0x002D, //
og 1:8dfbf62450e6 574 0x0053,0x003E, //
og 1:8dfbf62450e6 575 0x0054,0x003F, //
og 1:8dfbf62450e6 576 0x0055,0x003F, //
og 1:8dfbf62450e6 577 0x0056,0x002C, //
og 1:8dfbf62450e6 578 0x0057,0x0077, //
og 1:8dfbf62450e6 579 0x0058,0x0009, //
og 1:8dfbf62450e6 580 0x0059,0x0006, //
og 1:8dfbf62450e6 581 0x005A,0x0006, //
og 1:8dfbf62450e6 582 0x005B,0x000A, //
og 1:8dfbf62450e6 583 0x005C,0x001D, //
og 1:8dfbf62450e6 584 0x005D,0x00CC, //
og 1:8dfbf62450e6 585 //Power Voltage Setting
og 1:8dfbf62450e6 586 0x001B,0x001B, //VRH=4.65V
og 1:8dfbf62450e6 587 0x001A,0x0001, //BT (VGH~15V);(VGL~-10V);(DDVDH~5V)
og 1:8dfbf62450e6 588 0x0024,0x002F, //VMH(VCOM High voltage ~3.2V)
og 1:8dfbf62450e6 589 0x0025,0x0057, //VML(VCOM Low voltage -1.2V)
og 1:8dfbf62450e6 590 //****VCOM offset**///
og 1:8dfbf62450e6 591 0x0023,0x0097, //for Flicker adjust //can reload from OTP
og 1:8dfbf62450e6 592 //Power on Setting
og 1:8dfbf62450e6 593 0x0018,0x0036, //I/P_RADJ);(N/P_RADJ);( Normal mode 75Hz
og 1:8dfbf62450e6 594 0x0019,0x0001, //OSC_EN='1');( start Osc
og 1:8dfbf62450e6 595 0x0001,0x0000, //DP_STB='0');( out deep sleep
og 1:8dfbf62450e6 596 0x001F,0x0088,// GAS=1);( VOMG=00);( PON=0);( DK=1);( XDK=0)
og 1:8dfbf62450e6 597
og 1:8dfbf62450e6 598 0xF0,5,
og 1:8dfbf62450e6 599 0x001F,0x0080,// GAS=1);( VOMG=00);( PON=0);( DK=0);( XDK=0);(
og 1:8dfbf62450e6 600
og 1:8dfbf62450e6 601 0xF0,5,
og 1:8dfbf62450e6 602 0x001F,0x0090,// GAS=1);( VOMG=00);( PON=1);( DK=0);( XDK=0);(
og 1:8dfbf62450e6 603
og 1:8dfbf62450e6 604 0xF0,5,
og 1:8dfbf62450e6 605 0x001F,0x00D0,// GAS=1);( VOMG=10);( PON=1);( DK=0);( XDK=0);(
og 1:8dfbf62450e6 606
og 1:8dfbf62450e6 607 0xF0,5,
og 1:8dfbf62450e6 608 //262k/65k color selection
og 1:8dfbf62450e6 609 0x0017,0x0005, //default 0x0006 262k color // 0x0005 65k color
og 1:8dfbf62450e6 610 //SET PANEL
og 1:8dfbf62450e6 611 0x0036,0x0000, //SS_P);( GS_P);(REV_P);(BGR_P
og 1:8dfbf62450e6 612 //Display ON Setting
og 1:8dfbf62450e6 613 0x0028,0x0038, //GON=1);( DTE=1);( D=1000
og 1:8dfbf62450e6 614 0xF0,40,
og 1:8dfbf62450e6 615 0x0028,0x003C, //GON=1);( DTE=1);( D=1100
og 1:8dfbf62450e6 616 //
og 1:8dfbf62450e6 617 0x0016, 0x0008,
og 1:8dfbf62450e6 618 //Set GRAM Area
og 1:8dfbf62450e6 619 0x0002,0x0000,
og 1:8dfbf62450e6 620 0x0003,0x0000, //Column Start
og 1:8dfbf62450e6 621 0x0004,0x0000,
og 1:8dfbf62450e6 622 0x0005,0x00EF, //Column End
og 1:8dfbf62450e6 623 0x0006,0x0000,
og 1:8dfbf62450e6 624 0x0007,0x0000, //Row Start
og 1:8dfbf62450e6 625 0x0008,0x0001,
og 1:8dfbf62450e6 626 0x0009,0x003F, //Row End
og 1:8dfbf62450e6 627 0xF1, 0x0022, //Start GRAM write
og 1:8dfbf62450e6 628 };
og 1:8dfbf62450e6 629
og 1:8dfbf62450e6 630 void HX8347D::initDisplay(void) {
og 1:8dfbf62450e6 631 uint16_t a, d;
og 1:8dfbf62450e6 632 reset();
og 1:8dfbf62450e6 633 for (uint8_t i = 0; i < sizeof(_regValues) / 4; i++) {
og 1:8dfbf62450e6 634 a = _regValues[i*2];
og 1:8dfbf62450e6 635 d = _regValues[i*2 + 1];
og 3:233295bc63af 636 if (a == 0xF0) { // _delay
og 3:233295bc63af 637 _delay(d);
og 1:8dfbf62450e6 638 } else if (a== 0xF1) { // write command
og 1:8dfbf62450e6 639 writeCommand(d);
og 1:8dfbf62450e6 640 } else
og 1:8dfbf62450e6 641 {
og 1:8dfbf62450e6 642 writeRegister(a, d);
og 1:8dfbf62450e6 643 }
og 1:8dfbf62450e6 644 }
og 1:8dfbf62450e6 645 }
og 1:8dfbf62450e6 646
og 1:8dfbf62450e6 647 uint8_t HX8347D::getRotation(void) {
og 1:8dfbf62450e6 648 return rotation;
og 1:8dfbf62450e6 649 }
og 1:8dfbf62450e6 650
og 1:8dfbf62450e6 651
og 1:8dfbf62450e6 652 void HX8347D::BlockWrite(uint16_t Xstart,uint16_t Xend,uint16_t Ystart,uint16_t Yend)
og 1:8dfbf62450e6 653 {
og 1:8dfbf62450e6 654
og 1:8dfbf62450e6 655 writeRegister(HX8347D_COL_AD_START2,Xstart>>8);
og 1:8dfbf62450e6 656 writeRegister(HX8347D_COL_AD_START1,Xstart);
og 1:8dfbf62450e6 657 writeRegister(HX8347D_COL_AD_END2,Xend>>8);
og 1:8dfbf62450e6 658 writeRegister(HX8347D_COL_AD_END1,Xend);
og 1:8dfbf62450e6 659
og 1:8dfbf62450e6 660 writeRegister(HX8347D_ROW_AD_START2,Ystart>>8);
og 1:8dfbf62450e6 661 writeRegister(HX8347D_ROW_AD_START1,Ystart);
og 1:8dfbf62450e6 662 writeRegister(HX8347D_ROW_AD_END2,Yend>>8);
og 1:8dfbf62450e6 663 writeRegister(HX8347D_ROW_AD_END1,Yend);
og 1:8dfbf62450e6 664
og 1:8dfbf62450e6 665 writeCommand(HX8347D_SRAM_WR_CTRL); // write ram
og 1:8dfbf62450e6 666 }
og 1:8dfbf62450e6 667 void HX8347D::DispColor(uint16_t color)
og 1:8dfbf62450e6 668 {
og 1:8dfbf62450e6 669 uint16_t i,j;
og 1:8dfbf62450e6 670
og 1:8dfbf62450e6 671 BlockWrite(0,TFTWIDTH-1,0,TFTHEIGHT-1);// #define ROW 320 #define COL 240
og 1:8dfbf62450e6 672
og 1:8dfbf62450e6 673 _cs=0;
og 1:8dfbf62450e6 674 _rs=1;
og 1:8dfbf62450e6 675 _rd=1;
og 1:8dfbf62450e6 676
og 1:8dfbf62450e6 677 for(i=0;i<TFTHEIGHT;i++)
og 1:8dfbf62450e6 678 {
og 1:8dfbf62450e6 679 for(j=0;j<TFTWIDTH;j++)
og 1:8dfbf62450e6 680 {
og 1:8dfbf62450e6 681 writeData_unsafe(color);
og 1:8dfbf62450e6 682 }
og 1:8dfbf62450e6 683 }
og 1:8dfbf62450e6 684
og 1:8dfbf62450e6 685 _cs=1;
og 1:8dfbf62450e6 686 }
og 1:8dfbf62450e6 687
og 1:8dfbf62450e6 688 void HX8347D::reset(void)
og 1:8dfbf62450e6 689 {
og 1:8dfbf62450e6 690 _rst=0;
og 3:233295bc63af 691 _delay(1000);
og 1:8dfbf62450e6 692 _rst=1;
og 3:233295bc63af 693 _delay(1000);
og 1:8dfbf62450e6 694 }
og 1:8dfbf62450e6 695
og 1:8dfbf62450e6 696 void HX8347D::setWriteDir(void) {
og 1:8dfbf62450e6 697 _d.output();
og 1:8dfbf62450e6 698 }
og 1:8dfbf62450e6 699
og 1:8dfbf62450e6 700 void HX8347D::setReadDir(void) {
og 1:8dfbf62450e6 701 _d.input();
og 1:8dfbf62450e6 702 }
og 1:8dfbf62450e6 703
og 1:8dfbf62450e6 704 void HX8347D::write8(uint8_t d) {
og 1:8dfbf62450e6 705 _d = d;
og 1:8dfbf62450e6 706 }
og 1:8dfbf62450e6 707 uint8_t HX8347D::read8(void) {
og 1:8dfbf62450e6 708 return _d;
og 1:8dfbf62450e6 709 }
og 1:8dfbf62450e6 710
og 1:8dfbf62450e6 711 void HX8347D::_write(uint16_t rs, uint16_t d) {
og 1:8dfbf62450e6 712 _cs=0;
og 1:8dfbf62450e6 713 _rd=1;
og 1:8dfbf62450e6 714 _rs=rs;
og 1:8dfbf62450e6 715
og 1:8dfbf62450e6 716 _d.output();
og 1:8dfbf62450e6 717 _d=d>>8;
og 1:8dfbf62450e6 718 _wr=0;
og 3:233295bc63af 719 _delay(1);
og 1:8dfbf62450e6 720 _wr=1;
og 1:8dfbf62450e6 721
og 1:8dfbf62450e6 722 _d=d;
og 1:8dfbf62450e6 723 _wr=0;
og 3:233295bc63af 724 _delay(1);
og 1:8dfbf62450e6 725 _wr=1;
og 1:8dfbf62450e6 726
og 1:8dfbf62450e6 727 _cs=1;
og 1:8dfbf62450e6 728 }
og 1:8dfbf62450e6 729
og 1:8dfbf62450e6 730 void HX8347D::writeData_unsafe(uint16_t d)
og 1:8dfbf62450e6 731 {
og 1:8dfbf62450e6 732 //_d.output();
og 1:8dfbf62450e6 733 _d = d >> 8;
og 1:8dfbf62450e6 734
og 1:8dfbf62450e6 735 _wr = 0;
og 3:233295bc63af 736 _delay(1);
og 1:8dfbf62450e6 737 _wr = 1;
og 1:8dfbf62450e6 738
og 1:8dfbf62450e6 739 _d = d;
og 1:8dfbf62450e6 740
og 1:8dfbf62450e6 741 _wr = 0;
og 3:233295bc63af 742 _delay(1);
og 1:8dfbf62450e6 743 _wr = 1;
og 1:8dfbf62450e6 744 }
og 1:8dfbf62450e6 745
og 1:8dfbf62450e6 746 uint16_t HX8347D::readData(){
og 1:8dfbf62450e6 747 uint16_t d = 0;
og 1:8dfbf62450e6 748 _d.input();
og 1:8dfbf62450e6 749 _cs = 0;
og 1:8dfbf62450e6 750 _rs = 1;
og 1:8dfbf62450e6 751 _wr = 1;
og 1:8dfbf62450e6 752 _rd = 1;
og 1:8dfbf62450e6 753
og 1:8dfbf62450e6 754 //wait_us(1);
og 1:8dfbf62450e6 755 //_rd = 0;
og 1:8dfbf62450e6 756 //wait_us(10);
og 1:8dfbf62450e6 757 //d = _d;
og 1:8dfbf62450e6 758 //d <<= 8;
og 1:8dfbf62450e6 759 //_rd = 1;
og 1:8dfbf62450e6 760
og 3:233295bc63af 761 _delay(1);
og 1:8dfbf62450e6 762 _rd = 0;
og 3:233295bc63af 763 _delay(10);
og 1:8dfbf62450e6 764 d = _d;
og 1:8dfbf62450e6 765 d <<= 8;
og 1:8dfbf62450e6 766 _rd = 1;
og 1:8dfbf62450e6 767
og 3:233295bc63af 768 _delay(1);
og 1:8dfbf62450e6 769 _rd = 0;
og 3:233295bc63af 770 _delay(10);
og 1:8dfbf62450e6 771 d |= _d;
og 1:8dfbf62450e6 772 _rd = 1;
og 1:8dfbf62450e6 773 _cs = 1;
og 1:8dfbf62450e6 774 return d;
og 1:8dfbf62450e6 775 }
og 1:8dfbf62450e6 776
og 1:8dfbf62450e6 777
og 1:8dfbf62450e6 778 /************************************* medium level data reading/writing */
og 1:8dfbf62450e6 779
og 1:8dfbf62450e6 780 uint16_t HX8347D::readRegister(uint16_t addr) {
og 1:8dfbf62450e6 781 writeCommand(addr);
og 1:8dfbf62450e6 782 return readData();
og 1:8dfbf62450e6 783 }
og 1:8dfbf62450e6 784 void HX8347D::writeRegister(uint16_t addr, uint16_t data) {
og 1:8dfbf62450e6 785 writeCommand(addr);
og 1:8dfbf62450e6 786 writeData(data);
og 1:8dfbf62450e6 787 }
og 1:8dfbf62450e6 788
og 1:8dfbf62450e6 789 void HX8347D::goTo(uint16_t x, uint16_t y) {
og 1:8dfbf62450e6 790 uint16_t x1 = width() - 1;
og 1:8dfbf62450e6 791 uint16_t y1 = height() - 1;
og 1:8dfbf62450e6 792 //calcGRAMPosition(&x, &y);
og 1:8dfbf62450e6 793 //writeRegister(TFTLCD_GRAM_HOR_AD, x); // GRAM Address Set (Horizontal Address) (R20h)
og 1:8dfbf62450e6 794 writeRegister(HX8347D_COL_AD_START2,x>>8);
og 1:8dfbf62450e6 795 writeRegister(HX8347D_COL_AD_START1,x);
og 1:8dfbf62450e6 796 writeRegister(HX8347D_COL_AD_END2,x1>>8);
og 1:8dfbf62450e6 797 writeRegister(HX8347D_COL_AD_END1,x1);
og 1:8dfbf62450e6 798 writeRegister(HX8347D_ROW_AD_START2,y>>8);
og 1:8dfbf62450e6 799 writeRegister(HX8347D_ROW_AD_START1,y);
og 1:8dfbf62450e6 800 writeRegister(HX8347D_ROW_AD_END2,y1>>8);
og 1:8dfbf62450e6 801 writeRegister(HX8347D_ROW_AD_END1,y1);
og 1:8dfbf62450e6 802 writeCommand(HX8347D_SRAM_WR_CTRL); // write ram
og 1:8dfbf62450e6 803 }
og 1:8dfbf62450e6 804
og 1:8dfbf62450e6 805
og 1:8dfbf62450e6 806 void HX8347D::setDefaultViewport()
og 1:8dfbf62450e6 807 {
og 1:8dfbf62450e6 808 uint16_t x1 = width() - 1;
og 1:8dfbf62450e6 809 uint16_t y1 = height() - 1;
og 1:8dfbf62450e6 810 writeRegister(HX8347D_COL_AD_START2,0);
og 1:8dfbf62450e6 811 writeRegister(HX8347D_COL_AD_START1,0);
og 1:8dfbf62450e6 812 writeRegister(HX8347D_COL_AD_END2,x1>>8);
og 1:8dfbf62450e6 813 writeRegister(HX8347D_COL_AD_END1,x1);
og 1:8dfbf62450e6 814 writeRegister(HX8347D_ROW_AD_START2,0);
og 1:8dfbf62450e6 815 writeRegister(HX8347D_ROW_AD_START1,0);
og 1:8dfbf62450e6 816 writeRegister(HX8347D_ROW_AD_END2,y1>>8);
og 1:8dfbf62450e6 817 writeRegister(HX8347D_ROW_AD_END1,y1);
og 1:8dfbf62450e6 818 }
og 1:8dfbf62450e6 819
og 1:8dfbf62450e6 820 void HX8347D::getViewport(uint16_t *bx, uint16_t *by, uint16_t *ex, uint16_t *ey)
og 1:8dfbf62450e6 821 {
og 1:8dfbf62450e6 822 *bx = readRegister(HX8347D_COL_AD_START2)<<8;
og 1:8dfbf62450e6 823 *bx |= readRegister(HX8347D_COL_AD_START1);
og 1:8dfbf62450e6 824 *ex = readRegister(HX8347D_COL_AD_END2)<<8;
og 1:8dfbf62450e6 825 *ex |= readRegister(HX8347D_COL_AD_END1);
og 1:8dfbf62450e6 826 *by = readRegister(HX8347D_ROW_AD_START2)<<8;
og 1:8dfbf62450e6 827 *by |= readRegister(HX8347D_ROW_AD_START1);
og 1:8dfbf62450e6 828 *ey = readRegister(HX8347D_ROW_AD_END2)<<8;
og 1:8dfbf62450e6 829 *ey |= readRegister(HX8347D_ROW_AD_END1);
og 1:8dfbf62450e6 830 }
og 1:8dfbf62450e6 831
og 1:8dfbf62450e6 832 void HX8347D::setViewport(uint16_t bx, uint16_t by, uint16_t ex, uint16_t ey)
og 1:8dfbf62450e6 833 {
og 1:8dfbf62450e6 834 calcGRAMPosition(&bx, &by);
og 1:8dfbf62450e6 835 calcGRAMPosition(&ex, &ey);
og 1:8dfbf62450e6 836
og 1:8dfbf62450e6 837 // Fix coordinates to be in order
og 1:8dfbf62450e6 838 if( ey < by )
og 1:8dfbf62450e6 839 swap(ey, by);
og 1:8dfbf62450e6 840 if( ex < bx )
og 1:8dfbf62450e6 841 swap(ex, bx);
og 1:8dfbf62450e6 842
og 1:8dfbf62450e6 843 writeRegister(HX8347D_COL_AD_START2,bx>>8);
og 1:8dfbf62450e6 844 writeRegister(HX8347D_COL_AD_START1,bx);
og 1:8dfbf62450e6 845 writeRegister(HX8347D_COL_AD_END2,ex>>8);
og 1:8dfbf62450e6 846 writeRegister(HX8347D_COL_AD_END1,ex);
og 1:8dfbf62450e6 847 writeRegister(HX8347D_ROW_AD_START2,by>>8);
og 1:8dfbf62450e6 848 writeRegister(HX8347D_ROW_AD_START1,by);
og 1:8dfbf62450e6 849 writeRegister(HX8347D_ROW_AD_END2,ey>>8);
og 1:8dfbf62450e6 850 writeRegister(HX8347D_ROW_AD_END1,ey);
og 1:8dfbf62450e6 851 }
og 1:8dfbf62450e6 852
og 1:8dfbf62450e6 853 void HX8347D::calcGRAMPosition(uint16_t *posx, uint16_t *posy)
og 1:8dfbf62450e6 854 {
og 1:8dfbf62450e6 855 uint16_t x = *posx;
og 1:8dfbf62450e6 856 uint16_t y = *posy;
og 1:8dfbf62450e6 857 switch( rotation )
og 1:8dfbf62450e6 858 {
og 1:8dfbf62450e6 859 case 0:
og 1:8dfbf62450e6 860 x = X(x);
og 1:8dfbf62450e6 861 y = Y(y);
og 1:8dfbf62450e6 862 break;
og 1:8dfbf62450e6 863 case 1: // 90
og 1:8dfbf62450e6 864 swap(x, y);
og 1:8dfbf62450e6 865 x = I_X(x);
og 1:8dfbf62450e6 866 y = Y(y);
og 1:8dfbf62450e6 867 break;
og 1:8dfbf62450e6 868 case 2: // 180
og 1:8dfbf62450e6 869 x = I_X(x);
og 1:8dfbf62450e6 870 y = I_Y(y);
og 1:8dfbf62450e6 871 break;
og 1:8dfbf62450e6 872 case 3: // 270
og 1:8dfbf62450e6 873 swap(x, y);
og 1:8dfbf62450e6 874 y = I_Y(y);
og 1:8dfbf62450e6 875 break;
og 1:8dfbf62450e6 876 }
og 1:8dfbf62450e6 877 *posx = x;
og 1:8dfbf62450e6 878 *posy = y;
og 1:8dfbf62450e6 879 }
og 1:8dfbf62450e6 880
og 3:233295bc63af 881 void HX8347D::_delay(uint16_t t)
og 3:233295bc63af 882 {
og 3:233295bc63af 883 }
og 1:8dfbf62450e6 884