UniGraphics Library Fork to support mbed os 6.3 Release for ILI9341

Dependents:   TFT_ILI9341_UniGraphic TFT_ILI9341_os6

Committer:
amouroug
Date:
Thu Oct 08 18:11:03 2020 -0500
Revision:
2:59188908eb60
Parent:
0:bb2bda4f5846
Added GraphicsDisplay GFX API to draw triangle.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
amouroug 0:bb2bda4f5846 1 /* mbed UniGraphic library - Graphics class
amouroug 0:bb2bda4f5846 2 * Copyright (c) 2015 Giuliano Dianda
amouroug 0:bb2bda4f5846 3 * Released under the MIT License: http://mbed.org/license/mit
amouroug 0:bb2bda4f5846 4 *
amouroug 0:bb2bda4f5846 5 * Derived work of:
amouroug 0:bb2bda4f5846 6 *
amouroug 0:bb2bda4f5846 7 * mbed GraphicsDisplay Display Library Base Class
amouroug 0:bb2bda4f5846 8 * Copyright (c) 2007-2009 sford
amouroug 0:bb2bda4f5846 9 * Released under the MIT License: http://mbed.org/license/mit
amouroug 0:bb2bda4f5846 10 *
amouroug 0:bb2bda4f5846 11 * mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
amouroug 0:bb2bda4f5846 12 * Copyright (c) 2013 Peter Drescher - DC2PD
amouroug 0:bb2bda4f5846 13 *
amouroug 0:bb2bda4f5846 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
amouroug 0:bb2bda4f5846 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
amouroug 0:bb2bda4f5846 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
amouroug 0:bb2bda4f5846 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
amouroug 0:bb2bda4f5846 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
amouroug 0:bb2bda4f5846 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
amouroug 0:bb2bda4f5846 20 * THE SOFTWARE.
amouroug 0:bb2bda4f5846 21 */
amouroug 0:bb2bda4f5846 22
amouroug 0:bb2bda4f5846 23
amouroug 0:bb2bda4f5846 24
amouroug 0:bb2bda4f5846 25 #include "GraphicsDisplay.h"
amouroug 0:bb2bda4f5846 26 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
amouroug 0:bb2bda4f5846 27 GraphicsDisplay::GraphicsDisplay(const char *name):TextDisplay(name) {
amouroug 0:bb2bda4f5846 28 set_font((unsigned char*)Terminal6x8,32,127,true);
amouroug 0:bb2bda4f5846 29 // foreground(0xFFFF);
amouroug 0:bb2bda4f5846 30 // background(0x0000);
amouroug 0:bb2bda4f5846 31 char_x = 0;
amouroug 0:bb2bda4f5846 32 char_y = 0;
amouroug 0:bb2bda4f5846 33 oriented_width=0;
amouroug 0:bb2bda4f5846 34 oriented_height=0;
amouroug 0:bb2bda4f5846 35 fontzoomver=1;
amouroug 0:bb2bda4f5846 36 fontzoomhor=1;
amouroug 0:bb2bda4f5846 37 auto_up = true;
amouroug 0:bb2bda4f5846 38 }
amouroug 0:bb2bda4f5846 39
amouroug 0:bb2bda4f5846 40 void GraphicsDisplay::WindowMax (void)
amouroug 0:bb2bda4f5846 41 {
amouroug 0:bb2bda4f5846 42 window (0, 0, oriented_width, oriented_height);
amouroug 0:bb2bda4f5846 43 }
amouroug 0:bb2bda4f5846 44 void GraphicsDisplay::set_width(int width)
amouroug 0:bb2bda4f5846 45 {
amouroug 0:bb2bda4f5846 46 oriented_width = width;
amouroug 0:bb2bda4f5846 47 }
amouroug 0:bb2bda4f5846 48 void GraphicsDisplay::set_height(int height)
amouroug 0:bb2bda4f5846 49 {
amouroug 0:bb2bda4f5846 50 oriented_height = height;
amouroug 0:bb2bda4f5846 51 }
amouroug 0:bb2bda4f5846 52 int GraphicsDisplay::width()
amouroug 0:bb2bda4f5846 53 {
amouroug 0:bb2bda4f5846 54 return oriented_width;
amouroug 0:bb2bda4f5846 55 }
amouroug 0:bb2bda4f5846 56 int GraphicsDisplay::height()
amouroug 0:bb2bda4f5846 57 {
amouroug 0:bb2bda4f5846 58 return oriented_height;
amouroug 0:bb2bda4f5846 59 }
amouroug 0:bb2bda4f5846 60 void GraphicsDisplay::circle(int x0, int y0, int r, unsigned short color)
amouroug 0:bb2bda4f5846 61 {
amouroug 0:bb2bda4f5846 62 int x = -r, y = 0, err = 2-2*r, e2;
amouroug 0:bb2bda4f5846 63 do {
amouroug 0:bb2bda4f5846 64 pixel(x0-x, y0+y,color);
amouroug 0:bb2bda4f5846 65 pixel(x0+x, y0+y,color);
amouroug 0:bb2bda4f5846 66 pixel(x0+x, y0-y,color);
amouroug 0:bb2bda4f5846 67 pixel(x0-x, y0-y,color);
amouroug 0:bb2bda4f5846 68 e2 = err;
amouroug 0:bb2bda4f5846 69 if (e2 <= y) {
amouroug 0:bb2bda4f5846 70 err += ++y*2+1;
amouroug 0:bb2bda4f5846 71 if (-x == y && e2 <= x) e2 = 0;
amouroug 0:bb2bda4f5846 72 }
amouroug 0:bb2bda4f5846 73 if (e2 > x) err += ++x*2+1;
amouroug 0:bb2bda4f5846 74 } while (x <= 0);
amouroug 0:bb2bda4f5846 75 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 76 }
amouroug 0:bb2bda4f5846 77 void GraphicsDisplay::fillcircle(int x0, int y0, int r, unsigned short color)
amouroug 0:bb2bda4f5846 78 {
amouroug 0:bb2bda4f5846 79 bool old_auto_up=auto_up;
amouroug 0:bb2bda4f5846 80 if(auto_up) auto_up=false;
amouroug 0:bb2bda4f5846 81 int x = -r, y = 0, err = 2-2*r, e2;
amouroug 0:bb2bda4f5846 82 do {
amouroug 0:bb2bda4f5846 83 vline(x0-x, y0-y, y0+y, color);
amouroug 0:bb2bda4f5846 84 vline(x0+x, y0-y, y0+y, color);
amouroug 0:bb2bda4f5846 85 e2 = err;
amouroug 0:bb2bda4f5846 86 if (e2 <= y) {
amouroug 0:bb2bda4f5846 87 err += ++y*2+1;
amouroug 0:bb2bda4f5846 88 if (-x == y && e2 <= x) e2 = 0;
amouroug 0:bb2bda4f5846 89 }
amouroug 0:bb2bda4f5846 90 if (e2 > x) err += ++x*2+1;
amouroug 0:bb2bda4f5846 91 } while (x <= 0);
amouroug 0:bb2bda4f5846 92 if(old_auto_up)
amouroug 0:bb2bda4f5846 93 {
amouroug 0:bb2bda4f5846 94 auto_up=true;
amouroug 0:bb2bda4f5846 95 copy_to_lcd();
amouroug 0:bb2bda4f5846 96 }
amouroug 0:bb2bda4f5846 97 }
amouroug 0:bb2bda4f5846 98 void GraphicsDisplay::hline(int x0, int x1, int y, unsigned short color)
amouroug 0:bb2bda4f5846 99 {
amouroug 0:bb2bda4f5846 100 int len = x1 - x0 + 1;
amouroug 0:bb2bda4f5846 101 window(x0,y,len,1);
amouroug 0:bb2bda4f5846 102 // for (int j=0; j<len; j++) window_pushpixel(color);
amouroug 0:bb2bda4f5846 103 window_pushpixel(color, len);
amouroug 0:bb2bda4f5846 104 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 105 return;
amouroug 0:bb2bda4f5846 106 }
amouroug 0:bb2bda4f5846 107 void GraphicsDisplay::vline(int x, int y0, int y1, unsigned short color)
amouroug 0:bb2bda4f5846 108 {
amouroug 0:bb2bda4f5846 109 int len = y1 - y0 + 1;
amouroug 0:bb2bda4f5846 110 window(x,y0,1,len);
amouroug 0:bb2bda4f5846 111 // for (int y=0; y<len; y++) window_pushpixel(color);
amouroug 0:bb2bda4f5846 112 window_pushpixel(color, len);
amouroug 0:bb2bda4f5846 113 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 114 return;
amouroug 0:bb2bda4f5846 115 }
amouroug 0:bb2bda4f5846 116 void GraphicsDisplay::line(int x0, int y0, int x1, int y1, unsigned short color)
amouroug 0:bb2bda4f5846 117 {
amouroug 0:bb2bda4f5846 118 //WindowMax();
amouroug 0:bb2bda4f5846 119 int dx = 0, dy = 0;
amouroug 0:bb2bda4f5846 120 int dx_sym = 0, dy_sym = 0;
amouroug 0:bb2bda4f5846 121 int dx_x2 = 0, dy_x2 = 0;
amouroug 0:bb2bda4f5846 122 int di = 0;
amouroug 0:bb2bda4f5846 123
amouroug 0:bb2bda4f5846 124 dx = x1-x0;
amouroug 0:bb2bda4f5846 125 dy = y1-y0;
amouroug 0:bb2bda4f5846 126
amouroug 0:bb2bda4f5846 127 if (dx == 0) { /* vertical line */
amouroug 0:bb2bda4f5846 128 if (y1 < y0) SWAP(y0,y1);
amouroug 0:bb2bda4f5846 129 vline(x0,y0,y1,color);
amouroug 0:bb2bda4f5846 130 return;
amouroug 0:bb2bda4f5846 131 }
amouroug 0:bb2bda4f5846 132
amouroug 0:bb2bda4f5846 133 if (dx > 0) {
amouroug 0:bb2bda4f5846 134 dx_sym = 1;
amouroug 0:bb2bda4f5846 135 } else {
amouroug 0:bb2bda4f5846 136 dx_sym = -1;
amouroug 0:bb2bda4f5846 137 }
amouroug 0:bb2bda4f5846 138 if (dy == 0) { /* horizontal line */
amouroug 0:bb2bda4f5846 139 if (x1 < x0) SWAP(x1,x0);
amouroug 0:bb2bda4f5846 140 hline(x0,x1,y0,color);
amouroug 0:bb2bda4f5846 141 return;
amouroug 0:bb2bda4f5846 142 }
amouroug 0:bb2bda4f5846 143
amouroug 0:bb2bda4f5846 144 if (dy > 0) {
amouroug 0:bb2bda4f5846 145 dy_sym = 1;
amouroug 0:bb2bda4f5846 146 } else {
amouroug 0:bb2bda4f5846 147 dy_sym = -1;
amouroug 0:bb2bda4f5846 148 }
amouroug 0:bb2bda4f5846 149
amouroug 0:bb2bda4f5846 150 dx = dx_sym*dx;
amouroug 0:bb2bda4f5846 151 dy = dy_sym*dy;
amouroug 0:bb2bda4f5846 152
amouroug 0:bb2bda4f5846 153 dx_x2 = dx*2;
amouroug 0:bb2bda4f5846 154 dy_x2 = dy*2;
amouroug 0:bb2bda4f5846 155
amouroug 0:bb2bda4f5846 156 if (dx >= dy) {
amouroug 0:bb2bda4f5846 157 di = dy_x2 - dx;
amouroug 0:bb2bda4f5846 158 while (x0 != x1) {
amouroug 0:bb2bda4f5846 159
amouroug 0:bb2bda4f5846 160 pixel(x0, y0, color);
amouroug 0:bb2bda4f5846 161 x0 += dx_sym;
amouroug 0:bb2bda4f5846 162 if (di<0) {
amouroug 0:bb2bda4f5846 163 di += dy_x2;
amouroug 0:bb2bda4f5846 164 } else {
amouroug 0:bb2bda4f5846 165 di += dy_x2 - dx_x2;
amouroug 0:bb2bda4f5846 166 y0 += dy_sym;
amouroug 0:bb2bda4f5846 167 }
amouroug 0:bb2bda4f5846 168 }
amouroug 0:bb2bda4f5846 169 pixel(x0, y0, color);
amouroug 0:bb2bda4f5846 170 } else {
amouroug 0:bb2bda4f5846 171 di = dx_x2 - dy;
amouroug 0:bb2bda4f5846 172 while (y0 != y1) {
amouroug 0:bb2bda4f5846 173 pixel(x0, y0, color);
amouroug 0:bb2bda4f5846 174 y0 += dy_sym;
amouroug 0:bb2bda4f5846 175 if (di < 0) {
amouroug 0:bb2bda4f5846 176 di += dx_x2;
amouroug 0:bb2bda4f5846 177 } else {
amouroug 0:bb2bda4f5846 178 di += dx_x2 - dy_x2;
amouroug 0:bb2bda4f5846 179 x0 += dx_sym;
amouroug 0:bb2bda4f5846 180 }
amouroug 0:bb2bda4f5846 181 }
amouroug 0:bb2bda4f5846 182 pixel(x0, y0, color);
amouroug 0:bb2bda4f5846 183 }
amouroug 0:bb2bda4f5846 184 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 185 return;
amouroug 0:bb2bda4f5846 186 }
amouroug 0:bb2bda4f5846 187 void GraphicsDisplay::rect(int x0, int y0, int x1, int y1, unsigned short color)
amouroug 0:bb2bda4f5846 188 {
amouroug 0:bb2bda4f5846 189 bool old_auto_up=auto_up;
amouroug 0:bb2bda4f5846 190 if(auto_up) auto_up=0;
amouroug 0:bb2bda4f5846 191 if (x1 > x0) hline(x0,x1,y0,color);
amouroug 0:bb2bda4f5846 192 else hline(x1,x0,y0,color);
amouroug 0:bb2bda4f5846 193
amouroug 0:bb2bda4f5846 194 if (y1 > y0) vline(x0,y0,y1,color);
amouroug 0:bb2bda4f5846 195 else vline(x0,y1,y0,color);
amouroug 0:bb2bda4f5846 196
amouroug 0:bb2bda4f5846 197 if (x1 > x0) hline(x0,x1,y1,color);
amouroug 0:bb2bda4f5846 198 else hline(x1,x0,y1,color);
amouroug 0:bb2bda4f5846 199
amouroug 0:bb2bda4f5846 200 if (y1 > y0) vline(x1,y0,y1,color);
amouroug 0:bb2bda4f5846 201 else vline(x1,y1,y0,color);
amouroug 0:bb2bda4f5846 202 if(old_auto_up)
amouroug 0:bb2bda4f5846 203 {
amouroug 0:bb2bda4f5846 204 auto_up=true;
amouroug 0:bb2bda4f5846 205 copy_to_lcd();
amouroug 0:bb2bda4f5846 206 }
amouroug 0:bb2bda4f5846 207 return;
amouroug 0:bb2bda4f5846 208 }
amouroug 0:bb2bda4f5846 209 void GraphicsDisplay::fillrect(int x0, int y0, int x1, int y1, unsigned short color)
amouroug 0:bb2bda4f5846 210 {
amouroug 0:bb2bda4f5846 211 if(x0 > x1) SWAP(x0,x1);
amouroug 0:bb2bda4f5846 212 if(y0 > y1) SWAP(y0,y1);
amouroug 0:bb2bda4f5846 213
amouroug 0:bb2bda4f5846 214 int h = y1 - y0 + 1;
amouroug 0:bb2bda4f5846 215 int w = x1 - x0 + 1;
amouroug 0:bb2bda4f5846 216 unsigned int pixels = h * w;
amouroug 0:bb2bda4f5846 217 window(x0,y0,w,h);
amouroug 0:bb2bda4f5846 218 // for (unsigned int p=0; p<pixels; p++) window_pushpixel(color);
amouroug 0:bb2bda4f5846 219 window_pushpixel(color, pixels);
amouroug 0:bb2bda4f5846 220 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 221 return;
amouroug 0:bb2bda4f5846 222 }
amouroug 0:bb2bda4f5846 223 void GraphicsDisplay::locate(int x, int y)
amouroug 0:bb2bda4f5846 224 {
amouroug 0:bb2bda4f5846 225 char_x = x;
amouroug 0:bb2bda4f5846 226 char_y = y;
amouroug 0:bb2bda4f5846 227 }
amouroug 0:bb2bda4f5846 228 int GraphicsDisplay::columns()
amouroug 0:bb2bda4f5846 229 {
amouroug 0:bb2bda4f5846 230 return oriented_width / fonthor;
amouroug 0:bb2bda4f5846 231 }
amouroug 0:bb2bda4f5846 232 int GraphicsDisplay::rows()
amouroug 0:bb2bda4f5846 233 {
amouroug 0:bb2bda4f5846 234 return oriented_height / fontvert;
amouroug 0:bb2bda4f5846 235 }
amouroug 0:bb2bda4f5846 236 void GraphicsDisplay::set_font(unsigned char* f, unsigned char firstascii, unsigned char lastascii, bool proportional)
amouroug 0:bb2bda4f5846 237 {
amouroug 0:bb2bda4f5846 238 font = f;
amouroug 0:bb2bda4f5846 239 // read font parameter from start of array
amouroug 0:bb2bda4f5846 240 //fontoffset = font[0]; // bytes / char
amouroug 0:bb2bda4f5846 241 fonthor = font[1]; // get hor size of font
amouroug 0:bb2bda4f5846 242 fontvert = font[2]; // get vert size of font
amouroug 0:bb2bda4f5846 243 //fontbpl = font[3]; // bytes per line
amouroug 0:bb2bda4f5846 244 fontbpl = (fontvert+7)>>3; //bytes per line, rounded up to multiple of 8
amouroug 0:bb2bda4f5846 245 fontoffset = (fonthor*fontbpl)+1;
amouroug 0:bb2bda4f5846 246 firstch = firstascii; // first ascii code present in font array (usually 32)
amouroug 0:bb2bda4f5846 247 lastch = lastascii; // last ascii code present in font array (usually 127)
amouroug 0:bb2bda4f5846 248 fontprop=proportional;
amouroug 0:bb2bda4f5846 249 set_font_zoom(1,1);
amouroug 0:bb2bda4f5846 250 }
amouroug 0:bb2bda4f5846 251 void GraphicsDisplay::set_font_zoom(unsigned char x_mul, unsigned char y_mul)
amouroug 0:bb2bda4f5846 252 {
amouroug 0:bb2bda4f5846 253 fontzoomhor=((x_mul==0) ? 1:x_mul);
amouroug 0:bb2bda4f5846 254 fontzoomver=((y_mul==0) ? 1:y_mul);
amouroug 0:bb2bda4f5846 255 }
amouroug 0:bb2bda4f5846 256 int GraphicsDisplay::_putc(int value)
amouroug 0:bb2bda4f5846 257 {
amouroug 0:bb2bda4f5846 258 if (value == '\n') { // new line
amouroug 0:bb2bda4f5846 259 char_x = 0;
amouroug 0:bb2bda4f5846 260 char_y = char_y + fontvert*fontzoomver;
amouroug 0:bb2bda4f5846 261 if (char_y >= oriented_height - fontvert*fontzoomver) {
amouroug 0:bb2bda4f5846 262 char_y = 0;
amouroug 0:bb2bda4f5846 263 }
amouroug 0:bb2bda4f5846 264 } else {
amouroug 0:bb2bda4f5846 265 character(char_x, char_y, value);
amouroug 0:bb2bda4f5846 266 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 267 }
amouroug 0:bb2bda4f5846 268 return value;
amouroug 0:bb2bda4f5846 269 }
amouroug 0:bb2bda4f5846 270 void GraphicsDisplay::character(int x, int y, int c)
amouroug 0:bb2bda4f5846 271 {
amouroug 0:bb2bda4f5846 272 char_x=x;
amouroug 0:bb2bda4f5846 273 char_y=y;
amouroug 0:bb2bda4f5846 274 int j,i,b;
amouroug 0:bb2bda4f5846 275 unsigned char* zeichen;
amouroug 0:bb2bda4f5846 276 unsigned char z,w,v;
amouroug 0:bb2bda4f5846 277
amouroug 0:bb2bda4f5846 278 /* // read font parameter from start of array
amouroug 0:bb2bda4f5846 279 offset = font[0]; // bytes / char
amouroug 0:bb2bda4f5846 280 hor = font[1]; // get hor size of font
amouroug 0:bb2bda4f5846 281 vert = font[2]; // get vert size of font
amouroug 0:bb2bda4f5846 282 bpl = font[3]; // bytes per line
amouroug 0:bb2bda4f5846 283 */
amouroug 0:bb2bda4f5846 284 if (char_x + fonthor*fontzoomhor > oriented_width) {
amouroug 0:bb2bda4f5846 285 char_x = 0;
amouroug 0:bb2bda4f5846 286 char_y = char_y + fontvert*fontzoomver;
amouroug 0:bb2bda4f5846 287 if (char_y > oriented_height - fontvert*fontzoomver) {
amouroug 0:bb2bda4f5846 288 char_y = 0;
amouroug 0:bb2bda4f5846 289 }
amouroug 0:bb2bda4f5846 290 }
amouroug 0:bb2bda4f5846 291 window(char_x, char_y,fonthor*fontzoomhor,fontvert*fontzoomver); // char box
amouroug 0:bb2bda4f5846 292 if ((c < firstch) || (c > lastch)) { // test char range - if not exist fill with blank
amouroug 0:bb2bda4f5846 293 for (i = 0; i < fonthor*fontvert*fontzoomver;i++){
amouroug 0:bb2bda4f5846 294 window_pushpixel(_background, fontzoomhor); //(color, howmany)
amouroug 0:bb2bda4f5846 295 }
amouroug 0:bb2bda4f5846 296 }
amouroug 0:bb2bda4f5846 297 else{
amouroug 0:bb2bda4f5846 298 zeichen = &font[((c-firstch) * fontoffset) + 4]; // start of char bitmap
amouroug 0:bb2bda4f5846 299 w = zeichen[0]; // width of actual char
amouroug 0:bb2bda4f5846 300 // construct the char into the buffer
amouroug 0:bb2bda4f5846 301 for (j=0; j<fontvert; j++) { // vert line
amouroug 0:bb2bda4f5846 302 for (v=0; v<fontzoomver; v++) { // repeat horiz line for vertical zooming
amouroug 0:bb2bda4f5846 303 for (i=0; i<fonthor; i++) { // horz line
amouroug 0:bb2bda4f5846 304 z = zeichen[(fontbpl * i) + ((j & 0xF8) >> 3)+1];
amouroug 0:bb2bda4f5846 305 b = 1 << (j & 0x07);
amouroug 0:bb2bda4f5846 306 if (( z & b ) == 0x00) {
amouroug 0:bb2bda4f5846 307 // pixel(char_x+i,char_y+j,0);
amouroug 0:bb2bda4f5846 308 window_pushpixel(_background, fontzoomhor); //(color, howmany)
amouroug 0:bb2bda4f5846 309 } else {
amouroug 0:bb2bda4f5846 310 // pixel(char_x+i,char_y+j,1);
amouroug 0:bb2bda4f5846 311 window_pushpixel(_foreground, fontzoomhor);
amouroug 0:bb2bda4f5846 312 }
amouroug 0:bb2bda4f5846 313 }
amouroug 0:bb2bda4f5846 314 } //for each zoomed vert
amouroug 0:bb2bda4f5846 315 }
amouroug 0:bb2bda4f5846 316 }
amouroug 0:bb2bda4f5846 317 if(fontprop)
amouroug 0:bb2bda4f5846 318 {
amouroug 0:bb2bda4f5846 319 if((w+1)<fonthor) char_x += (w*fontzoomhor)+1; // put at least 1 blank after variable-width characters, except characters that occupy whole fonthor space like "_"
amouroug 0:bb2bda4f5846 320 else char_x += fonthor*fontzoomhor;
amouroug 0:bb2bda4f5846 321 }
amouroug 0:bb2bda4f5846 322 else char_x += fonthor*fontzoomhor; // fixed width
amouroug 0:bb2bda4f5846 323 }
amouroug 0:bb2bda4f5846 324 void GraphicsDisplay::Bitmap_BW(Bitmap_s bm, int x, int y)
amouroug 0:bb2bda4f5846 325 {
amouroug 0:bb2bda4f5846 326 int h,v,b;
amouroug 0:bb2bda4f5846 327 // int cropX;
amouroug 0:bb2bda4f5846 328 char d;
amouroug 0:bb2bda4f5846 329 if(x<0) x=0;
amouroug 0:bb2bda4f5846 330 if(y<0) y=0;
amouroug 0:bb2bda4f5846 331 int cropX = (x+bm.xSize)-oriented_width;
amouroug 0:bb2bda4f5846 332 if(cropX<0) cropX=0;
amouroug 0:bb2bda4f5846 333 window(x, y, bm.xSize-cropX, bm.ySize);
amouroug 0:bb2bda4f5846 334 for(v=0; v < bm.ySize; v++) { // lines
amouroug 0:bb2bda4f5846 335 if((v + y) >= oriented_height) break; // no need to crop Y
amouroug 0:bb2bda4f5846 336 for(h=0; h < bm.xSize; h++) { // pixel
amouroug 0:bb2bda4f5846 337 if((h + x) >= oriented_width) break;
amouroug 0:bb2bda4f5846 338 d = bm.data[bm.Byte_in_Line * v + ((h & 0xF8) >> 3)];
amouroug 0:bb2bda4f5846 339 b = 0x80 >> (h & 0x07);
amouroug 0:bb2bda4f5846 340 if((d & b) == 0) {
amouroug 0:bb2bda4f5846 341 window_pushpixel(_background);
amouroug 0:bb2bda4f5846 342 } else {
amouroug 0:bb2bda4f5846 343 window_pushpixel(_foreground);
amouroug 0:bb2bda4f5846 344 }
amouroug 0:bb2bda4f5846 345 }
amouroug 0:bb2bda4f5846 346 }
amouroug 0:bb2bda4f5846 347 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 348 }
amouroug 0:bb2bda4f5846 349 void GraphicsDisplay::Bitmap(int x, int y, int w, int h,unsigned char *bitmap)
amouroug 0:bb2bda4f5846 350 {
amouroug 0:bb2bda4f5846 351 int j;
amouroug 0:bb2bda4f5846 352 unsigned char padd;
amouroug 0:bb2bda4f5846 353 unsigned short *bitmap_ptr = (unsigned short *)bitmap;
amouroug 0:bb2bda4f5846 354
amouroug 0:bb2bda4f5846 355 padd = w%2; // the lines are padded to multiple of 4 bytes in a bitmap
amouroug 0:bb2bda4f5846 356 if(x<0) x=0;
amouroug 0:bb2bda4f5846 357 else if(x>=oriented_width) return;
amouroug 0:bb2bda4f5846 358 if(y<0) y=0;
amouroug 0:bb2bda4f5846 359 else if(y>=oriented_height) return;
amouroug 0:bb2bda4f5846 360 int cropX = (x+w)-oriented_width;
amouroug 0:bb2bda4f5846 361 if(cropX<0) cropX=0;
amouroug 0:bb2bda4f5846 362 int cropY = (y+h)-oriented_height;
amouroug 0:bb2bda4f5846 363 if(cropY<0) cropY=0;
amouroug 0:bb2bda4f5846 364 window(x, y, w-cropX, h-cropY);
amouroug 0:bb2bda4f5846 365 bitmap_ptr += ((h - 1)* (w + padd)); // begin of last line in array (first line of image)(standard bmp scan direction is left->right bottom->top)
amouroug 0:bb2bda4f5846 366 for (j = 0; j < h-cropY; j++) { //Lines
amouroug 0:bb2bda4f5846 367 window_pushpixelbuf(bitmap_ptr, w-cropX);
amouroug 0:bb2bda4f5846 368 bitmap_ptr -= w+padd;
amouroug 0:bb2bda4f5846 369 }
amouroug 0:bb2bda4f5846 370 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 371 }
amouroug 0:bb2bda4f5846 372
amouroug 0:bb2bda4f5846 373 // local filesystem is not implemented in kinetis board , but you can add a SD card
amouroug 0:bb2bda4f5846 374 // fixme this whole functions needs testing and speedup
amouroug 0:bb2bda4f5846 375 int GraphicsDisplay::BMP_16(int x, int y, const char *Name_BMP)
amouroug 0:bb2bda4f5846 376 {
amouroug 0:bb2bda4f5846 377
amouroug 0:bb2bda4f5846 378 #define OffsetPixelWidth 18
amouroug 0:bb2bda4f5846 379 #define OffsetPixelHeigh 22
amouroug 0:bb2bda4f5846 380 #define OffsetFileSize 34
amouroug 0:bb2bda4f5846 381 #define OffsetPixData 10
amouroug 0:bb2bda4f5846 382 #define OffsetBPP 28
amouroug 0:bb2bda4f5846 383
amouroug 0:bb2bda4f5846 384 char filename[50];
amouroug 0:bb2bda4f5846 385 unsigned char BMP_Header[54];
amouroug 0:bb2bda4f5846 386 unsigned short BPP_t;
amouroug 0:bb2bda4f5846 387 unsigned int PixelWidth,PixelHeigh,start_data;
amouroug 0:bb2bda4f5846 388 unsigned int i,off;
amouroug 0:bb2bda4f5846 389 int padd,j;
amouroug 0:bb2bda4f5846 390 unsigned short *line;
amouroug 0:bb2bda4f5846 391
amouroug 0:bb2bda4f5846 392 // get the filename
amouroug 0:bb2bda4f5846 393 i=0;
amouroug 0:bb2bda4f5846 394 while (*Name_BMP!='\0') {
amouroug 0:bb2bda4f5846 395 filename[i++]=*Name_BMP++;
amouroug 0:bb2bda4f5846 396 }
amouroug 0:bb2bda4f5846 397 filename[i] = 0;
amouroug 0:bb2bda4f5846 398
amouroug 0:bb2bda4f5846 399 FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file
amouroug 0:bb2bda4f5846 400 if (!Image) {
amouroug 0:bb2bda4f5846 401 return(0); // error file not found !
amouroug 0:bb2bda4f5846 402 }
amouroug 0:bb2bda4f5846 403
amouroug 0:bb2bda4f5846 404 fread(&BMP_Header[0],1,54,Image); // get the BMP Header
amouroug 0:bb2bda4f5846 405
amouroug 0:bb2bda4f5846 406 if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte
amouroug 0:bb2bda4f5846 407 fclose(Image);
amouroug 0:bb2bda4f5846 408 return(-1); // error no BMP file
amouroug 0:bb2bda4f5846 409 }
amouroug 0:bb2bda4f5846 410
amouroug 0:bb2bda4f5846 411 BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8);
amouroug 0:bb2bda4f5846 412 if (BPP_t != 0x0010) {
amouroug 0:bb2bda4f5846 413 fclose(Image);
amouroug 0:bb2bda4f5846 414 return(-2); // error no 16 bit BMP
amouroug 0:bb2bda4f5846 415 }
amouroug 0:bb2bda4f5846 416
amouroug 0:bb2bda4f5846 417 PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24);
amouroug 0:bb2bda4f5846 418 PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24);
amouroug 0:bb2bda4f5846 419 if (PixelHeigh > oriented_height + y || PixelWidth > oriented_width + x) {
amouroug 0:bb2bda4f5846 420 fclose(Image);
amouroug 0:bb2bda4f5846 421 return(-3); // to big
amouroug 0:bb2bda4f5846 422 }
amouroug 0:bb2bda4f5846 423
amouroug 0:bb2bda4f5846 424 start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24);
amouroug 0:bb2bda4f5846 425
amouroug 0:bb2bda4f5846 426 line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line
amouroug 0:bb2bda4f5846 427 if (line == NULL) {
amouroug 0:bb2bda4f5846 428 return(-4); // error no memory
amouroug 0:bb2bda4f5846 429 }
amouroug 0:bb2bda4f5846 430
amouroug 0:bb2bda4f5846 431 // the bmp lines are padded to multiple of 4 bytes
amouroug 0:bb2bda4f5846 432 padd = -1;
amouroug 0:bb2bda4f5846 433 do {
amouroug 0:bb2bda4f5846 434 padd ++;
amouroug 0:bb2bda4f5846 435 } while ((PixelWidth * 2 + padd)%4 != 0);
amouroug 0:bb2bda4f5846 436
amouroug 0:bb2bda4f5846 437 window(x, y,PixelWidth ,PixelHeigh);
amouroug 0:bb2bda4f5846 438 // wr_cmd(0x2C); // send pixel
amouroug 0:bb2bda4f5846 439 for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up
amouroug 0:bb2bda4f5846 440 off = j * (PixelWidth * 2 + padd) + start_data; // start of line
amouroug 0:bb2bda4f5846 441 fseek(Image, off ,SEEK_SET);
amouroug 0:bb2bda4f5846 442 fread(line,1,PixelWidth * 2,Image); // read a line - slow
amouroug 0:bb2bda4f5846 443 /* for (i = 0; i < PixelWidth; i++)
amouroug 0:bb2bda4f5846 444 { // copy pixel data to TFT
amouroug 0:bb2bda4f5846 445 // wr_16(line[i]); // one 16 bit pixel
amouroug 0:bb2bda4f5846 446 window_pushpixel(line[i]);
amouroug 0:bb2bda4f5846 447
amouroug 0:bb2bda4f5846 448 } */
amouroug 0:bb2bda4f5846 449 window_pushpixelbuf(line, PixelWidth);
amouroug 0:bb2bda4f5846 450 }
amouroug 0:bb2bda4f5846 451 free (line);
amouroug 0:bb2bda4f5846 452 fclose(Image);
amouroug 0:bb2bda4f5846 453 if(auto_up) copy_to_lcd();
amouroug 0:bb2bda4f5846 454 return(1);
amouroug 0:bb2bda4f5846 455 }
amouroug 0:bb2bda4f5846 456
amouroug 0:bb2bda4f5846 457 void GraphicsDisplay::cls (void)
amouroug 0:bb2bda4f5846 458 {
amouroug 0:bb2bda4f5846 459 unsigned int pixels = ( oriented_width * oriented_height);
amouroug 0:bb2bda4f5846 460 WindowMax();
amouroug 0:bb2bda4f5846 461 for (unsigned int i = 0; i < pixels; i++)
amouroug 0:bb2bda4f5846 462 {
amouroug 0:bb2bda4f5846 463 window_pushpixel(_background);
amouroug 0:bb2bda4f5846 464 }
amouroug 0:bb2bda4f5846 465 }
amouroug 0:bb2bda4f5846 466 void GraphicsDisplay::set_auto_up(bool up)
amouroug 0:bb2bda4f5846 467 {
amouroug 0:bb2bda4f5846 468 if(up) auto_up = true;
amouroug 0:bb2bda4f5846 469 else auto_up = false;
amouroug 0:bb2bda4f5846 470 }
amouroug 0:bb2bda4f5846 471 bool GraphicsDisplay::get_auto_up(void)
amouroug 0:bb2bda4f5846 472 {
amouroug 0:bb2bda4f5846 473 return (auto_up);
amouroug 0:bb2bda4f5846 474 }
amouroug 0:bb2bda4f5846 475
amouroug 2:59188908eb60 476 void GraphicsDisplay::triangle(int x0, int y0,
amouroug 2:59188908eb60 477 int x1, int y1,
amouroug 2:59188908eb60 478 int x2, int y2, unsigned short color) {
amouroug 2:59188908eb60 479
amouroug 2:59188908eb60 480 line(x0, y0, x1, y1, color);
amouroug 2:59188908eb60 481 line(x1, y1, x2, y2, color);
amouroug 2:59188908eb60 482 line(x2, y2, x0, y0, color);
amouroug 2:59188908eb60 483 }
amouroug 2:59188908eb60 484
amouroug 2:59188908eb60 485 // fill a triangle!
amouroug 2:59188908eb60 486 void GraphicsDisplay::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned short color)
amouroug 2:59188908eb60 487 {
amouroug 2:59188908eb60 488 int a, b, y, last;
amouroug 2:59188908eb60 489
amouroug 2:59188908eb60 490 // Sort coordinates by Y order (y2 >= y1 >= y0)
amouroug 2:59188908eb60 491 if (y0 > y1) {
amouroug 2:59188908eb60 492 swap(y0, y1); swap(x0, x1);
amouroug 2:59188908eb60 493 }
amouroug 2:59188908eb60 494 if (y1 > y2) {
amouroug 2:59188908eb60 495 swap(y2, y1); swap(x2, x1);
amouroug 2:59188908eb60 496 }
amouroug 2:59188908eb60 497 if (y0 > y1) {
amouroug 2:59188908eb60 498 swap(y0, y1); swap(x0, x1);
amouroug 2:59188908eb60 499 }
amouroug 2:59188908eb60 500
amouroug 2:59188908eb60 501 if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing
amouroug 2:59188908eb60 502 a = b = x0;
amouroug 2:59188908eb60 503 if(x1 < a) a = x1;
amouroug 2:59188908eb60 504 else if(x1 > b) b = x1;
amouroug 2:59188908eb60 505 if(x2 < a) a = x2;
amouroug 2:59188908eb60 506 else if(x2 > b) b = x2;
amouroug 2:59188908eb60 507 hline(a, y0, b-a+1, color);
amouroug 2:59188908eb60 508 return;
amouroug 2:59188908eb60 509 }
amouroug 2:59188908eb60 510
amouroug 2:59188908eb60 511 int16_t
amouroug 2:59188908eb60 512 dx01 = x1 - x0,
amouroug 2:59188908eb60 513 dy01 = y1 - y0,
amouroug 2:59188908eb60 514 dx02 = x2 - x0,
amouroug 2:59188908eb60 515 dy02 = y2 - y0,
amouroug 2:59188908eb60 516 dx12 = x2 - x1,
amouroug 2:59188908eb60 517 dy12 = y2 - y1,
amouroug 2:59188908eb60 518 sa = 0,
amouroug 2:59188908eb60 519 sb = 0;
amouroug 2:59188908eb60 520
amouroug 2:59188908eb60 521 // For upper part of triangle, find scanline crossings for segments
amouroug 2:59188908eb60 522 // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
amouroug 2:59188908eb60 523 // is included here (and second loop will be skipped, avoiding a /0
amouroug 2:59188908eb60 524 // error there), otherwise scanline y1 is skipped here and handled
amouroug 2:59188908eb60 525 // in the second loop...which also avoids a /0 error here if y0=y1
amouroug 2:59188908eb60 526 // (flat-topped triangle).
amouroug 2:59188908eb60 527 if(y1 == y2) last = y1; // Include y1 scanline
amouroug 2:59188908eb60 528 else last = y1-1; // Skip it
amouroug 2:59188908eb60 529
amouroug 2:59188908eb60 530 for(y=y0; y<=last; y++) {
amouroug 2:59188908eb60 531 a = x0 + sa / dy01;
amouroug 2:59188908eb60 532 b = x0 + sb / dy02;
amouroug 2:59188908eb60 533 sa += dx01;
amouroug 2:59188908eb60 534 sb += dx02;
amouroug 2:59188908eb60 535 /* longhand:
amouroug 2:59188908eb60 536 a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
amouroug 2:59188908eb60 537 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
amouroug 2:59188908eb60 538 */
amouroug 2:59188908eb60 539 if(a > b) swap(a,b);
amouroug 2:59188908eb60 540 hline(a, y, b-a+1, color);
amouroug 2:59188908eb60 541 }
amouroug 2:59188908eb60 542
amouroug 2:59188908eb60 543 // For lower part of triangle, find scanline crossings for segments
amouroug 2:59188908eb60 544 // 0-2 and 1-2. This loop is skipped if y1=y2.
amouroug 2:59188908eb60 545 sa = dx12 * (y - y1);
amouroug 2:59188908eb60 546 sb = dx02 * (y - y0);
amouroug 2:59188908eb60 547 for(; y<=y2; y++) {
amouroug 2:59188908eb60 548 a = x1 + sa / dy12;
amouroug 2:59188908eb60 549 b = x0 + sb / dy02;
amouroug 2:59188908eb60 550 sa += dx12;
amouroug 2:59188908eb60 551 sb += dx02;
amouroug 2:59188908eb60 552 /* longhand:
amouroug 2:59188908eb60 553 a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
amouroug 2:59188908eb60 554 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
amouroug 2:59188908eb60 555 */
amouroug 2:59188908eb60 556 if(a > b) swap(a,b);
amouroug 2:59188908eb60 557 hline(a, y, b-a+1, color);
amouroug 2:59188908eb60 558 }
amouroug 2:59188908eb60 559 }
amouroug 2:59188908eb60 560