def attach_irq(self, event, callback=None, user_object=None, debounce_time=50): """ Attach (enable) or reconfigure GPIO interrupt event. :param event: GPIO interrupt event. Can have one of these values: GPIO.RISE, GPIO.FALL, GPIO.CHANGE, \ GPIO.LOW or GPIO.HIGH. :param callback: User callback function. This function is executed when the interrupt event is received. \ It should take two arguments: interrupt event description and user object. Interrupt event descriptor is \ dictionary with three fields: 'id' - the interrupt ID (interrupt channel), 'event' - interrupt event type \ and 'values' - the logical values on each of interrupt channel (N-th bit represents logical pin value of \ interrupt channel N). User object is the same object as user_object. :param user_object: User defined object, which will be passed back to the callback function. Optional, \ default is None. :param debounce_time: Interrupt disable time in milliseconds after the triggering event. This is used to \ "debounce" buttons or to protect communication channel from data flood. Optional, default is 50ms. :return: Logical interrupt ID :rtype: int :raise: IoTPy_APIError """ try: irq_id = self.board.interrupts.index(self.logical_pin) self.board.uper_io(0, encode_sfp(7, [irq_id])) # detach interrupt except ValueError: try: irq_id = self.board.interrupts.index(None) self.board.interrupts[irq_id] = self.logical_pin except ValueError: errmsg("UPER API: more than 8 interrupts requested") raise IoTPy_APIError("Too many interrupts.") self.board.callbackdict[self.logical_pin] = {'mode': event, 'callback': callback, 'userobject': user_object} self.board.uper_io(0, encode_sfp(6, [irq_id, self.logical_pin, event, debounce_time])) return irq_id
def __init__(self, board, pin): self.board = board if self.board.pinout[pin].capabilities & CAP_ADC: self.logical_pin = self.board.pinout[pin].pinID else: errmsg("IO API: Pin "+str(pin)+" is not an ADC pin.") raise IoTPy_APIError("Trying to assign ADC function to non ADC pin.") self.adc_pin = self.board.pinout[pin].extra[0] self.board.uper_io(0, encode_sfp(3, [self.logical_pin, 0])) # set GPIO to HIGH_Z self.board.uper_io(0, encode_sfp(2, [self.logical_pin])) # set secondary pin function self.primary = False
def __init__(self, board, pin): self.board = board if self.board.pinout[pin].capabilities & CAP_GPIO: self.logical_pin = self.board.pinout[pin].pinID else: errmsg("UPER API: Pin No:%d is not GPIO pin.", pin) raise IoTPy_APIError("Trying to assign GPIO function to non GPIO pin.") # Configure default state to be input with pull-up resistor self.board.uper_io(0, encode_sfp(1, [self.logical_pin])) # set primary self.direction = GPIO.INPUT self.resistor = GPIO.PULL_UP self.setup(self.direction, self.resistor) # default GPIO pin state is INPUT and PULL_UP
def distance(self, distance_unit = CM): """ Measure distance to closest object. :param distance_unit: The units to measure the distance in. Optional, default Srf08.CM. :type distance_unit: Srf08.CM, Srf08.INCH or Srf08.MS. :return: The distance in specified units. :rtype: int :raise: IoTPy_ThingError """ if distance_unit not in (Srf08.CM, Srf08.INCH, Srf08.MS): errmsg("Wrong units for distance, should be 'c' or 'i' or 'm'.") raise IoTPy_ThingError("Wrong units for distance, should be 'c' or 'i' or 'm'.") try: self.interface.transaction(self.address, Srf08.CMD + distance_unit, 0) sleep(0.08) distance = unpack('>H', self.interface.transaction(self.address, Srf08.RESULT, 2)[:2])[0] except IoTPy_ThingError: raise IoTPy_ThingError("srf08 - distance reading error.") return distance
def __init__(self, board, port=0, divider=1, mode=SPI.MODE_0): divider = min(max(divider, 1), 256) self.board = board self.port = port self.divider = divider self.mode = mode if self.port == 1: self.board.uper_io(0, encode_sfp(2, [4])) self.board.uper_io(0, encode_sfp(2, [5])) self.board.uper_io(0, encode_sfp(2, [11])) self.board.uper_io(0, encode_sfp(30, [self.divider, self.mode])) elif self.port == 0: self.board.uper_io(0, encode_sfp(2, [12])) self.board.uper_io(0, encode_sfp(2, [13])) self.board.uper_io(0, encode_sfp(2, [14])) self.board.uper_io(0, encode_sfp(20, [self.divider, self.mode])) else: errmsg("UPER API: Wrong SPI port number.", self.port) raise IoTPy_APIError("SPI port must be 0 or 1, trying to assign something else.")