def loop(): old_first = False old_left = False old_right = False old_last = False index = 0 while True: # POLL BUTTONS # remember they are inverted first = not GPIO.input(BUTTON_FIRST) left = not GPIO.input(BUTTON_LEFT) right = not GPIO.input(BUTTON_RIGHT) last = not GPIO.input(BUTTON_LAST) # REPORT ANY CHANGED BUTTONS if first != old_first: print("FIRST=" + str(first)) old_first = first if left != old_left: print("LEFT=" + str(left)) old_left = left if right != old_right: print("RIGHT=" + str(right)) old_right = right if last != old_last: print("LAST=" + str(last)) old_last = last # PROCESS HELD BUTTONS IN PRIORITY ORDER if first: index = 0 elif last: index = len(LED_GPIO) - 1 elif left: if index > 0: index -= 1 elif right: if index < len(LED_GPIO) - 1: index += 1 # FLASH PRESENTLY SELECTED LED GPIO.output(LED_GPIO[index], True) time.sleep(FLASH_TIME / 2) GPIO.output(LED_GPIO[index], False) time.sleep(FLASH_TIME / 2)
def checkstatus(): # call f() again in pollTime seconds threading.Timer(increment, checkstatus).start() global pollcount global closed global writeentry # input_state = GPIO.input(17) if not GPIO.input(17): # Bathroom is closed updatehtml("Bathroom", 1) pollcount += increment closed = datetime.now() writeentry = True else: # Bathroom is open updatehtml("Bathroom", 0) if writeentry: writeentry = False contime = sqlite3.connect("PathtoDB") curtime = contime.cursor() curtime.execute( "INSERT INTO '" + datetime.now().strftime("%m/%d/%Y") + "_time_log' (in_time, out_time) VALUES " "('" + (closed + timedelta(0, pollcount * -1)).strftime("%H:%M:%S") + "','" + closed.strftime("%H:%M:%S") + "')") contime.commit() contime.close() pollcount = 0
def pinread(): while True: time.sleep(TIME) b = GPIO.input(BUTTON) if not b: print("pressed") else: print("released")
def distance(self,a=1): GPIO.output(self.TRIG, False) GPIO.output(self.TRIG, True) sleep(0.00001) GPIO.output(self.TRIG, False) while GPIO.input(self.ECHO)==0: starttime=time.time() while GPIO.input(self.ECHO)==1: stoptime=time.time() d_cm=(stoptime-starttime)*34300 if d_cm>20: break if a==1: return d_cm elif a==2: if d_cm>19: return "go" else: return "turn"
def startprocess(self): GPIO.output(self.TRIG, True) sleep(0.00001) GPIO.output(self.TRIG, False) while GPIO.input(self.ECHO)==0: starttime=time.time() while GPIO.input(self.ECHO)==1: stoptime=time.time() if stoptime-starttime > l_of_box/speedofmoter: lol=False break if lol!= False: d_cm=(stoptime-starttime)*34300 if d_cm > l_of_box: return "go" else: # if goinginx==True: # objfinded(objco=[mycoo[0]+1,mycoo[1]]) # elif goinginy==True: # objfinded(objco=[mycoo[0],mycoo[1]+1]) return "wait" else: return "go"
def _get_state(self): if self._pwm: return self._duty_cycle else: return GPIO.input(self._number)
outputs = [22,23,24,5] BUTT = 25 GPIO.setmode(GPIO.BCM) #sleep(t) GPIO.setup(outputs, GPIO.OUT) #sleep(t) GPIO.setup(BUTT,GPIO.IN,pull_up_down=GPIO.PUD_UP) #sleep(t) try: while True: while(GPIO.input(BUTT)): pass GPIO.output(22, True) sleep(t) GPIO.output(23, True) sleep(t) GPIO.output(24, True) sleep(2) GPIO.output(22, False) sleep(t) GPIO.output(23, False) sleep(t) GPIO.output(24, False) sleep(2) GPIO.output(5,True) sleep(0.1)
RTKGPIO.setup(gpio, RTKGPIO.IN) #sleep(0.1) print("Setting up GPIO Ins on the RPi Board") for gpio in gpios: print(gpio) RPIGPIO.setup(gpio, RPIGPIO.OUT) print("Now Testing") for gpio in gpios: print("Testing GPIO%s", str(gpio)) print("Turning off") RPIGPIO.output(gpio, 0) sleep(0.1) print("Reading input") input1 = RTKGPIO.input(gpio) print("Turning on") RPIGPIO.output(gpio, True) sleep(0.1) print("Reading input") input2 = RTKGPIO.input(gpio) if (input1 == 0 and input2 == 1): print("GPIO Pin Passed") else: errorPins.append(gpio) print("GPIO Pin Failed") print(input1) print(input2) #sleep(0.1) #Sleep buffers required
print("Ctrl+C captured, ending read.") continue_reading = False GPIO.cleanup() print("GPIO Cleaned") # Hook the SIGINT signal.signal(signal.SIGINT, end_read) parser = argparse.ArgumentParser(description='Write NFC tags for sonos.') parser.add_argument('-test', type=bool, default=False, help='Just test the read but dont perform the action on sonos') parser.add_argument('-sonosUri', type=str, default=SonosController.SONOS_BASE_URI, help='The Sonos base Uri to use') parser.add_argument('-sonosRoom', type=str, default=SonosController.SONOS_ROOM, help='The Sonos room to play the content at') #parser.add_argument('-nfcKey', type=str, default='FF:FF:FF:FF:FF:FF', help='The hex code of the nfc key to writ the content default: FF:FF:FF:FF:FF:FF') args = parser.parse_args() is_test = args.test SonosController.SONOS_BASE_URI = args.sonosUri SonosController.SONOS_ROOM = args.sonosRoom print("Wait for button press ...") # Program start GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP) while continue_reading: input_state = GPIO.input(33) if input_state == False: print('Next Button Pressed') nfcData = 'next' SonosController.play(nfcData)