Summary of Writing a python module to simulate a LCD
This article describes creating a Python module named "mlcd" to simulate a 16x2 LCD screen on a computer monitor. This allows developers to test game logic without physical hardware, replacing the simulation with real LCD code upon completion. The module uses Pygame to render text and provides initialization and drawing functions for easy integration into projects.
Parts used in the mlcd project:
- Raspberry Pi
- 16x2 character LCD screen
- Python programming language
- Pygame library
- Computer monitor
- Laptop
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.
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.
- We begin by creating a folder , I shall name this folder “mlcd” (for mock lcd)
- Then we create a python file called “__init__.py”
- The “__init__.py” is the file where our module code is going to be.
so let us look at the code .
mlcd Module :
- import pygame
- def init(chars,lines):
- global screen
- global myfont
- pygame.init()
- size = [12*chars,20*lines]
- screen= pygame.display.set_mode(size)
- pygame.display.set_caption(“Mock LCD”)
- myfont = pygame.font.SysFont(“monospace”, 20)
- def draw(args):
- i=0;
- global screen
- global myfont
- screen.fill((0,0,0))#erase screen contents
- while(i < len(args))
- line= myfont.render(args[i], 2, (255,255,0))
- screen.blit(line, (0, 20*i))
- i+=1
- pygame.display.flip()
- 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 - 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.
- Why did the author create this module?
To avoid clutter from connecting an LCD while coding and to allow testing on a laptop. - What is the name of the folder created for this module?
The folder is named mlcd, which stands for mock lcd. - Which file contains the main module code?
The __init__.py file holds the module code. - How does the module initialize the display?
It uses the init function with characters and lines parameters to set up the screen size. - What library is used to render the output?
The module uses pygame to render the output. - What arguments does the draw function accept?
It accepts a python list containing strings corresponding to each line. - When should the draw function be called?
It needs to be called whenever the user wishes to update the output. - Can this module support multiple displays?
Currently it works with one display but can be modified using classes for multiple displays. - How do you include this module in a project?
Copy the mlcd folder into your working directory and import the module.
