/* * lcd.cpp * * Created on: 2014/06/19 * Author: sunaga */ #include #include "lcd.h" lcd::lcd() { m_Gpio.InitDevice(0); m_Gpio.SetPinMode(25, 1); m_Gpio.SetPinHigh(25); m_Spi.InitDevie(0, SPI_MODE_0, 16000000, 8); m_pImage = new unsigned char[IMAGE_SIZE]; Init(); } lcd::~lcd() { if(m_pImage != NULL) { delete[] m_pImage; } } void lcd::Set8Bits(unsigned char* lpData) { m_Spi.WriteOnly(1, lpData); } void lcd::SetCommand(unsigned char com) { unsigned char d; d = 0; m_Gpio.SetPinLow(25); d = 0; Set8Bits(&d); d = com; Set8Bits(&d); m_Gpio.SetPinHigh(25); } void lcd::SetCommandData(unsigned char com, unsigned char dat1, unsigned char dat2) { unsigned char d; // D/C=0(コマンド) m_Gpio.SetPinLow(25); d = 0; Set8Bits(&d); d = com; Set8Bits(&d); usleep(1); // D/C=1(以降はデータ) m_Gpio.SetPinHigh(25); usleep(1); Set8Bits(&dat1); Set8Bits(&dat2); usleep(1); } int lcd::Init() { // LCD への初期化コマンド SetCommandData(0x00, 0x00, 0x01); // OSCEN=1 (start oscillator) SetCommandData(0x03, 0xa2, 0xa4); // power control 1 SetCommandData(0x0c, 0x00, 0x04); // power control 2 SetCommandData(0x0d, 0x03, 0x08); // power control 3 SetCommandData(0x0e, 0x30, 0x00); // power control 4 SetCommandData(0x1e, 0x00, 0xea); // power control 5 SetCommandData(0x01, 0x2b, 0x3f); // !RL,REV,!CAD,BGR,!SM,TB,MUX=13f SetCommandData(0x02, 0x06, 0x00); // !FLD,!ENWS,DC,EOR,!WSMD,NW=00 SetCommandData(0x10, 0x00, 0x00); // !SLP (exit sleep mode) usleep(30000); SetCommandData(0x11, 0x60, 0x18); // 65k, TypeA, landscape(ID=10,AM=1) SetCommandData(0x07, 0x00, 0x33); // GON=1, DTE=1, D=11 SetCommandData(0x0b, 0x00, 0x39); // frame cycle SetCommandData(0x0f, 0x00, 0x00); // gate scan start usleep(10000); return 0; } void lcd::Invalidate() { SetCommandData(0x4f, 0x1, 0x3f); // x = 0 SetCommandData(0x4e, 0x00, 0xef); // y = 0 SetCommand(0x22); // 描画コマンド送信 m_Spi.WriteOnly(IMAGE_SIZE, m_pImage); } #define RGB24TO16(r,g,b) ((((r)&0xf8)<<8)|(((g)&0xfc)<<3)|(((b)&0xf8)>>3)) void lcd::MakeTestPtn() { unsigned short colors[3]; short col; int x, y; unsigned char *buffer = m_pImage; // 上段の7帯 colors[0] = RGB24TO16(255, 0, 0); // red colors[1] = RGB24TO16(0 , 255, 0); // green colors[2] = RGB24TO16( 0, 0, 255); // blue for(y = 0; y < IMAGE_HEIGHT; y++) { if((y / 80) == 0) { col = colors[0]; } else if( (y / 80) == 1) { col = colors[1]; } else { col = colors[2]; } for(x = 0; x < IMAGE_WIDTH; x++) { char* p; p = (char*)&col; *buffer++ = *(p + 1); *buffer++ = *(p); } } } #include #include #include #include #include #include #include #include #include #define FB_DEVICE "/dev/fb0" int lcd::InitFrameBuffer() { int ret; // open frame buffer (/dev/fb0) int fd = open(FB_DEVICE, O_RDWR); if (fd < 0) { printf("error: cannot open %s (fb_init)\n", FB_DEVICE); return -1; } //get screen info struct fb_var_screeninfo sinfo; ret = ioctl(fd, FBIOGET_VSCREENINFO, &sinfo); if (ret < 0) { printf("error: cannot get screen info \n"); return -1; } if (sinfo.bits_per_pixel != 16) { // must be 16bits printf("error: color is not 16bit\n"); return -1; } struct fb_fix_screeninfo finfo; ret = ioctl(fd, FBIOGET_FSCREENINFO, &finfo); if (ret < 0) { printf("error: cannot get fix screen info\n"); return -1; } if (finfo.line_length != sinfo.xres * 2) { printf("error: line length must be width*2 \n"); return -1; } // map /dev/fb0 to m_pFrameBuffer m_pFrameBuffer = (unsigned char *) mmap(NULL, IMAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (m_pFrameBuffer == NULL) { printf("error: cannot map /dev/mem \n"); return -1; } close(fd); return 0; } int lcd::DrawFrameBuffer() { unsigned char *pIm, *pFb, *pEnd; pEnd = m_pImage + IMAGE_SIZE; pIm = m_pImage; pFb = (unsigned char *)m_pFrameBuffer; while(pIm < pEnd) { *pIm = *(pFb +1); *(pIm + 1) = *(pFb); // *pIm = 0xf8; // *(pIm + 1) = 0x00; pIm += 2; pFb += 2; } Invalidate(); return 0; }