Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pi experiments #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions examples/lldp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env python3
# requires `lldpctl` to be installed

import sys
import subprocess
import os
from time import sleep
import pifacecad

UPDATE_INTERVAL = 30 # 10 sec

GET_LLDP_HOSTNAME_CMD = "lldpctl | grep SysName | awk '{print $2}'"
GET_LLDP_PORT_CMD = "lldpctl | grep PortDescr | awk '{print $2}'"


def run_cmd(cmd):
return subprocess.check_output(cmd, shell=True).decode('utf-8')

def get_lldp_hostname():
return run_cmd(GET_LLDP_HOSTNAME_CMD)[:-1]

def get_lldp_port():
return run_cmd(GET_LLDP_PORT_CMD)[:-1]


def wait_for_neighbour():
hostname = ""
while len(hostname) <= 0:
sleep(1)
hostname = get_lldp_hostname()


def show_lldpinfo():
while True:
cad.lcd.clear()
cad.lcd.write(get_lldp_hostname()+"\n"+get_lldp_port())
sleep(UPDATE_INTERVAL)


if __name__ == "__main__":
# test for lldpd
try:
subprocess.call(["lldpctl"], stdout=open('/dev/null'))
except OSError as e:
if e.errno == os.errno.ENOENT:
print(
"lldpctl was not found, install with "
"`sudo apt-get install lldpd`")
sys.exit(1)
else:
raise # Something else went wrong while trying to run `lldpd`

cad = pifacecad.PiFaceCAD()
cad.lcd.blink_off()
cad.lcd.cursor_off()

if "clear" in sys.argv:
cad.lcd.clear()
cad.lcd.display_off()
cad.lcd.backlight_off()
else:
cad.lcd.backlight_on()
cad.lcd.write("Waiting for LLDP..")
wait_for_neighbour()
show_lldpinfo()
43 changes: 31 additions & 12 deletions examples/sysinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@
from time import sleep
import pifacecad


UPDATE_INTERVAL = 60 * 5 # 5 mins
GET_IP_CMD = "hostname --all-ip-addresses"
UPDATE_INTERVAL = 10 # 10 sec
GET_IP_CMD = "hostname --all-ip-addresses | awk '{print $1}'"
GET_TEMP_CMD = "/opt/vc/bin/vcgencmd measure_temp"
TOTAL_MEM_CMD = "free | grep 'Mem' | awk '{print $2}'"
USED_MEM_CMD = "free | grep '\-\/+' | awk '{print $3}'"
CPU_LOAD_CMD = "uptime | awk '{print $9}' | sed s/,/""/g"
HOSTNAME_CMD = "hostname"
FREE_DISK_CMD = "df -h | grep rootfs | awk '{print $5}'"

temperature_symbol = pifacecad.LCDBitmap(
[0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0])
memory_symbol = pifacecad.LCDBitmap(
[0xe, 0x1f, 0xe, 0x1f, 0xe, 0x1f, 0xe, 0x0])
temp_symbol_index, memory_symbol_index = 0, 1
cpu_symbol = pifacecad.LCDBitmap( [0x0, 0x1f, 0x11, 0x15, 0x11, 0x1f, 0x0, 0x0] )
disk_symbol = pifacecad.LCDBitmap( [0x0, 0x1c, 0x1e, 0x1e, 0x1e, 0x1e, 0x0, 0x0] )
temperature_symbol = pifacecad.LCDBitmap( [0x4, 0x4, 0x4, 0x4, 0xe, 0xe, 0xe, 0x0] )
memory_symbol = pifacecad.LCDBitmap( [0xe, 0x1f, 0xe, 0x1f, 0xe, 0x1f, 0xe, 0x0] )

temp_symbol_index, memory_symbol_index, cpu_symbol_index, disk_symbol_index = 0, 1, 2, 3

def run_cmd(cmd):
return subprocess.check_output(cmd, shell=True).decode('utf-8')

def get_cpu_load():
return run_cmd(CPU_LOAD_CMD)[:-1]

def get_disk_usage():
return run_cmd(FREE_DISK_CMD)[:-1]

def get_my_ip():
return run_cmd(GET_IP_CMD)[:-1]

def get_hostname():
return run_cmd(HOSTNAME_CMD)[:-1]

def get_my_temp():
return run_cmd(GET_TEMP_CMD)[5:9]
Expand All @@ -48,12 +57,20 @@ def show_sysinfo():
while True:
cad.lcd.clear()
cad.lcd.write("IP:{}\n".format(get_my_ip()))
cad.lcd.write_custom_bitmap(cpu_symbol_index)
cad.lcd.write(":{} ".format(get_cpu_load()))
cad.lcd.write_custom_bitmap(memory_symbol_index)
cad.lcd.write(":{}".format(get_my_free_mem()))

sleep(UPDATE_INTERVAL)

cad.lcd.clear()
cad.lcd.write(format(get_hostname())+"\n")
cad.lcd.write_custom_bitmap(temp_symbol_index)
cad.lcd.write(":{}C ".format(get_my_temp()))
cad.lcd.write(":{}C ".format(get_my_temp()))
cad.lcd.write_custom_bitmap(disk_symbol_index)
cad.lcd.write(":{}".format(get_disk_usage()))

cad.lcd.write_custom_bitmap(memory_symbol_index)
cad.lcd.write(":{}".format(get_my_free_mem()))
sleep(UPDATE_INTERVAL)


Expand All @@ -69,7 +86,9 @@ def show_sysinfo():
else:
cad.lcd.store_custom_bitmap(temp_symbol_index, temperature_symbol)
cad.lcd.store_custom_bitmap(memory_symbol_index, memory_symbol)
cad.lcd.store_custom_bitmap(cpu_symbol_index, cpu_symbol)
cad.lcd.store_custom_bitmap(disk_symbol_index, disk_symbol)
cad.lcd.backlight_on()
cad.lcd.write("Waiting for IP..")
wait_for_ip()
show_sysinfo()
show_sysinfo()