def toggle(gpio, duration):
GPIO.output(gpio, True)
time.sleep(duration)
GPIO.output(gpio, False)
time.sleep(duration)
return
# Set according to status
python类output()的实例源码
def setstatus(status):
if status != "":
alloff()
if status == "SUCCESS":
GPIO.output(getcode('green'), True)
if status == "UNSTABLE":
GPIO.output(getcode('yellow'), True)
if status == "FAILURE":
GPIO.output(getcode('red'), True)
return
def led1(on_off):
GPIO.output(7, bool(on_off))
def led2(on_off):
GPIO.output(37, bool(on_off))
def led1(on_off):
GPIO.output(7, bool(on_off))
def led2(on_off):
GPIO.output(37, bool(on_off))
def switchOnLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, True)
def switchOffLight(PIN):
GPIO.setup(PIN, GPIO.OUT)
GPIO.output(PIN, False)
def beepOn():
global DisableBeep
if DisableBeep == False:
GPIO.output(BeepPin, GPIO.LOW)
def beepOff():
GPIO.output(BeepPin, GPIO.HIGH)
def ledOn():
GPIO.output(LedPin, GPIO.LOW)
def ledOff():
GPIO.output(LedPin, GPIO.HIGH)
# Event Listener
def setstate(roomNumber, accNumber, state):
if len(outPin[roomNumber]) != 0:
GPIO.output(outPin[roomNumber][accNumber], 1 - state)
#subprocess.call(['./echo.sh'], shell=True)
else:
#action for other rooms
subprocess.call(['./echo.sh'], shell=True)
return "0"
def toggle(roomNumber, accNumber):
if len(outPin[roomNumber]) != 0:
state= 1 - GPIO.input(outPin[roomNumber][accNumber])
GPIO.output(outPin[roomNumber][accNumber], state)
#subprocess.call(['./echo.sh'], shell=True)
else:
#action for other rooms
subprocess.call(['./echo.sh'], shell=True)
#print(roomNumber, accNumber)
buttonHtmlName = accName[roomNumber][accNumber].replace(" ", "<br>")
passer="<button class='%s' onclick='toggle(%d,%d)'>%s</button>" % (accState(roomNumber,accNumber), roomNumber, accNumber, buttonHtmlName)
return passer
def setio(self, io_number, state):
if self.importlib is not None:
GPIO.output(io_number, state)
def switchio(self, io_number):
if self.importlib is not None:
GPIO.output(io_number, not self.getio(io_number))
def reset_led(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
for gpio in self.gpios:
gpio = int(gpio)
GPIO.setup(gpio, GPIO.OUT)
GPIO.output(gpio, GPIO.HIGH)
def enable_tally(self, enable):
if enable:
GPIO.output(self.gpio_red, GPIO.LOW)
else:
GPIO.output(self.gpio_red, GPIO.HIGH)
def get_state(sleep_time=wheel_pulse):
# clear trigger sensor
gpio.output(trigger, False)
time.sleep(sleep_time)
# send trigger pulse
gpio.output(trigger, True)
time.sleep(0.00001)
gpio.output(trigger, False)
while gpio.input(echo) == 0:
pulse_start = time.time()
while gpio.input(echo) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 343 * 100 / 2. # speed of sound m/s * m to cm / round trip
if distance > 2 and distance < 400: # sensor range
distance = distance + distance_from_sensor_to_car_front
# don't worry about things further 4'
# this also reduces the size of the state machine
if distance >= max_distance:
distance = max_distance - 1
return int(distance)
##############################################################################
# perform action
##############################################################################
def forward(t=wheel_pulse):
gpio.output(forward_right, gpio.HIGH)
gpio.output(forward_left, gpio.HIGH)
sleep(t)
gpio.output(forward_right, gpio.LOW)
gpio.output(forward_left, gpio.LOW)