Writing a python module to simulate a LCD

So recently I got a Raspberry pi and a 16×2 character LCD screen , I thought , lets make a simple game that can be played on the lcd. my first instinct was to code directly for the lcd on the pi , but as I started coding I realized that the clutter of having the lcd connected wasn’t really necessary while i am programming the game’s logic .

I decided to make a python module that gives me the lcd output on my monitor , this way i no longer need to work with my lcd connected and can even code the game on my laptop and test the results quickly . Moreover once im done with the coding , i can simply replace the module code , for the lcd control code , and my game is ready to deploy.

Writing a python module to simulate a LCD

The game shall have a post for itself, for now , lets focus on the module.

Here’s how to make a really simple python LCD simulator module.

  1. We begin by creating a folder , I shall name this folder “mlcd” (for mock lcd)
  2. Then we create a python file called “__init__.py”
  3. The “__init__.py” is the file where our module code is going to be.

so let us look at the code .

mlcd Module :

  1. import pygame
  2. def init(chars,lines):
  3.     global screen
  4.     global myfont
  5.     pygame.init()
  6.     size = [12*chars,20*lines]
  7.     screen= pygame.display.set_mode(size)
  8.     pygame.display.set_caption(“Mock LCD”)
  9.     myfont = pygame.font.SysFont(“monospace”, 20)
  10. def draw(args):
  11.     i=0;
  12.     global screen
  13.     global myfont
  14.     screen.fill((0,0,0))#erase screen contents
  15.     while(i < len(args))
  16.         line= myfont.render(args[i], 2, (255,255,0))
  17.         screen.blit(line, (0, 20*i))
  18.         i+=1
  19.     pygame.display.flip()
The module works by using pygame to render the output , and simply has 2 functions
  1.  init(chars,lines)
    to initialize a display that is “chars” characters wide and “lines” lines in height
    this only needs to be run once.this code is designed to work with only one display
    ,however should one require multiple displays , the module can be modified to use classes
  2. draw(args)
    this is the function that draws the characters on to the screen , args is a python list (array)
    containing as many strings as there are lines . this function needs to be called again only if one wishes to update the  output.
to include this module in your python project , simply copy the mlcd folder , into your working directory and import the module

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter