def _set_function(self, value): if value != 'input': self._pull = 'floating' if value in ('input', 'output') and value in self.GPIO_FUNCTIONS: GPIO.setup(self._number, self.GPIO_FUNCTIONS[value], self.GPIO_PULL_UPS[self._pull]) else: raise PinInvalidFunction('invalid function "%s" for pin %r' % (value, self))
def lcd_toggle_enable(): # Toggle enable time.sleep(E_DELAY) GPIO.output(LCD_E, True) time.sleep(E_PULSE) GPIO.output(LCD_E, False) time.sleep(E_DELAY)
def input_with_pull(self, pull): if pull != 'up' and self.PI_INFO.pulled_up('GPIO%d' % self._number): raise PinFixedPull('%r has a physical pull-up resistor' % self) try: GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[pull]) self._pull = pull except KeyError: raise PinInvalidPull('invalid pull "%s" for pin %r' % (pull, self))
def _set_pull(self, value): if self.function != 'input': raise PinFixedPull('cannot set pull on non-input pin %r' % self) if value != 'up' and self.PI_INFO.pulled_up('GPIO%d' % self._number): raise PinFixedPull('%r has a physical pull-up resistor' % self) try: GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[value]) self._pull = value except KeyError: raise PinInvalidPull('invalid pull "%s" for pin %r' % (value, self))
def _set_when_changed(self, value): if self._when_changed is None and value is not None: self._when_changed = value GPIO.add_event_detect( self._number, self._edges, callback=lambda channel: self._when_changed(), bouncetime=self._bounce) elif self._when_changed is not None and value is None: GPIO.remove_event_detect(self._number) self._when_changed = None else: self._when_changed = value
def _set_state(self, value): if self._pwm: try: self._pwm.ChangeDutyCycle(value * 100) except ValueError: raise PinInvalidState('invalid state "%s" for pin %r' % (value, self)) self._duty_cycle = value else: try: GPIO.output(self._number, value) except ValueError: raise PinInvalidState('invalid state "%s" for pin %r' % (value, self)) except RuntimeError: raise PinSetInput('cannot set state of pin %r' % self)
def setup(): GPIO.setmode(GPIO.BCM) for l in LED_GPIO: GPIO.setup(l, GPIO.OUT) GPIO.output(l, False) for b in BUTTON_GPIO: GPIO.setup(b, GPIO.IN)
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 __new__(cls, number): if not cls._PINS: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) if cls.PI_INFO is None: cls.PI_INFO = pi_info("900092") #Fake a Pi Zero 1.2, Closest device as just has GPIO Pins and no camera. #cls.PI_INFO = 900092 try: return cls._PINS[number] except KeyError: self = super(RTkGPIOPin, cls).__new__(cls) try: cls.PI_INFO.physical_pin('GPIO%d' % number) except PinNoPins: warnings.warn( PinNonPhysical( 'no physical pins exist for GPIO%d' % number)) self._number = number self._pull = 'up' if cls.PI_INFO.pulled_up('GPIO%d' % number) else 'floating' self._pwm = None self._frequency = None self._duty_cycle = None self._bounce = -666 self._when_changed = None self._edges = GPIO.BOTH GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[self._pull]) cls._PINS[number] = self return self
def init(delay=0.0015): global running, StepCount, StepDir, stepsToDo, StepPosition, StepPins global StepCounter, Seq, WaitTime # Use physical pin numbers GPIO.setmode(GPIO.BCM) # Define GPIO signals to use # StepPins = [35,36,32,33] # RoboHat StepPins = [4, 17, 27, 18] # ZeroPoint # Set all pins as output for pin in StepPins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, False) # Define pin sequence Seq = [[1, 0, 0, 1], [1, 0, 1, 0], [0, 1, 1, 0], [0, 1, 0, 1]] StepCount = len(Seq) StepDir = 1 # 1 == clockwise, -1 = anticlockwise StepsToDo = 0 #number of steps to move StepPosition = 0 # current steps anti-clockwise from the zero position # Initialise variables StepCounter = 0 WaitTime = delay running = True # Move pointer to zero position StepDir = -1 stepsToDo = 700 step()
def __new__(cls, number): if not cls._PINS: GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) if cls.PI_INFO is None: cls.PI_INFO = pi_info("900092") #Fake a Pi Zero 1.2, Closest device as just has GPIO Pins and no camera. #cls.PI_INFO = 900092 try: return cls._PINS[number] except KeyError: self = super(RTkGPIOPin, cls).__new__(cls) try: cls.PI_INFO.physical_pin('GPIO%d' % number) except PinNoPins: warnings.warn( PinNonPhysical('no physical pins exist for GPIO%d' % number)) self._number = number self._pull = 'up' if cls.PI_INFO.pulled_up('GPIO%d' % number) else 'floating' self._pwm = None self._frequency = None self._duty_cycle = None self._bounce = -666 self._when_changed = None self._edges = GPIO.BOTH GPIO.setup(self._number, GPIO.IN, self.GPIO_PULL_UPS[self._pull]) cls._PINS[number] = self return self
def __init__(self,Ena,in1,in2): # in1= input 1 forward # in2=input 2 backward self.Ena=Ena self.in1=in1 self.in2=in2 GPIO.setup(self.in1,GPIO.OUT) GPIO.setup(self.in2,GPIO.OUT) GPIO.setup(self.Ena,GPIO.OUT) self.p=GPIO.PWM(Ena,1000)
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 _set_frequency(self, value): if self._frequency is None and value is not None: try: self._pwm = GPIO.PWM(self._number, value) except RuntimeError: raise PinPWMFixedValue('cannot start PWM on pin %r' % self) self._pwm.start(0) self._duty_cycle = 0 self._frequency = value elif self._frequency is not None and value is not None: self._pwm.ChangeFrequency(value) self._frequency = value elif self._frequency is not None and value is None: self._pwm.stop() self._pwm = None self._duty_cycle = None self._frequency = None
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 step(): global running, StepCounter, stepsToDo while running and stepsToDo > 0: for pin in range(0, 4): xpin = StepPins[pin] # Get GPIO if Seq[StepCounter][pin] != 0: GPIO.output(xpin, True) else: GPIO.output(xpin, False) StepCounter += StepDir if (StepCounter >= StepCount): StepCounter = 0 if (StepCounter < 0): StepCounter = StepCount + StepDir stepsToDo -= 1 #print stepsToDo time.sleep(WaitTime) # clear the output pins for pin in StepPins: GPIO.output(pin, False) running = False
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 __init__(self,TRIG,ECHO): self.TRIG=TRIG self.ECHO=ECHO GPIO.setup(self.TRIG,GPIO.OUT) GPIO.setup(self.ECHO,GPIO.IN) GPIO.output(self.TRIG, False)
def pinwrite(): while True: GPIO.output(GRN, False) GPIO.output(YEL, True) time.sleep(TIME * 10) GPIO.output(YEL, False) GPIO.output(RED, True) time.sleep(TIME * 10) GPIO.output(YEL, True) time.sleep(TIME * 10) GPIO.output(RED, False) GPIO.output(YEL, False) GPIO.output(GRN, True) time.sleep(TIME * 10)
def Stop(self): GPIO.output(self.in1,False) GPIO.output(self.in2,False) self.p.ChangeDutyCycle(0)
def moveB(self,s=30): self.p.ChangeDutyCycle(s) GPIO.output(self.in1,False) GPIO.output(self.in2,True)
#Pibrella Demo from time import sleep t = 0.2 import RTk.GPIO as GPIO RED = 27 YEL = 17 GRN = 4 BUZZ = 18 OUT1 = 22 OUT2 = 23 OUT3 = 24 OUT4 = 25 GPIO.setmode(GPIO.BCM) GPIO.setup(RED, GPIO.OUT) GPIO.setup(YEL, GPIO.OUT) GPIO.setup(GRN, GPIO.OUT) GPIO.setup(OUT1, GPIO.OUT) GPIO.setup(OUT2, GPIO.OUT) GPIO.setup(OUT3, GPIO.OUT) GPIO.setup(OUT4, GPIO.OUT) try: while True: GPIO.output(RED, True) sleep(t) GPIO.output(YEL, True) sleep(t) GPIO.output(GRN, True)
from RTk import GPIO # from testclass import test from time import sleep import playsound import time import json import pyttsx3 from tkinter import * engine = pyttsx3.init() engine.setProperty('volume',1.0) voices = engine.getProperty('voices') engine.setProperty('voice', voices[1].id) GPIO.setmode(GPIO.BCM) speedofmoter=0 l_of_box=100 w_of_box=100 # moter 1 in1A = 24 in2A = 23 enA = 25 # moter 2 in1B = 3 in2B = 2 enB = 1 # ultrasonic 1 TRIG=7 ECHO=8 # robo global forward=True c_speed=0 mydir="N"
def output_with_state(self, state): self._pull = 'floating' GPIO.setup(self._number, GPIO.OUT, initial=state)
def close(self): self.frequency = None self.when_changed = None GPIO.cleanup(self._number)
def _get_state(self): if self._pwm: return self._duty_cycle else: return GPIO.input(self._number)
def _get_function(self): return self.GPIO_FUNCTION_NAMES[GPIO.gpio_function(self._number)]
def movef(self,s=30): self.p.ChangeDutyCycle(s) # GPIO.PWM(self.Ena,1000).ChangeDutyCycle(s) GPIO.output(self.in1,True) GPIO.output(self.in2,False)
def motor_control(direction): # print direction if (direction == "fwd"): GPIO.output(12, True) GPIO.output(13, False) GPIO.output(5, False) GPIO.output(6, False) print ("forward position led flow") if (direction == "bwd"): GPIO.output(13, True) GPIO.output(12, False) GPIO.output(5, False) GPIO.output(6, False) print ("backward position led flow") if (direction == "left"): GPIO.output(5, True) GPIO.output(12, False) GPIO.output(13, False) GPIO.output(6, False) print ("LEFT position led flow") if (direction == "right"): GPIO.output(6, True) GPIO.output(5, False) GPIO.output(12, False) GPIO.output(13, False) print ("RIGHT position led flow") if (direction == "stop"): GPIO.setmode(GPIO.BCM) GPIO.output(12, False) GPIO.output(13, False) GPIO.output(5, False) GPIO.output(6, False) print ("STOP position led flow")
from RTk import GPIO from time import sleep Green_LED = 26 Amber_LED = 19 Red_LED = 13 GPIO.setmode(GPIO.BCM) GPIO.setup(Green_LED, GPIO.OUT) GPIO.setup(Amber_LED, GPIO.OUT) GPIO.setup(Red_LED, GPIO.OUT) Doze = 2.0 while True: GPIO.output(Red_LED, 1) sleep(Doze) GPIO.output(Red_LED, 0) GPIO.output(Amber_LED, 0) sleep(Doze / 4) GPIO.output(Amber_LED, 1) sleep(Doze / 4) GPIO.output(Amber_LED, 0) sleep(Doze / 4) GPIO.output(Amber_LED, 1) sleep(Doze / 4) GPIO.output(Amber_LED, 0) sleep(Doze / 4) GPIO.output(Amber_LED, 1) sleep(Doze / 4) GPIO.output(Amber_LED, 0) sleep(Doze / 4)
#TrafficHAT KS Demo from time import sleep t = 0.1 import RTk.GPIO as GPIO gpios = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 ] GPIO.setmode(GPIO.BCM) while True: for cGPIO in gpios: print("Testing GPIO", cGPIO) GPIO.setup(cGPIO, GPIO.OUT) raw_input("Press enter to continue") GPIO.output(cGPIO, 1) sleep(1) for cGPIO in gpios: #sleep(t) raw_input("Press enter to continue") GPIO.output(cGPIO, 0) #sleep(t) sleep(1)
# client side import time # import RPi.GPIO as GPIO from RTk import GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT) GPIO.setup(13, GPIO.OUT) GPIO.setup(5, GPIO.OUT) GPIO.setup(6, GPIO.OUT) def motor_control(direction): # print direction if (direction == "fwd"): GPIO.output(12, True) GPIO.output(13, False) GPIO.output(5, False) GPIO.output(6, False) print ("forward position led flow") if (direction == "bwd"): GPIO.output(13, True) GPIO.output(12, False) GPIO.output(5, False) GPIO.output(6, False) print ("backward position led flow")