#!/usr/bin/env python

#
# Edison LED blinker
#
# Copyright (c) 2014, Intel Corporation.
# Fabien Chereau <fabien.chereau@intel.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

# This is a very poor implementation as it uses subprocesses for init

import subprocess
import time
import argparse
import signal
import sys

initial_led_state = "high"

def write_led(value):
    with open("/sys/class/gpio/gpio40/direction","w") as lf:
        lf.write(value)

def blink_once():
    write_led("low")
    time.sleep(1./args.frequency*args.duty_cycle)
    write_led("high")
    time.sleep(1./args.frequency*(1.-args.duty_cycle))

def deinit_led_gpio():
    # Revert to default state with LED activated
    write_led(initial_led_state)

    # Deinit LED GPIO
    subprocess.call("""
    echo 40 >/sys/class/gpio/unexport
    echo 214 >/sys/class/gpio/unexport
    echo 243 >/sys/class/gpio/unexport
    echo 261 >/sys/class/gpio/unexport
    """, shell=True)

def signal_term_handler(signal, frame):
    print 'Signal intercepted: de-initing LED GPIOs'
    deinit_led_gpio()
    sys.exit(0)


parser = argparse.ArgumentParser(description='Blink the Edison Arduino board LED.')
parser.add_argument('--frequency', type=float, default=4, help='blink frequency in Hz')
parser.add_argument('--duration',  type=float, default=-1, help='duration of the blink in seconds. Negative value means no timeout, i.e. it will stop when the program is killed.')
parser.add_argument('--duty_cycle',  type=float, default=0.5, help='duty cycle between 0 and 1')

args = parser.parse_args()

# Allows to quit cleanly with CRTL+C or SIGTERM (systemd use SIGTERM to kill a service by default)
signal.signal(signal.SIGTERM, signal_term_handler)
signal.signal(signal.SIGINT, signal_term_handler)

# Init GPIO mux for LED control
subprocess.call("""
echo 40 >/sys/class/gpio/export
echo 214 >/sys/class/gpio/export
echo 243 >/sys/class/gpio/export
echo 261 >/sys/class/gpio/export
echo high >/sys/class/gpio/gpio214/direction
echo mode0 > /sys/kernel/debug/gpio_debug/gpio40/current_pinmux
echo low >/sys/class/gpio/gpio243/direction
echo high >/sys/class/gpio/gpio261/direction
echo low >/sys/class/gpio/gpio214/direction""", shell=True)

# Save current LED value for reverting to proper state at exit
try:
    lf =  open("/sys/class/gpio/gpio40/value","r")
    v = lf.read()
    lf.close()
    if v[0] == '0':
        initial_led_state = "low"
    else:
        initial_led_state = "high"
except:
    print "Can't get current LED state"

# Blink LED
if args.duration >= 0:
    for i in range(0, int(args.duration*args.frequency)):
        blink_once()
else:
    while True:
        blink_once()

deinit_led_gpio()

