#!/usr/bin/env python
#
# Raspberry Pi 4-Chanel-relay-board control using push buttons via RPi.GPIO
#

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM) # GPIO Numbers instead of board numbers

# oooooooooooooooooooo
# oooooooooooooooxxxxo
RELAY_1_GPIO = 6
RELAY_2_GPIO = 13
RELAY_3_GPIO = 19
RELAY_4_GPIO = 26

GPIO.setup(RELAY_1_GPIO, GPIO.OUT)   # GPIO Assign OUTPUT mode
GPIO.output(RELAY_1_GPIO, GPIO.HIGH) # Relay OFF
GPIO.setup(RELAY_2_GPIO, GPIO.OUT)   # GPIO Assign OUTPUT mode
GPIO.output(RELAY_2_GPIO, GPIO.HIGH) # Relay OFF
GPIO.setup(RELAY_3_GPIO, GPIO.OUT)   # GPIO Assign OUTPUT mode
GPIO.output(RELAY_3_GPIO, GPIO.HIGH) # Relay OFF
GPIO.setup(RELAY_4_GPIO, GPIO.OUT)   # GPIO Assign OUTPUT mode
GPIO.output(RELAY_4_GPIO, GPIO.HIGH) # Relay OFF

# oooooooooooooooxoxxx
# oooooooooooooooooooo
SWITCH_1_GPIO = 12
SWITCH_2_GPIO = 16
SWITCH_3_GPIO = 20
SWITCH_4_GPIO = 21

GPIO.setup(SWITCH_1_GPIO, GPIO.IN, GPIO.PUD_UP)   # GPIO Assign INPUT mode and pull up resistor (HIGH by defaultand LOW when the button is pressed)
GPIO.setup(SWITCH_2_GPIO, GPIO.IN, GPIO.PUD_UP)   # GPIO Assign INPUT mode and pull up resistor (HIGH by defaultand LOW when the button is pressed)
GPIO.setup(SWITCH_3_GPIO, GPIO.IN, GPIO.PUD_UP)   # GPIO Assign INPUT mode and pull up resistor (HIGH by defaultand LOW when the button is pressed)
GPIO.setup(SWITCH_4_GPIO, GPIO.IN, GPIO.PUD_UP)   # GPIO Assign INPUT mode and pull up resistor (HIGH by defaultand LOW when the button is pressed)


# Controlling the 4 relays with 4 buttons
try:													# main loop
	while True:
	# check if one of the switches is pressed and keep checking
		while GPIO.input(SWITCH_1_GPIO) == GPIO.HIGH and GPIO.input(SWITCH_2_GPIO) == GPIO.HIGH and GPIO.input(SWITCH_3_GPIO) == GPIO.HIGH and GPIO.input(SWITCH_4_GPIO) == GPIO.HIGH:
#			print "waiting for switch press"
			time.sleep(0.5)

	# checks current relay state and changes it to the other.
		else:
			if GPIO.input(SWITCH_1_GPIO) == GPIO.LOW:		# If Switch nb 1 is pressed
				if GPIO.input(RELAY_1_GPIO) == GPIO.HIGH:	# If Relay nb 1 is switched OFF
#					print "Relay nb.1 ON"
					GPIO.output(RELAY_1_GPIO, GPIO.LOW)		# then switch the Relay nb 1 ON
					time.sleep(1)
				else:										# If Relay nb 1 is switched ON
#-------------------------------------------------------------
#					print "Keep pressed during 5s to confirm"
#					GPIO.wait_for_edge(SWITCH_1_GPIO, GPIO.FALLING)	# wait for the button pressed
#					print "Pressed"
#					start = time.time()
#					time.sleep(0.2)							# wait 200ms as a switch debounce
#					while GPIO.input(SWITCH_1_GPIO) == GPIO.LOW:
#						time.sleep(0.01)					# wait in a loop until the button is released
#					duration = time.time() - start			# calculate the time the button was pressed
#					print(duration)
#					if duration > 5:						# must be pressed 5s to confirm the switching-off
#						print "Relay nb.1 OFF"
#						GPIO.output(RELAY_1_GPIO, GPIO.HIGH)# then switch the Relay nb 1 OFF
#-------------------------------------------------------------
#					print "Relay nb.1 OFF"
					GPIO.output(RELAY_1_GPIO, GPIO.HIGH)# then switch the Relay nb 1 OFF
					time.sleep(1)

			if GPIO.input(SWITCH_2_GPIO) == GPIO.LOW:		# If Switch nb 2 is pressed
				if GPIO.input(RELAY_2_GPIO) == GPIO.HIGH:	# If Relay nb 2 is switched OFF
#					print "Relay nb.2 ON"
					GPIO.output(RELAY_2_GPIO, GPIO.LOW)
					time.sleep(1)
				else:										# If Relay nb 2 is switched ON
#					print "Relay nb.2 OFF"
					GPIO.output(RELAY_2_GPIO, GPIO.HIGH)
					time.sleep(1)

			if GPIO.input(SWITCH_3_GPIO) == GPIO.LOW:		# If Switch nb 3 is pressed
				if GPIO.input(RELAY_3_GPIO) == GPIO.HIGH:	# If Relay nb 3 is switched OFF
#					print "Relay nb.3 ON"
					GPIO.output(RELAY_3_GPIO, GPIO.LOW)
					time.sleep(1)
				else:										# If Relay nb 3 is switched ON
#					print "Relay nb.3 OFF"
					GPIO.output(RELAY_3_GPIO, GPIO.HIGH)
					time.sleep(1)

			if GPIO.input(SWITCH_4_GPIO) == GPIO.LOW:		# If Switch nb 4 is pressed
				if GPIO.input(RELAY_4_GPIO) == GPIO.HIGH:	# If Relay nb 4 is switched OFF
#					print "Relay nb.4 ON"
					GPIO.output(RELAY_4_GPIO, GPIO.LOW)
					time.sleep(1)
				else:										# If Relay nb 4 is switched ON
#					print "Relay nb.4 OFF"
					GPIO.output(RELAY_4_GPIO, GPIO.HIGH)
					time.sleep(1)

	# check for switch released and keep checking
		while GPIO.input(SWITCH_1_GPIO) == GPIO.LOW or GPIO.input(SWITCH_2_GPIO) == GPIO.LOW or GPIO.input(SWITCH_3_GPIO) == GPIO.LOW or GPIO.input(SWITCH_4_GPIO) == GPIO.LOW:
#			print "waiting for switch release"
			time.sleep(0.5)

except KeyboardInterrupt:
	print("Quit")
	GPIO.cleanup()
	exit()

#-------------- Liens interessants
# https://www.framboise314.fr/une-interface-web-simple-pour-piloter-des-led-avec-html5-et-python-sur-le-raspberry-pi/
# Rendre le script executable: sudo chmod 777 /home/pi/script/Relay_board_control.py
# Tester le programme ./Relay_board_control.py
# sudo cp Relay_board_control.py /usr/local/bin
# Execution du script python au demarrage: sudo nano /etc/rc.local
# placer le nom du programme avec son chemin : /home/pi/scripts/Relay_board_control-2.py & (entre fi et exit 0)

# https://www.raspberrypi.org/forums/viewtopic.php?t=171987#p1101859
# http://invent.module143.com/daskal_tutorial/raspberry-pi-3-gpio-controlling-a-relay/
# https://www.instructables.com/id/Multi-Task-Arduino-UNO/
# http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html
# https://www.raspberrypi.org/forums/viewtopic.php?t=126227

"""
https://blog-du-grouik.tinad.fr/post/2015/10/05/Octopi-Controler-l-%C3%A9clairage-12-V-de-l-imprimante
Pour qu’Octopi mette la pin en ecriture à son demarrage, 
ajoutez dans le script /etc/init.d/octoprint 
dans le bloc do_start() juste apres do_start() RETVAL="$?
gpio export 6 out
gpio -g write 6 1
gpio export 13 out
gpio -g write 13 1
gpio export 19 out
gpio -g write 19 1
gpio export 26 out
gpio -g write 126 1

Pour ajouter les commandes sur l’interface d’Octopi, editez le fichier /home/pi/.octoprint/config.yaml et ajoutez

  - action: light ON
    command: gpio write 1 1
    name: light ON
  - action: light OFF
    command: gpio write 1 0
    name: light OFF
dans le bloc system.
"""