Skip to content

Commit

Permalink
Add pioled as service
Browse files Browse the repository at this point in the history
  • Loading branch information
jetsonhacks committed Dec 1, 2019
1 parent 8281a6b commit 6404de7
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 77 deletions.
22 changes: 22 additions & 0 deletions createService.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# Install the pioled_stats service for the Adafruit PiOLED (3527)
# The driver for the display is a SSD1306
# for NVIDIA Jetson Nano Developer Kit, L4T
# Copyright (c) 2019 Jetsonhacks
# MIT License

python3 -m pip install --user --upgrade setuptools wheel
# Create a wheel installer for the stat program
sudo python3 setup.py sdist bdist_wheel
cd dist
sudo pip3 install pioled-1.0-py3-none-any.whl
# Create the service, and start 'er up
cd ../utils
python3 create_stats_service.py
sudo mv pioled_stats.service /etc/systemd/system/pioled_stats.service
sudo systemctl daemon-reload
sudo systemctl enable pioled_stats
sudo systemctl start pioled_stats



Empty file added pioled/__init__.py
Empty file.
Binary file added pioled/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added pioled/__pycache__/stats.cpython-36.pyc
Binary file not shown.
84 changes: 46 additions & 38 deletions apps/stats.py → pioled/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,49 @@

import time

import Adafruit_SSD1306
import Adafruit_SSD1306 # This is the driver chip for the Adafruit PiOLED

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# from jetbot.utils.utils import get_ip_address

import subprocess


def get_network_interface_state(interface):
return subprocess.check_output('cat /sys/class/net/%s/operstate' % interface, shell=True).decode('ascii')[:-1]


def get_ip_address(interface):
if get_network_interface_state(interface) == 'down':
return None
cmd = "ifconfig %s | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'" % interface
return subprocess.check_output(cmd, shell=True).decode('ascii')[:-1]

# Return a float representing the percentage of GPU being used.
# Return a string representing the percentage of CPU in use


def get_cpu_usage():
# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell=True)
return CPU

# Return a float representing the percentage of GPU in use.
# On the Jetson Nano, the GPU is GPU0


def get_gpu_usage():
GPU = 0.0
with open ("/sys/devices/gpu.0/load", encoding="utf-8") as gpu_file:
GPU=gpu_file.readline()
GPU=int(GPU)/10
with open("/sys/devices/gpu.0/load", encoding="utf-8") as gpu_file:
GPU = gpu_file.readline()
GPU = int(GPU)/10
return GPU


def get_network_interface_state(interface):
return subprocess.check_output('cat /sys/class/net/%s/operstate' % interface, shell=True).decode('ascii')[:-1]


# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=None, i2c_bus=1, gpio=1) # setting gpio to 1 is hack to avoid platform detection
# setting gpio to 1 is hack to avoid platform detection
disp = Adafruit_SSD1306.SSD1306_128_32(rst=None, i2c_bus=1, gpio=1)

# Initialize library.
disp.begin()
Expand All @@ -73,7 +85,7 @@ def get_network_interface_state(interface):
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.rectangle((0, 0, width, height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
Expand All @@ -86,50 +98,46 @@ def get_network_interface_state(interface):
# Load default font.
font = ImageFont.load_default()


while True:

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
draw.rectangle((0, 0, width, height), outline=0, fill=0)

# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell = True )
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell = True )
cmd = "free -m | awk 'NR==2{printf \"Mem: %.0f%% %s/%s M\", $3*100/$2, $3,$2 }'"
MemUsage = subprocess.check_output(cmd, shell=True)
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
Disk = subprocess.check_output(cmd, shell = True )
Disk = subprocess.check_output(cmd, shell=True)

# Print the IP address
# Two examples here, wired and wireless
draw.text((x, top), "eth0: " + str(get_ip_address('eth0')), font=font, fill=255)
# Print the IP address
# Two examples here, wired and wireless
draw.text((x, top), "eth0: " +
str(get_ip_address('eth0')), font=font, fill=255)
# draw.text((x, top+8), "wlan0: " + str(get_ip_address('wlan0')), font=font, fill=255)

# Draw the GPU usage as a bar graph
# Alternate solution: Draw the GPU usage as text
# draw.text((x, top+8), "GPU: " +"{:3.1f}".format(GPU)+" %", font=font, fill=255)
string_width, string_height=font.getsize("GPU: ")
# We draw the GPU usage as a bar graph
string_width, string_height = font.getsize("GPU: ")
# Figure out the width of the bar
full_bar_width=width-(x+string_width)-1
gpu_usage=get_gpu_usage()
full_bar_width = width-(x+string_width)-1
gpu_usage = get_gpu_usage()
# Avoid divide by zero ...
if gpu_usage == 0.0 :
gpu_usage=0.001
draw_bar_width=int(full_bar_width*(gpu_usage/100))
if gpu_usage == 0.0:
gpu_usage = 0.001
draw_bar_width = int(full_bar_width*(gpu_usage/100))
draw.text((x, top+8), "GPU: ", font=font, fill=255)
draw.rectangle((x+string_width,top+12,x+string_width+draw_bar_width,top+14), outline=1, fill=1)
draw.rectangle((x+string_width, top+12, x+string_width +
draw_bar_width, top+14), outline=1, fill=1)

# Show the memory Usage
# The MemUsage string is too long to display, we cut it down some here
mem_usage=MemUsage.decode('utf-8').split()
# slice off the last character (which is a % character)
mem_percent_use=float(mem_usage[2][:-1])
draw.text((x, top+16),mem_usage[0]+" "+"{:3.0f}".format(mem_percent_use)+"%"+" "+mem_usage[1], font=font, fill=255)
# draw.text((x, top+16), str(MemUsage.decode('utf-8')), font=font, fill=255)
draw.text((x, top+16), str(MemUsage.decode('utf-8')), font=font, fill=255)
# Show the amount of disk being used
draw.text((x, top+25), str(Disk.decode('utf-8')), font=font, fill=255)
draw.text((x, top+25), str(Disk.decode('utf-8')), font=font, fill=255)

# Display image.
# Set the SSD1306 image to the PIL image we have made, then dispaly
disp.image(image)
disp.display()
# Update 4x a second; 1 = 1 second
# Update 4x a second; 1.0 = 1 second
time.sleep(0.25)
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import glob
import subprocess
from setuptools import setup, find_packages, Extension

setup(
name='pioled',
version='1.0',
description='Adafruit PiOLED for the NVIDIA Jetson Nano',
packages=find_packages(),
install_requires=[
'Adafruit-SSD1306',
],
package_data={},
)
8 changes: 4 additions & 4 deletions utils/create_stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

STATS_SERVICE_TEMPLATE = """
[Unit]
Description=JetBot stats display service
Description=PiOLED stats display service
[Service]
Type=simple
User=%s
ExecStart=/bin/sh -c "python3 -m jetbot.apps.stats"
ExecStart=/bin/sh -c "python3 -m pioled.stats"
WorkingDirectory=%s
Restart=always
[Install]
WantedBy=multi-user.target
"""

STATS_SERVICE_NAME = 'jetbot_stats'
STATS_SERVICE_NAME = 'pioled_stats'


def get_stats_service():
Expand All @@ -26,7 +26,7 @@ def get_stats_service():

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--output', default='jetbot_stats.service')
parser.add_argument('--output', default='pioled_stats.service')
args = parser.parse_args()

with open(args.output, 'w') as f:
Expand Down
35 changes: 0 additions & 35 deletions utils/utils.py

This file was deleted.

0 comments on commit 6404de7

Please sign in to comment.