Question

I have the python source below to show some information on the LCD screen attached to my Raspberry Pi. It workd perfectly but I can't make it to loop unlimited times. Can someone help me?

import RPi.GPIO as GPIO
import time
from time import sleep
import socket
import fcntl
import struct
from pycgminer import CgminerAPI

LCD_RS = 25
LCD_E  = 24
LCD_D4 = 23 
LCD_D5 = 17
LCD_D6 = 18
LCD_D7 = 22

LCD_WIDTH = 16 
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0 

E_PULSE = 0.00005
E_DELAY = 0.00005

def main():

  GPIO.setmode(GPIO.BCM)       
  GPIO.setup(LCD_E, GPIO.OUT)  
  GPIO.setup(LCD_RS, GPIO.OUT) 
  GPIO.setup(LCD_D4, GPIO.OUT) 
  GPIO.setup(LCD_D5, GPIO.OUT) 
  GPIO.setup(LCD_D6, GPIO.OUT) 
  GPIO.setup(LCD_D7, GPIO.OUT) 

  lcd_init()

  #UNIT

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("UNIT NAME",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("MODEL NUMBER",2)
  sleep(7)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

  #IP ADDRESS

  pi_ip = get_ip_address('eth0')
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("IP Address",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(pi_ip,2)
  sleep(7)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

  # AVG SPEED

  cgminer = CgminerAPI()
  summary = cgminer.summary()
  avgspeed = avg_speed()
  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("Avg. Speed",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string(str(avgspeed) + " MH/s", 2)
  sleep(7)

  #CLEAR

  lcd_byte(LCD_LINE_1, LCD_CMD)
  lcd_string("",2)
  lcd_byte(LCD_LINE_2, LCD_CMD)
  lcd_string("",2)
  sleep(0.4)

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

def avg_speed():
    cgminer = CgminerAPI()
    summary = cgminer.summary()
    avg_speed_value = cgminer.command('summary')['SUMMARY'][0]['MHS av']
    return avg_speed_value

def lcd_init():
  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,style):
  if style==1:
    message = message.ljust(LCD_WIDTH," ")  
  elif style==2:
    message = message.center(LCD_WIDTH," ")
  elif style==3:
    message = message.rjust(LCD_WIDTH," ")

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

def lcd_byte(bits, mode):
  GPIO.output(LCD_RS, mode) 
  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)

  time.sleep(E_DELAY)    
  GPIO.output(LCD_E, True)  
  time.sleep(E_PULSE)
  GPIO.output(LCD_E, False)  
  time.sleep(E_DELAY)      

  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)

  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()

Basicly, what I am trying to do is to repeat everything under def main()

Was it helpful?

Solution

Dumb solution :

def main():
    while True:
        # all your code here

but that's probably not such a good idea...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top