robodude666 / Mbed 2 deprecated QVGATest

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // General Drivers
00002 #include "mbed.h"
00003 
00004 #include "ClockControl.h"
00005 
00006 // QVGA LCD Drivers
00007 
00008 ////////
00009 #include "font_courier.h"
00010 
00011 // LCD Control
00012 DigitalOut lcd_rst(p13);
00013 DigitalOut lcd_bl(p14);
00014 DigitalOut lcd_rs(p15);
00015 DigitalOut lcd_cs(p16);
00016 DigitalOut lcd_rd(p17);
00017 DigitalOut lcd_wr(p18);
00018 
00019 BusInOut lcd_db(p5, p6, p7, p8, p9, p10, p11, p12);
00020 
00021 // Touch Screen Control
00022 /*
00023 #define TC_CS_PORT        &PORTB
00024 #define TC_CS_PIN        4
00025 
00026 #define TC_PEN_PORT        &PORTB
00027 #define TC_PEN_PINP        PINB
00028 #define TC_PEN_PIN        2
00029 
00030 #define MOSI_PORT        &PORTB
00031 #define MOSI_PIN        5
00032 
00033 #define SCK_PORT        &PORTB
00034 #define SCK_PIN            7
00035 */
00036 
00037 //touch screen LCD configuration
00038 //#define TS_ORN_PORTRAIT
00039 
00040 #ifdef TS_ORN_PORTRAIT
00041 #define TS_SIZE_X                    240
00042 #define TS_SIZE_Y                    320
00043 #define TS_VAL_ENTRY_MOD            0x0030
00044 #define TS_INS_GRAM_ADX                TS_INS_GRAM_HOR_AD
00045 #define TS_INS_GRAM_ADY                TS_INS_GRAM_VER_AD
00046 #define TS_INS_START_ADX               TS_INS_HOR_START_AD
00047 #define TS_INS_END_ADX               TS_INS_HOR_END_AD
00048 #define TS_INS_START_ADY               TS_INS_VER_START_AD
00049 #define TS_INS_END_ADY               TS_INS_VER_END_AD
00050 #else
00051 #define TS_SIZE_X                    320
00052 #define TS_SIZE_Y                    240
00053 #define TS_VAL_ENTRY_MOD            0x0028
00054 #define TS_INS_GRAM_ADX                TS_INS_GRAM_VER_AD
00055 #define TS_INS_GRAM_ADY                TS_INS_GRAM_HOR_AD
00056 #define TS_INS_START_ADX               TS_INS_VER_START_AD
00057 #define TS_INS_END_ADX               TS_INS_VER_END_AD
00058 #define TS_INS_START_ADY               TS_INS_HOR_START_AD
00059 #define TS_INS_END_ADY               TS_INS_HOR_END_AD
00060 #endif
00061 
00062 //pen status type definition
00063 typedef enum
00064 {
00065     PST_NOTFOUND,
00066     PST_DOWN,
00067     PST_HOLD,
00068     PST_UP
00069 } pstatus_t;
00070 
00071 //#define so_pos_t        short
00072 
00073 #ifndef so_pos_t
00074 typedef short so_pos_t;
00075 #endif
00076 
00077 //////
00078 
00079 #define TS_INS_START_OSC            0x00 //data read at this instruction should be 0x0789 --> use for test connection
00080 #define TS_INS_DRIV_OUT_CTRL        0x01
00081 #define TS_INS_DRIV_WAV_CTRL        0x02
00082 #define TS_INS_ENTRY_MOD            0x03
00083 #define TS_INS_RESIZE_CTRL            0x04
00084 #define TS_INS_DISP_CTRL1            0x07
00085 #define TS_INS_DISP_CTRL2            0x08
00086 #define TS_INS_DISP_CTRL3            0x09
00087 #define TS_INS_DISP_CTRL4            0x0A
00088 #define TS_INS_RGB_DISP_IF_CTRL1    0x0C
00089 #define TS_INS_FRM_MARKER_POS        0x0D
00090 #define TS_INS_RGB_DISP_IF_CTRL2    0x0F
00091 #define TS_INS_POW_CTRL1            0x10
00092 #define TS_INS_POW_CTRL2            0x11
00093 #define TS_INS_POW_CTRL3            0x12
00094 #define TS_INS_POW_CTRL4            0x13
00095 #define TS_INS_GRAM_HOR_AD            0x20
00096 #define TS_INS_GRAM_VER_AD            0x21
00097 #define TS_INS_RW_GRAM                0x22
00098 #define TS_INS_POW_CTRL7            0x29
00099 #define TS_INS_FRM_RATE_COL_CTRL    0x2B
00100 #define TS_INS_GAMMA_CTRL1            0x30
00101 #define TS_INS_GAMMA_CTRL2            0x31
00102 #define TS_INS_GAMMA_CTRL3            0x32
00103 #define TS_INS_GAMMA_CTRL4            0x35 
00104 #define TS_INS_GAMMA_CTRL5            0x36
00105 #define TS_INS_GAMMA_CTRL6            0x37
00106 #define TS_INS_GAMMA_CTRL7            0x38
00107 #define TS_INS_GAMMA_CTRL8            0x39
00108 #define TS_INS_GAMMA_CTRL9            0x3C
00109 #define TS_INS_GAMMA_CTRL10            0x3D
00110 #define TS_INS_HOR_START_AD            0x50
00111 #define TS_INS_HOR_END_AD            0x51
00112 #define TS_INS_VER_START_AD            0x52
00113 #define TS_INS_VER_END_AD            0x53
00114 #define TS_INS_GATE_SCAN_CTRL1        0x60
00115 #define TS_INS_GATE_SCAN_CTRL2        0x61
00116 #define TS_INS_GATE_SCAN_CTRL3        0x6A
00117 #define TS_INS_PART_IMG1_DISP_POS    0x80
00118 #define TS_INS_PART_IMG1_START_AD    0x81
00119 #define TS_INS_PART_IMG1_END_AD        0x82
00120 #define TS_INS_PART_IMG2_DISP_POS    0x83
00121 #define TS_INS_PART_IMG2_START_AD    0x84
00122 #define TS_INS_PART_IMG2_END_AD        0x85
00123 #define TS_INS_PANEL_IF_CTRL1        0x90
00124 #define TS_INS_PANEL_IF_CTRL2        0x92
00125 #define TS_INS_PANEL_IF_CTRL3        0x93
00126 #define TS_INS_PANEL_IF_CTRL4        0x95
00127 #define TS_INS_PANEL_IF_CTRL5        0x97
00128 #define TS_INS_PANEL_IF_CTRL6        0x98
00129 
00130 #define TS_COL_RED                    0x001F
00131 #define TS_COL_GREEN                0x07E0
00132 #define TS_COL_BLUE                    0xF800
00133 #define TS_COL_YELLOW                0x07FF
00134 #define TS_COL_PURPLE                0xF81F
00135 #define TS_COL_BLACK                0x0000
00136 #define TS_COL_WHITE                0xFFFF
00137 
00138 #define ts_pos_t                    int
00139 
00140 typedef enum
00141 {
00142     TS_MODE_NORMAL,
00143     TS_MODE_INVERSE,
00144     TS_MODE_FULL,
00145 } ts_mode_t;
00146 
00147 void TSLCDOutIns(unsigned short ins); //write instruction to LCD
00148 void TSLCDOutDat(unsigned short dat); //write data to LCD
00149 void TSLCDOutDat2(unsigned char dath,unsigned char datl); //write data to LCD
00150 unsigned short TSLCDInIns(void); //read data from LCD
00151 unsigned short TSLCDInDat(void); //read data from LCD
00152 
00153 void TSLCDRst(void); //pulse reset signal to LCD
00154 void TSLCDInit(void); //initial LCD
00155 void TSLCDShowPic2(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,const unsigned short *pic,ts_mode_t mode);
00156 //show picture from code memory with specific size
00157 void TSLCDFillRect(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,unsigned short color,ts_mode_t mode); //draw a rectangular
00158 void TSLCDFillCirc(ts_pos_t cx,ts_pos_t cy,ts_pos_t rad,unsigned short color, ts_mode_t mode); //draw a circle
00159 void TSLCDSetMargins(ts_pos_t xl,ts_pos_t xr,ts_pos_t yu,ts_pos_t yl); //set margins for FillRect,FillCirc
00160 void TSLCDSetMarginsDefault(void); //reset margins to default value
00161 
00162 void TSLCDSetFontColor(unsigned short color); //set text's color
00163 void TSLCDSetBackColor(unsigned short color); //set back color for TS_MODE_FULL
00164 void TSLCDSetOffset(ts_pos_t x,ts_pos_t y); //set LCD offset for character display
00165 void TSLCDPrintStr(unsigned char line,unsigned char column,char *str,ts_mode_t mode); //print string on LCD
00166 void TSLCDPrintTxt(unsigned char line,unsigned char column,const char *txt,ts_mode_t mode); //print text from code memory
00167 void TSLCDPrintCh(unsigned char line,unsigned char column,char c,ts_mode_t mode); //print a character on LCD
00168 
00169 
00170 
00171 #define FONT_BIT_WIDTH 8
00172 #define FONT_BYTE_HIGHT 2
00173 #define FONT_SIZE FONT_BIT_WIDTH*FONT_BYTE_HIGHT
00174 
00175 #define FONT_WIDTH FONT_BIT_WIDTH
00176 #define FONT_HEIGHT FONT_BYTE_HIGHT*8
00177 
00178 void TSLCDCharDisp(char charactor,ts_pos_t sx,ts_pos_t sy,ts_mode_t mode); //low level function to print a character on LCD
00179 
00180 unsigned char char_buf[FONT_BIT_WIDTH][FONT_BYTE_HIGHT];
00181 
00182 void buf_store(unsigned char charactor);
00183 
00184 unsigned short font_color;
00185 unsigned short back_color;
00186 ts_pos_t offsetx, offsety;
00187 ts_pos_t ts_margin_xl = 0;
00188 ts_pos_t ts_margin_xr = TS_SIZE_X - 1;
00189 ts_pos_t ts_margin_yu = 0;
00190 ts_pos_t ts_margin_yl = TS_SIZE_Y - 1;
00191 
00192 #define TSLCDGetMarginXl()            ts_margin_xl
00193 #define TSLCDGetMarginXr()            ts_margin_xr
00194 #define TSLCDGetMarginYu()            ts_margin_yu
00195 #define TSLCDGetMarginYl()            ts_margin_yl
00196 
00197 void TSLCDSetFontColor(unsigned short color) //set text's color
00198 {
00199     font_color = color;
00200 }
00201 
00202 void TSLCDSetBackColor(unsigned short color) //set back color for TS_MODE_FULL
00203 {
00204     back_color = color;
00205 }
00206 
00207 void TSLCDOutDat(unsigned short dat) //write data to LCD
00208 {
00209     lcd_rs.write(1);
00210 
00211     lcd_rd.write(1);
00212     lcd_wr.write(0);
00213 
00214     lcd_db.output();
00215         
00216     lcd_db = dat >> 8;
00217 
00218     lcd_cs.write(0);
00219     lcd_cs.write(1);
00220 
00221     lcd_db = dat;
00222 
00223     lcd_cs.write(0);
00224     lcd_cs.write(1);
00225 
00226     lcd_wr.write(1);
00227 
00228     lcd_db.input();
00229 }
00230 
00231 void TSLCDOutDat2(unsigned char dath,unsigned char datl) //write data to LCD
00232 {
00233     lcd_rs.write(1);
00234 
00235     lcd_rd.write(1);
00236     lcd_wr.write(0);
00237 
00238     lcd_db.output();
00239         
00240     lcd_db = dath;    
00241 
00242     lcd_cs.write(0);
00243     lcd_cs.write(1);
00244 
00245     lcd_db = datl;
00246 
00247     lcd_cs.write(0);
00248     lcd_cs.write(1);
00249 
00250     lcd_wr.write(1);
00251 
00252     lcd_db.input();
00253 }
00254 
00255 void TSLCDOutIns(unsigned short ins) //write instruction to LCD
00256 {
00257     lcd_rs.write(0);
00258 
00259     lcd_rd.write(1);
00260     lcd_wr.write(0);
00261 
00262     lcd_db.output();
00263         
00264     lcd_db = ins >> 8;
00265 
00266     lcd_cs.write(0);
00267     lcd_cs.write(1);
00268 
00269     lcd_db = ins;
00270 
00271     lcd_cs.write(0);
00272     lcd_cs.write(1);
00273 
00274     lcd_wr.write(1);
00275 
00276     lcd_db.input();
00277 }
00278 
00279 unsigned short TSLCDInDat(void) //read data from LCD
00280 {
00281     unsigned short dat = 0;
00282 
00283     lcd_db.input();
00284 
00285     lcd_rs.write(1);
00286 
00287     lcd_wr.write(1);
00288     lcd_rd.write(0);
00289 
00290     lcd_cs.write(0);
00291     lcd_cs.write(0);
00292     dat = lcd_db;
00293     lcd_cs.write(1);
00294 
00295     dat <<= 8;
00296 
00297     lcd_cs.write(0);
00298     lcd_cs.write(0);
00299     dat |= lcd_db;
00300     lcd_cs.write(1);
00301 
00302     lcd_rd.write(1);
00303 
00304     return (dat);
00305 }
00306 
00307 unsigned short TSLCDInIns(void) //read data from LCD
00308 {
00309     unsigned short ins = 0;
00310 
00311     lcd_db.input();
00312 
00313     lcd_rs.write(0);
00314 
00315     lcd_wr.write(1);
00316     lcd_rd.write(0);
00317 
00318     lcd_cs.write(0);
00319     lcd_cs.write(0);
00320     ins = lcd_db;
00321     lcd_cs.write(1);
00322 
00323     ins <<= 8;
00324 
00325     lcd_cs.write(0);
00326     lcd_cs.write(0);
00327     ins |= lcd_db;
00328     lcd_cs.write(1);
00329 
00330     lcd_rd.write(1);
00331 
00332     return (ins);
00333 }
00334 
00335 void TSLCDRst(void) //pulse reset signal to LCD
00336 {
00337     lcd_rst.write(0);
00338     wait_ms(50);
00339     lcd_rst.write(1);
00340 }
00341 
00342 void TSLCDInit(void) //initial LCD
00343 {
00344     wait_ms(100);
00345 
00346     TSLCDOutIns(0x00E5);
00347     TSLCDOutDat(0x8000);                     //set the internal vcore voltage
00348     TSLCDOutIns(TS_INS_START_OSC);
00349     TSLCDOutDat(0x0001);                     //start oscillator
00350     wait_ms(50);
00351 
00352     TSLCDOutIns(TS_INS_DRIV_OUT_CTRL);
00353     TSLCDOutDat(0x0100);                     //set SS, SM
00354     TSLCDOutIns(TS_INS_DRIV_WAV_CTRL);
00355     TSLCDOutDat(0x0700);                     //set 1 line inversion
00356     
00357     TSLCDOutIns(TS_INS_ENTRY_MOD);
00358     TSLCDOutDat(TS_VAL_ENTRY_MOD);            //set GRAM write direction, BGR=0
00359 
00360     TSLCDOutIns(TS_INS_RESIZE_CTRL);
00361     TSLCDOutDat(0x0000);                     //no resizing
00362 
00363     TSLCDOutIns(TS_INS_DISP_CTRL2);
00364     TSLCDOutDat(0x0202);                     //front & back porch periods = 2
00365     TSLCDOutIns(TS_INS_DISP_CTRL3);
00366     TSLCDOutDat(0x0000);                     
00367     TSLCDOutIns(TS_INS_DISP_CTRL4);
00368     TSLCDOutDat(0x0000);                     
00369     TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL1);
00370     TSLCDOutDat(0x0000);                     //select system interface                
00371     TSLCDOutIns(TS_INS_FRM_MARKER_POS);
00372     TSLCDOutDat(0x0000);                     
00373     TSLCDOutIns(TS_INS_RGB_DISP_IF_CTRL2);
00374     TSLCDOutDat(0x0000);                    
00375     
00376     TSLCDOutIns(TS_INS_POW_CTRL1);
00377     TSLCDOutDat(0x0000);
00378     TSLCDOutIns(TS_INS_POW_CTRL2);
00379     TSLCDOutDat(0x0000);                     
00380     TSLCDOutIns(TS_INS_POW_CTRL3);
00381     TSLCDOutDat(0x0000);
00382     TSLCDOutIns(TS_INS_POW_CTRL4);
00383     TSLCDOutDat(0x0000);                     
00384     wait_ms(200);
00385 
00386     TSLCDOutIns(TS_INS_POW_CTRL1);
00387     TSLCDOutDat(0x17B0);
00388     TSLCDOutIns(TS_INS_POW_CTRL2);
00389     TSLCDOutDat(0x0137);                     
00390     wait_ms(50);
00391 
00392     TSLCDOutIns(TS_INS_POW_CTRL3);
00393     TSLCDOutDat(0x013C);
00394     wait_ms(50);
00395 
00396     TSLCDOutIns(TS_INS_POW_CTRL4);
00397     TSLCDOutDat(0x1400);
00398     TSLCDOutIns(TS_INS_POW_CTRL7);
00399     TSLCDOutDat(0x0007);
00400     wait_ms(50);
00401 
00402     TSLCDOutIns(TS_INS_GRAM_HOR_AD);
00403     TSLCDOutDat(0x0000);
00404     TSLCDOutIns(TS_INS_GRAM_VER_AD);
00405     TSLCDOutDat(0x0000);
00406 
00407     TSLCDOutIns(TS_INS_GAMMA_CTRL1);
00408     TSLCDOutDat(0x0007);
00409     TSLCDOutIns(TS_INS_GAMMA_CTRL2);
00410     TSLCDOutDat(0x0504);
00411     TSLCDOutIns(TS_INS_GAMMA_CTRL3);
00412     TSLCDOutDat(0x0703);
00413     TSLCDOutIns(TS_INS_GAMMA_CTRL4);
00414     TSLCDOutDat(0x0002);
00415     TSLCDOutIns(TS_INS_GAMMA_CTRL5);
00416     TSLCDOutDat(0x0707);
00417     TSLCDOutIns(TS_INS_GAMMA_CTRL6);
00418     TSLCDOutDat(0x0406);
00419     TSLCDOutIns(TS_INS_GAMMA_CTRL7);
00420     TSLCDOutDat(0x0006);
00421     TSLCDOutIns(TS_INS_GAMMA_CTRL8);
00422     TSLCDOutDat(0x0404);
00423     TSLCDOutIns(TS_INS_GAMMA_CTRL9);
00424     TSLCDOutDat(0x0700);
00425     TSLCDOutIns(TS_INS_GAMMA_CTRL10);
00426     TSLCDOutDat(0x0A08);
00427 
00428     TSLCDOutIns(TS_INS_HOR_START_AD);
00429     TSLCDOutDat(0x0000);
00430     TSLCDOutIns(TS_INS_HOR_END_AD);
00431     TSLCDOutDat(0x00EF);
00432     TSLCDOutIns(TS_INS_VER_START_AD);
00433     TSLCDOutDat(0x0000);
00434     TSLCDOutIns(TS_INS_VER_END_AD);
00435     TSLCDOutDat(0x013F);
00436     TSLCDOutIns(TS_INS_GATE_SCAN_CTRL1);
00437     TSLCDOutDat(0x2700);
00438     TSLCDOutIns(TS_INS_GATE_SCAN_CTRL2);
00439     TSLCDOutDat(0x0001);
00440     TSLCDOutIns(TS_INS_GATE_SCAN_CTRL3);
00441     TSLCDOutDat(0x0000);
00442 
00443     TSLCDOutIns(TS_INS_PART_IMG1_DISP_POS);
00444     TSLCDOutDat(0x0000);
00445     TSLCDOutIns(TS_INS_PART_IMG1_START_AD);
00446     TSLCDOutDat(0x0000);
00447     TSLCDOutIns(TS_INS_PART_IMG1_END_AD);
00448     TSLCDOutDat(0x0000);
00449     TSLCDOutIns(TS_INS_PART_IMG2_DISP_POS);
00450     TSLCDOutDat(0x0000);
00451     TSLCDOutIns(TS_INS_PART_IMG2_START_AD);
00452     TSLCDOutDat(0x0000);
00453     TSLCDOutIns(TS_INS_PART_IMG2_END_AD);
00454     TSLCDOutDat(0x0000);
00455 
00456     TSLCDOutIns(TS_INS_PANEL_IF_CTRL1);
00457     TSLCDOutDat(0x0010);
00458     TSLCDOutIns(TS_INS_PANEL_IF_CTRL2);
00459     TSLCDOutDat(0x0000);
00460     TSLCDOutIns(TS_INS_PANEL_IF_CTRL3);
00461     TSLCDOutDat(0x0003);
00462     TSLCDOutIns(TS_INS_PANEL_IF_CTRL4);
00463     TSLCDOutDat(0x0110);
00464     TSLCDOutIns(TS_INS_PANEL_IF_CTRL5);
00465     TSLCDOutDat(0x0000);
00466     TSLCDOutIns(TS_INS_PANEL_IF_CTRL6);
00467     TSLCDOutDat(0x0000);
00468 
00469     TSLCDOutIns(TS_INS_DISP_CTRL1);
00470     TSLCDOutDat(0x0173);
00471 }
00472 
00473 void TSLCDShowPic2(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,const unsigned short *pic,ts_mode_t mode)
00474 //show picture from code memory with specific size
00475 {
00476     unsigned long k = 0;
00477     unsigned short color;
00478     unsigned int x,y;
00479     unsigned int i,j;
00480     if (sx < ts_margin_xl)
00481         sx = ts_margin_xl;
00482     if (ex > ts_margin_xr)
00483         ex = ts_margin_xr;
00484     if (sy < ts_margin_yu)
00485         sy = ts_margin_yu;
00486     if (ey > ts_margin_yl)             
00487         ey = ts_margin_yl;
00488 
00489     TSLCDOutIns(TS_INS_START_ADX);
00490     TSLCDOutDat(sx);
00491     TSLCDOutIns(TS_INS_END_ADX);
00492     TSLCDOutDat(ex);
00493     TSLCDOutIns(TS_INS_GRAM_ADX);
00494     TSLCDOutDat(sx);
00495     x = ex - sx + 1;
00496 
00497 #ifndef TS_ORN_PORTRAIT
00498     sy = TS_SIZE_Y - 1 - sy;     // mirror start y address
00499     ey = TS_SIZE_Y - 1 - ey;     // mirror end y address
00500     TSLCDOutIns(TS_INS_START_ADY);
00501     TSLCDOutDat(ey);
00502     TSLCDOutIns(TS_INS_END_ADY);
00503     TSLCDOutDat(sy);
00504     TSLCDOutIns(TS_INS_GRAM_ADY);
00505     TSLCDOutDat(sy);//fix from bug of v1_00
00506     y = sy - ey + 1;
00507 #else
00508     TSLCDOutIns(TS_INS_START_ADY);
00509     TSLCDOutDat(sy);
00510     TSLCDOutIns(TS_INS_END_ADY);
00511     TSLCDOutDat(ey);
00512     TSLCDOutIns(TS_INS_GRAM_ADY);
00513     TSLCDOutDat(sy);
00514     y = ey - sy + 1;
00515 #endif
00516 
00517     TSLCDOutIns(TS_INS_RW_GRAM);
00518 
00519     if (mode == TS_MODE_FULL)
00520     {
00521         for (j=0; j<y; j++)
00522             for (i=0; i<x; i++)
00523             {
00524                 TSLCDOutDat(*(&pic[k]));
00525                 k++;
00526             }
00527     }
00528     else
00529     if (mode == TS_MODE_NORMAL)
00530     {
00531         for (j=0; j<y; j++)
00532             for (i=0; i<x; i++)
00533             {
00534                 if (*(&pic[k]) == TS_COL_WHITE)
00535                 {
00536                     color = TSLCDInDat();         // ignore invalid data
00537                     color = TSLCDInDat();
00538                     TSLCDOutDat(color);
00539                 }
00540                 else
00541                 {
00542                     TSLCDOutDat(*(&pic[k]));
00543                 }
00544                 k++;
00545             }
00546     }
00547 }
00548 
00549 void TSLCDFillRect(ts_pos_t sx,ts_pos_t ex,ts_pos_t sy,ts_pos_t ey,unsigned short color,ts_mode_t mode) //draw a rectangular
00550 {
00551     unsigned int x,y;
00552     unsigned int i,j;
00553     if (sx < ts_margin_xl)
00554         sx = ts_margin_xl;
00555     if (ex > ts_margin_xr)
00556         ex = ts_margin_xr;
00557     if (sy < ts_margin_yu)
00558         sy = ts_margin_yu;
00559     if (ey > ts_margin_yl)             
00560         ey = ts_margin_yl;
00561 
00562     TSLCDOutIns(TS_INS_START_ADX);
00563     TSLCDOutDat(sx);
00564     TSLCDOutIns(TS_INS_END_ADX);
00565     TSLCDOutDat(ex);
00566     TSLCDOutIns(TS_INS_GRAM_ADX);
00567     TSLCDOutDat(sx);
00568     x = ex - sx + 1;
00569 
00570 #ifndef TS_ORN_PORTRAIT
00571     sy = TS_SIZE_Y - 1 - sy;     // mirror start y address
00572     ey = TS_SIZE_Y - 1 - ey;     // mirror end y address
00573     TSLCDOutIns(TS_INS_START_ADY);
00574     TSLCDOutDat(ey);
00575     TSLCDOutIns(TS_INS_END_ADY);
00576     TSLCDOutDat(sy);
00577     TSLCDOutIns(TS_INS_GRAM_ADY);
00578     TSLCDOutDat(sy);//fix from bug of v1_00
00579     y = sy - ey + 1;
00580 #else
00581     TSLCDOutIns(TS_INS_START_ADY);
00582     TSLCDOutDat(sy);
00583     TSLCDOutIns(TS_INS_END_ADY);
00584     TSLCDOutDat(ey);
00585     TSLCDOutIns(TS_INS_GRAM_ADY);
00586     TSLCDOutDat(sy);
00587     y = ey - sy + 1;
00588 #endif
00589 
00590     TSLCDOutIns(TS_INS_RW_GRAM);
00591 
00592     if ((mode == TS_MODE_NORMAL) || (mode == TS_MODE_FULL))
00593     {
00594         for (j=0; j<y; j++)
00595             for (i=0; i<x; i++)
00596             {
00597                 TSLCDOutDat(color);
00598             }
00599     }
00600     else
00601     if (mode == TS_MODE_INVERSE)
00602     {
00603         for (j=0; j<y; j++)
00604             for (i=0; i<x; i++)
00605             {
00606                 color = TSLCDInDat();         // ignore invalid data
00607                 color = TSLCDInDat();
00608                 TSLCDOutDat(~color);
00609             }
00610     }
00611 }
00612 
00613 void TSLCDFillCirc(ts_pos_t cx,ts_pos_t cy,ts_pos_t rad,unsigned short color, ts_mode_t mode) //draw a circle
00614 {
00615 #ifndef TS_ORN_PORTRAIT
00616     int sy_buf,ey_buf;
00617 #endif
00618     int sx,sy,ex,ey;
00619     int i,j;
00620     unsigned short color_buf;
00621     unsigned short rad2 = rad*rad;
00622     sx = cx - rad;
00623     ex = cx + rad;
00624     sy = cy - rad;
00625     ey = cy + rad;
00626 
00627     if (sx < ts_margin_xl)
00628         sx = ts_margin_xl;
00629     if (ex > ts_margin_xr)
00630         ex = ts_margin_xr;
00631     if (sy < ts_margin_yu)
00632         sy = ts_margin_yu;
00633     if (ey > ts_margin_yl)             
00634         ey = ts_margin_yl;
00635 
00636     TSLCDOutIns(TS_INS_START_ADX);
00637     TSLCDOutDat(sx);
00638     TSLCDOutIns(TS_INS_END_ADX);
00639     TSLCDOutDat(ex);
00640     TSLCDOutIns(TS_INS_GRAM_ADX);
00641     TSLCDOutDat(sx);
00642 //    x = ex - sx + 1;
00643 
00644 #ifndef TS_ORN_PORTRAIT
00645     sy_buf = TS_SIZE_Y - 1 - sy;     // mirror start y address
00646     ey_buf = TS_SIZE_Y - 1 - ey;     // mirror end y address
00647     TSLCDOutIns(TS_INS_START_ADY);
00648     TSLCDOutDat(ey_buf);
00649     TSLCDOutIns(TS_INS_END_ADY);
00650     TSLCDOutDat(sy_buf);
00651     TSLCDOutIns(TS_INS_GRAM_ADY);
00652     TSLCDOutDat(sy_buf);//fix from bug of v1_00
00653 //    y = sy_buf - ey_buf + 1;
00654 #else
00655     TSLCDOutIns(TS_INS_START_ADY);
00656     TSLCDOutDat(sy);
00657     TSLCDOutIns(TS_INS_END_ADY);
00658     TSLCDOutDat(ey);
00659     TSLCDOutIns(TS_INS_GRAM_ADY);
00660     TSLCDOutDat(sy);
00661 //    y = ey - sy + 1;
00662 #endif
00663 
00664     TSLCDOutIns(TS_INS_RW_GRAM);
00665 
00666     if (mode == TS_MODE_NORMAL)
00667     {
00668         for (j=sy-cy; j<=ey-cy; j++)
00669             for (i=sx-cx; i<=ex-cx; i++)
00670             {
00671                 if ((i)*(i) + (j)*(j) < rad2)
00672                 {
00673                     TSLCDOutDat(color);
00674                 }
00675                 else
00676                 {
00677                     color_buf = TSLCDInDat();         // ignore invalid data
00678                     color_buf = TSLCDInDat();
00679                     TSLCDOutDat(color_buf);
00680                 }
00681             }
00682     }
00683     else
00684     if (mode == TS_MODE_INVERSE)
00685     {
00686         for (j=sy-cy; j<=ey-cy; j++)
00687             for (i=sx-cx; i<=ex-cx; i++)
00688             {
00689                 if ((i)*(i) + (j)*(j) < rad2)
00690                 {
00691                     color_buf = TSLCDInDat();         // ignore invalid data
00692                     color_buf = TSLCDInDat();
00693                     TSLCDOutDat(~color_buf);
00694                 }
00695                 else
00696                 {
00697                     color_buf = TSLCDInDat();         // ignore invalid data
00698                     color_buf = TSLCDInDat();
00699                     TSLCDOutDat(color_buf);
00700                 }
00701             }
00702     }
00703     else
00704     if (mode == TS_MODE_FULL)
00705     {
00706         for (j=sy-cy; j<=ey-cy; j++)
00707             for (i=sx-cx; i<=ex-cx; i++)
00708             {
00709                 if ((i)*(i) + (j)*(j) < rad2)
00710                 {
00711                     TSLCDOutDat(color);
00712                 }
00713                 else
00714                 {
00715                     TSLCDOutDat(back_color);
00716                 }
00717             }
00718     }
00719 }
00720 
00721 void TSLCDSetMargins(ts_pos_t xl,ts_pos_t xr,ts_pos_t yu,ts_pos_t yl) //set margins for FillRect,FillCirc
00722 {
00723     ts_margin_xl = xl;
00724     ts_margin_xr = xr;
00725     ts_margin_yu = yu;
00726     ts_margin_yl = yl;
00727 }
00728 
00729 void TSLCDSetMarginsDefault(void) //Reset margins to default value
00730 {
00731     ts_margin_xl = 0;
00732     ts_margin_xr = TS_SIZE_X - 1;
00733     ts_margin_yu = 0;
00734     ts_margin_yl = TS_SIZE_Y - 1;
00735 }
00736 
00737 void buf_store(unsigned char charactor)
00738 {
00739     unsigned char i,j;
00740     int char_p = charactor*FONT_SIZE;
00741 
00742     for (i=0; i<FONT_BIT_WIDTH; i++)
00743         for (j=0; j<FONT_BYTE_HIGHT; j++)
00744         {
00745             char_buf[i][j] = *(&font[char_p]);
00746             char_p++;
00747         }
00748 }
00749 
00750 unsigned char buf_read(unsigned char column,unsigned char row)
00751 {
00752     unsigned char read_pixel;
00753 
00754     if (row < 8)
00755     {
00756         read_pixel = (char_buf[column][0] >> (7-row)) & 0x01;
00757     }
00758     else
00759     {
00760         row = row - 8;
00761         read_pixel = (char_buf[column][1] >> (7-row)) & 0x01;
00762     }
00763     return (read_pixel);
00764 }
00765 
00766 void TSLCDCharDisp(char charactor,ts_pos_t sx,ts_pos_t sy,ts_mode_t mode) //low level function to print a character on LCD
00767 {
00768     unsigned int x,y;
00769     unsigned char i,j;
00770     ts_pos_t ex,ey;
00771     unsigned short c;
00772 
00773     ex = sx + FONT_WIDTH - 1;
00774     ey = sy + FONT_HEIGHT - 1;
00775 
00776     buf_store(charactor - 0x20);
00777 
00778     TSLCDOutIns(TS_INS_START_ADX);
00779     TSLCDOutDat(sx);
00780     TSLCDOutIns(TS_INS_END_ADX);
00781     TSLCDOutDat(ex);
00782     TSLCDOutIns(TS_INS_GRAM_ADX);
00783     TSLCDOutDat(sx);
00784     x = ex - sx + 1;
00785 
00786 #ifndef TS_ORN_PORTRAIT
00787     sy = TS_SIZE_Y - 1 - sy;     // mirror start y address
00788     ey = TS_SIZE_Y - 1 - ey;     // mirror end y address
00789     TSLCDOutIns(TS_INS_START_ADY);
00790     TSLCDOutDat(ey);
00791     TSLCDOutIns(TS_INS_END_ADY);
00792     TSLCDOutDat(sy);
00793     TSLCDOutIns(TS_INS_GRAM_ADY);
00794     TSLCDOutDat(sy);//fix from bug of v1_00
00795     y = sy - ey + 1;
00796 #else
00797     TSLCDOutIns(TS_INS_START_ADY);
00798     TSLCDOutDat(sy);
00799     TSLCDOutIns(TS_INS_END_ADY);
00800     TSLCDOutDat(ey);
00801     TSLCDOutIns(TS_INS_GRAM_ADY);
00802     TSLCDOutDat(sy);
00803     y = ey - sy + 1;
00804 #endif
00805 
00806     TSLCDOutIns(TS_INS_RW_GRAM);
00807 
00808     if (mode == TS_MODE_NORMAL)
00809     {
00810         for (j=y; j>0; j--)
00811             for (i=0; i<x; i++)
00812             {
00813                 if (buf_read(i,j))
00814                 {
00815                     TSLCDOutDat(font_color);
00816                 }
00817                 else
00818                 {
00819                     c = TSLCDInDat();         // ignore invalid data
00820                     c = TSLCDInDat();
00821                     TSLCDOutDat(c);
00822                 }
00823             }
00824     }
00825     else
00826     if (mode == TS_MODE_INVERSE)
00827     {
00828         for (j=0; j<y; j++)
00829             for (i=0; i<x; i++)
00830             {
00831                 c = TSLCDInDat();             // ignore invalid data
00832                 c = TSLCDInDat();
00833                 if (buf_read(i,j))
00834                 {
00835                     TSLCDOutDat(~c);
00836                 }
00837                 else
00838                 {
00839                     TSLCDOutDat(c);
00840                 }
00841             }
00842     }
00843     else
00844     if (mode == TS_MODE_FULL)
00845     {
00846         for (j=0; j<y; j++)
00847             for (i=0; i<x; i++)
00848             {
00849                 if (buf_read(i,j))
00850                 {
00851                     TSLCDOutDat(font_color);
00852                 }
00853                 else
00854                 {
00855                     TSLCDOutDat(back_color);
00856                 }
00857             }
00858     }
00859 }
00860 
00861 void TSLCDSetOffset(ts_pos_t x,ts_pos_t y) //set LCD offset for character display
00862 {
00863     offsetx = x;
00864     offsety = y;
00865 }
00866 
00867 void TSLCDPrintStr(unsigned char line,unsigned char column,char *str,ts_mode_t mode) //print string on LCD
00868 {
00869     int i = 0;
00870     ts_pos_t posx,posy;
00871     posx = offsetx + column*FONT_WIDTH;
00872     posy = offsety + line*FONT_HEIGHT;
00873 
00874     while(str[i])
00875     {
00876         TSLCDCharDisp(str[i],posx,posy,mode);
00877         posx += FONT_WIDTH;
00878         i++;
00879     }
00880 }
00881 
00882 void TSLCDPrintTxt(unsigned char line,unsigned char column,const char *txt,ts_mode_t mode) //print text from code memory
00883 {
00884     int i = 0;
00885     ts_pos_t posx,posy;
00886     posx = offsetx + column*FONT_WIDTH;
00887     posy = offsety + line*FONT_HEIGHT;
00888 
00889     while((&txt[i]))
00890     {
00891         TSLCDCharDisp(*(&txt[i]),posx,posy,mode);
00892         posx += FONT_WIDTH;
00893         i++;
00894     }
00895 }
00896 
00897 void TSLCDPrintCh(unsigned char line,unsigned char column,char c,ts_mode_t mode) //print a character on LCD
00898 {
00899     ts_pos_t posx,posy;
00900     posx = offsetx + column*FONT_WIDTH;
00901     posy = offsety + line*FONT_HEIGHT;
00902 
00903     TSLCDCharDisp(c,posx,posy,mode);
00904 }
00905 /////
00906 
00907 int main() {
00908 
00909     //setSystemFrequency(3, 1, 16, 1);
00910        
00911     // Do QVGA Setup Stuff
00912     TSLCDRst();
00913     TSLCDInit();
00914     
00915     wait_ms(100);
00916     lcd_bl.write(1);
00917     
00918     TSLCDFillRect(0,TS_SIZE_X-1,0,TS_SIZE_Y-1,TS_COL_BLUE,TS_MODE_NORMAL);
00919     TSLCDFillRect(0,TS_SIZE_X-1,0,70,TS_COL_WHITE,TS_MODE_NORMAL);
00920     TSLCDSetFontColor(TS_COL_BLUE);
00921     TSLCDPrintStr(2,6,"Testing ELT240320TP with AVR",TS_MODE_NORMAL);
00922     TSLCDFillRect(20,80,90,130,TS_COL_BLACK,TS_MODE_NORMAL);
00923     TSLCDFillRect(30,90,100,140,TS_COL_YELLOW,TS_MODE_NORMAL);
00924     TSLCDFillRect(20,80,160,200,TS_COL_BLACK,TS_MODE_NORMAL);
00925     TSLCDFillRect(30,90,170,210,TS_COL_RED,TS_MODE_NORMAL);
00926     TSLCDFillRect(195,205,71,TS_SIZE_Y-1,TS_COL_WHITE,TS_MODE_NORMAL);
00927     TSLCDFillCirc(200,155,60,TS_COL_WHITE,TS_MODE_NORMAL);
00928     TSLCDFillCirc(200,155,50,TS_COL_BLUE,TS_MODE_NORMAL);
00929     TSLCDFillCirc(200,155,40,TS_COL_BLACK,TS_MODE_NORMAL);
00930     TSLCDFillCirc(200,155,30,TS_COL_RED,TS_MODE_NORMAL);
00931 
00932     TSLCDFillRect(5, 15, 5, 15, TS_COL_GREEN,TS_MODE_NORMAL);
00933     
00934     while(1){}
00935 }