05 July 2015

Writing a python module to simulate a LCD

            So recently I got a Raspberry pi and a 16x2 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.
  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

Usage example:
  1. import mlcd     #import the module
  2. mlcd.init(16,3) # initialize a 16x3 display
  3. #draw the three lines passed as a list
  4. mlcd.draw(["Hello",         
  5.            "     world",
  6.            "Mock LCD !!!"])
on running this script , we get the following output :

  •  Mono-space font is used because all characters must take equal amount of space on the screen , so changing the font might generate inaccurate results. 
  • The color of text and fontsize can be changed , however the size of the window must also be adjusted in line 6 of the mlcd module.
Download the code from Github

Are you Looking for a way to monetize your android or Ios app ? 
I've been using Startapp ,in my android apps .I have integrated it, in my game Space Zombies , and its fairly easy to integrate and they have bonus cash to earn based on your current earning , they support multiple platforms , from HTML5 to JAVA application to Unity  , give them a shot !