1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Committer:
ttodorov
Date:
Tue Dec 11 20:02:48 2012 +0000
Revision:
13:5ceeba86bbe4
Parent:
12:d0978272a340
Child:
20:4bdca8d8dadc
- completed the rework of bitmap drawing functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ttodorov 0:881ff0b71102 1 /*
ttodorov 0:881ff0b71102 2 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
ttodorov 0:881ff0b71102 3 * Copyright (C)2012 Todor Todorov.
ttodorov 0:881ff0b71102 4 *
ttodorov 0:881ff0b71102 5 * This library is free software; you can redistribute it and/or
ttodorov 0:881ff0b71102 6 * modify it under the terms of the GNU Lesser General Public
ttodorov 0:881ff0b71102 7 * License as published by the Free Software Foundation; either
ttodorov 0:881ff0b71102 8 * version 2.1 of the License, or (at your option) any later version.
ttodorov 0:881ff0b71102 9 *
ttodorov 0:881ff0b71102 10 * This library is distributed in the hope that it will be useful,
ttodorov 0:881ff0b71102 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ttodorov 0:881ff0b71102 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ttodorov 0:881ff0b71102 13 * Lesser General Public License for more details.
ttodorov 0:881ff0b71102 14 *
ttodorov 0:881ff0b71102 15 * You should have received a copy of the GNU Lesser General Public
ttodorov 0:881ff0b71102 16 * License along with this library; if not, write to:
ttodorov 0:881ff0b71102 17 *
ttodorov 0:881ff0b71102 18 * Free Software Foundation, Inc.
ttodorov 0:881ff0b71102 19 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
ttodorov 0:881ff0b71102 20 *
ttodorov 0:881ff0b71102 21 *********************************************************************/
ttodorov 0:881ff0b71102 22 #include "lcd_base.h"
ttodorov 3:64a5b67d5b51 23 #include "helpers.h"
ttodorov 0:881ff0b71102 24
ttodorov 4:3ac4239f6c9c 25 LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET )
ttodorov 4:3ac4239f6c9c 26 : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS ), _lcd_pin_reset( RESET )
ttodorov 4:3ac4239f6c9c 27 {
ttodorov 4:3ac4239f6c9c 28 SetForeground();
ttodorov 4:3ac4239f6c9c 29 SetBackground();
ttodorov 4:3ac4239f6c9c 30 _font.font = 0;
ttodorov 4:3ac4239f6c9c 31 }
ttodorov 0:881ff0b71102 32
ttodorov 0:881ff0b71102 33 inline
ttodorov 12:d0978272a340 34 void LCD::SetForeground( unsigned int color )
ttodorov 0:881ff0b71102 35 {
ttodorov 0:881ff0b71102 36 _foreground = color;
ttodorov 0:881ff0b71102 37 }
ttodorov 0:881ff0b71102 38
ttodorov 0:881ff0b71102 39 inline
ttodorov 12:d0978272a340 40 void LCD::SetBackground( unsigned int color )
ttodorov 0:881ff0b71102 41 {
ttodorov 0:881ff0b71102 42 _background = color;
ttodorov 0:881ff0b71102 43 }
ttodorov 0:881ff0b71102 44
ttodorov 0:881ff0b71102 45 void LCD::SetFont( const char *font )
ttodorov 0:881ff0b71102 46 {
ttodorov 0:881ff0b71102 47 _font.font = font;
ttodorov 0:881ff0b71102 48 _font.width = font[ 0 ];
ttodorov 0:881ff0b71102 49 _font.height = font[ 1 ];
ttodorov 0:881ff0b71102 50 _font.offset = font[ 2 ];
ttodorov 0:881ff0b71102 51 _font.numchars = font[ 3 ];
ttodorov 0:881ff0b71102 52 }
ttodorov 0:881ff0b71102 53
ttodorov 0:881ff0b71102 54 inline
ttodorov 0:881ff0b71102 55 unsigned short LCD::GetWidth( void )
ttodorov 0:881ff0b71102 56 {
ttodorov 12:d0978272a340 57 if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_height;
ttodorov 0:881ff0b71102 58 return _disp_width;
ttodorov 0:881ff0b71102 59 }
ttodorov 0:881ff0b71102 60
ttodorov 0:881ff0b71102 61 inline
ttodorov 0:881ff0b71102 62 unsigned short LCD::GetHeight( void )
ttodorov 0:881ff0b71102 63 {
ttodorov 12:d0978272a340 64 if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_width;
ttodorov 0:881ff0b71102 65 return _disp_height;
ttodorov 0:881ff0b71102 66 }
ttodorov 0:881ff0b71102 67
ttodorov 0:881ff0b71102 68 void LCD::FillScreen( int color )
ttodorov 0:881ff0b71102 69 {
ttodorov 12:d0978272a340 70 unsigned int rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 4:3ac4239f6c9c 71 Activate();
ttodorov 2:81ed304b7e9b 72 ClearXY();
ttodorov 1:14bef43daf6f 73 for ( int i = 0; i < ( ( _disp_width ) * ( _disp_height ) ); i++ )
ttodorov 10:69571adcfad5 74 SetPixelColor( rgb );
ttodorov 4:3ac4239f6c9c 75 Deactivate();
ttodorov 0:881ff0b71102 76 }
ttodorov 0:881ff0b71102 77
ttodorov 0:881ff0b71102 78 inline
ttodorov 0:881ff0b71102 79 void LCD::ClearScreen( void )
ttodorov 0:881ff0b71102 80 {
ttodorov 0:881ff0b71102 81 FillScreen( -1 );
ttodorov 0:881ff0b71102 82 }
ttodorov 0:881ff0b71102 83
ttodorov 0:881ff0b71102 84 void LCD::DrawPixel( unsigned short x, unsigned short y, int color )
ttodorov 0:881ff0b71102 85 {
ttodorov 4:3ac4239f6c9c 86 Activate();
ttodorov 2:81ed304b7e9b 87 SetXY( x, y, x, y );
ttodorov 10:69571adcfad5 88 SetPixelColor( color == -1 ? _background :
ttodorov 0:881ff0b71102 89 color == -2 ? _foreground : color );
ttodorov 4:3ac4239f6c9c 90 Deactivate();
ttodorov 0:881ff0b71102 91 }
ttodorov 0:881ff0b71102 92
ttodorov 0:881ff0b71102 93 void LCD::DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 94 {
ttodorov 0:881ff0b71102 95
ttodorov 0:881ff0b71102 96 double delta, tx, ty;
ttodorov 0:881ff0b71102 97
ttodorov 0:881ff0b71102 98 if ( ( ( x2 - x1 ) < 0 ) )
ttodorov 0:881ff0b71102 99 {
ttodorov 0:881ff0b71102 100 swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 101 swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 102 }
ttodorov 0:881ff0b71102 103 if ( ( ( y2 - y1 ) < 0 ) )
ttodorov 0:881ff0b71102 104 {
ttodorov 0:881ff0b71102 105 swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 106 swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 107 }
ttodorov 0:881ff0b71102 108
ttodorov 0:881ff0b71102 109 if ( y1 == y2 )
ttodorov 0:881ff0b71102 110 {
ttodorov 0:881ff0b71102 111 if ( x1 > x2 )
ttodorov 0:881ff0b71102 112 swap( ushort, x1, x2 )
ttodorov 2:81ed304b7e9b 113 DrawHLine( x1, y1, x2 - x1, color );
ttodorov 0:881ff0b71102 114 }
ttodorov 0:881ff0b71102 115 else if ( x1 == x2 )
ttodorov 0:881ff0b71102 116 {
ttodorov 0:881ff0b71102 117 if ( y1 > y2 )
ttodorov 0:881ff0b71102 118 swap( ushort, y1, y2 )
ttodorov 2:81ed304b7e9b 119 DrawVLine( x1, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 120 }
ttodorov 4:3ac4239f6c9c 121 else
ttodorov 0:881ff0b71102 122 {
ttodorov 12:d0978272a340 123 unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 4:3ac4239f6c9c 124 Activate();
ttodorov 4:3ac4239f6c9c 125 if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
ttodorov 0:881ff0b71102 126 {
ttodorov 4:3ac4239f6c9c 127 delta = ( double( y2 - y1 ) / double( x2 - x1 ) );
ttodorov 4:3ac4239f6c9c 128 ty = double( y1 );
ttodorov 4:3ac4239f6c9c 129 if ( x1 > x2 )
ttodorov 0:881ff0b71102 130 {
ttodorov 4:3ac4239f6c9c 131 for ( int i = x1; i >= x2; i-- )
ttodorov 4:3ac4239f6c9c 132 {
ttodorov 4:3ac4239f6c9c 133 SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
ttodorov 10:69571adcfad5 134 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 135 ty = ty - delta;
ttodorov 4:3ac4239f6c9c 136 }
ttodorov 4:3ac4239f6c9c 137 }
ttodorov 4:3ac4239f6c9c 138 else
ttodorov 4:3ac4239f6c9c 139 {
ttodorov 4:3ac4239f6c9c 140 for ( int i = x1; i <= x2; i++ )
ttodorov 4:3ac4239f6c9c 141 {
ttodorov 4:3ac4239f6c9c 142 SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
ttodorov 10:69571adcfad5 143 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 144 ty = ty + delta;
ttodorov 4:3ac4239f6c9c 145 }
ttodorov 0:881ff0b71102 146 }
ttodorov 0:881ff0b71102 147 }
ttodorov 0:881ff0b71102 148 else
ttodorov 0:881ff0b71102 149 {
ttodorov 4:3ac4239f6c9c 150 delta = ( float( x2 - x1 ) / float( y2 - y1 ) );
ttodorov 4:3ac4239f6c9c 151 tx = float( x1 );
ttodorov 4:3ac4239f6c9c 152 if ( y1 > y2 )
ttodorov 0:881ff0b71102 153 {
ttodorov 4:3ac4239f6c9c 154 for ( int i = y2 + 1; i > y1; i-- )
ttodorov 4:3ac4239f6c9c 155 {
ttodorov 4:3ac4239f6c9c 156 SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
ttodorov 10:69571adcfad5 157 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 158 tx = tx + delta;
ttodorov 4:3ac4239f6c9c 159 }
ttodorov 4:3ac4239f6c9c 160 }
ttodorov 4:3ac4239f6c9c 161 else
ttodorov 4:3ac4239f6c9c 162 {
ttodorov 4:3ac4239f6c9c 163 for ( int i = y1; i < y2 + 1; i++ )
ttodorov 4:3ac4239f6c9c 164 {
ttodorov 4:3ac4239f6c9c 165 SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
ttodorov 10:69571adcfad5 166 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 167 tx = tx + delta;
ttodorov 4:3ac4239f6c9c 168 }
ttodorov 0:881ff0b71102 169 }
ttodorov 0:881ff0b71102 170 }
ttodorov 4:3ac4239f6c9c 171 Deactivate();
ttodorov 0:881ff0b71102 172 }
ttodorov 0:881ff0b71102 173 }
ttodorov 0:881ff0b71102 174
ttodorov 0:881ff0b71102 175 void LCD::DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 176 {
ttodorov 0:881ff0b71102 177 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 178 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 179
ttodorov 2:81ed304b7e9b 180 DrawHLine( x1, y1, x2 - x1, color );
ttodorov 2:81ed304b7e9b 181 DrawHLine( x1, y2, x2 - x1, color );
ttodorov 2:81ed304b7e9b 182 DrawVLine( x1, y1, y2 - y1, color );
ttodorov 2:81ed304b7e9b 183 DrawVLine( x2, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 184 }
ttodorov 0:881ff0b71102 185
ttodorov 0:881ff0b71102 186 void LCD::DrawRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 187 {
ttodorov 0:881ff0b71102 188 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 189 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 190
ttodorov 0:881ff0b71102 191 if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
ttodorov 0:881ff0b71102 192 {
ttodorov 0:881ff0b71102 193 DrawPixel( x1 + 1, y1 + 1, color );
ttodorov 0:881ff0b71102 194 DrawPixel( x2 - 1, y1 + 1, color );
ttodorov 0:881ff0b71102 195 DrawPixel( x1 + 1, y2 - 1, color );
ttodorov 0:881ff0b71102 196 DrawPixel( x2 - 1, y2 - 1, color );
ttodorov 2:81ed304b7e9b 197 DrawHLine( x1 + 2, y1, x2 - x1 - 4, color );
ttodorov 2:81ed304b7e9b 198 DrawHLine( x1 + 2, y2, x2 - x1 - 4, color );
ttodorov 2:81ed304b7e9b 199 DrawVLine( x1, y1 + 2, y2 - y1 - 4, color );
ttodorov 2:81ed304b7e9b 200 DrawVLine( x2, y1 + 2, y2 - y1 - 4, color );
ttodorov 0:881ff0b71102 201 }
ttodorov 0:881ff0b71102 202 }
ttodorov 0:881ff0b71102 203
ttodorov 0:881ff0b71102 204 void LCD::FillRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 205 {
ttodorov 0:881ff0b71102 206 if ( x1 > x2 ) swap( ushort, x1, x2 );
ttodorov 0:881ff0b71102 207 if ( y1 > y2 ) swap( ushort, y1, y2 );
ttodorov 0:881ff0b71102 208
ttodorov 12:d0978272a340 209 for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
ttodorov 0:881ff0b71102 210 {
ttodorov 12:d0978272a340 211 DrawHLine( x1, y1 + i, x2 - x1, color );
ttodorov 12:d0978272a340 212 DrawHLine( x1, y2 - i, x2 - x1, color );
ttodorov 0:881ff0b71102 213 }
ttodorov 0:881ff0b71102 214 }
ttodorov 0:881ff0b71102 215
ttodorov 0:881ff0b71102 216 void LCD::FillRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 217 {
ttodorov 0:881ff0b71102 218 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 219 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 220
ttodorov 0:881ff0b71102 221 if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
ttodorov 0:881ff0b71102 222 {
ttodorov 0:881ff0b71102 223 for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
ttodorov 0:881ff0b71102 224 {
ttodorov 0:881ff0b71102 225 switch ( i )
ttodorov 0:881ff0b71102 226 {
ttodorov 0:881ff0b71102 227 case 0:
ttodorov 2:81ed304b7e9b 228 DrawHLine( x1 + 2, y1 + i, x2 - x1 - 4, color );
ttodorov 2:81ed304b7e9b 229 DrawHLine( x1 + 2, y2 - i, x2 - x1 - 4, color );
ttodorov 0:881ff0b71102 230 break;
ttodorov 0:881ff0b71102 231
ttodorov 0:881ff0b71102 232 case 1:
ttodorov 2:81ed304b7e9b 233 DrawHLine( x1 + 1, y1 + i, x2 - x1 - 2, color );
ttodorov 2:81ed304b7e9b 234 DrawHLine( x1 + 1, y2 - i, x2 - x1 - 2, color );
ttodorov 0:881ff0b71102 235 break;
ttodorov 0:881ff0b71102 236
ttodorov 0:881ff0b71102 237 default:
ttodorov 2:81ed304b7e9b 238 DrawHLine( x1, y1 + i, x2 - x1, color );
ttodorov 2:81ed304b7e9b 239 DrawHLine( x1, y2 - i, x2 - x1, color );
ttodorov 0:881ff0b71102 240 break;
ttodorov 0:881ff0b71102 241 }
ttodorov 0:881ff0b71102 242 }
ttodorov 0:881ff0b71102 243 }
ttodorov 0:881ff0b71102 244 }
ttodorov 0:881ff0b71102 245
ttodorov 0:881ff0b71102 246 void LCD::DrawCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
ttodorov 0:881ff0b71102 247 {
ttodorov 0:881ff0b71102 248 int f = 1 - radius;
ttodorov 0:881ff0b71102 249 int ddF_x = 1;
ttodorov 0:881ff0b71102 250 int ddF_y = -2 * radius;
ttodorov 0:881ff0b71102 251 int x1 = 0;
ttodorov 0:881ff0b71102 252 int y1 = radius;
ttodorov 12:d0978272a340 253 unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 0:881ff0b71102 254
ttodorov 4:3ac4239f6c9c 255 Activate();
ttodorov 2:81ed304b7e9b 256 SetXY( x, y + radius, x, y + radius );
ttodorov 10:69571adcfad5 257 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 258 SetXY( x, y - radius, x, y - radius );
ttodorov 10:69571adcfad5 259 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 260 SetXY( x + radius, y, x + radius, y );
ttodorov 10:69571adcfad5 261 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 262 SetXY( x - radius, y, x - radius, y );
ttodorov 10:69571adcfad5 263 SetPixelColor( usedColor );
ttodorov 0:881ff0b71102 264
ttodorov 0:881ff0b71102 265 while ( x1 < y1 )
ttodorov 0:881ff0b71102 266 {
ttodorov 0:881ff0b71102 267 if ( f >= 0 )
ttodorov 0:881ff0b71102 268 {
ttodorov 0:881ff0b71102 269 y1--;
ttodorov 0:881ff0b71102 270 ddF_y += 2;
ttodorov 0:881ff0b71102 271 f += ddF_y;
ttodorov 0:881ff0b71102 272 }
ttodorov 0:881ff0b71102 273 x1++;
ttodorov 0:881ff0b71102 274 ddF_x += 2;
ttodorov 0:881ff0b71102 275 f += ddF_x;
ttodorov 2:81ed304b7e9b 276 SetXY( x + x1, y + y1, x + x1, y + y1 );
ttodorov 10:69571adcfad5 277 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 278 SetXY( x - x1, y + y1, x - x1, y + y1 );
ttodorov 10:69571adcfad5 279 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 280 SetXY( x + x1, y - y1, x + x1, y - y1 );
ttodorov 10:69571adcfad5 281 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 282 SetXY( x - x1, y - y1, x - x1, y - y1 );
ttodorov 10:69571adcfad5 283 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 284 SetXY( x + y1, y + x1, x + y1, y + x1 );
ttodorov 10:69571adcfad5 285 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 286 SetXY( x - y1, y + x1, x - y1, y + x1 );
ttodorov 10:69571adcfad5 287 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 288 SetXY( x + y1, y - x1, x + y1, y - x1 );
ttodorov 10:69571adcfad5 289 SetPixelColor( usedColor );
ttodorov 2:81ed304b7e9b 290 SetXY( x - y1, y - x1, x - y1, y - x1 );
ttodorov 10:69571adcfad5 291 SetPixelColor( usedColor );
ttodorov 0:881ff0b71102 292 }
ttodorov 4:3ac4239f6c9c 293 Deactivate();
ttodorov 0:881ff0b71102 294 }
ttodorov 0:881ff0b71102 295
ttodorov 0:881ff0b71102 296 void LCD::FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
ttodorov 0:881ff0b71102 297 {
ttodorov 12:d0978272a340 298 unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 4:3ac4239f6c9c 299 Activate();
ttodorov 0:881ff0b71102 300 for ( int y1 = -radius; y1 <= radius; y1++ )
ttodorov 0:881ff0b71102 301 for ( int x1 = -radius; x1 <= radius; x1++ )
ttodorov 0:881ff0b71102 302 if ( x1 * x1 + y1 * y1 <= radius * radius )
ttodorov 0:881ff0b71102 303 {
ttodorov 2:81ed304b7e9b 304 SetXY( x + x1, y + y1, x + x1, y + y1 );
ttodorov 10:69571adcfad5 305 SetPixelColor( usedColor );
ttodorov 0:881ff0b71102 306 }
ttodorov 4:3ac4239f6c9c 307 Deactivate();
ttodorov 0:881ff0b71102 308 }
ttodorov 0:881ff0b71102 309
ttodorov 0:881ff0b71102 310 void LCD::Print( const char *str, unsigned short x, unsigned short y, int fgColor, int bgColor, unsigned short deg )
ttodorov 0:881ff0b71102 311 {
ttodorov 0:881ff0b71102 312 int stl, i;
ttodorov 0:881ff0b71102 313
ttodorov 0:881ff0b71102 314 stl = strlen( str );
ttodorov 0:881ff0b71102 315
ttodorov 12:d0978272a340 316 if ( x == RIGHT )
ttodorov 12:d0978272a340 317 x = GetWidth() - ( stl * _font.width );
ttodorov 12:d0978272a340 318 if ( x == CENTER )
ttodorov 12:d0978272a340 319 x = ( GetWidth() - ( stl * _font.width ) ) / 2;
ttodorov 0:881ff0b71102 320
ttodorov 0:881ff0b71102 321 for ( i = 0; i < stl; i++ )
ttodorov 0:881ff0b71102 322 if ( deg == 0 )
ttodorov 2:81ed304b7e9b 323 PrintChar( *str++, x + ( i * ( _font.width ) ), y, fgColor, bgColor );
ttodorov 0:881ff0b71102 324 else
ttodorov 2:81ed304b7e9b 325 RotateChar( *str++, x, y, i, fgColor, bgColor, deg );
ttodorov 0:881ff0b71102 326 }
ttodorov 0:881ff0b71102 327
ttodorov 12:d0978272a340 328 void LCD::DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned char scale )
ttodorov 0:881ff0b71102 329 {
ttodorov 0:881ff0b71102 330 int tx, ty, tc, tsx, tsy;
ttodorov 0:881ff0b71102 331
ttodorov 4:3ac4239f6c9c 332 Activate();
ttodorov 0:881ff0b71102 333 if ( scale == 1 )
ttodorov 0:881ff0b71102 334 {
ttodorov 12:d0978272a340 335 SetXY( x, y, x + img->Width - 1, y + img->Height - 1 );
ttodorov 13:5ceeba86bbe4 336
ttodorov 13:5ceeba86bbe4 337 if ( img->Format == RGB16 )
ttodorov 0:881ff0b71102 338 {
ttodorov 13:5ceeba86bbe4 339 const unsigned short *pixel = ( const unsigned short* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 340 if ( _colorDepth == RGB16 )
ttodorov 12:d0978272a340 341 for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
ttodorov 12:d0978272a340 342 WriteData( *pixel++ );
ttodorov 13:5ceeba86bbe4 343 else if ( _colorDepth == RGB18 )
ttodorov 12:d0978272a340 344 {
ttodorov 13:5ceeba86bbe4 345 unsigned short r, g, b;
ttodorov 13:5ceeba86bbe4 346 for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
ttodorov 13:5ceeba86bbe4 347 {
ttodorov 13:5ceeba86bbe4 348 r = ( *pixel & 0xF800 ) >> 8;
ttodorov 13:5ceeba86bbe4 349 g = ( *pixel & 0x07E0 ) >> 3;
ttodorov 13:5ceeba86bbe4 350 b = ( *pixel & 0x001F ) << 3;
ttodorov 13:5ceeba86bbe4 351 pixel++;
ttodorov 13:5ceeba86bbe4 352 SetPixelColor( RGB( r, g, b ) );
ttodorov 13:5ceeba86bbe4 353 }
ttodorov 13:5ceeba86bbe4 354 }
ttodorov 13:5ceeba86bbe4 355 }
ttodorov 13:5ceeba86bbe4 356 else if ( img->Format == RGB18 )
ttodorov 13:5ceeba86bbe4 357 {
ttodorov 13:5ceeba86bbe4 358 const unsigned int *pixel = ( const unsigned int* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 359 if ( _colorDepth == RGB16 )
ttodorov 13:5ceeba86bbe4 360 {
ttodorov 13:5ceeba86bbe4 361 unsigned short r, g, b;
ttodorov 12:d0978272a340 362 for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
ttodorov 12:d0978272a340 363 {
ttodorov 12:d0978272a340 364 r = ( *pixel >> 16 ) & 0xF8;
ttodorov 12:d0978272a340 365 g = ( *pixel >> 8 ) & 0xFC;
ttodorov 12:d0978272a340 366 b = ( *pixel++ & 0xF8 ) >> 3;
ttodorov 12:d0978272a340 367 WriteData( ( r << 8 ) | ( g >> 5 ) | ( g << 3 ) | b );
ttodorov 12:d0978272a340 368 }
ttodorov 12:d0978272a340 369 }
ttodorov 13:5ceeba86bbe4 370 else if ( _colorDepth == RGB18 )
ttodorov 12:d0978272a340 371 for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
ttodorov 12:d0978272a340 372 SetPixelColor( *pixel++ );
ttodorov 0:881ff0b71102 373 }
ttodorov 0:881ff0b71102 374 }
ttodorov 0:881ff0b71102 375 else
ttodorov 0:881ff0b71102 376 {
ttodorov 13:5ceeba86bbe4 377 if ( img->Format == RGB16 )
ttodorov 0:881ff0b71102 378 {
ttodorov 13:5ceeba86bbe4 379 const unsigned short *pixel = ( const unsigned short* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 380 unsigned short r, g, b;
ttodorov 13:5ceeba86bbe4 381
ttodorov 13:5ceeba86bbe4 382 for ( ty = 0; ty < img->Height; ty++ )
ttodorov 0:881ff0b71102 383 {
ttodorov 13:5ceeba86bbe4 384 SetXY( x, y + ( ty * scale ), x + ( ( img->Width * scale ) - 1 ), y + ( ty * scale ) + scale );
ttodorov 0:881ff0b71102 385 for ( tsy = 0; tsy < scale; tsy++ )
ttodorov 0:881ff0b71102 386 {
ttodorov 13:5ceeba86bbe4 387 for ( tx = 0; tx < img->Width; tx++ )
ttodorov 13:5ceeba86bbe4 388 {
ttodorov 0:881ff0b71102 389 for ( tsx = 0; tsx < scale; tsx++ )
ttodorov 13:5ceeba86bbe4 390 {
ttodorov 13:5ceeba86bbe4 391 if ( _colorDepth == RGB16 )
ttodorov 13:5ceeba86bbe4 392 WriteData( pixel[ ( ty * img->Width ) + tx ] );
ttodorov 13:5ceeba86bbe4 393 else if ( _colorDepth == RGB18 )
ttodorov 13:5ceeba86bbe4 394 {
ttodorov 13:5ceeba86bbe4 395 r = ( pixel[ ( ty * img->Width ) + tx ] & 0xF800 ) >> 8;
ttodorov 13:5ceeba86bbe4 396 g = ( pixel[ ( ty * img->Width ) + tx ] & 0x07E0 ) >> 3;
ttodorov 13:5ceeba86bbe4 397 b = ( pixel[ ( ty * img->Width ) + tx ] & 0x001F ) << 3;
ttodorov 13:5ceeba86bbe4 398 SetPixelColor( RGB( r, g, b ) );
ttodorov 13:5ceeba86bbe4 399 }
ttodorov 13:5ceeba86bbe4 400 }
ttodorov 13:5ceeba86bbe4 401 }
ttodorov 0:881ff0b71102 402 }
ttodorov 0:881ff0b71102 403 }
ttodorov 0:881ff0b71102 404 }
ttodorov 13:5ceeba86bbe4 405 else if ( img->Format == RGB18 )
ttodorov 13:5ceeba86bbe4 406 {
ttodorov 13:5ceeba86bbe4 407 const unsigned int *pixel = ( const unsigned int* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 408 unsigned short r, g, b;
ttodorov 13:5ceeba86bbe4 409
ttodorov 13:5ceeba86bbe4 410 for ( ty = 0; ty < img->Height; ty++ )
ttodorov 13:5ceeba86bbe4 411 {
ttodorov 13:5ceeba86bbe4 412 SetXY( x, y + ( ty * scale ), x + ( ( img->Width * scale ) - 1 ), y + ( ty * scale ) + scale );
ttodorov 13:5ceeba86bbe4 413 for ( tsy = 0; tsy < scale; tsy++ )
ttodorov 13:5ceeba86bbe4 414 {
ttodorov 13:5ceeba86bbe4 415 for ( tx = 0; tx < img->Width; tx++ )
ttodorov 13:5ceeba86bbe4 416 {
ttodorov 13:5ceeba86bbe4 417 for ( tsx = 0; tsx < scale; tsx++ )
ttodorov 13:5ceeba86bbe4 418 {
ttodorov 13:5ceeba86bbe4 419 if ( _colorDepth == RGB16 )
ttodorov 13:5ceeba86bbe4 420 {
ttodorov 13:5ceeba86bbe4 421 r = ( pixel[ ( ty * img->Width ) + tx ] >> 16 ) & 0xF8;
ttodorov 13:5ceeba86bbe4 422 g = ( pixel[ ( ty * img->Width ) + tx ] >> 8 ) & 0xFC;
ttodorov 13:5ceeba86bbe4 423 b = ( pixel[ ( ty * img->Width ) + tx ] & 0xF8 ) >> 3;
ttodorov 13:5ceeba86bbe4 424 WriteData( ( r << 8 ) | ( g >> 5 ) | ( g << 3 ) | b );
ttodorov 13:5ceeba86bbe4 425 }
ttodorov 13:5ceeba86bbe4 426 else if ( _colorDepth == RGB18 )
ttodorov 13:5ceeba86bbe4 427 SetPixelColor( pixel[ ( ty * img->Width ) + tx ] );
ttodorov 13:5ceeba86bbe4 428 }
ttodorov 13:5ceeba86bbe4 429 }
ttodorov 13:5ceeba86bbe4 430 }
ttodorov 13:5ceeba86bbe4 431 }
ttodorov 13:5ceeba86bbe4 432 }
ttodorov 0:881ff0b71102 433 }
ttodorov 4:3ac4239f6c9c 434 Deactivate();
ttodorov 0:881ff0b71102 435 }
ttodorov 0:881ff0b71102 436
ttodorov 12:d0978272a340 437 void LCD::DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned short deg, unsigned short rox, unsigned short roy )
ttodorov 0:881ff0b71102 438 {
ttodorov 0:881ff0b71102 439 int tx, ty, newx, newy;
ttodorov 0:881ff0b71102 440 double radian;
ttodorov 0:881ff0b71102 441 radian = deg * 0.0175;
ttodorov 0:881ff0b71102 442
ttodorov 0:881ff0b71102 443 if ( deg == 0 )
ttodorov 12:d0978272a340 444 DrawBitmap( x, y, img );
ttodorov 0:881ff0b71102 445 else
ttodorov 0:881ff0b71102 446 {
ttodorov 4:3ac4239f6c9c 447 Activate();
ttodorov 13:5ceeba86bbe4 448
ttodorov 13:5ceeba86bbe4 449 if ( img->Format == RGB16 )
ttodorov 13:5ceeba86bbe4 450 {
ttodorov 13:5ceeba86bbe4 451 const unsigned short *pixel = ( const unsigned short* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 452 unsigned short r, g, b;
ttodorov 13:5ceeba86bbe4 453
ttodorov 13:5ceeba86bbe4 454 for ( ty = 0; ty < img->Height; ty++ )
ttodorov 13:5ceeba86bbe4 455 for ( tx = 0; tx < img->Width; tx++ )
ttodorov 13:5ceeba86bbe4 456 {
ttodorov 13:5ceeba86bbe4 457 newx = x + rox + ( ( ( tx - rox ) * cos( radian ) ) - ( ( ty - roy ) * sin( radian ) ) );
ttodorov 13:5ceeba86bbe4 458 newy = y + roy + ( ( ( ty - roy ) * cos( radian ) ) + ( ( tx - rox ) * sin( radian ) ) );
ttodorov 13:5ceeba86bbe4 459
ttodorov 13:5ceeba86bbe4 460 SetXY( newx, newy, newx, newy );
ttodorov 13:5ceeba86bbe4 461 if ( _colorDepth == RGB16 )
ttodorov 13:5ceeba86bbe4 462 WriteData( pixel[ ( ty * img->Width ) + tx ] );
ttodorov 13:5ceeba86bbe4 463 else if ( _colorDepth == RGB18 )
ttodorov 13:5ceeba86bbe4 464 {
ttodorov 13:5ceeba86bbe4 465 r = ( pixel[ ( ty * img->Width ) + tx ] & 0xF800 ) >> 8;
ttodorov 13:5ceeba86bbe4 466 g = ( pixel[ ( ty * img->Width ) + tx ] & 0x07E0 ) >> 3;
ttodorov 13:5ceeba86bbe4 467 b = ( pixel[ ( ty * img->Width ) + tx ] & 0x001F ) << 3;
ttodorov 13:5ceeba86bbe4 468 SetPixelColor( RGB( r, g, b ) );
ttodorov 13:5ceeba86bbe4 469 }
ttodorov 13:5ceeba86bbe4 470 }
ttodorov 13:5ceeba86bbe4 471 }
ttodorov 13:5ceeba86bbe4 472 else if ( img->Format == RGB18 )
ttodorov 13:5ceeba86bbe4 473 {
ttodorov 13:5ceeba86bbe4 474 const unsigned int *pixel = ( const unsigned int* ) img->PixelData;
ttodorov 13:5ceeba86bbe4 475 unsigned short r, g, b;
ttodorov 13:5ceeba86bbe4 476
ttodorov 13:5ceeba86bbe4 477 for ( ty = 0; ty < img->Height; ty++ )
ttodorov 13:5ceeba86bbe4 478 for ( tx = 0; tx < img->Width; tx++ )
ttodorov 13:5ceeba86bbe4 479 {
ttodorov 13:5ceeba86bbe4 480 newx = x + rox + ( ( ( tx - rox ) * cos( radian ) ) - ( ( ty - roy ) * sin( radian ) ) );
ttodorov 13:5ceeba86bbe4 481 newy = y + roy + ( ( ( ty - roy ) * cos( radian ) ) + ( ( tx - rox ) * sin( radian ) ) );
ttodorov 13:5ceeba86bbe4 482
ttodorov 13:5ceeba86bbe4 483 SetXY( newx, newy, newx, newy );
ttodorov 13:5ceeba86bbe4 484 if ( _colorDepth == RGB16 )
ttodorov 13:5ceeba86bbe4 485 {
ttodorov 13:5ceeba86bbe4 486 r = ( pixel[ ( ty * img->Width ) + tx ] >> 16 ) & 0xF8;
ttodorov 13:5ceeba86bbe4 487 g = ( pixel[ ( ty * img->Width ) + tx ] >> 8 ) & 0xFC;
ttodorov 13:5ceeba86bbe4 488 b = ( pixel[ ( ty * img->Width ) + tx ] & 0xF8 ) >> 3;
ttodorov 13:5ceeba86bbe4 489 WriteData( ( r << 8 ) | ( g >> 5 ) | ( g << 3 ) | b );
ttodorov 13:5ceeba86bbe4 490 }
ttodorov 13:5ceeba86bbe4 491 else if ( _colorDepth == RGB18 )
ttodorov 13:5ceeba86bbe4 492 SetPixelColor( pixel[ ( ty * img->Width ) + tx ] );
ttodorov 13:5ceeba86bbe4 493 }
ttodorov 13:5ceeba86bbe4 494 }
ttodorov 4:3ac4239f6c9c 495 Deactivate();
ttodorov 0:881ff0b71102 496 }
ttodorov 4:3ac4239f6c9c 497 }
ttodorov 4:3ac4239f6c9c 498
ttodorov 4:3ac4239f6c9c 499 inline
ttodorov 4:3ac4239f6c9c 500 void LCD::Activate( void )
ttodorov 4:3ac4239f6c9c 501 {
ttodorov 4:3ac4239f6c9c 502 _lcd_pin_cs = LOW;
ttodorov 4:3ac4239f6c9c 503 }
ttodorov 4:3ac4239f6c9c 504
ttodorov 4:3ac4239f6c9c 505 inline
ttodorov 4:3ac4239f6c9c 506 void LCD::Deactivate( void )
ttodorov 4:3ac4239f6c9c 507 {
ttodorov 4:3ac4239f6c9c 508 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 509 }
ttodorov 0:881ff0b71102 510
ttodorov 0:881ff0b71102 511 inline
ttodorov 2:81ed304b7e9b 512 void LCD::WriteCmdData( unsigned short cmd, unsigned short data )
ttodorov 0:881ff0b71102 513 {
ttodorov 2:81ed304b7e9b 514 WriteCmd( cmd );
ttodorov 2:81ed304b7e9b 515 WriteData( data );
ttodorov 0:881ff0b71102 516 }
ttodorov 0:881ff0b71102 517
ttodorov 12:d0978272a340 518 inline
ttodorov 12:d0978272a340 519 void LCD::ClearXY( void )
ttodorov 0:881ff0b71102 520 {
ttodorov 12:d0978272a340 521 SetXY( 0, 0, GetWidth() - 1, GetHeight() - 1 );
ttodorov 0:881ff0b71102 522 }
ttodorov 0:881ff0b71102 523
ttodorov 2:81ed304b7e9b 524 void LCD::DrawHLine( unsigned short x, unsigned short y, unsigned short len, int color )
ttodorov 0:881ff0b71102 525 {
ttodorov 12:d0978272a340 526 unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 0:881ff0b71102 527
ttodorov 4:3ac4239f6c9c 528 Activate();
ttodorov 2:81ed304b7e9b 529 SetXY( x, y, x + len, y );
ttodorov 0:881ff0b71102 530 for ( int i = 0; i < len + 1; i++ )
ttodorov 10:69571adcfad5 531 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 532 Deactivate();
ttodorov 0:881ff0b71102 533 }
ttodorov 0:881ff0b71102 534
ttodorov 2:81ed304b7e9b 535 void LCD::DrawVLine( unsigned short x, unsigned short y, unsigned short len, int color )
ttodorov 0:881ff0b71102 536 {
ttodorov 12:d0978272a340 537 unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
ttodorov 0:881ff0b71102 538
ttodorov 4:3ac4239f6c9c 539 Activate();
ttodorov 2:81ed304b7e9b 540 SetXY( x, y, x, y + len );
ttodorov 0:881ff0b71102 541 for ( int i = 0; i < len; i++ )
ttodorov 10:69571adcfad5 542 SetPixelColor( usedColor );
ttodorov 4:3ac4239f6c9c 543 Deactivate();
ttodorov 0:881ff0b71102 544 }
ttodorov 0:881ff0b71102 545
ttodorov 2:81ed304b7e9b 546 void LCD::PrintChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
ttodorov 0:881ff0b71102 547 {
ttodorov 0:881ff0b71102 548 uint8_t i, ch;
ttodorov 0:881ff0b71102 549 uint16_t j;
ttodorov 0:881ff0b71102 550 uint16_t temp;
ttodorov 12:d0978272a340 551 unsigned int usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned int ) fgColor;
ttodorov 12:d0978272a340 552 unsigned int usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned int ) bgColor;
ttodorov 0:881ff0b71102 553
ttodorov 4:3ac4239f6c9c 554 Activate();
ttodorov 0:881ff0b71102 555
ttodorov 12:d0978272a340 556 SetXY( x, y, x + _font.width - 1, y + _font.height - 1 );
ttodorov 12:d0978272a340 557
ttodorov 12:d0978272a340 558 temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
ttodorov 12:d0978272a340 559 for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j++ )
ttodorov 0:881ff0b71102 560 {
ttodorov 12:d0978272a340 561 ch = _font.font[ temp ];
ttodorov 12:d0978272a340 562 for ( i = 0; i < 8; i++ )
ttodorov 0:881ff0b71102 563 {
ttodorov 12:d0978272a340 564 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
ttodorov 12:d0978272a340 565 SetPixelColor( usedColorFG );
ttodorov 12:d0978272a340 566 else
ttodorov 12:d0978272a340 567 SetPixelColor( usedColorBG );
ttodorov 0:881ff0b71102 568 }
ttodorov 12:d0978272a340 569 temp++;
ttodorov 0:881ff0b71102 570 }
ttodorov 4:3ac4239f6c9c 571 Deactivate();
ttodorov 0:881ff0b71102 572 }
ttodorov 0:881ff0b71102 573
ttodorov 2:81ed304b7e9b 574 void LCD::RotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor, int bgColor, unsigned short deg )
ttodorov 0:881ff0b71102 575 {
ttodorov 0:881ff0b71102 576 uint8_t i, j, ch;
ttodorov 0:881ff0b71102 577 uint16_t temp;
ttodorov 0:881ff0b71102 578 int newx, newy;
ttodorov 0:881ff0b71102 579 double radian;
ttodorov 0:881ff0b71102 580 radian = deg * 0.0175;
ttodorov 0:881ff0b71102 581
ttodorov 12:d0978272a340 582 unsigned int usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned int ) fgColor;
ttodorov 12:d0978272a340 583 unsigned int usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned int ) bgColor;
ttodorov 0:881ff0b71102 584
ttodorov 4:3ac4239f6c9c 585 Activate();
ttodorov 0:881ff0b71102 586
ttodorov 0:881ff0b71102 587 temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
ttodorov 0:881ff0b71102 588 for ( j = 0; j < _font.height; j++ )
ttodorov 0:881ff0b71102 589 {
ttodorov 0:881ff0b71102 590 for ( int zz = 0; zz < ( _font.width / 8 ); zz++ )
ttodorov 0:881ff0b71102 591 {
ttodorov 0:881ff0b71102 592 ch = _font.font[ temp + zz ];
ttodorov 0:881ff0b71102 593 for ( i = 0; i < 8; i++ )
ttodorov 0:881ff0b71102 594 {
ttodorov 0:881ff0b71102 595 newx = x + ( ( ( i + ( zz * 8 ) + ( pos * _font.width ) ) * cos( radian ) ) - ( ( j ) * sin( radian ) ) );
ttodorov 0:881ff0b71102 596 newy = y + ( ( ( j ) * cos( radian ) ) + ( ( i + ( zz * 8 ) + ( pos * _font.width ) ) * sin( radian ) ) );
ttodorov 0:881ff0b71102 597
ttodorov 2:81ed304b7e9b 598 SetXY( newx, newy, newx + 1, newy + 1 );
ttodorov 0:881ff0b71102 599
ttodorov 0:881ff0b71102 600 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
ttodorov 10:69571adcfad5 601 SetPixelColor( usedColorFG );
ttodorov 0:881ff0b71102 602 else
ttodorov 10:69571adcfad5 603 SetPixelColor( usedColorBG );
ttodorov 0:881ff0b71102 604 }
ttodorov 0:881ff0b71102 605 }
ttodorov 0:881ff0b71102 606 temp += ( _font.width / 8 );
ttodorov 0:881ff0b71102 607 }
ttodorov 4:3ac4239f6c9c 608 Deactivate();
ttodorov 0:881ff0b71102 609 }