Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of Tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

This file is part of tendril 

See the COPYING, README, and INSTALL files for more information 

""" 

 

 

class InstrumentChannelBase(object): 

    def __init__(self, parent): 

        self._parent = parent 

 

    @property 

    def is_enabled(self): 

        if self in self._parent.channels: 

            return True 

        else: 

            return False 

 

 

class InstrumentInputChannelBase(InstrumentChannelBase): 

    def __init__(self, parent): 

        super(InstrumentInputChannelBase, self).__init__(parent) 

 

    def get(self): 

        raise NotImplementedError 

 

 

class InstrumentOutputChannelBase(InstrumentChannelBase): 

    def __init__(self, parent): 

        super(InstrumentOutputChannelBase, self).__init__(parent) 

 

    def set(self, signal): 

        raise NotImplementedError 

 

 

class SignalWaveInputChannel(InstrumentInputChannelBase): 

    """ 

    This class provides the bulk of the accessors to the instrument 

    channels providing vanilla SignalWave types. More complex implementations 

    should provide their own channel classes. 

 

    :param parent: The instrument of which this channel is a part of. 

    :param interface: The interface to the instrument. 

    :param chidx: The channel index. 

 

    """ 

    def __init__(self, parent, interface, chidx): 

        super(SignalWaveInputChannel, self).__init__(parent) 

        self._interface = interface 

        self._chidx = chidx 

 

    def get(self, unitclass=None, flush=True): 

        """ 

        Gets the latest data point in the channel's point buffer. By default, 

        it also flushes any older points from the buffer. This behavior can be 

        suppressed by passing flush=False. 

 

        :param unitclass: Unit class of which the data point is expected, or 

                          None if you don't care. 

        :param flush: Whether or not older points should be flushed. 

                      Default True. 

        :return: Latest datapoint. 

        :rtype: :class:`tendril.utils.types.signalbase.SignalPoint` 

 

        """ 

        value = self._interface.latest_point(chidx=self._chidx, flush=flush) 

        if unitclass is None or value.unitclass == unitclass: 

            return value 

        else: 

            raise TypeError 

 

    def get_next_chunk(self, unitclass=None, maxlen=None): 

        """ 

        Gets the next chunk of data from the instrument channel. Note that the 

        chunk returned has already been removed from the channel's 

        point buffer. 

 

        :param unitclass: Unit class of which the data point is expected, or 

                          None if you don't care. 

        :return: Wave containing all but the latest data point in the 

                 channel's point buffer. 

        :rtype: :class:`tendril.utils.types.signalbase.SignalWave` 

 

        """ 

        chunk = self._interface.next_chunk(chidx=self._chidx) 

        if unitclass is None or chunk.unitclass == unitclass: 

            if maxlen is not None: 

                if maxlen > chunk.maxlen: 

                    chunk.maxlen = maxlen 

                return chunk 

            else: 

                return chunk 

        else: 

            raise TypeError 

 

    def reset_wave(self): 

        """ 

        Resets the point / wave buffer in the channel 

        """ 

        self._interface.reset_buffer(chidx=self._chidx) 

 

    @property 

    def data_available(self): 

        return self._interface.data_available(chidx=self._chidx) 

 

 

class InstrumentBase(object): 

    def __init__(self, channels=None): 

        self._ident = None 

        self._sno = None 

        self._channels = channels 

        self._configurations = [] 

 

        self._connected = None 

        self._consumers = [] 

 

    def consumer_register(self, consumer): 

        if self._connected is not True: 

            try: 

                self.connect() 

            except NotImplementedError: 

                pass 

        self._consumers.append(consumer) 

 

    def consumer_done(self, consumer): 

        self._consumers.remove(consumer) 

        if len(self._consumers) == 0 and self._connected is True: 

            try: 

                self.disconnect() 

            except NotImplementedError: 

                pass 

 

    @property 

    def connected(self): 

        return self._connected 

 

    def connect(self): 

        raise NotImplementedError 

 

    def disconnect(self): 

        raise NotImplementedError 

 

    def _detect(self): 

        raise NotImplementedError 

 

    def configure(self, configuration): 

        raise NotImplementedError 

 

    @property 

    def configurations(self): 

        return self._configurations 

 

    @property 

    def channels(self): 

        return self._channels 

 

    @property 

    def ident(self): 

        if self._sno is not None: 

            return str(self._ident) + ' ' + str(self._sno) 

        else: 

            return self._ident