132 x 176 STN LCD Controller

Committer:
og
Date:
Sat Sep 24 12:48:46 2016 +0000
Revision:
0:56ce29613827
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
og 0:56ce29613827 1 #include "HD66766.h"
og 0:56ce29613827 2 #include "glcdfont.h"
og 0:56ce29613827 3
og 0:56ce29613827 4 HD66766::HD66766(
og 0:56ce29613827 5 PinName rd, PinName wr, PinName rs, PinName cs, PinName rst,
og 0:56ce29613827 6 PinName d0, PinName d1, PinName d2, PinName d3,
og 0:56ce29613827 7 PinName d4, PinName d5, PinName d6, PinName d7,
og 0:56ce29613827 8 PinName d8, PinName d9, PinName d10, PinName d11,
og 0:56ce29613827 9 PinName d12, PinName d13, PinName d14, PinName d15
og 0:56ce29613827 10 ):
og 0:56ce29613827 11 _rd(rd), _wr(wr), _rs(rs), _cs(cs), _rst(rst),
og 0:56ce29613827 12 _d(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15)
og 0:56ce29613827 13 {
og 0:56ce29613827 14 _width = LCDWIDTH;
og 0:56ce29613827 15 _height = LCDHEIGHT;
og 0:56ce29613827 16
og 0:56ce29613827 17 _rd=1;
og 0:56ce29613827 18 _wr=1;
og 0:56ce29613827 19 _rs=1;
og 0:56ce29613827 20 _cs=1;
og 0:56ce29613827 21 _rst = 1;
og 0:56ce29613827 22
og 0:56ce29613827 23 cursor_y = cursor_x = 0;
og 0:56ce29613827 24 textsize = 1;
og 0:56ce29613827 25 textcolor = 0xFFFF;
og 0:56ce29613827 26 }
og 0:56ce29613827 27
og 0:56ce29613827 28 void HD66766::goHome(void) {
og 0:56ce29613827 29 goTo(0,0);
og 0:56ce29613827 30 }
og 0:56ce29613827 31
og 0:56ce29613827 32 uint16_t HD66766::width(void) {
og 0:56ce29613827 33 return _width;
og 0:56ce29613827 34 }
og 0:56ce29613827 35 uint16_t HD66766::height(void) {
og 0:56ce29613827 36 return _height;
og 0:56ce29613827 37 }
og 0:56ce29613827 38 uint16_t HD66766::rb_swap(uint16_t c) {
og 0:56ce29613827 39 uint16_t rc;
og 0:56ce29613827 40 if (_rb_swap == 0) {
og 0:56ce29613827 41 rc = c;
og 0:56ce29613827 42 }
og 0:56ce29613827 43 else {
og 0:56ce29613827 44 rc = c & 0x07E0;
og 0:56ce29613827 45 rc |= (c >> 11) & 0x001F;
og 0:56ce29613827 46 rc |= (c & 0x001F) << 11;
og 0:56ce29613827 47 }
og 0:56ce29613827 48 return rc;
og 0:56ce29613827 49 }
og 0:56ce29613827 50
og 0:56ce29613827 51 void HD66766::fillScreen(uint16_t color) {
og 0:56ce29613827 52 setWriteDir();
og 0:56ce29613827 53 writeRegister(0x0021, _gramstart);
og 0:56ce29613827 54 writeCommand(0x0022);
og 0:56ce29613827 55 _cs = 0;
og 0:56ce29613827 56 _rs = 1;
og 0:56ce29613827 57 _rd = 1;
og 0:56ce29613827 58 _wr = 1;
og 0:56ce29613827 59 for (int i = 0x0000; i < 0x5000; i++) {
og 0:56ce29613827 60 writeData_unsafe(rb_swap(color));
og 0:56ce29613827 61 //wait_ms(10);
og 0:56ce29613827 62 }
og 0:56ce29613827 63 _cs = 1;
og 0:56ce29613827 64 }
og 0:56ce29613827 65
og 0:56ce29613827 66
og 0:56ce29613827 67 void HD66766::drawPixel(uint16_t x, uint16_t y, uint16_t color)
og 0:56ce29613827 68 {
og 0:56ce29613827 69 uint16_t AC;
og 0:56ce29613827 70 AC = (y<<8)+x + _gramstart;
og 0:56ce29613827 71 writeRegister(0x0021,AC);
og 0:56ce29613827 72 writeRegister(0x0022, rb_swap(color));
og 0:56ce29613827 73 }
og 0:56ce29613827 74
og 0:56ce29613827 75 void HD66766::initDisplay(void) {
og 0:56ce29613827 76 //uint16_t a, d;
og 0:56ce29613827 77 reset();
og 0:56ce29613827 78 writeRegister(0x0000, 0x0001);
og 0:56ce29613827 79 mirror(1);
og 0:56ce29613827 80 //writeRegister(0x0001, 0x0215); // Driver Output Control
og 0:56ce29613827 81 //writeRegister(0x0001, 0x0013); // Driver Output Control
og 0:56ce29613827 82 writeRegister(0x0002, 0x0000); // LCD-Driving-Waveform Control
og 0:56ce29613827 83 //writeRegister(0x0003, 0x7e6c); // Power Control 1
og 0:56ce29613827 84 //writeRegister(0x0003, 0x6e78); // Power Control 1
og 0:56ce29613827 85 writeRegister(0x0003, 0x9e78); // Power Control 1
og 0:56ce29613827 86 writeRegister(0x0004, 0x0000); // Contrast Control
og 0:56ce29613827 87 //writeRegister(0x0005, 0x0030); // Entry Mode
og 0:56ce29613827 88 writeRegister(0x0005, 0x0030); // Entry Mode
og 0:56ce29613827 89 writeRegister(0x0007, 0x0003); // Display on
og 0:56ce29613827 90 writeRegister(0x000c, 0x0006); // Power Control 2
og 0:56ce29613827 91 writeRegister(0x0011, 0x0000); // Vertical Scroll Control
og 0:56ce29613827 92 writeRegister(0x0016, 0x7F00); // Set window horizontal position to top to bottom of ram address 127 to 0 (128)
og 0:56ce29613827 93 writeRegister(0x0017, 0xAF00); // Set window vertical position to top to bottom of ram address 159 to 0 (160)
og 0:56ce29613827 94 writeRegister(0x0020, 0x0000); // RAM Write Data Mask
og 0:56ce29613827 95 writeRegister(0x0021, _gramstart); // GRAM Address Set
og 0:56ce29613827 96 }
og 0:56ce29613827 97
og 0:56ce29613827 98 void HD66766::setWriteDir(void) {
og 0:56ce29613827 99 _d.output();
og 0:56ce29613827 100 }
og 0:56ce29613827 101
og 0:56ce29613827 102 void HD66766::setReadDir(void) {
og 0:56ce29613827 103 _d.input();
og 0:56ce29613827 104 }
og 0:56ce29613827 105
og 0:56ce29613827 106 void HD66766::_write(uint16_t rs, uint16_t d) {
og 0:56ce29613827 107 _cs=0;
og 0:56ce29613827 108 _rd=1;
og 0:56ce29613827 109 _rs=rs;
og 0:56ce29613827 110
og 0:56ce29613827 111 _d.output();
og 0:56ce29613827 112 _d=d;
og 0:56ce29613827 113 _wr=0;
og 0:56ce29613827 114 _delay(1);
og 0:56ce29613827 115 _wr=1;
og 0:56ce29613827 116
og 0:56ce29613827 117 _cs=1;
og 0:56ce29613827 118 }
og 0:56ce29613827 119
og 0:56ce29613827 120 void HD66766::writeData_unsafe(uint16_t d)
og 0:56ce29613827 121 {
og 0:56ce29613827 122 //_d.output();
og 0:56ce29613827 123 _d = d;
og 0:56ce29613827 124 _wr = 0;
og 0:56ce29613827 125 _delay(1);
og 0:56ce29613827 126 _wr = 1;
og 0:56ce29613827 127 }
og 0:56ce29613827 128
og 0:56ce29613827 129 void HD66766::reset(void)
og 0:56ce29613827 130 {
og 0:56ce29613827 131 _rst=0;
og 0:56ce29613827 132 _delay(1000);
og 0:56ce29613827 133 _rst=1;
og 0:56ce29613827 134 _delay(1000);
og 0:56ce29613827 135 }
og 0:56ce29613827 136
og 0:56ce29613827 137 uint16_t HD66766::readData(){
og 0:56ce29613827 138 uint16_t d = 0;
og 0:56ce29613827 139 _d.input();
og 0:56ce29613827 140 _cs = 0;
og 0:56ce29613827 141 _rs = 1;
og 0:56ce29613827 142 _wr = 1;
og 0:56ce29613827 143 _rd = 1;
og 0:56ce29613827 144
og 0:56ce29613827 145
og 0:56ce29613827 146 _delay(1);
og 0:56ce29613827 147 _rd = 0;
og 0:56ce29613827 148 _delay(10);
og 0:56ce29613827 149 d = _d;
og 0:56ce29613827 150 _rd = 1;
og 0:56ce29613827 151 _cs = 1;
og 0:56ce29613827 152 return d;
og 0:56ce29613827 153 }
og 0:56ce29613827 154
og 0:56ce29613827 155 uint16_t HD66766::readRegister(uint16_t addr) {
og 0:56ce29613827 156 writeCommand(addr);
og 0:56ce29613827 157 return readData();
og 0:56ce29613827 158 }
og 0:56ce29613827 159 void HD66766::writeRegister(uint16_t addr, uint16_t data) {
og 0:56ce29613827 160 writeCommand(addr);
og 0:56ce29613827 161 writeData(data);
og 0:56ce29613827 162 }
og 0:56ce29613827 163 void HD66766::goTo(uint16_t x, uint16_t y) {
og 0:56ce29613827 164 uint16_t AC;
og 0:56ce29613827 165 AC = (y<<8)+x + _gramstart;
og 0:56ce29613827 166 writeRegister(0x0021,AC);
og 0:56ce29613827 167 }
og 0:56ce29613827 168
og 0:56ce29613827 169 void HD66766::drawVerticalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
og 0:56ce29613827 170 {
og 0:56ce29613827 171 if (x >= _width) return;
og 0:56ce29613827 172
og 0:56ce29613827 173 drawLine(x, y, x, y+length, color);
og 0:56ce29613827 174 }
og 0:56ce29613827 175
og 0:56ce29613827 176 void HD66766::drawHorizontalLine(uint16_t x, uint16_t y, uint16_t length, uint16_t color)
og 0:56ce29613827 177 {
og 0:56ce29613827 178 if (y >= _height) return;
og 0:56ce29613827 179 drawLine(x, y, x+length, y, color);
og 0:56ce29613827 180 }
og 0:56ce29613827 181
og 0:56ce29613827 182 void HD66766::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
og 0:56ce29613827 183 uint16_t color) {
og 0:56ce29613827 184 // if you're in rotation 1 or 3, we need to swap the X and Y's
og 0:56ce29613827 185
og 0:56ce29613827 186 int16_t steep = abs(y1 - y0) > abs(x1 - x0);
og 0:56ce29613827 187 if (steep) {
og 0:56ce29613827 188 swap(x0, y0);
og 0:56ce29613827 189 swap(x1, y1);
og 0:56ce29613827 190 }
og 0:56ce29613827 191
og 0:56ce29613827 192 if (x0 > x1) {
og 0:56ce29613827 193 swap(x0, x1);
og 0:56ce29613827 194 swap(y0, y1);
og 0:56ce29613827 195 }
og 0:56ce29613827 196
og 0:56ce29613827 197 int16_t dx, dy;
og 0:56ce29613827 198 dx = x1 - x0;
og 0:56ce29613827 199 //dy = abs(y1 - y0);
og 0:56ce29613827 200 dy = abs(y1 - y0);
og 0:56ce29613827 201
og 0:56ce29613827 202 int16_t err = dx / 2;
og 0:56ce29613827 203 int16_t ystep;
og 0:56ce29613827 204
og 0:56ce29613827 205 if (y0 < y1) {
og 0:56ce29613827 206 ystep = 1;
og 0:56ce29613827 207 } else {
og 0:56ce29613827 208 ystep = -1;}
og 0:56ce29613827 209
og 0:56ce29613827 210 for (; x0<=x1; x0++) {
og 0:56ce29613827 211 if (steep) {
og 0:56ce29613827 212 drawPixel(y0, x0, color);
og 0:56ce29613827 213 } else {
og 0:56ce29613827 214 drawPixel(x0, y0, color);
og 0:56ce29613827 215 }
og 0:56ce29613827 216 err -= dy;
og 0:56ce29613827 217 if (err < 0) {
og 0:56ce29613827 218 y0 += ystep;
og 0:56ce29613827 219 err += dx;
og 0:56ce29613827 220 }
og 0:56ce29613827 221 }
og 0:56ce29613827 222 }
og 0:56ce29613827 223
og 0:56ce29613827 224
og 0:56ce29613827 225 void HD66766::drawTriangle(uint16_t x0, uint16_t y0,
og 0:56ce29613827 226 uint16_t x1, uint16_t y1,
og 0:56ce29613827 227 uint16_t x2, uint16_t y2, uint16_t color)
og 0:56ce29613827 228 {
og 0:56ce29613827 229 drawLine(x0, y0, x1, y1, color);
og 0:56ce29613827 230 drawLine(x1, y1, x2, y2, color);
og 0:56ce29613827 231 drawLine(x2, y2, x0, y0, color);
og 0:56ce29613827 232 }
og 0:56ce29613827 233
og 0:56ce29613827 234 void HD66766::fillTriangle ( int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
og 0:56ce29613827 235 {
og 0:56ce29613827 236 if (y0 > y1) {
og 0:56ce29613827 237 swap(y0, y1); swap(x0, x1);
og 0:56ce29613827 238 }
og 0:56ce29613827 239 if (y1 > y2) {
og 0:56ce29613827 240 swap(y2, y1); swap(x2, x1);
og 0:56ce29613827 241 }
og 0:56ce29613827 242 if (y0 > y1) {
og 0:56ce29613827 243 swap(y0, y1); swap(x0, x1);
og 0:56ce29613827 244 }
og 0:56ce29613827 245
og 0:56ce29613827 246 int32_t dx1, dx2, dx3; // Interpolation deltas
og 0:56ce29613827 247 int32_t sx1, sx2, sy; // Scanline co-ordinates
og 0:56ce29613827 248
og 0:56ce29613827 249 sx2=(int32_t)x0 * (int32_t)1000; // Use fixed point math for x axis values
og 0:56ce29613827 250 sx1 = sx2;
og 0:56ce29613827 251 sy=y0;
og 0:56ce29613827 252
og 0:56ce29613827 253 // Calculate interpolation deltas
og 0:56ce29613827 254 if (y1-y0 > 0) dx1=((x1-x0)*1000)/(y1-y0);
og 0:56ce29613827 255 else dx1=0;
og 0:56ce29613827 256 if (y2-y0 > 0) dx2=((x2-x0)*1000)/(y2-y0);
og 0:56ce29613827 257 else dx2=0;
og 0:56ce29613827 258 if (y2-y1 > 0) dx3=((x2-x1)*1000)/(y2-y1);
og 0:56ce29613827 259 else dx3=0;
og 0:56ce29613827 260
og 0:56ce29613827 261 // Render scanlines (horizontal lines are the fastest rendering method)
og 0:56ce29613827 262 if (dx1 > dx2)
og 0:56ce29613827 263 {
og 0:56ce29613827 264 for(; sy<=y1; sy++, sx1+=dx2, sx2+=dx1)
og 0:56ce29613827 265 {
og 0:56ce29613827 266 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 0:56ce29613827 267 }
og 0:56ce29613827 268 sx2 = x1*1000;
og 0:56ce29613827 269 sy = y1;
og 0:56ce29613827 270 for(; sy<=y2; sy++, sx1+=dx2, sx2+=dx3)
og 0:56ce29613827 271 {
og 0:56ce29613827 272 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 0:56ce29613827 273 }
og 0:56ce29613827 274 }
og 0:56ce29613827 275 else
og 0:56ce29613827 276 {
og 0:56ce29613827 277 for(; sy<=y1; sy++, sx1+=dx1, sx2+=dx2)
og 0:56ce29613827 278 {
og 0:56ce29613827 279 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 0:56ce29613827 280 }
og 0:56ce29613827 281 sx1 = x1*1000;
og 0:56ce29613827 282 sy = y1;
og 0:56ce29613827 283 for(; sy<=y2; sy++, sx1+=dx3, sx2+=dx2)
og 0:56ce29613827 284 {
og 0:56ce29613827 285 drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
og 0:56ce29613827 286 }
og 0:56ce29613827 287 }
og 0:56ce29613827 288 }
og 0:56ce29613827 289
og 0:56ce29613827 290 uint16_t HD66766::Color565(uint8_t r, uint8_t g, uint8_t b) {
og 0:56ce29613827 291 uint16_t c;
og 0:56ce29613827 292 c = r >> 3;
og 0:56ce29613827 293 c <<= 6;
og 0:56ce29613827 294 c |= g >> 2;
og 0:56ce29613827 295 c <<= 5;
og 0:56ce29613827 296 c |= b >> 3;
og 0:56ce29613827 297
og 0:56ce29613827 298 //printf("%x\r\n", c);
og 0:56ce29613827 299 return c;
og 0:56ce29613827 300 }
og 0:56ce29613827 301
og 0:56ce29613827 302 // draw a rectangle
og 0:56ce29613827 303 void HD66766::drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
og 0:56ce29613827 304 uint16_t color) {
og 0:56ce29613827 305 // smarter version
og 0:56ce29613827 306 drawHorizontalLine(x, y, w, color);
og 0:56ce29613827 307 drawHorizontalLine(x, y+h-1, w, color);
og 0:56ce29613827 308 drawVerticalLine(x, y, h, color);
og 0:56ce29613827 309 drawVerticalLine(x+w-1, y, h, color);
og 0:56ce29613827 310 }
og 0:56ce29613827 311
og 0:56ce29613827 312 // draw a rounded rectangle
og 0:56ce29613827 313 void HD66766::drawRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
og 0:56ce29613827 314 uint16_t color) {
og 0:56ce29613827 315 // smarter version
og 0:56ce29613827 316 drawHorizontalLine(x+r, y, w-2*r, color);
og 0:56ce29613827 317 drawHorizontalLine(x+r, y+h-1, w-2*r, color);
og 0:56ce29613827 318 drawVerticalLine(x, y+r, h-2*r, color);
og 0:56ce29613827 319 drawVerticalLine(x+w-1, y+r, h-2*r, color);
og 0:56ce29613827 320 // draw four corners
og 0:56ce29613827 321 drawCircleHelper(x+r, y+r, r, 1, color);
og 0:56ce29613827 322 drawCircleHelper(x+w-r-1, y+r, r, 2, color);
og 0:56ce29613827 323 drawCircleHelper(x+w-r-1, y+h-r-1, r, 4, color);
og 0:56ce29613827 324 drawCircleHelper(x+r, y+h-r-1, r, 8, color);
og 0:56ce29613827 325 }
og 0:56ce29613827 326
og 0:56ce29613827 327
og 0:56ce29613827 328 // fill a rounded rectangle
og 0:56ce29613827 329 void HD66766::fillRoundRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
og 0:56ce29613827 330 uint16_t color) {
og 0:56ce29613827 331 // smarter version
og 0:56ce29613827 332 fillRect(x+r, y, w-2*r, h, color);
og 0:56ce29613827 333
og 0:56ce29613827 334 // draw four corners
og 0:56ce29613827 335 fillCircleHelper(x+w-r-1, y+r, r, 1, h-2*r-1, color);
og 0:56ce29613827 336 fillCircleHelper(x+r, y+r, r, 2, h-2*r-1, color);
og 0:56ce29613827 337 }
og 0:56ce29613827 338
og 0:56ce29613827 339 // fill a circle
og 0:56ce29613827 340 void HD66766::fillCircle(uint16_t x0, uint16_t y0, uint16_t r, uint16_t color) {
og 0:56ce29613827 341 //writeRegister(TFTLCD_ENTRY_MOD, 0x1030);
og 0:56ce29613827 342 drawVerticalLine(x0, y0-r, 2*r+1, color);
og 0:56ce29613827 343 fillCircleHelper(x0, y0, r, 3, 0, color);
og 0:56ce29613827 344 }
og 0:56ce29613827 345
og 0:56ce29613827 346
og 0:56ce29613827 347 // used to do circles and roundrects!
og 0:56ce29613827 348 void HD66766::fillCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername, uint16_t delta,
og 0:56ce29613827 349 uint16_t color) {
og 0:56ce29613827 350
og 0:56ce29613827 351 int16_t f = 1 - r;
og 0:56ce29613827 352 int16_t ddF_x = 1;
og 0:56ce29613827 353 int16_t ddF_y = -2 * r;
og 0:56ce29613827 354 int16_t x = 0;
og 0:56ce29613827 355 int16_t y = r;
og 0:56ce29613827 356
og 0:56ce29613827 357 while (x<y) {
og 0:56ce29613827 358 if (f >= 0) {
og 0:56ce29613827 359 y--;
og 0:56ce29613827 360 ddF_y += 2;
og 0:56ce29613827 361 f += ddF_y;
og 0:56ce29613827 362 }
og 0:56ce29613827 363 x++;
og 0:56ce29613827 364 ddF_x += 2;
og 0:56ce29613827 365 f += ddF_x;
og 0:56ce29613827 366
og 0:56ce29613827 367 if (cornername & 0x1) {
og 0:56ce29613827 368 drawVerticalLine(x0+x, y0-y, 2*y+1+delta, color);
og 0:56ce29613827 369 drawVerticalLine(x0+y, y0-x, 2*x+1+delta, color);
og 0:56ce29613827 370 }
og 0:56ce29613827 371 if (cornername & 0x2) {
og 0:56ce29613827 372 drawVerticalLine(x0-x, y0-y, 2*y+1+delta, color);
og 0:56ce29613827 373 drawVerticalLine(x0-y, y0-x, 2*x+1+delta, color);
og 0:56ce29613827 374 }
og 0:56ce29613827 375 }
og 0:56ce29613827 376 }
og 0:56ce29613827 377
og 0:56ce29613827 378
og 0:56ce29613827 379 // draw a circle outline
og 0:56ce29613827 380
og 0:56ce29613827 381 void HD66766::drawCircle(uint16_t x0, uint16_t y0, uint16_t r,
og 0:56ce29613827 382 uint16_t color) {
og 0:56ce29613827 383 drawPixel(x0, y0+r, color);
og 0:56ce29613827 384 drawPixel(x0, y0-r, color);
og 0:56ce29613827 385 drawPixel(x0+r, y0, color);
og 0:56ce29613827 386 drawPixel(x0-r, y0, color);
og 0:56ce29613827 387
og 0:56ce29613827 388 drawCircleHelper(x0, y0, r, 0xF, color);
og 0:56ce29613827 389 }
og 0:56ce29613827 390
og 0:56ce29613827 391 void HD66766::drawCircleHelper(uint16_t x0, uint16_t y0, uint16_t r, uint8_t cornername,
og 0:56ce29613827 392 uint16_t color) {
og 0:56ce29613827 393 int16_t f = 1 - r;
og 0:56ce29613827 394 int16_t ddF_x = 1;
og 0:56ce29613827 395 int16_t ddF_y = -2 * r;
og 0:56ce29613827 396 int16_t x = 0;
og 0:56ce29613827 397 int16_t y = r;
og 0:56ce29613827 398
og 0:56ce29613827 399
og 0:56ce29613827 400 while (x<y) {
og 0:56ce29613827 401 if (f >= 0) {
og 0:56ce29613827 402 y--;
og 0:56ce29613827 403 ddF_y += 2;
og 0:56ce29613827 404 f += ddF_y;
og 0:56ce29613827 405 }
og 0:56ce29613827 406 x++;
og 0:56ce29613827 407 ddF_x += 2;
og 0:56ce29613827 408 f += ddF_x;
og 0:56ce29613827 409 if (cornername & 0x4) {
og 0:56ce29613827 410 drawPixel(x0 + x, y0 + y, color);
og 0:56ce29613827 411 drawPixel(x0 + y, y0 + x, color);
og 0:56ce29613827 412 }
og 0:56ce29613827 413 if (cornername & 0x2) {
og 0:56ce29613827 414 drawPixel(x0 + x, y0 - y, color);
og 0:56ce29613827 415 drawPixel(x0 + y, y0 - x, color);
og 0:56ce29613827 416 }
og 0:56ce29613827 417 if (cornername & 0x8) {
og 0:56ce29613827 418 drawPixel(x0 - y, y0 + x, color);
og 0:56ce29613827 419 drawPixel(x0 - x, y0 + y, color);
og 0:56ce29613827 420 }
og 0:56ce29613827 421 if (cornername & 0x1) {
og 0:56ce29613827 422 drawPixel(x0 - y, y0 - x, color);
og 0:56ce29613827 423 drawPixel(x0 - x, y0 - y, color);
og 0:56ce29613827 424 }
og 0:56ce29613827 425 }
og 0:56ce29613827 426 }
og 0:56ce29613827 427
og 0:56ce29613827 428 // fill a rectangle
og 0:56ce29613827 429 void HD66766::fillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
og 0:56ce29613827 430 uint16_t fillcolor) {
og 0:56ce29613827 431 // smarter version
og 0:56ce29613827 432 while (h--)
og 0:56ce29613827 433 drawHorizontalLine(x, y++, w, fillcolor);
og 0:56ce29613827 434 }
og 0:56ce29613827 435
og 0:56ce29613827 436
og 0:56ce29613827 437 void HD66766::setCursor(uint16_t x, uint16_t y) {
og 0:56ce29613827 438 cursor_x = x;
og 0:56ce29613827 439 cursor_y = y;
og 0:56ce29613827 440 }
og 0:56ce29613827 441
og 0:56ce29613827 442 void HD66766::setTextSize(uint8_t s) {
og 0:56ce29613827 443 textsize = s;
og 0:56ce29613827 444 }
og 0:56ce29613827 445
og 0:56ce29613827 446 void HD66766::setTextColor(uint16_t c) {
og 0:56ce29613827 447 textcolor = c;
og 0:56ce29613827 448 }
og 0:56ce29613827 449
og 0:56ce29613827 450 size_t HD66766::println(char *c){
og 0:56ce29613827 451 while (c[0] != 0) {
og 0:56ce29613827 452 write(c[0]);
og 0:56ce29613827 453 c++;
og 0:56ce29613827 454 }
og 0:56ce29613827 455 //drawString(cursor_x, cursor_y, c, textcolor);
og 0:56ce29613827 456 return sizeof(c);
og 0:56ce29613827 457 }
og 0:56ce29613827 458
og 0:56ce29613827 459 size_t HD66766::write(uint8_t c) {
og 0:56ce29613827 460 if (c == '\n') {
og 0:56ce29613827 461 cursor_y += textsize*8;
og 0:56ce29613827 462 cursor_x = 0;
og 0:56ce29613827 463 } else if (c == '\r') {
og 0:56ce29613827 464 // skip em
og 0:56ce29613827 465 } else {
og 0:56ce29613827 466 drawChar(cursor_x, cursor_y, c, textcolor, textsize);
og 0:56ce29613827 467 cursor_x += textsize*6;
og 0:56ce29613827 468 }
og 0:56ce29613827 469 return 0;
og 0:56ce29613827 470 }
og 0:56ce29613827 471
og 0:56ce29613827 472 // draw a character
og 0:56ce29613827 473 void HD66766::drawChar(uint16_t x, uint16_t y, char c,
og 0:56ce29613827 474 uint16_t color, uint8_t size) {
og 0:56ce29613827 475 for (uint8_t i =0; i<5; i++ ) {
og 0:56ce29613827 476 uint8_t line = font[(c*5)+i];
og 0:56ce29613827 477 for (uint8_t j = 0; j<8; j++) {
og 0:56ce29613827 478 if (line & 0x1) {
og 0:56ce29613827 479 if (size == 1) // default size
og 0:56ce29613827 480 drawPixel(x+i, y+j, color);
og 0:56ce29613827 481 else { // big size
og 0:56ce29613827 482 fillRect(x+i*size, y+j*size, size, size, color);
og 0:56ce29613827 483 }
og 0:56ce29613827 484 }
og 0:56ce29613827 485 line >>= 1;
og 0:56ce29613827 486 }
og 0:56ce29613827 487 }
og 0:56ce29613827 488 }
og 0:56ce29613827 489
og 0:56ce29613827 490 void HD66766::mirror(uint16_t d) {
og 0:56ce29613827 491 switch(d) {
og 0:56ce29613827 492 case 0: {
og 0:56ce29613827 493 _gramstart = 0x0000;
og 0:56ce29613827 494 _rb_swap = 0;
og 0:56ce29613827 495 writeRegister(0x0001, 0x0013); // Driver Output Control
og 0:56ce29613827 496 writeRegister(0x0016, 0x7F00); // Set window horizontal position to top to bottom of ram address 127 to 0 (128)
og 0:56ce29613827 497 writeRegister(0x0017, 0xAF00); // Set window vertical position to top to bottom of ram address 159 to 0 (160)
og 0:56ce29613827 498 break;
og 0:56ce29613827 499 }
og 0:56ce29613827 500 case 1: {
og 0:56ce29613827 501 _rb_swap = 0;
og 0:56ce29613827 502 _gramstart = 0x1000;
og 0:56ce29613827 503 writeRegister(0x0001, 0x0215); // Driver Output Control
og 0:56ce29613827 504 writeRegister(0x0016, 0x7F00); // Set window horizontal position to top to bottom of ram address 127 to 0 (128)
og 0:56ce29613827 505 writeRegister(0x0017, 0xAF00); // Set window vertical position to top to bottom of ram address 159 to 0 (160)
og 0:56ce29613827 506 break;
og 0:56ce29613827 507 }
og 0:56ce29613827 508 case 2: {
og 0:56ce29613827 509 _rb_swap = 1;
og 0:56ce29613827 510 _gramstart = 0x0004;
og 0:56ce29613827 511 writeRegister(0x0001, 0x0113); // Driver Output Control
og 0:56ce29613827 512 writeRegister(0x0016, 0x8304); // Set window horizontal position to top to bottom of ram address 127 to 0 (128)
og 0:56ce29613827 513 writeRegister(0x0017, 0xAF00); // Set window vertical position to top to bottom of ram address 159 to 0 (160)
og 0:56ce29613827 514 break;
og 0:56ce29613827 515 }
og 0:56ce29613827 516 case 3: {
og 0:56ce29613827 517 _gramstart = 0x1004;
og 0:56ce29613827 518 _rb_swap = 1;
og 0:56ce29613827 519 writeRegister(0x0001, 0x0315); // Driver Output Control
og 0:56ce29613827 520 writeRegister(0x0016, 0x8304); // Set window horizontal position to top to bottom of ram address 127 to 0 (128)
og 0:56ce29613827 521 writeRegister(0x0017, 0xAF00); // Set window vertical position to top to bottom of ram address 159 to 0 (160)
og 0:56ce29613827 522 break;
og 0:56ce29613827 523 }
og 0:56ce29613827 524 }
og 0:56ce29613827 525 }
og 0:56ce29613827 526
og 0:56ce29613827 527 void HD66766::_delay(uint16_t t)
og 0:56ce29613827 528 {
og 0:56ce29613827 529 //wait_us(1);
og 0:56ce29613827 530 }