1#!/usr/bin/env python3
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from acts.controllers.relay_lib.relay import RelayState
18from acts.controllers.relay_lib.relay_board import RelayBoard
19from pylibftdi import BitBangDevice
20
21
22class UsbRelayBoardBase(RelayBoard):
23
24    VALID_RELAY_POSITIONS = [1, 2, 3, 4, 5, 6, 7, 8]
25    NUM_RELAYS = 8
26
27    def __init__(self, config):
28        self.status_dict = dict()
29        self.device = config["device"]
30        super(UsbRelayBoardBase, self).__init__(config)
31        self.address = {
32            1: 0x1,
33            2: 0x2,
34            3: 0x4,
35            4: 0x8,
36            5: 0x10,
37            6: 0x20,
38            7: 0x40,
39            8: 0x80,
40            "select_all": 0xFF
41        }
42
43    def get_relay_position_list(self):
44        return self.VALID_RELAY_POSITIONS
45
46    def test_bit(self, int_type, offset):
47        """Function to get status for the given relay position.
48
49        Args:
50            int_type: Port value for given relay.
51            offset: offset for given Relay_position.
52
53        Returns:
54            returns current status for given relay_position.
55        """
56        mask = 1 << offset
57        return (int_type & mask)
58
59    def _get_relay_state(self, data, relay):
60        """Function to get status for the given relay position.
61
62        Args:
63            data: Port value for given relay.
64            relay: Relay_position.
65
66        Returns:
67            returns current status for given relay_position.
68        """
69        if relay == 1:
70            return self.test_bit(data, 1)
71        if relay == 2:
72            return self.test_bit(data, 3)
73        if relay == 3:
74            return self.test_bit(data, 5)
75        if relay == 4:
76            return self.test_bit(data, 7)
77        if relay == 5:
78            return self.test_bit(data, 2)
79        if relay == 6:
80            return self.test_bit(data, 4)
81        if relay == 7:
82            return self.test_bit(data, 6)
83        if relay == 8:
84            return self.test_bit(data, 8)
85
86    def get_relay_status(self, relay_position):
87        """Get relay status for the given relay position.
88
89        Args:
90            relay_position: Status for given Relay position.
91
92        Returns:
93            returns current status for given relay_position.
94        """
95        with BitBangDevice(self.device) as bb:
96            self.status_dict[relay_position] = self._get_relay_state(
97                bb.port, relay_position)
98        return self.status_dict[relay_position]
99
100    def set(self, relay_position, value):
101        """Returns the current status of the passed in relay.
102
103        Args:
104            relay_position: Relay position.
105            value: Turn_on or Turn_off the relay for the given relay_position.
106        """
107        raise NotImplementedError
108