Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time, threading
- import ASUS.GPIO as GPIO
- class GPIOA():
- PINS = [
- [252, False], #1
- [253, False], #2
- [17, False], #3
- [164, False], #4
- [166, False], #5
- [167, False], #6
- [257, False], #7
- [256, False], #8
- [254, False], #9
- [165, False], #10
- [168, False], #11
- [238, False], #12
- [185, False], #13
- [224, False], #14
- [161, False], #15
- [160, False], #16
- [184, False], #17
- [162, False], #18
- [163, False], #19
- [171, False], #20
- [255, False], #21
- [251, False], #22
- [239, False], #23
- [223, False], #24
- [187, False], #25
- [188, False]] #26
- def __init__(self):
- GPIO.setwarnings(False)
- GPIO.setmode(GPIO.ASUS)
- for id in range(len(self.PINS)):
- pin = self.PINS[id]
- GPIO.setup(pin[0], GPIO.OUT)
- GPIO.output(pin[0], GPIO.LOW)
- def on(self, id):
- pin = self.PINS[id]
- if not (pin[1]):
- GPIO.output(pin[0], GPIO.HIGH)
- return True
- return False
- def off(self, id):
- pin = self.PINS[id]
- if not (pin[1]):
- GPIO.output(pin[0], GPIO.LOW)
- return True
- return False
- def switch(self, id):
- if self.state(id): return self.off(id)
- return self.on(id)
- def mode(self, id, bol_input):
- try:
- if (bol_input): GPIO.setup(self.PINS[id][0], GPIO.IN)
- else: GPIO.setup(self.PINS[id][0], GPIO.OUT)
- self.PINS[id][1] = bol_input
- return True
- except:
- return False
- def modeSwitch(self, id):
- return self.mode(id, not self.PINS[id][1])
- def allOn(self):
- try:
- for i in range(len(self.PINS)): self.on(i)
- return True
- except:
- return False
- def allOff(self):
- try:
- for i in range(len(self.PINS)): self.off(i)
- return True
- except:
- return False
- def state(self, id):
- return GPIO.input(self.PINS[id][0])
- def isInput(self, id):
- return self.PINS[id][1]
- def cleanup(self):
- try:
- GPIO.cleanup()
- return True
- except:
- return False
- def millis():
- return int(round(time.time() * 1000))
- def log(message):
- print(message)
- def simulationDone(milli_start, milli_stop, milli_sensor1, milli_sensor2, milli_sensor3):
- log("Calculating test run...")
- values = {"start" : milli_start, "stop" : milli_stop, "sensor1" : milli_sensor1, "sensor2" : milli_sensor2, "sensor3" : milli_sensor3}
- log( str( values ) )
- class checkSimulation(threading.Thread):
- running = True
- def run(self):
- milli_start = millis()
- milli_sensor1 = 0
- milli_sensor2 = 0
- milli_sensor3 = 0
- while self.running:
- if not (GPIOH.state(3)):
- if (milli_sensor1 == 0):
- milli_sensor1 = millis()
- log("1")
- if not (GPIOH.state(4)):
- if (milli_sensor2 == 0):
- milli_sensor2 = millis()
- log("2")
- if not (GPIOH.state(5)):
- if (milli_sensor3 == 0):
- milli_sensor3 = millis()
- self.running = False
- log("3")
- milli_stop = millis()
- simulationDone(milli_start, milli_stop, milli_sensor1, milli_sensor2, milli_sensor3)
- GPIOH = GPIOA()
- message = ""
- if (GPIOH.mode(3, True)): log("OK 1")
- if (GPIOH.mode(4, True)): log("OK 2")
- if (GPIOH.mode(5, True)): log("OK 3")
- test = checkSimulation()
- while message != 'q':
- try:
- message = str( raw_input(" >> ") )
- cmd = message.split(" ")
- if (cmd[0] == "start"):
- if (cmd[1] == "1"):
- GPIOH.on(0)
- else:
- GPIOH.off(0)
- if (cmd[2] == "1"):
- GPIOH.on(1)
- else:
- GPIOH.off(1)
- if (cmd[3] == "1"):
- GPIOH.on(2)
- else:
- GPIOH.off(2)
- test = checkSimulation()
- test.start()
- elif (cmd[0] == "stop"):
- GPIOH.allOff()
- log("Turned all engines off!")
- test.running = False
- else:
- log("Unknown command!")
- except:
- log("Error while handling command!")
- GPIOH.allOff()
- test.running = False
- GPIOH.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement