Beispiel #1
0
    def setup(self, direction, resistor=GPIO.PULL_UP):
        if not direction in [GPIO.OUTPUT, GPIO.INPUT]:
            raise IoTPy_APIError(
                "Invalid GPIO direction. Should be GPIO.INPUT or GPIO.OUTPUT")

        if direction == GPIO.INPUT and not resistor in [
                GPIO.NONE, GPIO.PULL_UP, GPIO.PULL_DOWN
        ]:
            raise IoTPy_APIError(
                "Invalid GPIO resistor setting. Should be GPIO.NONE, GPIO.PULL_UP or GPIO.PULL_DOWN"
            )

        self.direction = direction

        if direction == GPIO.INPUT:
            self.resistor = resistor

            if resistor == GPIO.PULL_UP:
                mode = 4  # PULL_UP
            elif resistor == GPIO.PULL_DOWN:
                mode = 2  # PULL_DOWN
            else:
                mode = 0  # HIGH_Z
        else:
            mode = 1  # OUTPUT

        self.board.uper_io(
            0,
            self.board.encode_sfp(
                3, [self._logical_pins,
                    chr(mode) * len(self._logical_pins)]))
Beispiel #2
0
    def __init__(self, pinout, serial_port=None):
        """__init__(self, pinout, serial_port=None)"""
        ser = serial_port
        if serial_port is None:
            my_platform = platform.system()
            if my_platform == "Windows":
                ports_list = []
                for i in xrange(256):
                    try:
                        ser = serial.Serial(i)
                        ports_list.append('COM' + str(i + 1))
                        ser.close()
                    except serial.SerialException:
                        pass
            elif my_platform == "Darwin":
                ports_list = glob.glob("/dev/tty.usbmodem*")
            elif my_platform == "Linux":
                ports_list = glob.glob("/dev/ttyACM*")

            for my_port in ports_list:
                try:
                    port_to_try = serial.Serial(
                        port=my_port,
                        baudrate=
                        230400,  #virtual com port on USB is always max speed
                        parity=serial.PARITY_ODD,
                        stopbits=serial.STOPBITS_ONE,
                        bytesize=serial.EIGHTBITS,
                        timeout=0.1)
                    port_to_try.write(self.encode_sfp(255, []))
                    uper_response = port_to_try.read(1)  # read one, blocking
                    n = port_to_try.inWaiting()  # look if there is more
                    if n:
                        uper_response = uper_response + port_to_try.read(n)
                        if self.decode_sfp(uper_response
                                           )[0] == -1:  # found port with UPER
                            ser = port_to_try
                            break
                    port_to_try.close()
                except:
                    raise IoTPy_APIError("Unrecoverable serial port error.")

        if not ser:
            raise IoTPy_APIError("No UPER found on USB/serial ports.")

        self.interrupts = [None] * 8
        self.callbackdict = {}

        self.ser = ser
        self.ser.flush()
        self.outq = Queue.Queue()
        self.reader = Reader(self.ser, self.outq, self.internalCallBack,
                             self.decode_sfp)

        self.devicename = "uper"
        self.version = __version__
        self.pinout = pinout
Beispiel #3
0
 def uper_io(self, ret, output_buf):
     try:
         self.ser.write(output_buf)
     except:
         raise IoTPy_APIError(
             "Unrecoverable serial port writing error, dying.")
     data = None
     if ret != 0:
         try:
             data = self.outq.get(True, 1)
         except Queue.Empty:
             raise IoTPy_APIError(
                 "Nothing to read on serial port exception.")
     return data
Beispiel #4
0
 def detach(self):
     try:
         interruptID = self.board.interrupts.index(self.logical_pin)
     except ValueError:
         errmsg("UPER API: trying to detach non existing interrupt.")
         IoTPy_APIError("trying to detaching non existing interrupt.")
     self.board.interrupts[interruptID] = None
     try:
         del self.board.callbackdict[self.logical_pin]
     except KeyError:
         errmsg("UPER API: trying to detach non existing interrupt.")
         IoTPy_APIError("trying to detaching non existing interrupt.")
     self.board.uper_io(0, self.board.encode_sfp(7, [interruptID]))
     return True
Beispiel #5
0
 def __init__(self, *args, **kw):
     super(IoPinout, self).__init__(*args, **kw)
     for key in self:
         if not isinstance(key, int) or not isinstance(self[key], IoParams):
             raise IoTPy_APIError(
                 "IoPinout must consist of integer keys and IoParams values."
             )
Beispiel #6
0
    def set_pulse_time(self, pulse_us):
        """
        Set PWM high (on state) time.

        :param pulse_us: Pulse time in microseconds.
        :type pulse_us: int
        :raise: IoTPy_APIError
        """
        if self.primary:
            self.board.uper_io(0, self.board.encode_sfp(
                2, [self.logical_pin]))  # set pin secondary function
            self.primary = False
        if 0 <= pulse_us <= PWM_PORT_RUNNING[self.pwm_port]['period']:
            self.pulse_time = pulse_us

            high_time = pulse_us
            if self.polarity == 0:
                high_time = PWM_PORT_RUNNING[
                    self.pwm_port]['period'] - pulse_us

            self.board.uper_io(
                0,
                self.board.encode_sfp(
                    UPER1_PWM._PWM_PORT_FUNCTIONS[self.pwm_port][1],
                    [self.pwm_pin, high_time]))
        else:
            errmsg(
                "UPER error: PWM high time is out of range on logical pin %d."
                % self.logical_pin)
            raise IoTPy_APIError("PWM high time is out of range.")
Beispiel #7
0
    def width_us(self, hightime):
        """
        Set PWM high (on state) time.

        :param hightime: On state time in microseconds.
        :type hightime: int
        :raise: IoTPy_APIError
        """
        if self.primary:
            self.board.uper_io(0, self.board.encode_sfp(
                2, [self.logical_pin]))  # set pin secondary function
            self.primary = False
        if 0 <= hightime <= PWM_PORT_RUNNING[self.pwm_port]['period']:
            self.hightime = hightime
            if self.polarity == 1:
                hightime = PWM_PORT_RUNNING[self.pwm_port]['period'] - hightime
            self.board.uper_io(
                0,
                self.board.encode_sfp(
                    PWM._PWM_PORT_FUNCTIONS[self.pwm_port][1],
                    [self.pwm_pin, hightime]))
        else:
            errmsg(
                "UPER error: PWM high time is out of range on logical pin %d."
                % self.logical_pin)
            raise IoTPy_APIError("PWM high time is out of range.")
Beispiel #8
0
    def attach(self, mode, callback, user_object=None, debounce_time=50):
        """
        Attach (enable) or reconfigure GPIO interrupt event.

        :param mode: GPIO interrupt mode.
        :param callback: User callback function. This function is executed when the interrupt event is received.
        :param user_object: Optional user defined object, which will be passed back to the callback function.
        :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: True
        :raise: IoTPy_APIError
        """
        try:
            interruptID = self.board.interrupts.index(self.logical_pin)
            self.board.uper_io(0, self.board.encode_sfp(
                7, [interruptID]))  #detach interrupt
        except ValueError:
            try:
                interruptID = self.board.interrupts.index(None)
                self.board.interrupts[interruptID] = 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': mode,
            'callback': callback,
            'userobject': user_object
        }
        self.board.uper_io(
            0,
            self.board.encode_sfp(
                6, [interruptID, self.logical_pin, mode, debounce_time]))
        return True
Beispiel #9
0
    def SPI(self, name, clock=1000000, mode=SPI.MODE_0, *args, **kwargs):
        _names = {"SPI0": 0, "SPI1": 1}
        if isinstance(name, int):
            port = name
        elif isinstance(name, str):
            if _names.has_key(name):
                port = _names[name]
            else:
                raise IoTPy_APIError(
                    "Invalid SPI name %s. Must be one of %s." %
                    (name, ", ".join(sorted(_names.keys()))))
        else:
            raise IoTPy_APIError("PWM name must be an integer or a string")

        divider = int(round(2.0e6 / clock))

        return UPER1_SPI(self, port, divider, mode)
Beispiel #10
0
    def decode_sfp(self, buffer):
        """
        Decode SFP command from byte buffer.

        :param buffer: A byte buffer which stores SFP command.
        :type buffer: str
        :return: A list containing decoded SFP function ID and arguments (if any).
        """
        result = []
        if buffer[0:1] != '\xd4':
            return result
        buflen = struct.unpack('>H', buffer[1:3])[0] + 3
        result.append(struct.unpack('b', buffer[3:4])[0])
        pointer = 4
        args = []
        while pointer < buflen:
            argtype = ord(buffer[pointer:pointer + 1])
            pointer += 1
            if argtype < 64:  # short int
                args.append(argtype)
            elif argtype < 128:  # short str
                arglen = argtype & 0x3f
                args.append(buffer[pointer:pointer + arglen])
                pointer += arglen
            else:
                arglen = argtype & 0x0f
                if arglen < 4:  # decoding integers
                    if arglen == 0:
                        args.append(ord(buffer[pointer:pointer + 1]))
                    elif arglen == 1:
                        args.append(
                            struct.unpack('>H',
                                          buffer[pointer:pointer + 2])[0])
                    elif arglen == 2:
                        args.append(
                            struct.unpack('>I', '\x00' +
                                          buffer[pointer:pointer + 3])[0])
                    elif arglen == 3:
                        args.append(
                            struct.unpack('>I',
                                          buffer[pointer:pointer + 4])[0])
                    pointer += arglen + 1
                else:
                    if argtype == 0xc4:  # decoding strings
                        arglen = ord(buffer[pointer:pointer + 1])
                    elif argtype == 0xc5:
                        arglen = struct.unpack('>H',
                                               buffer[pointer:pointer + 2])[0]
                        pointer += 1
                    else:
                        raise IoTPy_APIError(
                            "UPER API: Bad parameter type in decodeSFP method."
                        )
                    pointer += 1
                    args.append(buffer[pointer:pointer + arglen])
                    pointer += arglen
        result.append(args)
        return result
Beispiel #11
0
 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, can't attach interrupt.",
             pin)
         raise IoTPy_APIError("Wrong Pin number.")
Beispiel #12
0
 def __init__(self, board, pin):
     self.board = board
     if self.board.pinout[pin][0] & self.board.cap_gpio:
         self.logical_pin = self.board.pinout[pin][1][0]
     else:
         errmsg(
             "UPER API: Pin No:%d is not GPIO pin, can't attach interrupt.",
             pin)
         raise IoTPy_APIError("Wrong Pin number.")
Beispiel #13
0
    def stop(self):

        #for i in range(7):
        #    self.detachInterrupt(i)
        try:
            self.reader.stop()
            self.ser.flush()
            self.ser.close()
        except:
            raise IoTPy_APIError("UPER API: Serial/USB port disconnected.")
Beispiel #14
0
 def _encode_bytes(self, bytestr):
     if len(bytestr) < 64:
         return chr(0x40 | len(bytestr)) + bytestr
     packedlen = struct.pack('>I', len(bytestr)).lstrip('\x00')
     if len(packedlen) == 1:
         return '\xc4' + packedlen + bytestr
     elif len(packedlen) == 2:
         return '\xc5' + packedlen + bytestr
     else:
         raise IoTPy_APIError(
             "UPER API: - too long string passed to UPER, encode_bytes can't handle it."
         )
Beispiel #15
0
 def __init__(self, board, pin):
     self.board = board
     if self.board.pinout[pin][0] & self.board.cap_adc:
         self.logical_pin = self.board.pinout[pin][1][0]
     else:
         errmsg("UPER API: Pin No:%d is not an ADC pin.", pin)
         raise IoTPy_APIError("Trying to assign ADC function to non ADC pin.")
     self.adc_pin = self.board.pinout[pin][1][1]
     self.board.uper_io(0, self.board.encode_sfp(3, [self.logical_pin, self.HIGH_Z]))
     self.board.uper_io(0, self.board.encode_sfp(2, [self.logical_pin])) # set secondary pin function
     self.pull = self.HIGH_Z
     self.primary = False
Beispiel #16
0
    def PWM(self, name, freq=100, polarity=1, *args, **kwargs):
        _names = {
            "PWM0_0": 27,
            "PWM0_1": 28,
            "PWM0_2": 34,
            "PWM1_0": 10,
            "PWM1_1": 39,
            "PWM1_2": 3
        }
        if isinstance(name, int):
            pin = name
        elif isinstance(name, str):
            if _names.has_key(name):
                pin = _names[name]
            else:
                raise IoTPy_APIError(
                    "Invalid PWM name %s. Must be one of %s." %
                    (name, ", ".join(sorted(_names.keys()))))
        else:
            raise IoTPy_APIError("PWM name must be an integer or a string")

        return UPER1_PWM(self, pin, freq, polarity)
Beispiel #17
0
 def decode_sfp(self, buffer):
     result = []
     if buffer[0:1] != '\xd4':
         return result
     buflen = struct.unpack('>H', buffer[1:3])[0] + 3
     result.append(struct.unpack('b', buffer[3:4])[0])
     pointer = 4
     args = []
     while pointer < buflen:
         argtype = ord(buffer[pointer:pointer + 1])
         pointer += 1
         if argtype < 64:  #short int
             args.append(argtype)
         elif argtype < 128:  #short str
             arglen = argtype & 0x3f
             args.append(buffer[pointer:pointer + arglen])
             pointer += arglen
         else:
             arglen = argtype & 0x0f
             if arglen < 4:  #decoding integers
                 if arglen == 0:
                     args.append(ord(buffer[pointer:pointer + 1]))
                 elif arglen == 1:
                     args.append(
                         struct.unpack('>H',
                                       buffer[pointer:pointer + 2])[0])
                 elif arglen == 2:
                     args.append(
                         struct.unpack('>I', '\x00' +
                                       buffer[pointer:pointer + 3])[0])
                 elif arglen == 3:
                     args.append(
                         struct.unpack('>I',
                                       buffer[pointer:pointer + 4])[0])
                 pointer += arglen + 1
             else:
                 if argtype == 0xc4:  #decoding strings
                     arglen = ord(buffer[pointer:pointer + 1])
                 elif argtype == 0xc5:
                     arglen = struct.unpack('>H',
                                            buffer[pointer:pointer + 2])[0]
                     pointer += 1
                 else:
                     raise IoTPy_APIError(
                         "UPER API: Bad parameter type in decodeSFP method."
                     )
                 pointer += 1
                 args.append(buffer[pointer:pointer + arglen])
                 pointer += arglen
     result.append(args)
     return result
Beispiel #18
0
 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.")
     self.direction = self.INPUT
     self.pull = self.PULL_UP
     self.board.uper_io(0, self.board.encode_sfp(
         1, [self.logical_pin]))  # set primary
     self.primary = True
     self.input_pullmode = self.PULL_UP
Beispiel #19
0
 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("UPER API: Pin No:%d is not an ADC pin.", 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, self.board.encode_sfp(
         3, [self.logical_pin, 0]))  # set GPIO to HIGH_Z
     self.board.uper_io(0, self.board.encode_sfp(
         2, [self.logical_pin]))  # set secondary pin function
     self.primary = False
Beispiel #20
0
 def period(self, period):
     if 0 <= period <= self.PWM_PORT_MAX[self.pwm_port]:
         if PWM_PORT_RUNNING[self.pwm_port][1] != period:
             self.board.uper_io(
                 0,
                 self.board.encode_sfp(
                     self.PWM_PORT_FUNCTIONS[self.pwm_port][0], [period]))
             PWM_PORT_RUNNING[self.pwm_port][1] = period
             self.PWM_PERIOD = period
     else:
         errmsg(
             "UPER API: PWM period for port %d can be only between 0-%d" %
             (self.pwm_port, self.PWM_PORT_MAX[self.pwm_port]))
         raise IoTPy_APIError("PWM period is out of range.")
Beispiel #21
0
    def setup(self, direction, resistor=GPIO.PULL_UP):
        """
        Configure GPIO.

        :param direction: GPIO direction: GPIO.OUTPUT or GPIO.INPUT
        :param resistor: GPIO internal resistor mode. Used when direction is GPIO.INPUT. Should be GPIO.PULL_UP, \
        GPIO.PULL_DOWN or GPIO.NONE.

        :raise: IoTPy_APIError
        """
        if not direction in [GPIO.OUTPUT, GPIO.INPUT]:
            raise IoTPy_APIError(
                "Invalid GPIO direction. Should be GPIO.INPUT or GPIO.OUTPUT")

        if direction == GPIO.INPUT and not resistor in [
                GPIO.NONE, GPIO.PULL_UP, GPIO.PULL_DOWN
        ]:
            raise IoTPy_APIError(
                "Invalid GPIO resistor setting. Should be GPIO.NONE, GPIO.PULL_UP or GPIO.PULL_DOWN"
            )

        self.direction = direction

        if direction == GPIO.INPUT:
            self.resistor = resistor

            if resistor == GPIO.PULL_UP:
                mode = 4  # PULL_UP
            elif resistor == GPIO.PULL_DOWN:
                mode = 2  # PULL_DOWN
            else:
                mode = 0  # HIGH_Z
        else:
            mode = 1  # OUTPUT

        self.board.uper_io(0, self.board.encode_sfp(3,
                                                    [self.logical_pin, mode]))
Beispiel #22
0
 def mode(self, pin_mode):
     if pin_mode in [
             self.HIGH_Z, self.PULL_UP, self.PULL_DOWN, self.OUTPUT
     ]:
         if pin_mode != self.OUTPUT:
             self.direction = self.INPUT
             self.input_pullmode = pin_mode
             self.pull = pin_mode
         else:
             self.direction = self.OUTPUT
         self.board.uper_io(
             0, self.board.encode_sfp(3, [self.logical_pin, pin_mode]))
     else:
         errmsg("UPER API: pinMode - Illegal pin mode - %d", pin_mode)
         raise IoTPy_APIError("Illegal pin mode.")
Beispiel #23
0
    def stop(self):
        """
        Stop all communications with the board and close serial communication port.

        :raise: IoTPy_APIError
        """

        #for i in range(7):
        #    self.detachInterrupt(i)
        try:
            self.reader.stop()
            self.ser.flush()
            self.ser.close()
        except:
            raise IoTPy_APIError("UPER API: Serial/USB port disconnected.")
Beispiel #24
0
    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.direction = GPIO.INPUT
        self.resistor = GPIO.PULL_UP
        self.setup(self.direction, self.resistor)
        self.board.uper_io(0, self.board.encode_sfp(
            1, [self.logical_pin]))  # set primary
Beispiel #25
0
    def ADC(self, name, *args, **kwargs):
        _names = {
            "ADC0": 23,
            "ADC1": 24,
            "ADC2": 25,
            "ADC3": 26,
            "ADC4": 30,
            "ADC5": 31,
            "ADC6": 32,
            "ADC7": 33
        }
        if isinstance(name, int):
            pin = name
        elif isinstance(name, str):
            if _names.has_key(name):
                pin = _names[name]
            else:
                raise IoTPy_APIError(
                    "Invalid ADC name %s. Must be one of %s." %
                    (name, ", ".join(sorted(_names.keys()))))
        else:
            raise IoTPy_APIError("ADC name must be an integer or a string")

        return UPER1_ADC(self, pin)
Beispiel #26
0
 def __init__(self, board, pin, polarity=1):
     self.board = board
     if self.board.pinout[pin][0] & self.board.cap_pwm:
         self.logical_pin = self.board.pinout[pin][1][0]
     else:
         errmsg("UPER API: Pin No:%d is not a PWM pin.", pin)
         raise IoTPy_APIError(
             "Trying to assign PWM function to non PWM pin.")
     self.pwm_port = self.board.pinout[pin][1][1]
     self.pwm_pin = self.board.pinout[pin][1][2]
     self.primary = True
     self.hightime = 0
     self.polarity = polarity
     PWM_PORT_RUNNING[self.pwm_port][0] += 1
     if PWM_PORT_RUNNING[self.pwm_port][0] == 1:
         self.period(self.PWM_PERIOD)
Beispiel #27
0
 def attach(self, mode, callback):
     try:
         interruptID = self.board.interrupts.index(self.logical_pin)
         self.board.uper_io(0, self.board.encode_sfp(
             7, [interruptID]))  #detach interrupt
     except ValueError:
         try:
             interruptID = self.board.interrupts.index(None)
             self.board.interrupts[interruptID] = 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, callback]
     self.board.uper_io(
         0, self.board.encode_sfp(6, [interruptID, self.logical_pin, mode]))
     return True
Beispiel #28
0
 def __init__(self, board, pin, polarity=1):
     self.board = board
     if self.board.pinout[pin].capabilities & CAP_PWM:
         self.logical_pin = self.board.pinout[pin].pinID
     else:
         errmsg("UPER API: Pin No:%d is not a PWM pin.", pin)
         raise IoTPy_APIError(
             "Trying to assign PWM function to non PWM pin.")
     self.pwm_port = self.board.pinout[pin].extra[0]
     self.pwm_pin = self.board.pinout[pin].extra[1]
     self.primary = True
     self.hightime = 0
     self.polarity = polarity
     PWM_PORT_RUNNING[self.pwm_port]['channels'] += 1
     if PWM_PORT_RUNNING[self.pwm_port]['channels'] == 1:
         self.period(10000)
Beispiel #29
0
 def get_device_info(self):
     device_info = []
     result = self.decode_sfp(self.uper_io(1, self.encode_sfp(255, [])))
     if result[0] != -1:
         errmsg("UPER error: get_device_info wrong code.")
         raise IoTPy_APIError("")
     result = result[1]
     if result[0] >> 24 != 0x55:  # 0x55 == 'U'
         print "UPER error: getDeviceInfo unknown device/firmware type"
         return
     device_info.append("UPER")  # type
     device_info.append((result[0] & 0x00ff0000) >> 16)  #fw major
     device_info.append(result[0] & 0x0000ffff)  #fw minor
     device_info.append(result[1])  # 16 bytes long unique ID from UPER CPU
     device_info.append(result[2])  # UPER LPC CPU part number
     device_info.append(result[3])  # UPER LPC CPU bootload code version
     return device_info
Beispiel #30
0
    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, self.board.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,
            self.board.encode_sfp(
                6, [irq_id, self.logical_pin, event, debounce_time]))
        return irq_id