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

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.