Skip to content

Commit

Permalink
Added button service
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-dob committed Sep 15, 2016
1 parent 2ecc621 commit 7399e7a
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 35 deletions.
70 changes: 54 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ Table of contents
* [Configuration](#configuration-of-raspberry-pi)
* [Installation](#installation)
* [Operation](#operation)
* [Button on the EPD board](#button-on-the-epd-board)
* [SW Architecture](#sw-architecture)
* [Input files formatting](#input-files-formatting)
* [Uninstalling and updating](#uninstalling-and-updating)

## Configuration of Raspberry Pi

In order to be able to install this software, the SPI communication has to be enabled. It is done by entering the configuration menu with command

~~~~~
$ sudo raspi-config

~~~~~
than navigating to Advanced Options and enabling SPI there. Afterwards the system has to be rebooted.


Expand All @@ -32,27 +33,26 @@ __NOTE__: EPD driver files are incuded in this repositury. However, this clone o

- delete the folder /gratis/ from directory ../ea-epd-sensor-values
- execute the following command in the directory ../ea-epd-sensor-values to clone the newest verion of driver repository


~~~~~
$ sudo git clone https://github.com/embeddedartists/gratis.git
~~~~~

- replace the file ../gratis/PlatformWithOS/driver-common/epd_v2.c with the version from the directory
../ea-epd-sensor-values/epd_v2.c
(user can also instead make respective changes in the original file epd_v2.c; compare the commented part in the modified file)

After running the script install_epd_driver.sh the directory ../gratis can be deleted. Furthermore python package ea_epd_sensor_values must be installed. It is done by executing

~~~~~
$ python setup.py install
~~~~~
in the directory ../ea-epd-sensor-values.



## Operation
In order to enable the use of EPD_json script from anywhere within the system, an executable script EPD_json from ea-epd-sensor-values/bin/ directory is automatically copied to root/bin directory. Executing the command

~~~~~
$ EPD_json [arg]
where arg is
Expand All @@ -63,20 +63,58 @@ In order to enable the use of EPD_json script from anywhere within the system, a
- bibaqr - displays BIBA QR code
- ip - displays the IP and MAC addresses for eth0 and wlan0 network connections
- without any argument the script clears the EPD
~~~~~
will display data or clear the EPD.

The EPD can also be operated via python script EPD_json.py from Bash shell with

~~~~~
$python path/to/EPD_json.py [arg]

~~~~~
or from another python script using the installed package ea_epd_sensor_values with

~~~~~
>>> import ea_epd_sensor_values
>>> ea_epd_sensor_values.main([ "arg" ])
~~~~~

## Button on the EPD board

The display board is equipped with one button which can be probed from RPi GPIO. You can install a service which will probe this button in an endless loop offering an input for a program even in a headless application.
The installed script will print MAC and IP address on the EPD at a press of the button, which is useful when working on the device remotely. The routine however is to be easily adjusted by the user.

**To change the service routine**
- Create copy of the python script EPD_button in ea_epd_sensor_values/bin/ directory.
- In the copy, make changes as indicated in the script file. This is the routine to be triggered by the service.
- Adjust the file epd_button in the directory ea_epd_sensor_values/daemon/. Only three, clearly indicated lines really need to be changed. This is the actual service script calling the python script
- Place the new python routine script in the directory /usr/local/bin/ and make it executable. From the script's directory use
~~~~~
$sudo cp <your_script> /usr/local/bin/
$sudo chmod 755 /usr/local/bin/<your_script>
~~~~~

- Substitute the old service with the new one and make it executable. From ea_epd_sensor_values/daemon/ use
~~~~~
$sudo cp epd_button /etc/init.d
$sudo chmod 755 /etc/init.d/epd_button
~~~~~
- Make appropriate updates in the service startup sequence
~~~~~
$sudo update-rc.d epd_button defaults
~~~~~

**To turn off/on/restart/test the service**

The service starts automatically at boot so to permanently disable it user has to clean up the start-up sequence andremove the service script epd_button.
~~~~~
$sudo update-rc.d epd_button remove
$sudo rm /etc/init.d/epd_button
~~~~~
To _start, stop, chceck status, restart, reload_ or _force-reload_ use
~~~~~
$sudo service epd_button <command>
~~~~~
**A more detailed and very comprehensible description of python services is presented here**
http://blog.scphillips.com/posts/2013/07/getting-a-python-script-to-run-in-the-background-as-a-service-on-boot/



## SW Architecture

The EPD_json.py loads data contained in a .json file and calls a subprogram EPD_display_values displaying N:(1-4) values with labels and information about communication status with respective sensor (in form of a warning sign if a problem occurs), on the EPD.
Expand All @@ -94,5 +132,5 @@ The .json files in the directory /json_dummies/ shall serve as templates for use

## Uninstalling and updating

In order to fully uninstall the EPD software first the device needs to be unmounted from the system. Afterwards the python package and the bash executable script has to be removed. All this is done by executing the script __remove_EPD_json.sh__.
To update the software source files need to be updated and again installed with the script __install_EPD_json.sh__.
In order to fully uninstall the EPD software first the device needs to be unmounted from the system. Afterwards the python package and the bash executable script has to be removed. At the end the button service must be removed. All this is done by executing the script __remove_EPD_json.sh__.
To update the software it first needs to uninstalled, source files need to be updated and again installed with the script __install_EPD_json.sh__.
14 changes: 0 additions & 14 deletions ea_epd_sensor_values/EPD_ip.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
# Copyright 2016 BIBA - Bremer Institut für Produktion und Logistik
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
Expand Down
4 changes: 3 additions & 1 deletion ea_epd_sensor_values/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from EPD_json import main
from EPD_json import main
from EPD import *
from EPD_ip import *
74 changes: 74 additions & 0 deletions ea_epd_sensor_values/bin/EPD_button
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#! /usr/bin/env python
# EPD_button : python script for probing the EPD button.
# More in /usr/local/lib/python2.7/dist-packages/ea_epd_sensor_values/README .

# To introduce your own reaction to pressing the button create use a copy of this file, change the marked
# service routine part and, when needed, the modules to be imported.

import logging
import logging.handlers
import argparse
import sys
import time # this is only being used as part of the example,import time
import RPi.GPIO as GPIO
from ea_epd_sensor_values import *

# Deafults
LOG_FILENAME = "/tmp/epd_button.log"
LOG_LEVEL = logging.INFO # Could be e.g. "DEBUG" or "WARNING"

# Define and parse command line arguments
parser = argparse.ArgumentParser(description="Python service for probing the EPD button.")
parser.add_argument("-l", "--log", help="file to write log to (default '" + LOG_FILENAME + "')")

# If the log file is specified on the command line then override the default
args = parser.parse_args()
if args.log:
LOG_FILENAME = args.log

# Configure logging to log to a file, making a new file at midnight and keeping the last 3 day's data
# Give the logger a unique name (good practice)
logger = logging.getLogger(__name__)
# Set the log level to LOG_LEVEL
logger.setLevel(LOG_LEVEL)
# Make a handler that writes to a file, making a new file at midnight and keeping 3 backups
handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when="midnight", backupCount=3)
# Format each log message like this
formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s')
# Attach the formatter to the handler
handler.setFormatter(formatter)
# Attach the handler to the logger
logger.addHandler(handler)

# Make a class we can use to capture stdout and sterr in the log
class MyLogger(object):
def __init__(self, logger, level):
"""Needs a logger and a logger level."""
self.logger = logger
self.level = level

def write(self, message):
# Only log if there is a message (not just a new line)
if message.rstrip() != "":
self.logger.log(self.level, message.rstrip())

# Replace stdout with logging to file at INFO level
sys.stdout = MyLogger(logger, logging.INFO)
# Replace stderr with logging to file at ERROR level
sys.stderr = MyLogger(logger, logging.ERROR)

######################################
# Your service routine below #
######################################

GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN)
logger.info("Pins initialized.")

epd = EPD()
logger.info("EPD initialized. Probing starts.")
while True:
if ( not GPIO.input(15)):
EPD_ip(epd)
time.sleep(1)

66 changes: 66 additions & 0 deletions ea_epd_sensor_values/daemon/epd_button
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: epd_button
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Should-Start: $portmap
# Should-Stop: $portmap
# X-Start-Before: nis
# X-Stop-After: nis
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Enable printing IP and MAC at the EPD with a press of the button of EPD.
# Description: Execute a python script to probe the EPD button in an infinite loop.
### END INIT INFO

# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin/
DAEMON=$DIR/EPD_button
DAEMON_NAME=epd_button

# Add any command line options for your daemon here
DAEMON_OPTS=""

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
log_daemon_msg "Starting system $DAEMON_NAME daemon"
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON -- $DAEMON_OPTS
log_end_msg $?
}
do_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME daemon"
start-stop-daemon --stop --pidfile $PIDFILE --retry 10
log_end_msg $?
}

case "$1" in

start|stop)
do_${1}
;;

restart|reload|force-reload)
do_stop
do_start
;;

status)
status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
;;

*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
exit 1
;;

esac
exit 0
1 change: 1 addition & 0 deletions install_EPD_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#
sudo bash ./install_epd_driver.sh
sudo python setup.py install
sudo bash ./install_button_service.sh
5 changes: 5 additions & 0 deletions install_button_service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sudo cp ea_epd_sensor_values/bin/EPD_button /usr/local/bin/
sudo chmod 755 /usr/local/bin/EPD_button
sudo cp ea_epd_sensor_values/daemon/epd_button /etc/init.d
sudo chmod 755 /etc/init.d/epd_button
sudo update-rc.d epd_button defaults
4 changes: 3 additions & 1 deletion remove_EDP_json.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
cd gratis/PlatformWithOS
sudo COG_VERSION=V2 make rpi-remove
sudo rm -rf /usr/local/lib/python2.7/dist-packages/ea_epd_sensor_values*
sudo rm -rf /usr/local/bin/EPD_json
sudo rm -rf /usr/local/bin/EPD_*
sudo update-rc.d epd_button remove
sudo rm /etc/init.d/epd_button
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from setuptools import setup

setup(name='ea_epd_sensor_values',
version='1.0',
version='1.01',
description='Display .json file on Embedded Artists 2.7" EPD',
url='https://gitlab.ips.biba.uni-bremen.de/iotfablab/ea-epd-sensor-values',
author='Mikolaj Dobielewski',
author_email='[email protected]',
license='MIT',
license='Apache License, Version 2.0',
packages=['ea_epd_sensor_values'],
zip_safe=False,
include_package_data=True,
install_requires=['Pillow'],
scripts=['ea_epd_sensor_values/bin/EPD_json']
scripts=['ea_epd_sensor_values/bin/EPD_json', 'ea_epd_sensor_values/bin/EPD_button']
)

0 comments on commit 7399e7a

Please sign in to comment.