1.44 tft lcd display

Dependencies:   TFT_fonts mbed-os

Fork of newTFTLCD by madhu sudhana

Committer:
ttodorov
Date:
Sat Dec 01 19:38:53 2012 +0000
Revision:
0:881ff0b71102
Child:
1:14bef43daf6f
- first version of a standardized TFT library, which is to provide support for multiple LCD controllers and different data buses.

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 0:881ff0b71102 23
ttodorov 0:881ff0b71102 24 #ifndef ushort
ttodorov 0:881ff0b71102 25 typedef unsigned short ushort;
ttodorov 0:881ff0b71102 26 #endif
ttodorov 0:881ff0b71102 27
ttodorov 0:881ff0b71102 28 LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS )
ttodorov 0:881ff0b71102 29 : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS )
ttodorov 0:881ff0b71102 30 {}
ttodorov 0:881ff0b71102 31
ttodorov 0:881ff0b71102 32 inline
ttodorov 0:881ff0b71102 33 void LCD::SetForeground( unsigned short color )
ttodorov 0:881ff0b71102 34 {
ttodorov 0:881ff0b71102 35 _foreground = color;
ttodorov 0:881ff0b71102 36 }
ttodorov 0:881ff0b71102 37
ttodorov 0:881ff0b71102 38 inline
ttodorov 0:881ff0b71102 39 void LCD::SetBackground( unsigned short color )
ttodorov 0:881ff0b71102 40 {
ttodorov 0:881ff0b71102 41 _background = color;
ttodorov 0:881ff0b71102 42 }
ttodorov 0:881ff0b71102 43
ttodorov 0:881ff0b71102 44 void LCD::SetFont( const char *font )
ttodorov 0:881ff0b71102 45 {
ttodorov 0:881ff0b71102 46 _font.font = font;
ttodorov 0:881ff0b71102 47 _font.width = font[ 0 ];
ttodorov 0:881ff0b71102 48 _font.height = font[ 1 ];
ttodorov 0:881ff0b71102 49 _font.offset = font[ 2 ];
ttodorov 0:881ff0b71102 50 _font.numchars = font[ 3 ];
ttodorov 0:881ff0b71102 51 }
ttodorov 0:881ff0b71102 52
ttodorov 0:881ff0b71102 53 inline
ttodorov 0:881ff0b71102 54 unsigned short LCD::GetWidth( void )
ttodorov 0:881ff0b71102 55 {
ttodorov 0:881ff0b71102 56 if ( _orientation == LANDSCAPE ) return _disp_height;
ttodorov 0:881ff0b71102 57 return _disp_width;
ttodorov 0:881ff0b71102 58 }
ttodorov 0:881ff0b71102 59
ttodorov 0:881ff0b71102 60 inline
ttodorov 0:881ff0b71102 61 unsigned short LCD::GetHeight( void )
ttodorov 0:881ff0b71102 62 {
ttodorov 0:881ff0b71102 63 if ( _orientation == LANDSCAPE ) return _disp_width;
ttodorov 0:881ff0b71102 64 return _disp_height;
ttodorov 0:881ff0b71102 65 }
ttodorov 0:881ff0b71102 66
ttodorov 0:881ff0b71102 67 void LCD::FillScreen( int color )
ttodorov 0:881ff0b71102 68 {
ttodorov 0:881ff0b71102 69 unsigned short rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 70 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 71 clearXY();
ttodorov 0:881ff0b71102 72 _lcd_pin_rs = HIGH;
ttodorov 0:881ff0b71102 73 for ( int i = 0; i < ( ( _disp_width + 1 ) * ( _disp_height + 1 ) ); i++ )
ttodorov 0:881ff0b71102 74 writeData( rgb );
ttodorov 0:881ff0b71102 75 _lcd_pin_cs = HIGH;
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 0:881ff0b71102 86 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 87 setXY( x, y, x, y );
ttodorov 0:881ff0b71102 88 writeData( color == -1 ? _background :
ttodorov 0:881ff0b71102 89 color == -2 ? _foreground : color );
ttodorov 0:881ff0b71102 90 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 91 clearXY();
ttodorov 0:881ff0b71102 92 }
ttodorov 0:881ff0b71102 93
ttodorov 0:881ff0b71102 94 void LCD::DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 95 {
ttodorov 0:881ff0b71102 96
ttodorov 0:881ff0b71102 97 double delta, tx, ty;
ttodorov 0:881ff0b71102 98
ttodorov 0:881ff0b71102 99 if ( ( ( x2 - x1 ) < 0 ) )
ttodorov 0:881ff0b71102 100 {
ttodorov 0:881ff0b71102 101 swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 102 swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 103 }
ttodorov 0:881ff0b71102 104 if ( ( ( y2 - y1 ) < 0 ) )
ttodorov 0:881ff0b71102 105 {
ttodorov 0:881ff0b71102 106 swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 107 swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 108 }
ttodorov 0:881ff0b71102 109
ttodorov 0:881ff0b71102 110 if ( y1 == y2 )
ttodorov 0:881ff0b71102 111 {
ttodorov 0:881ff0b71102 112 if ( x1 > x2 )
ttodorov 0:881ff0b71102 113 swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 114 drawHLine( x1, y1, x2 - x1, color );
ttodorov 0:881ff0b71102 115 }
ttodorov 0:881ff0b71102 116 else if ( x1 == x2 )
ttodorov 0:881ff0b71102 117 {
ttodorov 0:881ff0b71102 118 if ( y1 > y2 )
ttodorov 0:881ff0b71102 119 swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 120 drawVLine( x1, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 121 }
ttodorov 0:881ff0b71102 122 else if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
ttodorov 0:881ff0b71102 123 {
ttodorov 0:881ff0b71102 124 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 125 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 126 delta = ( double( y2 - y1 ) / double( x2 - x1 ) );
ttodorov 0:881ff0b71102 127 ty = double( y1 );
ttodorov 0:881ff0b71102 128 if ( x1 > x2 )
ttodorov 0:881ff0b71102 129 {
ttodorov 0:881ff0b71102 130 for ( int i = x1; i >= x2; i-- )
ttodorov 0:881ff0b71102 131 {
ttodorov 0:881ff0b71102 132 setXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
ttodorov 0:881ff0b71102 133 writeData( usedColor );
ttodorov 0:881ff0b71102 134 ty = ty - delta;
ttodorov 0:881ff0b71102 135 }
ttodorov 0:881ff0b71102 136 }
ttodorov 0:881ff0b71102 137 else
ttodorov 0:881ff0b71102 138 {
ttodorov 0:881ff0b71102 139 for ( int i = x1; i <= x2; i++ )
ttodorov 0:881ff0b71102 140 {
ttodorov 0:881ff0b71102 141 setXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
ttodorov 0:881ff0b71102 142 writeData( usedColor );
ttodorov 0:881ff0b71102 143 ty = ty + delta;
ttodorov 0:881ff0b71102 144 }
ttodorov 0:881ff0b71102 145 }
ttodorov 0:881ff0b71102 146 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 147 }
ttodorov 0:881ff0b71102 148 else
ttodorov 0:881ff0b71102 149 {
ttodorov 0:881ff0b71102 150 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 151 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 152 delta = ( float( x2 - x1 ) / float( y2 - y1 ) );
ttodorov 0:881ff0b71102 153 tx = float( x1 );
ttodorov 0:881ff0b71102 154 if ( y1 > y2 )
ttodorov 0:881ff0b71102 155 {
ttodorov 0:881ff0b71102 156 for ( int i = y2 + 1; i > y1; i-- )
ttodorov 0:881ff0b71102 157 {
ttodorov 0:881ff0b71102 158 setXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
ttodorov 0:881ff0b71102 159 writeData( usedColor );
ttodorov 0:881ff0b71102 160 tx = tx + delta;
ttodorov 0:881ff0b71102 161 }
ttodorov 0:881ff0b71102 162 }
ttodorov 0:881ff0b71102 163 else
ttodorov 0:881ff0b71102 164 {
ttodorov 0:881ff0b71102 165 for ( int i = y1; i < y2 + 1; i++ )
ttodorov 0:881ff0b71102 166 {
ttodorov 0:881ff0b71102 167 setXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
ttodorov 0:881ff0b71102 168 writeData( usedColor );
ttodorov 0:881ff0b71102 169 tx = tx + delta;
ttodorov 0:881ff0b71102 170 }
ttodorov 0:881ff0b71102 171 }
ttodorov 0:881ff0b71102 172 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 173 }
ttodorov 0:881ff0b71102 174
ttodorov 0:881ff0b71102 175 clearXY();
ttodorov 0:881ff0b71102 176 }
ttodorov 0:881ff0b71102 177
ttodorov 0:881ff0b71102 178 void LCD::DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 179 {
ttodorov 0:881ff0b71102 180 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 181 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 182
ttodorov 0:881ff0b71102 183 drawHLine( x1, y1, x2 - x1, color );
ttodorov 0:881ff0b71102 184 drawHLine( x1, y2, x2 - x1, color );
ttodorov 0:881ff0b71102 185 drawVLine( x1, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 186 drawVLine( x2, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 187 }
ttodorov 0:881ff0b71102 188
ttodorov 0:881ff0b71102 189 void LCD::DrawRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 190 {
ttodorov 0:881ff0b71102 191 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 192 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 193
ttodorov 0:881ff0b71102 194 if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
ttodorov 0:881ff0b71102 195 {
ttodorov 0:881ff0b71102 196 DrawPixel( x1 + 1, y1 + 1, color );
ttodorov 0:881ff0b71102 197 DrawPixel( x2 - 1, y1 + 1, color );
ttodorov 0:881ff0b71102 198 DrawPixel( x1 + 1, y2 - 1, color );
ttodorov 0:881ff0b71102 199 DrawPixel( x2 - 1, y2 - 1, color );
ttodorov 0:881ff0b71102 200 drawHLine( x1 + 2, y1, x2 - x1 - 4, color );
ttodorov 0:881ff0b71102 201 drawHLine( x1 + 2, y2, x2 - x1 - 4, color );
ttodorov 0:881ff0b71102 202 drawVLine( x1, y1 + 2, y2 - y1 - 4, color );
ttodorov 0:881ff0b71102 203 drawVLine( x2, y1 + 2, y2 - y1 - 4, color );
ttodorov 0:881ff0b71102 204 }
ttodorov 0:881ff0b71102 205 }
ttodorov 0:881ff0b71102 206
ttodorov 0:881ff0b71102 207 void LCD::FillRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 208 {
ttodorov 0:881ff0b71102 209 if ( x1 > x2 ) swap( ushort, x1, x2 );
ttodorov 0:881ff0b71102 210 if ( y1 > y2 ) swap( ushort, y1, y2 );
ttodorov 0:881ff0b71102 211
ttodorov 0:881ff0b71102 212 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 213 {
ttodorov 0:881ff0b71102 214 for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
ttodorov 0:881ff0b71102 215 {
ttodorov 0:881ff0b71102 216 drawHLine( x1, y1 + i, x2 - x1, color );
ttodorov 0:881ff0b71102 217 drawHLine( x1, y2 - i, x2 - x1, color );
ttodorov 0:881ff0b71102 218 }
ttodorov 0:881ff0b71102 219 }
ttodorov 0:881ff0b71102 220 else
ttodorov 0:881ff0b71102 221 {
ttodorov 0:881ff0b71102 222 for ( int i = 0; i < ( ( x2 - x1 ) / 2 ) + 1; i++ )
ttodorov 0:881ff0b71102 223 {
ttodorov 0:881ff0b71102 224 drawVLine( x1 + i, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 225 drawVLine( x2 - i, y1, y2 - y1, color );
ttodorov 0:881ff0b71102 226 }
ttodorov 0:881ff0b71102 227 }
ttodorov 0:881ff0b71102 228 }
ttodorov 0:881ff0b71102 229
ttodorov 0:881ff0b71102 230 void LCD::FillRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
ttodorov 0:881ff0b71102 231 {
ttodorov 0:881ff0b71102 232 if ( x1 > x2 ) swap( ushort, x1, x2 )
ttodorov 0:881ff0b71102 233 if ( y1 > y2 ) swap( ushort, y1, y2 )
ttodorov 0:881ff0b71102 234
ttodorov 0:881ff0b71102 235 if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
ttodorov 0:881ff0b71102 236 {
ttodorov 0:881ff0b71102 237 for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
ttodorov 0:881ff0b71102 238 {
ttodorov 0:881ff0b71102 239 switch ( i )
ttodorov 0:881ff0b71102 240 {
ttodorov 0:881ff0b71102 241 case 0:
ttodorov 0:881ff0b71102 242 drawHLine( x1 + 2, y1 + i, x2 - x1 - 4, color );
ttodorov 0:881ff0b71102 243 drawHLine( x1 + 2, y2 - i, x2 - x1 - 4, color );
ttodorov 0:881ff0b71102 244 break;
ttodorov 0:881ff0b71102 245
ttodorov 0:881ff0b71102 246 case 1:
ttodorov 0:881ff0b71102 247 drawHLine( x1 + 1, y1 + i, x2 - x1 - 2, color );
ttodorov 0:881ff0b71102 248 drawHLine( x1 + 1, y2 - i, x2 - x1 - 2, color );
ttodorov 0:881ff0b71102 249 break;
ttodorov 0:881ff0b71102 250
ttodorov 0:881ff0b71102 251 default:
ttodorov 0:881ff0b71102 252 drawHLine( x1, y1 + i, x2 - x1, color );
ttodorov 0:881ff0b71102 253 drawHLine( x1, y2 - i, x2 - x1, color );
ttodorov 0:881ff0b71102 254 break;
ttodorov 0:881ff0b71102 255 }
ttodorov 0:881ff0b71102 256 }
ttodorov 0:881ff0b71102 257 }
ttodorov 0:881ff0b71102 258 }
ttodorov 0:881ff0b71102 259
ttodorov 0:881ff0b71102 260 void LCD::DrawCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
ttodorov 0:881ff0b71102 261 {
ttodorov 0:881ff0b71102 262 int f = 1 - radius;
ttodorov 0:881ff0b71102 263 int ddF_x = 1;
ttodorov 0:881ff0b71102 264 int ddF_y = -2 * radius;
ttodorov 0:881ff0b71102 265 int x1 = 0;
ttodorov 0:881ff0b71102 266 int y1 = radius;
ttodorov 0:881ff0b71102 267 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 268
ttodorov 0:881ff0b71102 269 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 270 setXY( x, y + radius, x, y + radius );
ttodorov 0:881ff0b71102 271 writeData( usedColor );
ttodorov 0:881ff0b71102 272 setXY( x, y - radius, x, y - radius );
ttodorov 0:881ff0b71102 273 writeData( usedColor );
ttodorov 0:881ff0b71102 274 setXY( x + radius, y, x + radius, y );
ttodorov 0:881ff0b71102 275 writeData( usedColor );
ttodorov 0:881ff0b71102 276 setXY( x - radius, y, x - radius, y );
ttodorov 0:881ff0b71102 277 writeData( usedColor );
ttodorov 0:881ff0b71102 278
ttodorov 0:881ff0b71102 279 while ( x1 < y1 )
ttodorov 0:881ff0b71102 280 {
ttodorov 0:881ff0b71102 281 if ( f >= 0 )
ttodorov 0:881ff0b71102 282 {
ttodorov 0:881ff0b71102 283 y1--;
ttodorov 0:881ff0b71102 284 ddF_y += 2;
ttodorov 0:881ff0b71102 285 f += ddF_y;
ttodorov 0:881ff0b71102 286 }
ttodorov 0:881ff0b71102 287 x1++;
ttodorov 0:881ff0b71102 288 ddF_x += 2;
ttodorov 0:881ff0b71102 289 f += ddF_x;
ttodorov 0:881ff0b71102 290 setXY( x + x1, y + y1, x + x1, y + y1 );
ttodorov 0:881ff0b71102 291 writeData( usedColor );
ttodorov 0:881ff0b71102 292 setXY( x - x1, y + y1, x - x1, y + y1 );
ttodorov 0:881ff0b71102 293 writeData( usedColor );
ttodorov 0:881ff0b71102 294 setXY( x + x1, y - y1, x + x1, y - y1 );
ttodorov 0:881ff0b71102 295 writeData( usedColor );
ttodorov 0:881ff0b71102 296 setXY( x - x1, y - y1, x - x1, y - y1 );
ttodorov 0:881ff0b71102 297 writeData( usedColor );
ttodorov 0:881ff0b71102 298 setXY( x + y1, y + x1, x + y1, y + x1 );
ttodorov 0:881ff0b71102 299 writeData( usedColor );
ttodorov 0:881ff0b71102 300 setXY( x - y1, y + x1, x - y1, y + x1 );
ttodorov 0:881ff0b71102 301 writeData( usedColor );
ttodorov 0:881ff0b71102 302 setXY( x + y1, y - x1, x + y1, y - x1 );
ttodorov 0:881ff0b71102 303 writeData( usedColor );
ttodorov 0:881ff0b71102 304 setXY( x - y1, y - x1, x - y1, y - x1 );
ttodorov 0:881ff0b71102 305 writeData( usedColor );
ttodorov 0:881ff0b71102 306 }
ttodorov 0:881ff0b71102 307 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 308 clearXY();
ttodorov 0:881ff0b71102 309 }
ttodorov 0:881ff0b71102 310
ttodorov 0:881ff0b71102 311 void LCD::FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
ttodorov 0:881ff0b71102 312 {
ttodorov 0:881ff0b71102 313 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 314 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 315 for ( int y1 = -radius; y1 <= radius; y1++ )
ttodorov 0:881ff0b71102 316 for ( int x1 = -radius; x1 <= radius; x1++ )
ttodorov 0:881ff0b71102 317 if ( x1 * x1 + y1 * y1 <= radius * radius )
ttodorov 0:881ff0b71102 318 {
ttodorov 0:881ff0b71102 319 setXY( x + x1, y + y1, x + x1, y + y1 );
ttodorov 0:881ff0b71102 320 writeData( usedColor );
ttodorov 0:881ff0b71102 321 }
ttodorov 0:881ff0b71102 322 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 323 clearXY();
ttodorov 0:881ff0b71102 324 }
ttodorov 0:881ff0b71102 325
ttodorov 0:881ff0b71102 326 void LCD::Print( const char *str, unsigned short x, unsigned short y, int fgColor, int bgColor, unsigned short deg )
ttodorov 0:881ff0b71102 327 {
ttodorov 0:881ff0b71102 328 int stl, i;
ttodorov 0:881ff0b71102 329
ttodorov 0:881ff0b71102 330 stl = strlen( str );
ttodorov 0:881ff0b71102 331
ttodorov 0:881ff0b71102 332 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 333 {
ttodorov 0:881ff0b71102 334 if ( x == RIGHT )
ttodorov 0:881ff0b71102 335 x = ( _disp_width + 1 ) - ( stl * _font.width );
ttodorov 0:881ff0b71102 336 if ( x == CENTER )
ttodorov 0:881ff0b71102 337 x = ( ( _disp_width + 1 ) - ( stl * _font.width ) ) / 2;
ttodorov 0:881ff0b71102 338 }
ttodorov 0:881ff0b71102 339 else
ttodorov 0:881ff0b71102 340 {
ttodorov 0:881ff0b71102 341 if ( x == RIGHT )
ttodorov 0:881ff0b71102 342 x = ( _disp_height + 1 ) - ( stl * _font.width );
ttodorov 0:881ff0b71102 343 if ( x == CENTER )
ttodorov 0:881ff0b71102 344 x = ( ( _disp_height + 1 ) - ( stl * _font.width ) ) / 2;
ttodorov 0:881ff0b71102 345 }
ttodorov 0:881ff0b71102 346
ttodorov 0:881ff0b71102 347 for ( i = 0; i < stl; i++ )
ttodorov 0:881ff0b71102 348 if ( deg == 0 )
ttodorov 0:881ff0b71102 349 printChar( *str++, x + ( i * ( _font.width ) ), y, fgColor, bgColor );
ttodorov 0:881ff0b71102 350 else
ttodorov 0:881ff0b71102 351 rotateChar( *str++, x, y, i, fgColor, bgColor, deg );
ttodorov 0:881ff0b71102 352 }
ttodorov 0:881ff0b71102 353
ttodorov 0:881ff0b71102 354 void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, bitmap_t data, unsigned char scale )
ttodorov 0:881ff0b71102 355 {
ttodorov 0:881ff0b71102 356 int tx, ty, tc, tsx, tsy;
ttodorov 0:881ff0b71102 357
ttodorov 0:881ff0b71102 358 if ( scale == 1 )
ttodorov 0:881ff0b71102 359 {
ttodorov 0:881ff0b71102 360 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 361 {
ttodorov 0:881ff0b71102 362 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 363 setXY( x, y, x + sx - 1, y + sy - 1 );
ttodorov 0:881ff0b71102 364 for ( tc = 0; tc < ( sx * sy ); tc++ )
ttodorov 0:881ff0b71102 365 writeData( data[ tc ] );
ttodorov 0:881ff0b71102 366 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 367 }
ttodorov 0:881ff0b71102 368 else
ttodorov 0:881ff0b71102 369 {
ttodorov 0:881ff0b71102 370 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 371 for ( ty = 0; ty < sy; ty++ )
ttodorov 0:881ff0b71102 372 {
ttodorov 0:881ff0b71102 373 setXY( x, y + ty, x + sx - 1, y + ty );
ttodorov 0:881ff0b71102 374 for ( tx = sx; tx >= 0; tx-- )
ttodorov 0:881ff0b71102 375 writeData( data[ ( ty * sx ) + tx ] );
ttodorov 0:881ff0b71102 376 }
ttodorov 0:881ff0b71102 377 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 378 }
ttodorov 0:881ff0b71102 379 }
ttodorov 0:881ff0b71102 380 else
ttodorov 0:881ff0b71102 381 {
ttodorov 0:881ff0b71102 382 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 383 {
ttodorov 0:881ff0b71102 384 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 385 for ( ty = 0; ty < sy; ty++ )
ttodorov 0:881ff0b71102 386 {
ttodorov 0:881ff0b71102 387 setXY( x, y + ( ty * scale ), x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + scale );
ttodorov 0:881ff0b71102 388 for ( tsy = 0; tsy < scale; tsy++ )
ttodorov 0:881ff0b71102 389 for ( tx = 0; tx < sx; tx++ )
ttodorov 0:881ff0b71102 390 for ( tsx = 0; tsx < scale; tsx++ )
ttodorov 0:881ff0b71102 391 writeData( data[ ( ty * sx ) + tx ] );
ttodorov 0:881ff0b71102 392 }
ttodorov 0:881ff0b71102 393 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 394 }
ttodorov 0:881ff0b71102 395 else
ttodorov 0:881ff0b71102 396 {
ttodorov 0:881ff0b71102 397 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 398 for ( ty = 0; ty < sy; ty++ )
ttodorov 0:881ff0b71102 399 {
ttodorov 0:881ff0b71102 400 for ( tsy = 0; tsy < scale; tsy++ )
ttodorov 0:881ff0b71102 401 {
ttodorov 0:881ff0b71102 402 setXY( x, y + ( ty * scale ) + tsy, x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + tsy );
ttodorov 0:881ff0b71102 403 for ( tx = sx; tx >= 0; tx-- )
ttodorov 0:881ff0b71102 404 for ( tsx = 0; tsx < scale; tsx++ )
ttodorov 0:881ff0b71102 405 writeData( data[ ( ty * sx ) + tx ] );
ttodorov 0:881ff0b71102 406 }
ttodorov 0:881ff0b71102 407 }
ttodorov 0:881ff0b71102 408 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 409 }
ttodorov 0:881ff0b71102 410 }
ttodorov 0:881ff0b71102 411 clearXY();
ttodorov 0:881ff0b71102 412 }
ttodorov 0:881ff0b71102 413
ttodorov 0:881ff0b71102 414 void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, bitmap_t data, unsigned short deg, unsigned short rox, unsigned short roy )
ttodorov 0:881ff0b71102 415 {
ttodorov 0:881ff0b71102 416 int tx, ty, newx, newy;
ttodorov 0:881ff0b71102 417 double radian;
ttodorov 0:881ff0b71102 418 radian = deg * 0.0175;
ttodorov 0:881ff0b71102 419
ttodorov 0:881ff0b71102 420 if ( deg == 0 )
ttodorov 0:881ff0b71102 421 DrawBitmap( x, y, sx, sy, data );
ttodorov 0:881ff0b71102 422 else
ttodorov 0:881ff0b71102 423 {
ttodorov 0:881ff0b71102 424 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 425 for ( ty = 0; ty < sy; ty++ )
ttodorov 0:881ff0b71102 426 for ( tx = 0; tx < sx; tx++ )
ttodorov 0:881ff0b71102 427 {
ttodorov 0:881ff0b71102 428 newx = x + rox + ( ( ( tx - rox ) * cos( radian ) ) - ( ( ty - roy ) * sin( radian ) ) );
ttodorov 0:881ff0b71102 429 newy = y + roy + ( ( ( ty - roy ) * cos( radian ) ) + ( ( tx - rox ) * sin( radian ) ) );
ttodorov 0:881ff0b71102 430
ttodorov 0:881ff0b71102 431 setXY( newx, newy, newx, newy );
ttodorov 0:881ff0b71102 432 writeData( data[ ( ty * sx ) + tx ] );
ttodorov 0:881ff0b71102 433 }
ttodorov 0:881ff0b71102 434 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 435 }
ttodorov 0:881ff0b71102 436 clearXY();
ttodorov 0:881ff0b71102 437 }
ttodorov 0:881ff0b71102 438
ttodorov 0:881ff0b71102 439 /*
ttodorov 0:881ff0b71102 440 void LCD::writeCmd( unsigned short cmd )
ttodorov 0:881ff0b71102 441 {
ttodorov 0:881ff0b71102 442 _lcd_pin_rs = LOW;
ttodorov 0:881ff0b71102 443 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 444 _lcd_port->write( cmd );
ttodorov 0:881ff0b71102 445 pulseLow( _lcd_pin_wr );
ttodorov 0:881ff0b71102 446 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 447 }
ttodorov 0:881ff0b71102 448 */
ttodorov 0:881ff0b71102 449
ttodorov 0:881ff0b71102 450 /*
ttodorov 0:881ff0b71102 451 void LCD::writeData( unsigned short data )
ttodorov 0:881ff0b71102 452 {
ttodorov 0:881ff0b71102 453 _lcd_pin_rs = HIGH;
ttodorov 0:881ff0b71102 454 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 455 _lcd_port->write( data );
ttodorov 0:881ff0b71102 456 pulseLow( _lcd_pin_wr );
ttodorov 0:881ff0b71102 457 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 458 }
ttodorov 0:881ff0b71102 459 */
ttodorov 0:881ff0b71102 460
ttodorov 0:881ff0b71102 461 inline
ttodorov 0:881ff0b71102 462 void LCD::writeCmdData( unsigned short cmd, unsigned short data )
ttodorov 0:881ff0b71102 463 {
ttodorov 0:881ff0b71102 464 writeCmd( cmd );
ttodorov 0:881ff0b71102 465 writeData( data );
ttodorov 0:881ff0b71102 466 }
ttodorov 0:881ff0b71102 467
ttodorov 0:881ff0b71102 468 /*
ttodorov 0:881ff0b71102 469 void LCD::setXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
ttodorov 0:881ff0b71102 470 {
ttodorov 0:881ff0b71102 471 if ( _orientation == LANDSCAPE )
ttodorov 0:881ff0b71102 472 {
ttodorov 0:881ff0b71102 473 swap( uint16_t, x1, y1 )
ttodorov 0:881ff0b71102 474 swap( uint16_t, x2, y2 )
ttodorov 0:881ff0b71102 475 y1 = _disp_height - y1;
ttodorov 0:881ff0b71102 476 y2 = _disp_height - y2;
ttodorov 0:881ff0b71102 477 swap( uint16_t, y1, y2 )
ttodorov 0:881ff0b71102 478 }
ttodorov 0:881ff0b71102 479
ttodorov 0:881ff0b71102 480 writeCmdData( 0x44, ( x2 << 8 ) + x1 );
ttodorov 0:881ff0b71102 481 writeCmdData( 0x45, y1 );
ttodorov 0:881ff0b71102 482 writeCmdData( 0x46, y2 );
ttodorov 0:881ff0b71102 483 writeCmdData( 0x4e, x1 );
ttodorov 0:881ff0b71102 484 writeCmdData( 0x4f, y1 );
ttodorov 0:881ff0b71102 485 writeCmd( 0x22 );
ttodorov 0:881ff0b71102 486 }
ttodorov 0:881ff0b71102 487 */
ttodorov 0:881ff0b71102 488
ttodorov 0:881ff0b71102 489 void LCD::clearXY()
ttodorov 0:881ff0b71102 490 {
ttodorov 0:881ff0b71102 491 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 492 setXY( 0, 0, _disp_width, _disp_height );
ttodorov 0:881ff0b71102 493 else
ttodorov 0:881ff0b71102 494 setXY( 0, 0, _disp_height, _disp_width );
ttodorov 0:881ff0b71102 495 }
ttodorov 0:881ff0b71102 496
ttodorov 0:881ff0b71102 497 void LCD::drawHLine( unsigned short x, unsigned short y, unsigned short len, int color )
ttodorov 0:881ff0b71102 498 {
ttodorov 0:881ff0b71102 499 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 500
ttodorov 0:881ff0b71102 501 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 502 setXY( x, y, x + len, y );
ttodorov 0:881ff0b71102 503 for ( int i = 0; i < len + 1; i++ )
ttodorov 0:881ff0b71102 504 writeData( usedColor );
ttodorov 0:881ff0b71102 505 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 506 clearXY();
ttodorov 0:881ff0b71102 507 }
ttodorov 0:881ff0b71102 508
ttodorov 0:881ff0b71102 509 void LCD::drawVLine( unsigned short x, unsigned short y, unsigned short len, int color )
ttodorov 0:881ff0b71102 510 {
ttodorov 0:881ff0b71102 511 unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
ttodorov 0:881ff0b71102 512
ttodorov 0:881ff0b71102 513 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 514 setXY( x, y, x, y + len );
ttodorov 0:881ff0b71102 515 for ( int i = 0; i < len; i++ )
ttodorov 0:881ff0b71102 516 writeData( usedColor );
ttodorov 0:881ff0b71102 517 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 518 clearXY();
ttodorov 0:881ff0b71102 519 }
ttodorov 0:881ff0b71102 520
ttodorov 0:881ff0b71102 521 void LCD::printChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
ttodorov 0:881ff0b71102 522 {
ttodorov 0:881ff0b71102 523 uint8_t i, ch;
ttodorov 0:881ff0b71102 524 uint16_t j;
ttodorov 0:881ff0b71102 525 uint16_t temp;
ttodorov 0:881ff0b71102 526 unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
ttodorov 0:881ff0b71102 527 unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
ttodorov 0:881ff0b71102 528
ttodorov 0:881ff0b71102 529 _lcd_pin_cs = LOW;
ttodorov 0:881ff0b71102 530
ttodorov 0:881ff0b71102 531 if ( _orientation == PORTRAIT )
ttodorov 0:881ff0b71102 532 {
ttodorov 0:881ff0b71102 533 setXY( x, y, x + _font.width - 1, y + _font.height - 1 );
ttodorov 0:881ff0b71102 534
ttodorov 0:881ff0b71102 535 temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
ttodorov 0:881ff0b71102 536 for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j++ )
ttodorov 0:881ff0b71102 537 {
ttodorov 0:881ff0b71102 538 ch = _font.font[ temp ];
ttodorov 0:881ff0b71102 539 for ( i = 0; i < 8; i++ )
ttodorov 0:881ff0b71102 540 {
ttodorov 0:881ff0b71102 541 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
ttodorov 0:881ff0b71102 542 writeData( usedColorFG );
ttodorov 0:881ff0b71102 543 else
ttodorov 0:881ff0b71102 544 writeData( usedColorBG );
ttodorov 0:881ff0b71102 545 }
ttodorov 0:881ff0b71102 546 temp++;
ttodorov 0:881ff0b71102 547 }
ttodorov 0:881ff0b71102 548 }
ttodorov 0:881ff0b71102 549 else
ttodorov 0:881ff0b71102 550 {
ttodorov 0:881ff0b71102 551 temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
ttodorov 0:881ff0b71102 552
ttodorov 0:881ff0b71102 553 for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j += ( _font.width / 8 ) )
ttodorov 0:881ff0b71102 554 {
ttodorov 0:881ff0b71102 555 setXY( x, y + ( j / ( _font.width / 8 ) ), x + _font.width - 1, y + ( j / ( _font.width / 8 ) ) );
ttodorov 0:881ff0b71102 556 for ( int zz = ( _font.width / 8 ) - 1; zz >= 0; zz-- )
ttodorov 0:881ff0b71102 557 {
ttodorov 0:881ff0b71102 558 ch = _font.font[ temp + zz ];
ttodorov 0:881ff0b71102 559 for ( i = 0; i < 8; i++ )
ttodorov 0:881ff0b71102 560 {
ttodorov 0:881ff0b71102 561 if ( ( ch & ( 1 << i ) ) != 0 )
ttodorov 0:881ff0b71102 562 writeData( usedColorFG );
ttodorov 0:881ff0b71102 563 else
ttodorov 0:881ff0b71102 564 writeData( usedColorBG );
ttodorov 0:881ff0b71102 565 }
ttodorov 0:881ff0b71102 566 }
ttodorov 0:881ff0b71102 567 temp += ( _font.width / 8 );
ttodorov 0:881ff0b71102 568 }
ttodorov 0:881ff0b71102 569 }
ttodorov 0:881ff0b71102 570 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 571 clearXY();
ttodorov 0:881ff0b71102 572 }
ttodorov 0:881ff0b71102 573
ttodorov 0:881ff0b71102 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 0:881ff0b71102 582 unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
ttodorov 0:881ff0b71102 583 unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
ttodorov 0:881ff0b71102 584
ttodorov 0:881ff0b71102 585 _lcd_pin_cs = LOW;
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 0:881ff0b71102 598 setXY( newx, newy, newx + 1, newy + 1 );
ttodorov 0:881ff0b71102 599
ttodorov 0:881ff0b71102 600 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
ttodorov 0:881ff0b71102 601 writeData( usedColorFG );
ttodorov 0:881ff0b71102 602 else
ttodorov 0:881ff0b71102 603 writeData( usedColorBG );
ttodorov 0:881ff0b71102 604 }
ttodorov 0:881ff0b71102 605 }
ttodorov 0:881ff0b71102 606 temp += ( _font.width / 8 );
ttodorov 0:881ff0b71102 607 }
ttodorov 0:881ff0b71102 608 _lcd_pin_cs = HIGH;
ttodorov 0:881ff0b71102 609 clearXY();
ttodorov 0:881ff0b71102 610 }