|
| 1 | +from pyscan.general.item_attribute import ItemAttribute |
| 2 | + |
| 3 | + |
| 4 | +class HeliosSDK(ItemAttribute): |
| 5 | + |
| 6 | + def __init__(self): |
| 7 | + |
| 8 | + self._version = "0.1.0" |
| 9 | + |
| 10 | + pass |
| 11 | + |
| 12 | + def add_device_property(self, settings): |
| 13 | + ''' |
| 14 | + Adds a property to the class based on a settings dictionary |
| 15 | +
|
| 16 | + Args: |
| 17 | + settings: dict containing settings for property |
| 18 | +
|
| 19 | + Returns function that generates the property of the class |
| 20 | + ''' |
| 21 | + |
| 22 | + if 'values' in settings: |
| 23 | + set_function = self.set_values_property |
| 24 | + elif 'range' in settings: |
| 25 | + set_function = self.set_range_property |
| 26 | + elif 'int_range' in settings: |
| 27 | + set_function = self.set_int_range_property |
| 28 | + elif 'indexed_values' in settings: |
| 29 | + set_function = self.set_indexed_values_property |
| 30 | + else: |
| 31 | + pass |
| 32 | + |
| 33 | + property_definition = property( |
| 34 | + fget=lambda obj: self.get_instrument_property(obj, settings), |
| 35 | + fset=lambda obj, new_value: set_function(obj, new_value, settings)) |
| 36 | + |
| 37 | + return setattr(self.__class__, settings['name'], property_definition) |
| 38 | + |
| 39 | + def get_instrument_property(self, obj, settings, debug=False): |
| 40 | + ''' |
| 41 | + Generator function for a query function of the instrument |
| 42 | + that sends the query string and formats the return based on |
| 43 | + settings['return_type'] |
| 44 | +
|
| 45 | + Args: |
| 46 | + obj - parent object |
| 47 | + settings - settings dictionary |
| 48 | + debug - returns query sring instead of querying instrument |
| 49 | +
|
| 50 | +
|
| 51 | + Returns value formatted to settings['return_type'] |
| 52 | + ''' |
| 53 | + |
| 54 | + value = settings['get_command']() |
| 55 | + value = settings['return_type'](value) |
| 56 | + |
| 57 | + setattr(obj, '_' + settings['name'], value) |
| 58 | + |
| 59 | + return value |
| 60 | + |
| 61 | + def set_values_property(self, obj, new_value, settings): |
| 62 | + ''' |
| 63 | + Generator function for settings dictionary with 'values' item |
| 64 | + Check that new_value is in settings['values'], if not, rejects command |
| 65 | +
|
| 66 | + Args: |
| 67 | + obj - parent class object |
| 68 | + new_value - new_value to be set on instrument |
| 69 | + settings - dictionarky with ['values'] item |
| 70 | +
|
| 71 | + returns None |
| 72 | + ''' |
| 73 | + |
| 74 | + values = settings['values'] |
| 75 | + |
| 76 | + if new_value in values: |
| 77 | + if not self.debug: |
| 78 | + settings['set_command'](new_value) |
| 79 | + setattr(self, '_' + settings['name'], new_value) |
| 80 | + else: |
| 81 | + setattr(self, '_' + settings['name'], |
| 82 | + settings['write_string'].format(new_value)) |
| 83 | + else: |
| 84 | + print('Value Error: ') |
| 85 | + print('{} must be one of: '.format(settings['name'])) |
| 86 | + for string in values: |
| 87 | + print('{}'.format(string)) |
| 88 | + |
| 89 | + def set_range_property(self, obj, new_value, settings): |
| 90 | + ''' |
| 91 | + Generator function for settings dictionary with 'range' item |
| 92 | + Check that new_value is in settings['range'], if not, rejects command |
| 93 | +
|
| 94 | + Args: |
| 95 | + obj - parent class object |
| 96 | + new_value - new_value to be set on instrument |
| 97 | + settings - dictionarky with ['range'] item |
| 98 | +
|
| 99 | + returns None |
| 100 | + ''' |
| 101 | + |
| 102 | + rng = settings['range'] |
| 103 | + |
| 104 | + if rng[0] <= new_value <= rng[1]: |
| 105 | + if not self.debug: |
| 106 | + settings['set_command'](new_value) |
| 107 | + setattr(self, '_' + settings['name'], new_value) |
| 108 | + else: |
| 109 | + setattr(self, '_' + settings['name'], |
| 110 | + settings['write_string'].format(new_value)) |
| 111 | + else: |
| 112 | + print('Range error: ') |
| 113 | + print('{} must be between {} and {}'.format( |
| 114 | + settings['name'], rng[0], rng[1])) |
| 115 | + |
| 116 | + def set_int_range_property(self, obj, new_value, settings): |
| 117 | + ''' |
| 118 | + Generator function for settings dictionary with 'range' item |
| 119 | + Check that new_value is in settings['range'], if not, rejects command |
| 120 | +
|
| 121 | + Args: |
| 122 | + obj - parent class object |
| 123 | + new_value - new_value to be set on instrument |
| 124 | + settings - dictionarky with ['range'] item |
| 125 | +
|
| 126 | + returns None |
| 127 | + ''' |
| 128 | + |
| 129 | + rng = settings['int_range'] |
| 130 | + |
| 131 | + if rng[0] <= new_value <= rng[1]: |
| 132 | + if not self.debug: |
| 133 | + settings['set_command'](new_value) |
| 134 | + setattr(self, '_' + settings['name'], new_value) |
| 135 | + else: |
| 136 | + setattr(self, '_' + settings['name'], |
| 137 | + settings['write_string'].format(new_value)) |
| 138 | + else: |
| 139 | + print('Integer Range error: ') |
| 140 | + print('{} must be an integer between {} and {}'.format( |
| 141 | + settings['name'], rng[0], rng[1])) |
| 142 | + |
| 143 | + def set_range_properties(self, obj, new_value, settings): |
| 144 | + ''' |
| 145 | + Generator function for settings dictionary with 'ranges' item |
| 146 | + Check that new_value is in settings['ranges'], if not, rejects command |
| 147 | +
|
| 148 | + Args: |
| 149 | + obj - parent class object |
| 150 | + new_value - new_value to be set on instrument |
| 151 | + settings - dictionarky with ['ranges'] item |
| 152 | +
|
| 153 | + returns None |
| 154 | + ''' |
| 155 | + |
| 156 | + rngs = settings['ranges'] |
| 157 | + |
| 158 | + if len(rngs) != len(new_value): |
| 159 | + print('Error: {} takes {} parameters, you passed {}.'.format(settings['name'], len(rngs), len(new_value))) |
| 160 | + elif all(rngi[0] <= new_valuei <= rngi[1] for new_valuei, rngi in zip(new_value, rngs)): |
| 161 | + if not self.debug: |
| 162 | + obj.write(settings['write_string'].format(*new_value)) |
| 163 | + setattr(self, '_' + settings['name'], new_value) |
| 164 | + else: |
| 165 | + setattr(self, '_' + settings['name'], settings['write_string'].format(*new_value)) |
| 166 | + else: |
| 167 | + print('Range error: ') |
| 168 | + print('Parameters must be in ranges {}\n\tYou passed {}.'.format(rngs, new_value)) |
| 169 | + |
| 170 | + def set_indexed_values_property(self, obj, new_value, settings): |
| 171 | + ''' |
| 172 | + Generator function for settings dictionary with 'indexed_values' item |
| 173 | + Check that new_value is in settings['indexed_values'], if not, rejects command |
| 174 | +
|
| 175 | + Args: |
| 176 | + obj - parent class object |
| 177 | + new_value - new_value to be set on instrument |
| 178 | + settings - dictionarky with ['indexed_values'] item |
| 179 | +
|
| 180 | + returns None |
| 181 | + ''' |
| 182 | + |
| 183 | + values = settings['indexed_values'] |
| 184 | + |
| 185 | + if new_value in values: |
| 186 | + index = values.index(new_value) |
| 187 | + if not self.debug: |
| 188 | + |
| 189 | + print(settings['write_string'].format(index)) |
| 190 | + |
| 191 | + obj.write(settings['write_string'].format(index)) |
| 192 | + setattr(self, '_' + settings['name'], new_value) |
| 193 | + else: |
| 194 | + setattr(self, '_' + settings['name'], |
| 195 | + settings['write_string'].format(index)) |
| 196 | + else: |
| 197 | + print('Value Error: ') |
| 198 | + print('{} must be one of: '.format(settings['name'])) |
| 199 | + for string in values: |
| 200 | + print('{}'.format(string)) |
| 201 | + |
| 202 | + |
| 203 | +def SensTqp_to_frequency(SensTqp): |
| 204 | + return 70e6 / 8 / (SensTqp + 30) |
| 205 | + |
| 206 | + |
| 207 | +def frequency_to_SenseTqp(frequency): |
| 208 | + |
| 209 | + return 70 * 1e6 / 8 / frequency - 30 |
| 210 | + |
0 commit comments