from synapse.platforms import * #------------------------------------------------------------------------------- DOORBELL_DISABLE_RINGER_GPIO = GPIO_3 DOORBELL_RING_GPIO = GPIO_4 DOORBELL_SENSE_BUTTON_GPIO = GPIO_6 #----------------- doorbell_sense_timeout = 0 doorbell_sense_last = False #------------------------------------------------------------------------------- @setHook(HOOK_STARTUP) def startupEvent(): #Turn on LED to show operation setPinDir(GPIO_1, True) writePin(GPIO_1, True) #Toggle LED to show operating setPinDir(GPIO_2, True) #writePin(GPIO_2, True) pulsePin(GPIO_2, 1000, True) #Set outputs setPinDir(DOORBELL_DISABLE_RINGER_GPIO, True) setPinDir(DOORBELL_RING_GPIO, True) #----------------- @setHook(HOOK_1S) def hook_1s(): #Doorbell sense debug #doorbell_sense_debug() #Sense doorbell reset timeout sense_doorbell_timeout() @setHook(HOOK_100MS) def hook_100ms(): #Check doorbell input for change sense_doorbell() #----------------- #Sense doorbell def sense_doorbell(): global doorbell_sense_last #If able to sense (i.e. not during timeout) if (doorbell_sense_timeout == 0): #Read current doorbell press status status = readPin(DOORBELL_SENSE_BUTTON_GPIO) #Check for change if (doorbell_sense_last != status): #If bell rung (GPIO changed to low) if (status == False): print 'Doorbell Ring' mcastRpc(1, 10, 'doorbell_ring_handle') #Force ring for at least 1 second doorbell_ring(1000); #Save last doorbell status doorbell_sense_last = status #Sense doorbell timeout reset def sense_doorbell_timeout(): global doorbell_sense_timeout #Decrement doorbell sense timeout if (doorbell_sense_timeout > 0): doorbell_sense_timeout = doorbell_sense_timeout - 1 #print 'Doorbell Timeout: ', doorbell_sense_timeout #Disable doorbell def doorbell_disable_ringer(status): #Disable/enable ringer writePin(DOORBELL_DISABLE_RINGER_GPIO, status) #Ring doorbell def doorbell_ring(duration_ms): global doorbell_sense_timeout #Turn on bell for specified duration pulsePin(DOORBELL_RING_GPIO, duration_ms, True) #Set timeout such that ringing does not create a loop that is detected and causes further triggering doorbell_sense_timeout = (duration_ms + 1000) / 1000 #Doorbell sense debug def doorbell_sense_debug(): status = readPin(DOORBELL_SENSE_BUTTON_GPIO) print "Doorbell: ", status writePin(GPIO_2, status)