Implementing Partial Screen Clear on LCD

Implementing Partial Screen Clear on LCD

Graphic Control Uncle

Composition conveys ideas

Reading has never been this easy!!!

01

Introduction

When developing multifunctional applications for an LCD screen, there are times when you need to clear the screen. Clearing can be done in two ways: full screen clear and partial screen clear. Today, I will document the code for partial screen clearing, so I can refer to it later!

02

Key Information

When displaying static images on an LCD screen, monochrome filling or BMP image format is often used. For clearing the screen, white or black is commonly used as the background color. Therefore, clearing involves filling the pixel points of the area that needs to be cleared with either white or black. The implementation of this specific case is as follows:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct 
{
  int lcd_fd;
  unsigned int *addr;
  
}LCD_DEV;
int uninit_LCD(LCD_DEV *p);
int init_LCD(LCD_DEV *p);
int show_400_240_color(int x, int y, unsigned int color, unsigned int *addr);
int Clear_the_screen(int x1, int y1,int x2, int y2, unsigned int color, unsigned int *addr);

int main(int argc, const char**argv)
{
  LCD_DEV lcd_dev;
  // Initialize LCD
  init_LCD(&lcd_dev);
  
  int i;
  // Color fill
  for(i=0; i<4; i++)
  {
    
    show_400_240_color(0, 0, 0xffff00, lcd_dev.addr);
    
    show_400_240_color(400, 0, 0xff00, lcd_dev.addr);
    
    show_400_240_color(0, 240, 0xff0000, lcd_dev.addr);
    
    show_400_240_color(400, 240, 0xff,lcd_dev.addr);
  }
  usleep(1000*10); // Delay for observation, can be commented out
  
  // Partial screen clear, setting white as background color
  Clear_the_screen(0,0,480,700,0xffffff,lcd_dev.addr);

  // Uninitialize
  uninit_LCD(&lcd_dev);
  
  return 0;
}
// Display a 400*240 color block
int show_400_240_color(int x, int y, unsigned int color, unsigned int *addr)
{
  int i, j;
  
  for(j=0+y; j<240+y; j++)
  {
    
    for(i=0+x; i<400+x; i++)
    {
      
      *(addr+800*j+i) = color;
      
    }
  }
  
  return 0;
}

/*
Clear_the_screen: Partial screen clear function
x1, y1: Starting point for clearing
x2, y2: Width and length of the clearing area
color: Clearing color
*addr: Color block address

Note on exceptional cases:
Due to the varying sizes of LCD screens in reality,
The LCD screen used here is: 480*800
Therefore, when filling in the values for x1, y1, x2, y2,
you need to fill them rationally to avoid pitfalls.
Documented on: 2020/7/3
*/
int Clear_the_screen(int x1, int y1,int x2, int y2, unsigned int color, unsigned int *addr)
{
  int i, j;
  
  for(j=x1; j<x1+x2; j++)
  {
    
    for(i=y1; i<y1+y2; i++)
    {
      
      *(addr+800*j+i) = color;
      
    }
    usleep(1000*2); // Delay for observation, can be commented out
    //printf("j is %d\n",j);
  }
  
  return 0;
}

// Initialize LCD
int init_LCD(LCD_DEV *p)
{
  
  // 1. Open LCD screen file
  p->lcd_fd = open("/dev/fb0", O_RDWR);
  if(p->lcd_fd == -1)
  {
    perror("open /dev/fb0 failed");
    return -1;
  }
  
  // 2. Memory mapping
  p->addr = mmap(NULL, 800*480*4, PROT_READ | PROT_WRITE, MAP_SHARED,p->lcd_fd, 0);
  if(p->addr == MAP_FAILED)
  {
    perror("mmap /dev/fb0 failed");
    return -1;
  }
  
  return 0;
  
}


// Uninitialize
int uninit_LCD(LCD_DEV *p)
{
  
  close(p->lcd_fd);
  
  munmap(p->addr, 800*480*4);
  return 0;
}

Effect demonstration

Implementing Partial Screen Clear on LCD

03

Conclusion

Today’s discussion on clearing operations related to the LCD screen ends here. If you have any questions, feel free to leave a message for discussion. Thank you for reading.

“Graphic Control Uncle”, progressing together with you!

Implementing Partial Screen Clear on LCD

Source: Graphic Control Uncle
Editor:Department of New Media, Electronics Division, Zhang Wanshan
Initial Review:Deputy Secretary of the Youth League Committee, Chen Yihong
Review: Secretary of the Youth League Committee, Wang Shuxiao
Final Review: Secretary of the Party Committee, Li Yingdong

Implementing Partial Screen Clear on LCD

Leave a Comment