#include "glcd.h" #include "fonts.h" #include static uint8_t orientation; static uint16_t DeviceCode, temp; static __inline void lcdWriteIndex(uint16_t index) { Clr_RS; Set_RD; LCD_DATA_PORT->ODR = index; Clr_WR; Set_WR; } static __inline void lcdWriteData(uint16_t data) { Set_RS; LCD_DATA_PORT->ODR = data; Clr_WR; Set_WR; } static __inline uint16_t lcdReadData(void) { uint16_t value; Set_RS; Set_WR; Clr_RD; // change pin mode to digital input LCD_DATA_PORT->CRH = 0x44444444; LCD_DATA_PORT->CRL = 0x44444444; value = LCD_DATA_PORT->IDR; // dummy value = LCD_DATA_PORT->IDR; // change pin mode back to digital output LCD_DATA_PORT->CRH = 0x33333333; LCD_DATA_PORT->CRL = 0x33333333; Set_RD; return value; } static __inline void lcdWriteReg(uint16_t lcdReg,uint16_t lcdRegValue) { Clr_CS; lcdWriteIndex(lcdReg); lcdWriteData(lcdRegValue); Set_CS; } static __inline uint16_t lcdReadReg(uint16_t lcdReg) { uint16_t lcdRAM; Clr_CS; lcdWriteIndex(lcdReg); lcdRAM = lcdReadData(); Set_CS; return lcdRAM; } static void lcdSetCursor(uint16_t x, uint16_t y) { if(DeviceCode==0x8989) { if(orientation == portrait) { lcdWriteReg(0x004e, x); lcdWriteReg(0x004f, y); } else if(orientation == landscape) { lcdWriteReg(0x004e, y); lcdWriteReg(0x004f, x); } } else if(DeviceCode==0x9919) { if(orientation == portrait) { lcdWriteReg(0x004e, x); lcdWriteReg(0x004f, y); } else if(orientation == landscape) { lcdWriteReg(0x004e, y); lcdWriteReg(0x004f, x); } } else { lcdWriteReg(0x0020, x); lcdWriteReg(0x0021, y); } } static void lcdDelay(uint16_t nCount) { uint16_t TimingDelay; while(nCount--) { for(TimingDelay=0;TimingDelay<10000;TimingDelay++) asm("nop"); } } void lcdSetOrientation(uint8_t newOrientation) { orientation = newOrientation; switch(orientation) { case portrait: lcdWriteReg(0x0001, 0x2B3F); lcdWriteReg(0x0011, 0x6070); break; case landscape: lcdWriteReg(0x0001, 0x293F); lcdWriteReg(0x0011, 0x6078); break; case 2: break; case 3: break; } } void lcdSetWindows(uint16_t x, uint16_t y, uint16_t width, uint16_t height) { if(orientation == portrait) { lcdWriteReg(0x0050, x); /* Horizontal GRAM Start Address */ lcdWriteReg(0x0051, x+width-1); /* Horizontal GRAM End Address (-1) */ lcdWriteReg(0x0052, y); /* Vertical GRAM Start Address */ lcdWriteReg(0x0053, y+height-1); /* Vertical GRAM End Address (-1) */ lcdWriteReg(0x0020, x); lcdWriteReg(0x0021, y); } else if(orientation == landscape) { lcdWriteReg(0x0050, y); /* Vertical GRAM Start Address */ lcdWriteReg(0x0051, y+height-1); /* Vertical GRAM End Address (-1) */ lcdWriteReg(0x0052, x); /* Horizontal GRAM Start Address */ lcdWriteReg(0x0053, x+width-1); /* Horizontal GRAM End Address (-1) */ lcdWriteReg(0x0020, y); lcdWriteReg(0x0021, x); } } void lcdClear(uint16_t color) { uint32_t index=0; lcdSetCursor(0,0); Clr_CS; lcdWriteIndex(0x0022); for(index=0;index<76800;index++) lcdWriteData(color); Set_CS; } uint16_t lcdGetPoint(uint16_t x, uint16_t y) { uint16_t dummy; lcdSetCursor(x,y); Clr_CS; lcdWriteIndex(0x0022); dummy = lcdReadData(); dummy = lcdReadData(); Set_CS; if( DeviceCode==0x7783 || DeviceCode==0x4531 || DeviceCode==0x8989 ) return dummy; else return lcdBGR2RGB(dummy); } void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t point) { lcdSetCursor(x, y); lcdWriteReg(0x0022, point); } void lcdDrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) { int16_t dy, dx; int16_t addx=1, addy=1; int16_t P, diff; int16_t i=0; dx = abs((int16_t)(x2 - x1)); dy = abs((int16_t)(y2 - y1)); if(x1 > x2) addx = -1; if(y1 > y2) addy = -1; if(dx >= dy) { dy *= 2; P = dy - dx; diff = P - dx; for(; i<=dx; ++i) { lcdDrawPixel(x1, y1, color); if(P < 0) { P += dy; x1 += addx; } else { P += diff; x1 += addx; y1 += addy; } } } else { dx *= 2; P = dx - dy; diff = P - dy; for(; i<=dy; ++i) { lcdDrawPixel(x1, y1, color); if(P < 0) { P += dx; y1 += addy; } else { P += diff; x1 += addx; y1 += addy; } } } } void lcdChar(uint16_t x, uint16_t y, unsigned char c, uint16_t charcolor, uint16_t bkcolor) { uint16_t i = 0; uint16_t j = 0; unsigned char buffer[16]; unsigned char tmp_char=0; GetASCIICode(buffer,c); for (i=0;i<16;i++) { tmp_char=buffer[i]; for (j=0;j<8;j++) { if (((tmp_char >> (7-j)) & 0x01) == 0x01) { lcdDrawPixel(x+j,y+i,charcolor); } else { lcdDrawPixel(x+j,y+i,bkcolor); } } } } void lcdString(uint16_t x, uint16_t y, uint8_t *str, uint16_t color, uint16_t bkcolor) { uint8_t TempChar; do { TempChar=*str++; lcdChar(x,y,TempChar,color,bkcolor); if (x<232) { x+=8; } else if (y<304) { x=0; y+=16; } else { x=0; y=0; } } while (*str!=0); } uint16_t lcdBGR2RGB(uint16_t color) { uint16_t r, g, b, rgb; b = ( color>>0 ) & 0x1f; g = ( color>>5 ) & 0x3f; r = ( color>>11 ) & 0x1f; rgb = (b<<11) + (g<<5) + (r<<0); return( rgb ); } void lcdFillArea(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color) { uint16_t i; for(i = y0; i < y1; i++) lcdDrawLine(x0, i, x1, i, color); } void lcdFillArea2(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color) { uint32_t index, area; area = ((x1-x0) * (y1-y0)); lcdSetWindows(x0, y0, x1, y1); lcdSetCursor(x0, x1); Clr_CS; lcdWriteIndex(0x0022); for(index = 0; index < area; index++) lcdWriteData(color); Set_CS; } void lcdDrawRect(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint8_t filled, uint16_t color) { uint16_t i, TempX; uint16_t j, TempY; if (x0 > x1) { TempX = x1; x1 = x0; x0 = TempX; } if (y0 > y1) { TempY = y1; y1 = y0; y0 = TempY; } if(filled) { for(i=x0; i