siltuz
Posts: 48
Joined: Thu May 16, 2013 8:08 pm

LCD Optimised code

Fri May 16, 2014 7:31 pm

I have made a basic LCD program mainly spliced from various sources and tutorials, and I was wondering if anyone could point me in the right direction for either learning to code LCD stuff properly (as I want to make a much more complicated version of this program with buttons that toggle the next screen to come up as well as logs being made), or a way to make this code more efficient. I also was wondering if anyone would know where to start for maybe having it check if I have anything on my google calendar and display a message accordingly

the code as it stands is:

Code: Select all

import RPi.GPIO as GPIO            # import RPi.GPIO module  
from time import sleep             # lets us have a delay  
import datetime
import time
import os
import time, datetime

Brightness = 17
LED_1 = 22
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18

LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False
 
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

E_PULSE = 0.00005
E_DELAY = 0.00005
try:
 while True:
   def main():
    # Main program block
    GPIO.setmode(GPIO.BCM)       # Use BCM GPIO numbers
    GPIO.setup(LCD_E, GPIO.OUT)  # E
    GPIO.setup(LCD_RS, GPIO.OUT) # RS
    GPIO.setup(LCD_D4, GPIO.OUT) # DB4
    GPIO.setup(LCD_D5, GPIO.OUT) # DB5
    GPIO.setup(LCD_D6, GPIO.OUT) # DB6
    GPIO.setup(LCD_D7, GPIO.OUT) # DB7
    sleep(10)
    # Initialise display
    lcd_init()

    mystring = ""
    mystring2 = ""
    mytime = ""  
    mytemp = ""  
    pretemp = "["  
    posttemp = "c]    "  
    f=os.popen("date")  
    for i in f.readlines():  
     mytime += i  
     mytime = mytime[11:-13]  
     f=os.popen("/opt/vc/bin/vcgencmd measure_temp")  
    for i in f.readlines():  
     mytemp += i  
     mytemp = mytemp[5:-3]  
     mystring = pretemp + mytemp + posttemp + mytime  
     lcd_byte(LCD_LINE_1, LCD_CMD)  
     lcd_string(mystring)  
     lcd_byte(LCD_LINE_2, LCD_CMD)
     lcd_string("      test")

   def lcd_init():
    # Initialise display
    lcd_byte(0x33,LCD_CMD)
    lcd_byte(0x32,LCD_CMD)
    lcd_byte(0x28,LCD_CMD)
    lcd_byte(0x0C,LCD_CMD)  
    lcd_byte(0x06,LCD_CMD)
    lcd_byte(0x01,LCD_CMD)  

   def lcd_string(message):
    # Send string to display

    message = message.ljust(LCD_WIDTH," ")  

    for i in range(LCD_WIDTH):
      lcd_byte(ord(message[i]),LCD_CHR)

   def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = data
  # mode = True  for character
  #        False for command

    GPIO.output(LCD_RS, mode) # RS

  # High bits
    GPIO.output(LCD_D4, False)
    GPIO.output(LCD_D5, False)
    GPIO.output(LCD_D6, False)
    GPIO.output(LCD_D7, False)
    if bits&0x10==0x10:
      GPIO.output(LCD_D4, True)
    if bits&0x20==0x20:
      GPIO.output(LCD_D5, True)
    if bits&0x40==0x40:
      GPIO.output(LCD_D6, True)
    if bits&0x80==0x80:
      GPIO.output(LCD_D7, True)

  # Toggle 'Enable' pin
    time.sleep(E_DELAY)    
    GPIO.output(LCD_E, True)  
    time.sleep(E_PULSE)
    GPIO.output(LCD_E, False)  
    time.sleep(E_DELAY)      

  # Low bits
    GPIO.output(LCD_D4, False)
    GPIO.output(LCD_D5, False)
    GPIO.output(LCD_D6, False)
    GPIO.output(LCD_D7, False)
    if bits&0x01==0x01:
      GPIO.output(LCD_D4, True)
    if bits&0x02==0x02:
      GPIO.output(LCD_D5, True)
    if bits&0x04==0x04:
      GPIO.output(LCD_D6, True)
    if bits&0x08==0x08:
      GPIO.output(LCD_D7, True)

    # Toggle 'Enable' pin
    time.sleep(E_DELAY)    
    GPIO.output(LCD_E, True)  
    time.sleep(E_PULSE)
    GPIO.output(LCD_E, False)  
    time.sleep(E_DELAY)   

   if __name__ == '__main__':
    main()

except KeyboardInterrupt:          # trap a CTRL+C keyboard interrupt
    GPIO.cleanup()                 # resets all GPIO ports used by this program  

riklaunim
Posts: 265
Joined: Tue Apr 22, 2014 7:34 pm

Re: LCD Optimised code

Fri May 16, 2014 8:41 pm

Some refactoring would be handy - split it apart classes and functions so that each functionality is isolated. You can also check my code examples for serial LCD - the code from shop example and my class for easy operations. When you write an application then various components will be tied together. If there will be separate classes, libraries for every component then combining it together, creating logic will be much easier.

Return to “Python”