Example #1
0
    def set(self, value):
        """Set the relay state.

        Valid values depend on which directions of the relay are controlled by
        the object.

        When set to kBothDirections, the relay can be set to any of the four
        states: 0v-0v, 12v-0v, 0v-12v, 12v-12v

        When set to kForwardOnly or kReverseOnly, you can specify the constant
        for the direction or you can simply specify kOff and kOn. Using only
        kOff and kOn is recommended.

        :param value: The state to set the relay.
        :type  value: :class:`Relay.Value`
        """
        if self.port is None:
            raise ValueError("operation on freed port")
        if value == self.Value.kOff:
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kForward):
                hal.setRelayForward(self.port, False)
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kReverse):
                hal.setRelayReverse(self.port, False)
        elif value == self.Value.kOn:
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kForward):
                hal.setRelayForward(self.port, True)
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kReverse):
                hal.setRelayReverse(self.port, True)
        elif value == self.Value.kForward:
            if self.direction == self.Direction.kReverse:
                raise ValueError("A relay configured for reverse cannot be set to forward")
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kForward):
                hal.setRelayForward(self.port, True)
            if self.direction == self.Direction.kBoth:
                hal.setRelayReverse(self.port, False)
        elif value == self.Value.kReverse:
            if self.direction == self.Direction.kForward:
                raise ValueError("A relay configured for forward cannot be set to reverse")
            if self.direction == self.Direction.kBoth:
                hal.setRelayForward(self.port, False)
            if (self.direction == self.Direction.kBoth or
                self.direction == self.Direction.kReverse):
                hal.setRelayReverse(self.port, True)
        else:
            raise ValueError("Invalid value argument '%s'" % value)
Example #2
0
def _freeRelay(port):
    hal.setRelayForward(port, False)
    hal.setRelayReverse(port, False)
    hal.freeDIO(port)