#!/usr/bin/env python

"""
Schaltet LEDs an oder aus

Kommandozeilen-Parameter:
  <LED: 1|2|3|4> <0|1>

"""

import sys
import RPi.GPIO as GPIO

# ----------------------- Konfigurationsparameter ---------------------------
LED = [22,23,24,25] # GPIOs fuer die LEDs
NumLED = len(LED)   # Anzahl aktiver LEDs
# ---------------------------------------------------------------------------

# get command line
if len(sys.argv) > 2:
  if sys.argv[1] != '':
    nled = int(sys.argv[1])
    onoff = int(sys.argv[2])
  else:
    print 'ERR: Kommandozeilenparameter: <LED: 1|2|3|4> <0|1>'
    exit(1)
else:
  print 'ERR: Kommandozeilenparameter: <LED: 1|2|3|4> <0|1>'
  exit(1)

try:
  GPIO.setmode(GPIO.BCM)
  GPIO.setwarnings(False)
  for i in LED:
    GPIO.setup(i, GPIO.OUT)

  if nled > 0 and nled <= NumLED and (onoff == 0 or onoff == 1):
    for i in range(0,NumLED):
      # Alten Zustand sichern
      GPIO.output(LED[i], GPIO.input(LED[i]))
    # neue LED setzen
    GPIO.output(LED[nled-1], onoff);
  else:
    print 'ERR: Kommandozeilenparameter: <LED: 1|2|3|4> <0|1>'
    exit(1)

except Exception, e1:
  print "ERR: GPIO LED " + str(e1)
  exit()

