from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.config import Config
import RPi.GPIO as GPIO
from Hole import *
from Kicker import *
from Switch import *
from Bumper import *
from Scrubmode import *
from Noobgate import *
from time import sleep
from MCP23017 import MCP23017
import numpy
#from ScoreChanger import *

GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering

#MCP I2C adresses
CHIPONE = 0x20
CHIPTWO = 0x21
CHIPTHREE = 0x22

#relais are turned off at a high signal
HIGH = 0
LOW = 1


print "Creating Switches"

# MCP23017 1

GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDEN = Switch("GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDEN", CHIPONE, 0)
print "GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDEN"
GPA1_R_OND6_S9_2_RUBBERBAND_KICKER_RECHTS_BOVEN_BOVENSTE = Switch("GPA1_R_OND6_S9_2_RUBBERBAND_KICKER_RECHTS_BOVEN_BOVENSTE", CHIPONE, 1)
print "GPA1_R_OND6_S9_2_RUBBERBAND_KICKER_RECHTS_BOVEN_BOVENSTE"
GPA2_R_OND7_S12_2_RUBBERBAND_KICKER_RECHTS_MIDDEN = Switch("GPA2_R_OND7_S12_2_RUBBERBAND_KICKER_RECHTS_MIDDEN", CHIPONE, 2)
print GPA2_R_OND7_S12_2_RUBBERBAND_KICKER_RECHTS_MIDDEN
GPA3_R_OND8_S13_PADDRAAD_LINKS_BOVEN = Switch("GPA3_R_OND8_S13_PADDRAAD_LINKS_BOVEN", CHIPONE, 3)
print "GPA3_R_OND8_S13_PADDRAAD_LINKS_BOVEN"
GPA4_R_OND5_TILT_SENSOR = Switch("GPA4_R_OND5_TILT_SENSOR", CHIPONE, 4)
print "GPA4_R_OND5_TILT_SENSOR"
GPA5_LEEG = 5
print "GPA5_LEEG"
GPA6_LEEG = 6
print "GPA6_LEEG"
GPA7_LEEG = 7
print "GPA7_LEEG"

GPB0_LEEG = 8
print "GPB0_LEEG"
GPB1_LEEG = 9
print "GPB1_LEEG"
GPB2_LEEG = 10
print "GPB2_LEEG"
GPB3_R_BVN9_S100_SWITCH_WIT_BOVENAAN = Switch("GPB3_R_BVN9_S100_SWITCH_WIT_BOVENAAN", CHIPONE, 11)
print "GPB3_R_BVN9_S100_SWITCH_WIT_BOVENAAN"
GPB4_R_BVN8_S8_PADDRAAD_RECHTS_BOVEN = Switch("GPB4_R_BVN8_S8_PADDRAAD_RECHTS_BOVEN", CHIPONE, 12)
print "GPB4_R_BVN8_S8_PADDRAAD_RECHTS_BOVEN"
GPB5_R_BVN7_S21_RUBBERBAND_LINKS_BOVEN_BOVENSTE = Switch("GPB5_R_BVN7_S21_RUBBERBAND_LINKS_BOVEN_BOVENSTE", CHIPONE, 13)
print "GPB5_R_BVN7_S21_RUBBERBAND_LINKS_BOVEN_BOVENSTE"
GPB6_R_BVN6_S9_1_RUBBERBAND_RECHTS_BOVEN_ONDERSTE = Switch("GPB6_R_BVN6_S9_1_RUBBERBAND_RECHTS_BOVEN_ONDERSTE", CHIPONE, 14)
print "GPB6_R_BVN6_S9_1_RUBBERBAND_RECHTS_BOVEN_ONDERSTE"
GPB7_R_BVN5_S16_RUBBERBAND_LINKS_MIDDEN = Switch("GPB7_R_BVN5_S16_RUBBERBAND_LINKS_MIDDEN", CHIPONE, 15)
print "GPB7_R_BVN5_S16_RUBBERBAND_LINKS_MIDDEN"

print ("Chip One Done")



# MCP23017 2

GPA0_R_OND1_S26_PADDRAAD_RECHTS_ONDER = Switch("GPA0_R_OND1_S26_PADDRAAD_RECHTS_ONDER", CHIPTWO, 0)
print "GPA0_R_OND1_S26_PADDRAAD_RECHTS_ONDER"
GPA1_R_OND2_S24_PADDRAAD_START_BALSCHIETEN = Switch("GPA1_R_OND2_S24_PADDRAAD_START_BALSCHIETEN", CHIPTWO, 1)
print "GPA1_R_OND2_S24_PADDRAAD_START_BALSCHIETEN"
GPA2_R_OND3_S10_WHEEL_MIDDLE = Switch("GPA2_R_OND3_S10_WHEEL_MIDDLE", CHIPTWO, 2)
print "GPA2_R_OND3_S10_WHEEL_MIDDLE"
GPA3_R_OND4_S2_WHEEL_RIGHT = Switch("GPA3_R_OND4_S2_WHEEL_RIGHT", CHIPTWO, 3)
print "GPA3_R_OND4_S2_WHEEL_RIGHT"
GPA4_L_OND1_S17_BUMPER_METAAL_LINKS_BENEDEN = Switch("GPA4_L_OND1_S17_BUMPER_METAAL_LINKS_BENEDEN", CHIPTWO, 4)
print "GPA4_L_OND1_S17_BUMPER_METAAL_LINKS_BENEDEN"
GPA5_L_OND2_S5_BUMPER_METAAL_RECHTS_BOVEN = Switch("GPA5_L_OND2_S5_BUMPER_METAAL_RECHTS_BOVEN", CHIPTWO, 5)
print "GPA5_L_OND2_S5_BUMPER_METAAL_RECHTS_BOVEN"
GPA6_L_OND3_S6_BUMPER_METAAL_RECHTS_BENEDEN = Switch("GPA6_L_OND3_S6_BUMPER_METAAL_RECHTS_BENEDEN", CHIPTWO, 6)
print "GPA6_L_OND3_S6_BUMPER_METAAL_RECHTS_BENEDEN"
GPA7_L_OND4_S15_RUBBERBAND_KICKER_RECHTS_MIDDEN = Switch("GPA7_L_OND4_S15_RUBBERBAND_KICKER_RECHTS_MIDDEN", CHIPTWO, 7)
print "GPA7_L_OND4_S15_RUBBERBAND_KICKER_RECHTS_MIDDEN"


GPB0_L_BVN4_S15_BUMPER_METAAL_LINKS_BOVEN = Switch("GPB0_L_BVN4_S15_BUMPER_METAAL_LINKS_BOVEN", CHIPTWO, 8)
print "GPB0_L_BVN4_S15_BUMPER_METAAL_LINKS_BOVEN"
GPB1_L_BVN3_S6_BUMPER_PLASTIC_RECHTS_BENEDEN = Switch("GPB1_L_BVN3_S6_BUMPER_PLASTIC_RECHTS_BENEDEN", CHIPTWO, 9)
print "GPB1_L_BVN3_S6_BUMPER_PLASTIC_RECHTS_BENEDEN"
GPB2_L_BVN2_S5_BUMPER_PLASTIC_RECHTS_BOVEN = Switch("GPB2_L_BVN2_S5_BUMPER_PLASTIC_RECHTS_BOVEN", CHIPTWO, 10)
print "GPB2_L_BVN2_S5_BUMPER_PLASTIC_RECHTS_BOVEN"
GPB3_L_BVN1_S17_BUMPER_PLASTIC_LINKS_BENEDEN = Switch("GPB3_L_BVN1_S17_BUMPER_PLASTIC_LINKS_BENEDEN", CHIPTWO, 11)
print "GPB3_L_BVN1_S17_BUMPER_PLASTIC_LINKS_BENEDEN"
GPB4_R_BVN4_S17_WHEEL_LEFT = Switch("GPB4_R_BVN4_S17_WHEEL_LEFT", CHIPTWO, 12)
print "GPB4_R_BVN4_S17_WHEEL_LEFT"
GPB5_R_BVN3_S101_RODE_KNOP_SCROBMODE = Switch("GPB5_R_BVN3_S101_RODE_KNOP_SCROBMODE", CHIPTWO, 13)
print "GPB5_R_BVN3_S101_RODE_KNOP_SCROBMODE"
GPB6_R_BVN2_S23_PADDRAAD_BALLPUSHER = Switch("GPB6_R_BVN2_S23_PADDRAAD_BALLPUSHER", CHIPTWO, 14)
print "GPB6_R_BVN2_S23_PADDRAAD_BALLPUSHER"
GPB7_R_BVN1_S21_PADDRAAD_LINKS_BENEDEN = Switch("GPB7_R_BVN1_S21_PADDRAAD_LINKS_BENEDEN", CHIPTWO, 15)
print "GPB7_R_BVN1_S21_PADDRAAD_LINKS_BENEDEN"

print ("Chip Two Done")


# MCP23017 3

GPA0_LEEG = 0
print "GPA0_LEEG"
GPA1_LEEG = 1
print "GPA1_LEEG"
GPA2_L_OND5_S25_BUMPER_PLASTIC_RECHTS_BENEDEN_BOVENSTE = Switch("GPA2_L_OND5_S25_BUMPER_PLASTIC_RECHTS_BENEDEN_BOVENSTE", CHIPTHREE, 2)
print "GPA2_L_OND5_S25_BUMPER_PLASTIC_RECHTS_BENEDEN_BOVENSTE"
GPA3_L_OND6_S25_NUMPER_PLASTIC_RECHTS_BENEDEN_ONDERSTE = Switch("GPA3_L_OND6_S25_NUMPER_PLASTIC_RECHTS_BENEDEN_ONDERSTE", CHIPTHREE, 3)
print "GPA3_L_OND6_S25_NUMPER_PLASTIC_RECHTS_BENEDEN_ONDERSTE"
GPA4_L_OND7_S103_BUMPER_PLASTIC_RECHTS_BENEDEN_SOLENOID = Switch("GPA4_L_OND7_S103_BUMPER_PLASTIC_RECHTS_BENEDEN_SOLENOID", CHIPTHREE, 4)
print "GPA4_L_OND7_S103_BUMPER_PLASTIC_RECHTS_BENEDEN_SOLENOID"
GPA5_L_OND8_S14_GELE_TARGET_LINKS_BOVEN = Switch("GPA5_L_OND8_S14_GELE_TARGET_LINKS_BOVEN", CHIPTHREE, 5)
print "GPA5_L_OND8_S14_GELE_TARGET_LINKS_BOVEN"
GPA6_L_OND9_S18_GELE_TARGET_LINKS_MIDDEN_BOVENSTE = Switch("GPA6_L_OND9_S18_GELE_TARGET_LINKS_MIDDEN_BOVENSTE", CHIPTHREE, 6)
print "GPA6_L_OND9_S18_GELE_TARGET_LINKS_MIDDEN_BOVENSTE"
GPA7_L_OND10_S20_GELE_TARGET_LINKS_BENEDEN = Switch("GPA7_L_OND10_S20_GELE_TARGET_LINKS_BENEDEN", CHIPTHREE, 7)
print "GPA7_L_OND10_S20_GELE_TARGET_LINKS_BENEDEN"


GPB0_L_BVN10_S1_GELE_TARGET_RECHTS_BENEDEN = Switch("GPB0_L_BVN10_S1_GELE_TARGET_RECHTS_BENEDEN", CHIPTHREE, 8)
print "GPB0_L_BVN10_S1_GELE_TARGET_RECHTS_BENEDEN"
GPB1_L_BVN9_S3_GELE_TARGET_RECHTS_MIDDEN = Switch("GPB1_L_BVN9_S3_GELE_TARGET_RECHTS_MIDDEN", CHIPTHREE, 9)
print "GPB1_L_BVN9_S3_GELE_TARGET_RECHTS_MIDDEN"
GPB2_L_BVN8_S7_GELE_TARGET_RECHTS_BOVEN = Switch("GPB2_L_BVN8_S7_GELE_TARGET_RECHTS_BOVEN", CHIPTHREE, 10)
print "GPB2_L_BVN8_S7_GELE_TARGET_RECHTS_BOVEN"
GPB3_L_BVN7_S105_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE_SOLENOID = Switch("GPB3_L_BVN7_S105_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE_SOLENOID", CHIPTHREE, 11)
print "GPB3_L_BVN7_S105_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE_SOLENOID"
GPB4_L_BVN6_S104_BUMPER_PLASTIC_LINKS_BENEDEN_ONDERSTE = Switch("GPB4_L_BVN6_S104_BUMPER_PLASTIC_LINKS_BENEDEN_ONDERSTE", CHIPTHREE, 12)
print "GPB4_L_BVN6_S104_BUMPER_PLASTIC_LINKS_BENEDEN_ONDERSTE"
GPB5_L_BVN5_S22_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE = Switch("GPB5_L_BVN5_S22_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE", CHIPTHREE, 13)
print "GPB5_L_BVN5_S22_BUMPER_PLASTIC_LINKS_BENEDEN_BOVENSTE"
GPB6_LEEG = 14
print "GPB6_LEEG"
GPB7_LEEG = 15
print "GPB7_LEEG"

print ("Chip Three Done")


   
if __name__ == '__main__':
    #MCP_ONE = [GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDEN, GPA1_R_OND6_S9_2_RUBBERBAND_KICKER_RECHTS_BOVEN_BOVENSTE, GPA2_R_OND7_S12_2_RUBBERBAND_KICKER_RECHTS_MIDDEN, GPA4_R_OND5_TILT_SENSOR, GPB3_R_BVN9_S100_SWITCH_WIT_BOVENAAN, GPB4_R_BVN8_S8_PADDRAAD_RECHTS_BOVEN, GPB5_R_BVN7_S21_RUBBERBAND_LINKS_BOVEN_BOVENSTE, GPB6_R_BVN6_S9_1_RUBBERBAND_RECHTS_BOVEN_ONDERSTE, GPB7_R_BVN5_S16_RUBBERBAND_LINKS_MIDDEN]
    print("Here we go! Press CTRL+C to exit")

    if GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDENself.mcp.input() == 1:        
        print ("BALL PUSHER IS AAN")
    else:
        print ("BALL PUSHER is NIET AAN")

    #if GPA0_R_OND5_S4_RUBBERBAND_KICKER_RECHTS_MIDDENself == 1:
    #    print ("BALL PUSHER IS AAN")

    #i = 0
    #for index, MCP_ONE in range(len(MCP_ONE)):
    #    if MCP_ONE.index(0:len(MCP_ONE)) == HIGH:
    #       print(MCP_ONE.index(index)
        
