pioneer600

Pioneer600 sample for headless raspberry pi servers
git clone git://git.ckyln.com/pioneer600.git
Log | Files | Refs | README

Pioneer600.py (6114B)


      1 #!/usr/bin/python
      2 # -*- coding:utf-8 -*-
      3 # Cem KEYLAN
      4 # 2018
      5 # Version 1.1
      6 
      7 
      8 import RPi.GPIO as GPIO
      9 import smbus
     10 import spidev as SPI
     11 import SSD1306
     12 import time
     13 import Image
     14 import ImageDraw
     15 import ImageFont
     16 import os
     17 import add_module
     18 
     19 KEY = 20
     20 address = 0x20
     21 main_menu = 1
     22 submenus = 1
     23 
     24 
     25 def beep_on():
     26 	bus.write_byte(address,0x7F&bus.read_byte(address))
     27 def beep_off():
     28 	bus.write_byte(address,0x80|bus.read_byte(address))
     29 def led_off():
     30 	bus.write_byte(address,0x10|bus.read_byte(address))
     31 def led_on():
     32 	bus.write_byte(address,0xEF&bus.read_byte(address))
     33 
     34 
     35 def oled(Line1, Line2, Line3=""):
     36         # Draw a black filled box to clear the image.
     37         draw.rectangle((0,0,width,height), outline=0, fill=0)
     38         draw.text((x, top), str(Line1), font=font1, fill=255)
     39         draw.text((x, top+20), str(Line2), font=font2, fill=255)
     40 	draw.text((x, top+40), str(Line3), font=font2, fill=255)
     41         disp.image(image)
     42         disp.display()
     43 
     44 def MyInterrupt(KEY):
     45 	print("KEY PRESS")
     46 
     47 def menu ():
     48 	global values
     49 	global submenus
     50 	global main_menu
     51 
     52 	if values == "up" :
     53 		submenus = submenus - 1
     54 	elif values == 'down' :
     55 		submenus = submenus + 1
     56 	elif values == "right" :
     57 		main_menu = main_menu + 1
     58 		submenus = 1
     59 	elif values == "left" :
     60 		main_menu = main_menu - 1
     61 		submenus = 1
     62 
     63 	if main_menu == 5:
     64 		main_menu = 1
     65 	if main_menu == 0:
     66 		main_menu = 4
     67 
     68 	# Main Information delivering up status
     69 	if main_menu == 1 :
     70 		strtime = time.strftime("%H : %M : %S")
     71 
     72 		if submenus == 4:
     73 			submenus = 1
     74 		if submenus ==0:
     75 			submenus =3
     76 
     77 		if submenus == 1:
     78 			if len(os.popen("hostname -I").read()) == 1 :
     79 				upstatus = "Down"
     80 			else:
     81 				upstatus = "UP"
     82 			oled("Status = " + upstatus,os.popen("/sbin/ifconfig eth0 | grep 'inet ' | cut -d: -f2 | awk '{ print $2}'").read(), strtime)
     83 		elif submenus == 3 :
     84 			if "active" in os.popen("sudo service ssh status").read():
     85 				upssh = "UP"
     86 			else:
     87 				upssh = "DOWN"
     88 			oled("SSH Server", upssh)
     89 		elif submenus == 2 :
     90 			if len(os.popen("hostname -I").read()) is not 1:
     91 				if "unreachable" in os.popen("nc 8.8.8.8 53 -zv").read():
     92 					oled("Connection", "could NOT be","established.")
     93 				else:
     94 					oled("Connection","could be","established.")
     95 
     96 
     97 	# Device Info
     98 	elif main_menu == 2:
     99 
    100 		if submenus == 5:
    101 			submenus = 1
    102 		if submenus ==0:
    103 			submenus =4
    104 
    105 		if submenus == 4 :
    106 			oled("Device Info","CPU Temperature=",(add_module.getCPUtemperature()+" C"))
    107 		elif submenus == 2 :
    108 			oled("Device Info","Free RAM",(str(int(add_module.getRAMinfo()[2])/1024)+" MB"))
    109 		elif submenus == 3 :
    110 			oled("Device Info","CPU Usage",(str(add_module.getCPUuse())+" %"))
    111 		elif submenus == 1 :
    112 			oled("Device Info","Disk Usage",add_module.getDiskSpace()[3])
    113 
    114 	#aƧ kapa
    115 	elif main_menu == 3 :
    116 
    117 		if submenus == 4:
    118 			submenus = 1
    119 		if submenus == 0:
    120 			submenus = 3
    121 
    122 		elif submenus == 1 :
    123 			oled("System","Close App","Press Button")
    124 			if GPIO.input(KEY) == 0:
    125 				if len(os.popen("hostname -I").read()) is not 1:
    126 					exit ()
    127 				else:
    128 					oled("System","Close App","Must be connected")
    129 					time.sleep(2)
    130 		elif submenus == 2 :
    131 			oled("System","Reboot","Press Button")
    132 			if GPIO.input(KEY) == 0:
    133 				os.popen('sudo reboot')
    134 		elif submenus == 3 :
    135 			oled("System","Halt System","Press Button")
    136 			if GPIO.input(KEY) == 0:
    137 				os.popen('sudo halt')
    138 
    139 
    140 	#interfaces
    141 	elif main_menu == 4 :
    142 		note = os.popen("sudo ifconfig").read()
    143 		if submenus == 3 :
    144 			submenus = 1
    145 		if submenus == 0 :
    146 			submenus = 2
    147 		elif submenus == 2 :
    148 			if "eth0" in note:
    149 				oled("Interfaces", "Eth0 Enabled", "Press Button")
    150 				if GPIO.input(KEY) == 0:
    151 					os.popen("sudo ifconfig eth0 down")
    152 			else:
    153 				oled("Interfaces", "Eth0 Disabled", "Press Button")
    154 				if GPIO.input(KEY) == 0:
    155 					os.popen("sudo ifconfig eth0 up")
    156 		elif submenus == 1:
    157 			if "wlan0" in note:
    158 				oled("Interfaces","Wlan0 Enabled", "Press Button")
    159 				if GPIO.input(KEY) == 0:
    160 					os.popen("sudo ifconfig wlan0 down")
    161 			else:
    162 				oled("Interfaces","Wlan0 Disabled", "Press Button")
    163 				if GPIO.input(KEY) == 0:
    164 					os.popen("sudo ifconfig wlan0 up")
    165 
    166 
    167 	else :
    168 		print ("Something went wrong")
    169 
    170 	return (submenus)
    171 
    172 # Raspberry Pi pin configuration:
    173 RST = 19
    174 # Note the following are only used with SPI:
    175 DC = 16
    176 bus = 0
    177 device = 0
    178 
    179 # 128x64 display with hardware SPI:
    180 disp = SSD1306.SSD1306(RST, DC, SPI.SpiDev(bus,device))
    181 
    182 # Initialize library.
    183 disp.begin()
    184 
    185 # Clear display.
    186 disp.clear()
    187 disp.display()
    188 
    189 # Create blank image for drawing.
    190 # Make sure to create image with mode '1' for 1-bit color.
    191 width = disp.width
    192 height = disp.height
    193 image = Image.new('1', (width, height))
    194 
    195 # Get drawing object to draw on image.
    196 draw = ImageDraw.Draw(image)
    197 
    198 # Draw a black filled box to clear the image.
    199 draw.rectangle((0,0,width,height), outline=0, fill=0)
    200 
    201 # Draw some shapes.
    202 # First define some constants to allow easy resizing of shapes.
    203 padding = 1
    204 top = padding
    205 x = padding
    206 # Load default font.
    207 # font = ImageFont.load_default()
    208 font_dir =  os.path.dirname(os.path.realpath(__file__)) +"/KeepCalm-Medium.ttf"
    209 font1 = ImageFont.truetype(font_dir, 15)
    210 font2 = ImageFont.truetype(font_dir, 14)
    211 
    212 GPIO.setmode(GPIO.BCM)
    213 GPIO.setup(KEY,GPIO.IN,GPIO.PUD_UP)
    214 #GPIO.add_event_detect(KEY,GPIO.FALLING,MyInterrupt,200)
    215 
    216 #bmp = BMP180()
    217 bus = smbus.SMBus(1)
    218 
    219 print("Starting...")
    220 print("Version 1.1")
    221 
    222 try:
    223 	while True:
    224 
    225 		bus.write_byte(address,0x0F|bus.read_byte(address))
    226 		value = bus.read_byte(address) | 0xF0
    227 		if value != 0xFF:
    228 			led_on()
    229 			if (value | 0xFE) != 0xFF:
    230 				values= "left"
    231 			elif (value | 0xFD) != 0xFF:
    232 				values= "up"
    233 			elif (value | 0xFB) != 0xFF:
    234 				values= "down"
    235 			else :
    236 				values= "right"
    237 			while value != 0xFF:
    238 				bus.write_byte(address,0x0F|bus.read_byte(address))
    239 				value = bus.read_byte(address) | 0xF0
    240 				time.sleep(0.01)
    241 			led_off()
    242 			submenus=menu()
    243 
    244 		time.sleep(0.1)
    245 		values= "YOK"
    246 		submenus=menu()
    247 
    248 # for keyboard interrupt
    249 except (KeyboardInterrupt, SystemExit):
    250 	print ("Keyboard Interrupt")
    251 	# Clear display.
    252 	disp.clear()
    253 	disp.display()
    254 
    255 except:
    256 	print ("ERROR")
    257 	# Clear display.
    258 	oled("There was an error","")
    259 	time.sleep(2)
    260 	disp.clear()
    261 	disp.display()