import RPi.GPIO as GPIO
from time import sleep 
from multiprocessing import Process
import os

class Breathe(object):

    def __init__(self):
        GPIO.setmode(GPIO.BCM) # We can also choose a BOARD numbering schemes.
        GPIO.setup(21, GPIO.OUT) # set GPIO 21 as output
        self.light = GPIO.PWM(21, 100) # create object for PWM on port 21 at 100 Hertz
        self.p = Process(target=calm, args=(self.light,))

    def shutdown(self):
        if (self.p.is_alive()):
            self.p.terminate()
        self.light.stop()
        GPIO.cleanup()
        print("breathing shut down.")
        
    def restart(self):
        print("restarting breathing!")
        if (self.p.is_alive()):
            self.p.terminate()
        self.light.stop()
        GPIO.cleanup()
        
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(21, GPIO.OUT)
        self.light = GPIO.PWM(21, 100)
        self.light.start(0)
        self.p = Process(target=calm, args=(self.light,))
        self.p.start()

    def calm(self):
        if (self.p.is_alive()):
            self.p.terminate()
        self.light.stop()
        self.p = Process(target=calm, args=(self.light,))
        self.p.start()
        
    def erratic(self):
        if (self.p.is_alive()):
            self.p.terminate()
        self.light.stop()
        self.p = Process(target=erratic, args=(self.light,))
        self.p.start()
        
def calm(light):
    print("starting calm breathing...")
    light.start(0)
    pause_time = 0.02
    try:
        while True:
            for i in range(0,101):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(100,-1,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
    finally:
        print("stopping calm breathing")
        light.stop()
        GPIO.cleanup()

def erratic(light):
    print("starting erratic breathing...")
    light.start(0)
    pause_time = 0.007

    try:
        while True:
            for i in range(0,75):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(75,15,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(15,45):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(45,25,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(25,70):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(70,10,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(10,30):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(30,5,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(5,75):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(75,10,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(10,50):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(50,25,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(25,100):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(100,0,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(0,15):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(15,0,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(0,35):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
            for i in range(35,0,-1):
                light.ChangeDutyCycle(i)
                sleep(pause_time)
    finally:
        print("stopping erratic breathing")
        light.stop()
        GPIO.cleanup()

@@ -416,16 +416,124 @@
 light,))
+%0A        self.state = breathe_state(('CALM', 'ERRATIC', 'OFF'))%0A        self.restart_state = self.state.CALM
 %0A%0A    de
@@ -1019,24 +1019,80 @@
 ht.start(0)%0A
+        if (self.restart_state == self.state.CALM):%0A    
         self
@@ -1137,32 +1137,36 @@
 ight,))%0A        
+    
 self.p.start()%0A%0A
@@ -1163,16 +1163,207 @@
 .start()
+%0A        elif (self.restart_state == self.state.ERRATIC):%0A            self.p = Process(target=erratic, args=(self.light,))%0A            self.p.start()%0A        else:%0A            self.shutdown()
 %0A%0A    de
@@ -1720,32 +1720,32 @@
 =(self.light,))%0A
-
         self.p.s
@@ -1742,32 +1742,335 @@
   self.p.start()
+%0A%0A    def set(self, state):%0A        self.restart_state = state%0A        print(%22state has been set:%22, self.restart_state)%0A%0Aclass breathe_state(object): %0A    def __init__(self, tupleList):%0A        self.tupleList = tupleList%0A%0A    def __getattr__(self, name):%0A        return self.tupleList.index(name)       
 %0A        %0Adef ca

Add CALM,ERRATIC, and OFF breathe state to breather