Advertisement
Pertile

LCD 16x2 i2C update engine Moode Raspberry - Python script

Aug 15th, 2017
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None | 0 0
  1. #!/usr/bin/python
  2. #----------------------------------------
  3. # Determinare la soundcard con il comando:
  4. # cat /proc/asound/cards
  5. # da inserire poi nel cmd6 e cmd7
  6. #----------------------------------------
  7. import smbus
  8. import time
  9. import os;
  10. import subprocess;
  11. str_pad = " " * 16  
  12.  
  13. # Define some device parameters
  14. I2C_ADDR  = 0x27 # I2C device address
  15. LCD_WIDTH = 16   # Maximum characters per line
  16.  
  17. # Define some device constants
  18. LCD_CHR = 1 # Mode - Sending data
  19. LCD_CMD = 0 # Mode - Sending command
  20.  
  21. LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
  22. LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
  23. LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
  24. LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
  25.  
  26. LCD_BACKLIGHT  = 0x08  # On
  27. #LCD_BACKLIGHT = 0x00  # Off
  28.  
  29. ENABLE = 0b00000100 # Enable bit
  30.  
  31. # Timing constants
  32. E_PULSE = 0.0005
  33. E_DELAY = 0.0005
  34.  
  35. #Open I2C interface
  36. #bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
  37. bus = smbus.SMBus(1) # Rev 2 Pi uses 1
  38.  
  39. def lcd_init():
  40.   # Initialise display
  41.   lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  42.   lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  43.   lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  44.   lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
  45.   lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  46.   lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  47.   time.sleep(E_DELAY)
  48.  
  49. def lcd_byte(bits, mode):
  50.   # Send byte to data pins
  51.   # bits = the data
  52.   # mode = 1 for data
  53.   #        0 for command
  54.  
  55.   bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  56.   bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
  57.  
  58.   # High bits
  59.   bus.write_byte(I2C_ADDR, bits_high)
  60.   lcd_toggle_enable(bits_high)
  61.  
  62.   # Low bits
  63.   bus.write_byte(I2C_ADDR, bits_low)
  64.   lcd_toggle_enable(bits_low)
  65.  
  66. def lcd_toggle_enable(bits):
  67.   # Toggle enable
  68.   time.sleep(E_DELAY)
  69.   bus.write_byte(I2C_ADDR, (bits | ENABLE))
  70.   time.sleep(E_PULSE)
  71.   bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  72.   time.sleep(E_DELAY)
  73.  
  74. def lcd_string(message,line):
  75.   # Send string to display
  76.  
  77.   message = message.ljust(LCD_WIDTH," ")
  78.  
  79.   lcd_byte(line, LCD_CMD)
  80.  
  81.   for i in range(LCD_WIDTH):
  82.     lcd_byte(ord(message[i]),LCD_CHR)
  83.  
  84. # Definizione carattere (0) antenna
  85.   lcd_byte(64,LCD_CMD)
  86.   lcd_byte(0b11111,LCD_CHR)
  87.   lcd_byte(0b01110,LCD_CHR)
  88.   lcd_byte(0b01110,LCD_CHR)
  89.   lcd_byte(0b00100,LCD_CHR)
  90.   lcd_byte(0b00100,LCD_CHR)
  91.   lcd_byte(0b00100,LCD_CHR)
  92.   lcd_byte(0b00100,LCD_CHR)
  93.   lcd_byte(0b00100,LCD_CHR)
  94.  
  95. # Definizione carattere (1) termometro
  96.   lcd_byte(0b00100,LCD_CHR)
  97.   lcd_byte(0b01010,LCD_CHR)
  98.   lcd_byte(0b01010,LCD_CHR)
  99.   lcd_byte(0b01110,LCD_CHR)
  100.   lcd_byte(0b01110,LCD_CHR)
  101.   lcd_byte(0b11111,LCD_CHR)
  102.   lcd_byte(0b11111,LCD_CHR)
  103.   lcd_byte(0b01110,LCD_CHR)
  104.  
  105. # Definizione carattere (2) gradi Celsius
  106.   lcd_byte(0b00000,LCD_CHR)
  107.   lcd_byte(0b00000,LCD_CHR)
  108.   lcd_byte(0b01000,LCD_CHR)
  109.   lcd_byte(0b00011,LCD_CHR)
  110.   lcd_byte(0b00100,LCD_CHR)
  111.   lcd_byte(0b00100,LCD_CHR)
  112.   lcd_byte(0b00011,LCD_CHR)
  113.   lcd_byte(0b00000,LCD_CHR)
  114.  
  115.  
  116. def main():
  117.  lcd_init()
  118.  
  119.  while True:
  120.   for counter in range (4):
  121.  
  122.    cmd1 = "mpc | head -1"        #gets the currently playing song from mpc
  123.    process = subprocess.Popen(cmd1, stdout=subprocess.PIPE , shell=True)
  124.    os.waitpid(process.pid, 0)[1]
  125.    mpc_song = process.stdout.read().strip()
  126.  
  127.    cmd2 = "mpc | head -2 | tail -1 | awk '{print $1}'" # gets the player state ie paused playing etc
  128.    process = subprocess.Popen(cmd2, stdout=subprocess.PIPE , shell=True)
  129.    os.waitpid(process.pid, 0)[1]
  130.    mpc_state = process.stdout.read().strip()
  131.  
  132.    cmd3 = "hostname -I"  #gets the current ip of the boombox
  133.    process = subprocess.Popen(cmd3, stdout=subprocess.PIPE , shell=True)
  134.    os.waitpid(process.pid, 0)[1]
  135.    ip_addr = process.stdout.read().strip()
  136.  
  137.    cmd4 = "iwconfig wlan0 | grep Signal | awk '{print $4}' | cut -d= -f2" #check wifi signal strength
  138.    process = subprocess.Popen(cmd4, stdout=subprocess.PIPE , shell=True)
  139.    os.waitpid(process.pid, 0)[1]
  140.    wifi_strength = process.stdout.read().strip()
  141.  
  142.    cmd5 = "/opt/vc/bin/vcgencmd measure_temp | cut -c6-9"   #Misura la temperatura della CPU
  143.    process = subprocess.Popen(cmd5, stdout=subprocess.PIPE , shell=True)
  144.    os.waitpid(process.pid, 0)[1]
  145.    temp  = process.stdout.read().strip()
  146.  
  147.    cmd6 = "awk '/^format/{print substr($2,2,2)}' /proc/asound/card0/pcm0p/sub0/hw_params"   #Misura bitrate
  148.    process = subprocess.Popen(cmd6, stdout=subprocess.PIPE , shell=True)
  149.    os.waitpid(process.pid, 0)[1]
  150.    freq1  = process.stdout.read().strip()
  151.  
  152.    cmd7 = "awk '/^rate/{print $2/1000}' /proc/asound/card0/pcm0p/sub0/hw_params"   #Misura bitrate
  153.    process = subprocess.Popen(cmd7, stdout=subprocess.PIPE , shell=True)
  154.    os.waitpid(process.pid, 0)[1]
  155.    freq2  = process.stdout.read().strip()
  156.  
  157.    cmd8 = "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq"   #Misura CPU Speed
  158.    process = subprocess.Popen(cmd8, stdout=subprocess.PIPE , shell=True)
  159.    os.waitpid(process.pid, 0)[1]
  160.    cpu  = process.stdout.read().strip()
  161.  
  162.    cmd9 = "top -b -n1 | grep Cpu | awk '{print $2 + $4}'"   #Misura CPU libera
  163.    process = subprocess.Popen(cmd9, stdout=subprocess.PIPE , shell=True)
  164.    os.waitpid(process.pid, 0)[1]
  165.    cpuload  = process.stdout.read().strip()
  166.    
  167.    
  168.    if counter == 0:  # Scrive riga 2 - Indirizzo IP e potenza WiFi
  169.      lcd_string(ip_addr,LCD_LINE_2)
  170.    
  171.    if counter == 1:   # Scrive riga 2 - Temperatura CPU e Frequenza
  172.      # Frequenza audio
  173.      freq = freq1 + "/" + freq2 + "kHz"
  174.      lcd_string(freq,LCD_LINE_2)
  175.      # Temperatura CPU
  176.      lcd_string(temp,LCD_LINE_2+13)
  177.      # Simbolo termometro
  178.      lcd_byte(LCD_LINE_2+12, LCD_CMD)
  179.      lcd_byte(1,LCD_CHR)
  180.      # Simbolo gradi Celsius
  181.      lcd_byte(LCD_LINE_2+15, LCD_CMD)
  182.      lcd_byte(2,LCD_CHR)
  183.  
  184.    if counter == 2:   #Scrive riga 2 - potenza wifi - CPU speed
  185.      lcd_byte(LCD_LINE_2, LCD_CMD)
  186.      lcd_byte(0,LCD_CHR) #Simbolo antenna
  187.      lcd_string(wifi_strength,LCD_LINE_2+1)
  188.      cpu = str(int(cpu)/1000) + "MHz"
  189.      lcd_string(cpu, LCD_LINE_2+10)
  190.  
  191.    if counter == 3:  #Scrive riga 2 - CPU Load
  192.      cpuload = "CPU Load " + cpuload + "%"
  193.      lcd_string(cpuload,LCD_LINE_2)
  194.  
  195.    # Scrive riga 1
  196.    my_long_string = str_pad + mpc_state + " " + mpc_song
  197.    for i in range (0, len(my_long_string)):  
  198.     lcd_text = my_long_string[i:(i+LCD_WIDTH)]  
  199.     lcd_string(lcd_text,LCD_LINE_1)  
  200.     time.sleep(0.1)  
  201.    lcd_string(str_pad,LCD_LINE_1)  
  202.    
  203.  
  204. if __name__ == '__main__':
  205.  
  206.   try:
  207.     main()
  208.   except KeyboardInterrupt:
  209.     pass
  210.   finally:
  211.     lcd_byte(0x01, LCD_CMD)
  212.     lcd_string("Goodbye!",LCD_LINE_1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement