def __init__(self): # button wiringpi2.pinMode(self.BUTTON_PIN, wiringpi2.GPIO.INPUT) wiringpi2.pullUpDnControl(self.BUTTON_PIN, wiringpi2.GPIO.PUD_UP) wiringpi2.wiringPiISR(self.BUTTON_PIN, wiringpi2.GPIO.INT_EDGE_BOTH, self.button_raw_press) self.button_callback = None # hardware PWM led wiringpi2.pinMode(self.LED_PIN, wiringpi2.GPIO.PWM_OUTPUT) self.thread_pwm_worker = Thread(target=self.pwm_worker) self.thread_pwm_worker.setDaemon(True) self.pwm_worker_working = False self.button_pressed = 0
def __init__(self, name, pinToWatch, triggerOnHigh = True, debounceTimeInSeconds = 0): self._watchPin = pinToWatch self._dataLock = Lock() self._name = name self._triggerOnHigh = triggerOnHigh self._debounceTimedelta = timedelta(seconds=debounceTimeInSeconds) wiringpi.wiringPiSetupGpio() wiringpi.piHiPri(99) wiringpi.wiringPiISR(self._watchPin, wiringpi.GPIO.INT_EDGE_BOTH, DoorbellSensor.gpioCallback, self) if bool(wiringpi.digitalRead(self._watchPin)) == self._triggerOnHigh: self._timeLastTriggered = datetime.today()
def __init__(self, startPin, startAction, stopPin, stopAction): wpi.wiringPiSetup() wpi.pinMode(startPin, wpi.GPIO.INPUT) wpi.pullUpDnControl(startPin, wpi.GPIO.PUD_UP) wpi.wiringPiISR(startPin, wpi.GPIO.INT_EDGE_FALLING, self.startTrigger) wpi.pinMode(stopPin, wpi.GPIO.INPUT) wpi.pullUpDnControl(stopPin, wpi.GPIO.PUD_UP) wpi.wiringPiISR(stopPin, wpi.GPIO.INT_EDGE_FALLING, self.stopTrigger) self.startAction = startAction self.stopAction = stopAction self.newEvent = False self.latestEvent = ''
def __init__(self, name, pinToWatch, triggerOnHigh = True): self._watchPin = pinToWatch self._dataLock = Lock() self._name = name self._triggerOnHigh = triggerOnHigh wiringpi.wiringPiSetupGpio() wiringpi.piHiPri(99) wiringpi.wiringPiISR(self._watchPin, wiringpi.GPIO.INT_EDGE_BOTH, Sensor.gpioCallback, self) if bool(wiringpi.digitalRead(self._watchPin)) == self._triggerOnHigh: self._lastStatus = True else: self._lastStatus = False self._triggerEnded = datetime.today()
import wiringpi2 PIN_TO_SENSE = 23 def gpio_callback(): print "GPIO_CALLBACK!" wiringpi2.wiringPiSetupGpio() wiringpi2.pinMode(PIN_TO_SENSE, wiringpi2.GPIO.INPUT) wiringpi2.pullUpDnControl(PIN_TO_SENSE, wiringpi2.GPIO.PUD_UP) wiringpi2.wiringPiISR(PIN_TO_SENSE, wiringpi2.GPIO.INT_EDGE_BOTH, gpio_callback) while True: wiringpi2.delay(2000)
import time import wiringpi2 PIN_TO_SENSE = 23 count = 0 last_interrupt_time = 0 def gpio_callback(): global count, last_interrupt_time if (time.time() - last_interrupt_time > 0.2): last_interrupt_time = time.time() count += 1 print(count) print("GPIO_CALLBACK!") wiringpi2.wiringPiSetup() wiringpi2.pinMode(PIN_TO_SENSE, wiringpi2.GPIO.INPUT) wiringpi2.wiringPiISR(PIN_TO_SENSE, wiringpi2.GPIO.INT_EDGE_FALLING, gpio_callback) while True: wiringpi2.delay(2000)
wiringpi2.pinMode(LIGHTS_PIN, wiringpi2.GPIO.PWM_OUTPUT) # hardware PWM mode count = 0 def button_raw_press(): # simple debounce with busy-waiting time.sleep(DEBOUNCE_MS // 1000) if not wiringpi2.digitalRead(BUTTON_PIN): global count count += 1 print("BUTTON PRESS: %d" % (count)) wiringpi2.pinMode(BUTTON_PIN, wiringpi2.GPIO.INPUT) wiringpi2.pullUpDnControl(BUTTON_PIN, wiringpi2.GPIO.PUD_UP) wiringpi2.wiringPiISR(BUTTON_PIN, wiringpi2.GPIO.INT_EDGE_BOTH, button_raw_press) try: while True: for i in xrange(LED_DUTY_MIN, LED_DUTY_MAX + 1, LED_DUTY_STEP): wiringpi2.pwmWrite(LED_PIN, i) wiringpi2.pwmWrite(LIGHTS_PIN,i) time.sleep(0.02) time.sleep(1.5) for i in xrange(LED_DUTY_MAX, LED_DUTY_MIN - 1, -LED_DUTY_STEP): wiringpi2.pwmWrite(LED_PIN, i) wiringpi2.pwmWrite(LIGHTS_PIN,i) time.sleep(0.02)
wiringpi2.pullUpDnControl(0, 1) # set pull UP resistor for pin 0 switch wiringpi2.pinMode(1,1) # Pin 1 for beeper, OUTPUT wiringpi2.softToneCreate(3) # initialize softTone on Pin 3 def beeper(beeps): "Beep all of the requested beeps in the beeps list" for i in beeps: wiringpi2.digitalWrite(1,1) # beep on time.sleep(i) # length of beep from beeps list wiringpi2.digitalWrite(1,0) # beep off time.sleep(0.025) # interval between beeps def gotButton(): "Double beeps after every button press from interrupt call" # beeper(twobeeps) print("Got a button press") return True # Main Program wiringpi2.wiringPiISR(0, 2, gotButton) k=3 while k>0 : for i in sequence: print(i) wiringpi2.softToneWrite(3, i) time.sleep(0.5) beeper(onebeep) k-=1 time.sleep(0.5) beeper(vbeep)
sys.stdout.flush() #initialize wiringPi wpi.wiringPiSetup() parseConfig() activeTimer = None #reference to an active timer #initialize the pin - input wpi.pinMode(conf["pin"], 0) print("Set pin "+str(conf["pin"])+" as input") sys.stdout.flush() # wake up on interrupts on this pin on the rising edge wpi.wiringPiISR(conf["pin"], 2, processIRQ) # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) sys.stdout.flush() # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. (result, mid) = client.subscribe(conf['mqttTopic']) print("Got subscription result for "+conf['mqttTopic']+":"+str(result)) sys.stdout.flush() client = mqtt.Client()