Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import smbus
- import time
- import psutil
- import socket
- import subprocess
- import os
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BOARD)
- GPIO.setwarnings(False)
- GPIO.setup(8, GPIO.IN)
- GPIO.setup(10, GPIO.IN)
- GPIO.setup(26, GPIO.OUT)
- GPIO.setup(24, GPIO.OUT)
- GPIO.setup(22, GPIO.OUT)
- GPIO.output(26, 0)
- GPIO.output(24, 0)
- GPIO.output(22, 0)
- ########################################################
- I2C_ADDR = 0x27
- LCD_WIDTH = 16
- LCD_CHR = 1
- LCD_CMD = 0
- LCD_LINE_1 = 0x80
- LCD_LINE_2 = 0xC0
- LCD_LINE_3 = 0x94
- LCD_LINE_4 = 0xD4
- LCD_BACKLIGHT = 0x08 # On
- #LCD_BACKLIGHT = 0x00 # Off
- ENABLE = 0b00000100
- E_PULSE = 0.0005
- E_DELAY = 0.0005
- bus = smbus.SMBus(1)
- def red():
- GPIO.output(26, 1)
- GPIO.output(24, 0)
- GPIO.output(22, 0)
- def green():
- GPIO.output(26, 0)
- GPIO.output(24, 1)
- GPIO.output(22, 0)
- def blue():
- GPIO.output(26, 0)
- GPIO.output(24, 0)
- GPIO.output(22, 1)
- def black():
- GPIO.output(26, 0)
- GPIO.output(24, 0)
- GPIO.output(22, 0)
- def lcd_init():
- lcd_byte(0x33,LCD_CMD)
- lcd_byte(0x32,LCD_CMD)
- lcd_byte(0x06,LCD_CMD)
- lcd_byte(0x0C,LCD_CMD)
- lcd_byte(0x28,LCD_CMD)
- lcd_byte(0x01,LCD_CMD)
- time.sleep(E_DELAY)
- def lcd_byte(bits, mode):
- bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
- bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT
- bus.write_byte(I2C_ADDR, bits_high)
- lcd_toggle_enable(bits_high)
- bus.write_byte(I2C_ADDR, bits_low)
- lcd_toggle_enable(bits_low)
- def lcd_toggle_enable(bits):
- time.sleep(E_DELAY)
- bus.write_byte(I2C_ADDR, (bits | ENABLE))
- time.sleep(E_PULSE)
- bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
- time.sleep(E_DELAY)
- def lcd_string(message,line):
- message = message.ljust(LCD_WIDTH," ")
- lcd_byte(line, LCD_CMD)
- for i in range(LCD_WIDTH):
- lcd_byte(ord(message[i]),LCD_CHR)
- def getCpuTemperature():
- tempFile = open( "/sys/class/thermal/thermal_zone0/temp" )
- cpu_temp = tempFile.read()
- tempFile.close()
- return float(cpu_temp)/1000
- def getRAMinfo():
- p = os.popen('free')
- i = 0
- while 1:
- i = i + 1
- line = p.readline()
- if i==2:
- return(line.split()[1:4])
- ########################################################
- host = socket.gethostname()
- ipnum = subprocess.check_output(["hostname", "-I"])
- ########################################################
- def main():
- # Main program block
- blue()
- # Initialise display
- lcd_init()
- ## state - decides what LED should be on and off
- ## initialize to 0 or all off
- state = 1
- ## increment - the direction of states
- ## initialize to 1 or increasing
- inc = 1
- ## Set prev_input's initial value to 2
- prev_input=2
- while True:
- ## When state toggle button is pressed
- if ( GPIO.input(8) == True ):
- green()
- time.sleep(1)
- state = state + 1
- else:
- time.sleep(0.2)
- if (state == 1): #CPU
- # print("CPU")
- blue()
- CPU_usage = psutil.cpu_percent(interval = .5)
- lcd_string("CPU Load %s%%" % (CPU_usage),LCD_LINE_1)
- lcd_string("CPU Temp %s" % (getCpuTemperature()),LCD_LINE_2)
- elif (state == 2): #RAM
- # print("RAM")
- blue()
- RAM_stats = getRAMinfo()
- RAM_used = round(int(RAM_stats[1]) / 1000,1)
- RAM_free = round(int(RAM_stats[2]) / 1000,1)
- lcd_string("RAM Used %sMB " % (RAM_used),LCD_LINE_1)
- lcd_string("RAM Free %sMB " % (RAM_free),LCD_LINE_2)
- elif (state == 3): #IP
- # print("IP")
- blue()
- lcd_string("%s" % (host),LCD_LINE_1)
- lcd_string("%s" % (ipnum)[0:14],LCD_LINE_2)
- # print(host)
- # print(ipnum)[0:14]
- elif (state == 4): #SHUTDOWN
- if ( state == 4 and GPIO.input(10) == True ):
- # print("SHUTDOWN BUTTON")
- red()
- time.sleep(2)
- os.system('sudo shutdown -h now')
- exit()
- else:
- # print("SHUTDOWN")
- blue()
- lcd_string("SHUTDOWN",LCD_LINE_1)
- lcd_string(" ",LCD_LINE_2)
- elif (state == 5): #REBOOT
- if ( state == 5 and GPIO.input(10) == True ):
- # print("REBOOT BUTTON")
- red()
- time.sleep(2)
- os.system('sudo shutdown -r now')
- exit()
- else:
- # print("REBOOT")
- blue()
- lcd_string("REBOOT",LCD_LINE_1)
- lcd_string(" ",LCD_LINE_2)
- elif (state == 6): #OFF
- # print("OFF")
- LCD_BACKLIGHT = 0x00
- black()
- state = state - 6
- else:
- time.sleep(0.2)
- if __name__ == '__main__':
- try:
- main()
- except (KeyboardInterrupt, SystemExit):
- LCD_BACKLIGHT = 0x00
- black()
- pass
- finally:
- lcd_byte(0x01, LCD_CMD)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement